This repository has been archived by the owner on Apr 13, 2024. It is now read-only.
forked from rmsolgi/geneticalgorithm
-
Notifications
You must be signed in to change notification settings - Fork 14
/
remove_dups.py
77 lines (48 loc) · 1.6 KB
/
remove_dups.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
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 18 21:15:55 2020
@author: qtckp
"""
import sys
sys.path.append('..')
import numpy as np
import matplotlib.pyplot as plt
from OppOpPopInit import OppositionOperators
from geneticalgorithm2 import geneticalgorithm2 as ga
dim = 15
np.random.seed(3)
rands = np.random.uniform(-10, 10, 100)
def func(X):
return np.sum(rands[X.astype(int)]) + X.sum()
iterations = 900
varbound = np.array([[0,99]]*dim)
model = ga(function=func, dimension=dim,
variable_type='int',
variable_boundaries=varbound,
algorithm_parameters={
'max_num_iteration': iterations
})
start_pop = np.random.randint(0, 10, size = (100, dim))
start_gen = (start_pop, None)
np.random.seed(3)
model.run(no_plot = True, start_generation=start_gen)
plt.plot(model.report, label = 'without dups removing')
np.random.seed(3)
model.run(no_plot = True,
start_generation=start_gen,
remove_duplicates_generation_step = 40,
)
plt.plot(model.report, label = 'with dups removing + random replace')
np.random.seed(3)
model.run(no_plot = True,
start_generation=start_gen,
remove_duplicates_generation_step = 40,
duplicates_oppositor=OppositionOperators.Discrete.integers_by_order(minimums = varbound[:,0], maximums = varbound[:, 1])
)
plt.plot(model.report, label = 'with dups removing + opposion replace')
plt.xlabel('Generation')
plt.ylabel('Minimized function')
plt.title('Duplicates removing')
plt.legend()
plt.savefig("./output/remove_dups.png", dpi = 300)
plt.show()