-
Notifications
You must be signed in to change notification settings - Fork 0
/
revolution.py
80 lines (52 loc) · 1.72 KB
/
revolution.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
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 18 20:05:30 2020
@author: qtckp
"""
import sys
sys.path.append('..')
import numpy as np
import matplotlib.pyplot as plt
from OppOpPopInit import OppositionOperators
from OptimizationTestFunctions import Eggholder
from geneticalgorithm2 import GeneticAlgorithm2 as ga
dim = 15
np.random.seed(3)
func = Eggholder(dim = dim)
iterations = 1000
varbound = np.array([[-500,500]]*dim)
model = ga(
function=func, dimension=dim,
variable_type='real',
variable_boundaries=varbound,
algorithm_parameters={
'max_num_iteration': iterations,
'population_size': 400,
}
)
start_pop = np.random.uniform(low = -500, high = 500, size = (400, dim))
start_gen = (start_pop, None)
# default running
model.run(no_plot = True, start_generation=start_gen)
plt.plot(model.report, label = 'without revolution')
# revolutions
model.run(no_plot = True,
start_generation=start_gen,
revolution_after_stagnation_step = 80,
revolution_part= 0.2,
revolution_oppositor = OppositionOperators.Continual.quasi(minimums = varbound[:,0], maximums = varbound[:, 1])
)
plt.plot(model.report, label = 'with revolution (quasi)')
model.run(no_plot = True,
start_generation=start_gen,
revolution_after_stagnation_step = 80,
revolution_part=0.2,
revolution_oppositor=OppositionOperators.Continual.quasi_reflect(minimums = varbound[:,0], maximums = varbound[:, 1])
)
plt.plot(model.report, label = 'with revolution (quasi_reflect)')
plt.xlabel('Generation')
plt.ylabel('Minimized function')
plt.title('Revolution')
plt.legend()
plt.savefig("./output/revolution.png", dpi = 300)
plt.show()