-
Notifications
You must be signed in to change notification settings - Fork 0
/
best_of_N_with_opp.py
82 lines (55 loc) · 1.76 KB
/
best_of_N_with_opp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 18 19:40:38 2020
@author: qtckp
"""
import sys
sys.path.append('..')
import numpy as np
import matplotlib.pyplot as plt
from OppOpPopInit import OppositionOperators
from OptimizationTestFunctions import Ackley
from geneticalgorithm2 import GeneticAlgorithm2 as ga
from geneticalgorithm2 import get_population_initializer
dim = 15
func = Ackley(dim = dim)
iterations = 150
varbound = np.array([[-4,3]]*dim)
model = ga(function=func, dimension=dim,
variable_type='real',
variable_boundaries=varbound,
algorithm_parameters={
'max_num_iteration': iterations,
'population_size': 400
})
oppositors = [
None,
[
OppositionOperators.Continual.quasi(minimums = varbound[:,0], maximums = varbound[:, 1])
],
[
OppositionOperators.Continual.quasi(minimums = varbound[:,0], maximums = varbound[:, 1]),
OppositionOperators.Continual.over(minimums = varbound[:,0], maximums = varbound[:, 1])
]
]
names = [
'No oppositor, just random',
'quasi oppositor',
'quasi + over oppositors'
]
for opp, name in zip(oppositors, names):
average_report = np.zeros(iterations)
for _ in range(40):
model.run(no_plot = True,
population_initializer=get_population_initializer(select_best_of = 3),
init_oppositors=opp
)
average_report += np.array(model.report)
average_report /= 40
plt.plot(average_report, label = name)
plt.xlabel('Generation')
plt.ylabel('Minimized function (40 simulations average)')
plt.title('Start gen. using oppositors')
plt.legend()
plt.savefig("./output/init_best_of_opp.png", dpi = 300)
plt.show()