-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions.py
220 lines (159 loc) · 4.73 KB
/
functions.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import numpy as np
from numpy import random as rnd
from SerialSGS import SerialSGS as SSGS
def CreateRandomSolution(model, sol='RandomKey'):
from metaheuristics import n
nAct = model["n"]
if sol == "ActivityList":
pob = {
"Eggs" : np.zeros((n,nAct))
}
for i in range(n):
pob["Eggs"][i] = rnd.permutation(nAct)
elif sol == "RandomKey":
pob = {
"Eggs" : rnd.rand(n,nAct)
}
else:
raise Exception('Error generating new random solutions')
# "Fitness" : [float('inf')] * n
pob.update({
"Fitness" : np.zeros(n)
})
for i in range(n):
# Convierte a las tareas en viables
x = SSGS(model, pob["Eggs"][i],0)
pob["Eggs"][i] = x["Sol"]
pob["Fitness"][i] = x["Cmax"]
return pob
def getBestNest(sol, newEgg, model):
for j in range(sol["Eggs"].shape[0]):
newSol = SSGS(model, sol["Eggs"][j] , 0)
if newSol["Cmax"] <= sol["Fitness"][j]:
sol["Fitness"][j] = newSol["Cmax"]
sol["Eggs"][j] = newEgg[j]
K = sol["Fitness"].index(min(sol["Fitness"]))
sol.update({
"sol" : SSGS(model, sol["Eggs"][K],0)
})
return K, sol
def getCuckoos(model, pob):
q = pob["Eggs"]
F = pob["Fitness"]
N = model["N"]
p, j = LevyFlights(F)
Q = q[j]
print "q[" + str(j) + "] = " + str(Q)
raw_input()
if p >= 0 and p <= 0.3:
qNew = Insertion(Q,N)
elif p > 0.3 and p <= 1:
qNew = Swap(Q,N)
else :
raise Exception('Error generating a new solution')
# Convierte a la tarea en viable
x = SSGS(model, qNew, 0)
return x["Sol"]
# TODO: Swap() e Insertion() son funciones bastante similares
def Swap(Q, N):
# TODO: There are some common lines between Swap() and Insertion(),
# they can be placed in a common function to avoid duplicates.
from extras import buscar, get_limits, search_index
# Selects the task to swap
i = rnd.randint(2, max(Q)+1) # Choose a non-dummy task [2;n)
idxI = buscar(Q == i)
print "Swap: j=" + str(i)
print "Q_0 = " + str(Q)
pred, sucs = get_limits(N, i, Q)
idxPos = search_index(pred.pop(), sucs.pop(), Q)
# NOTE: Swap() doesn't generate infeasible solutions
print "[" + str(idxI) + "] <-> [" + str(idxPos) + "]"
Q[idxI], Q[idxPos] = Q[idxPos], Q[idxI]
print "Q_1 = " + str(Q)
return Q
def Insertion(Q, N):
from extras import buscar, get_limits, search_index
# Selects the task to insert
i = rnd.randint(2, max(Q)+1) # Choose a non-dummy task [2;n)
idxI = buscar(Q == i)
print "Insertion: j=" + str(i) + "\t[idxI] = " + str(idxI)
print "Q_0 = " + str(Q)
pred, sucs = get_limits(N, i, Q)
idxPos = search_index(pred.pop(), sucs.pop(), Q)
# NOTE: Insertion() doesn't generate infeasible solutions
print "[" + str(idxPos) + "] -> [" + str(idxI) + "]"
np.insert(Q, idxPos, idxI)
print "Q_1 = " + str(Q)
return Q
def LevyFlights(Fitness):
from metaheuristics import n
import math
alpha = 1.5
s = 5.9
j = rnd.randint(0, n)
F = Fitness[j]
u = F - min(Fitness)
v = max(Fitness) - min(Fitness)
if v == 0:
step_size = 0
else :
u = u / v
step_size = math.exp(-s * (u ** alpha))
return step_size, j
def mutation(q,K,F,N):
from extras import get_limits
n = len(q[0])
qNew = np.zeros(q.shape)
f = min(F)
for j in range(1, q.shape[1]+1):
# To do the mutation, the task MUST BE DISCOVERED and
# NOT BE the BEST.
if K[j] and (not F[j] == f):
# Select the number of tasks to swap
M = np.randint(n);
for m in range(1,M+1):
# Select the task to swap
Q = q[j]
i = np.randint( max(Q) )
idxI = buscar(Q == i)
[Pred,Sucs] = get_limits(N,i,Q);
idxPos = search_index(Pred,Sucs,Q);
# NOTA: Esta operacion no genera soluciones inviables
if idxPos > idxI:
qNew[j] = [
q[j][:idxI-1],
q[j][idxPos],
q[j][idxI+1:idxPos-1],
q[j][idxI],
q[j][idxPos+1:]
]
elif idxPos < idxI:
qNew[j] = [
q[j][:idxPos-1],
q[j][idxI],
q[j][idxPos+1:idxI-1],
q[j][idxPos],
q[j][idxI+1:]
]
else:
qNew[j] = q[j]
else:
qNew[j] = q[j]
return qNew
def empty_nests(pob, rcpsp):
from metaheuristics import n, Pa
q = pob["Eggs"]
F = pob["Fitness"]
N = rcpsp["N"]
# The eggs are discovered and are replaced by new ones with a
# probability 'Pa'.
# 'K' indicates which eggs are discovered
# rnd.random.rand(nSol, 1) > Pa
K = [1 if itm > Pa else 0 for itm in np.random.rand(n)]
new_nest = {
"Eggs" : mutation(q,K,F,N),
"Fitness" : pob["Fitness"]
}
for j in range(1,n+1):
if K(j):
new_nest["Fitness"][j] = MakeSpan(rcpsp,new_nest["Eggs"][j])