-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathDE.py
102 lines (81 loc) · 2.46 KB
/
DE.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import numpy as np
from collections import namedtuple
import os
EAresult = namedtuple("EAresult", "fitness solution evaluations")
def random_population(domain, dimension, size):
"""
Return an initial population using a uniform random generator
"""
assert domain[0] < domain[1]
uniform = np.random.uniform(domain[0], domain[1], dimension * size)
return uniform.reshape((size, dimension))
def clip(domain, solution):
"""
Returns the solution clippd between the values of the domain.
Params
------
domain vector with the lower and upper values.
"""
assert domain[0] < domain[1]
return np.clip(solution, domain[0], domain[1])
def get_experiments_file(name_output, replace=False, times=1):
"""
Return
Params
------
name_output name of file output.
replace boolean value that indicates if the file output should be replaced.
times number of lines that should be in the output (they are maintained if
replace is False).
"""
if name_output is None:
fid = None
else:
# if it replaced it only return the last value
if not replace and os.path.isfile(name_output):
fin = open(name_output, 'rb')
lines = fin.readlines()
if len(lines) >= times:
(bestSolutionFitness, bestSol, bestEval,
evaluations) = lines[-1].split(',')
return EAresult(
fitness=bestSolutionFitness,
solution=bestSol,
evaluations=evaluations), None
if replace:
fid = open(name_output, 'w')
else:
fid = open(name_output, 'a')
return None, fid
def random_indexes(n, size, ignore=[]):
"""
Returns a group of n indexes between 0 and size, avoiding ignore indexes.
Params
------
n number of indexes.
size size of the vectors.
ignore indexes to ignore.
>>> random_indexes(1, 1)
0
>>> random_indexes(1, 2, [0])
1
>>> random_indexes(1, 2, [1])
0
>>> random_indexes(1, 3, [0, 1])
2
>>> random_indexes(1, 3, [0, 2])
1
>>> random_indexes(1, 3, [1, 2])
0
"""
indexes = [pos for pos in range(size) if pos not in ignore]
assert len(indexes) >= n
np.random.shuffle(indexes)
if n == 1:
return indexes[0]
else:
return indexes[:n]
if __name__ == "__main__":
import doctest
doctest.testmod()
a = [x for x in range(10)]