-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_process.py
307 lines (264 loc) · 12.2 KB
/
main_process.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
from amosa import AMOSAType
import random
import copy
from math import *
from real_mutate_ind import real_mutate_ind
from test_func import evaluate
from dominance import find_unsign_dom
from dominance import is_dominated
from clustering import clustering
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from real_time_plot import real_time_plot
import math
def runAMOSA(amosaParams):
r = int()
k = int()
flag = int()
pos = int()
deldom = float()
amount = float()
p = float()
count = int()
current = []
func_current = []
func_new = []
newsol = []
d_eval = []
real_time_graph_data = []
p2 = amosaParams.i_softl + 3
p1 = amosaParams.i_archivesize - 1
duplicate = 0
r = random.randint(0, p1)
current = copy.deepcopy(amosaParams.dd_archive[r])
flag = 1
pos = r
func_current = copy.deepcopy(amosaParams.dd_func_archive[r])
t = amosaParams.d_tmax
tt=0
def consoleprint(case,i):
print( "_____________________________________________________________________________________________________________________________________", end='\r')
print( 'iteration: ' + str(i) +'\t\t'+ 'case ' + str(case) + '\t'+ str(tt) + 'th temp \t Temperature: ' + '%.10f'%t + '\t\t'+ 'archivesize: ' + str(amosaParams.i_archivesize), end='\r')
while(t >= amosaParams.d_tmin):
for i in range(amosaParams.i_no_ofiter):
duplicate = 0
newsol = copy.deepcopy(current)
real_mutate_ind(newsol, amosaParams)
func_new = evaluate(newsol, amosaParams.c_problem,
amosaParams.i_no_offunc)
count1 = 0
count2 = 0
for j in range(amosaParams.i_no_offunc):
if(func_current[j] <= func_new[j]):
count1 = count1+1
if(func_current[j] >= func_new[j]):
count2 = count2+1
# case 1: If current dominates new-----------------------------------
if(count1 == amosaParams.i_no_offunc):
consoleprint(1,i)
deldom = 0.0
amount = find_unsign_dom(func_current, func_new, amosaParams)
deldom = deldom + amount
for j in range(amosaParams.i_archivesize):
count = 1
if(flag == 0 or i != r):
isdom = is_dominated(
amosaParams.dd_func_archive[j], func_new, amosaParams)
if(isdom):
count = count + 1
amount = find_unsign_dom(
amosaParams.dd_func_archive[j], func_new, amosaParams)
deldom = deldom + amount
# Probability for case 1
expp = float()
try:
expp = exp(deldom/t)
except OverflowError:
expp = math.inf
p = 1.0/(1.0 + expp)
# Selecting the new solution with probability p
ran2 = random.random()
if(p >= ran2):
current = copy.deepcopy(newsol)
func_current = copy.deepcopy(func_new)
flag = 0
# case 3: If new solution dominates the current----------------------
elif(count2 == amosaParams.i_no_offunc):
k = 0
count = 0
deldom = math.inf
consoleprint(3,i)
for j in range(amosaParams.i_archivesize):
isdom = is_dominated(
amosaParams.dd_func_archive[j], func_new, amosaParams)
if(isdom):
count = count+1
amount = find_unsign_dom(
amosaParams.dd_func_archive[j], func_new, amosaParams)
if(amount < deldom):
deldom = amount
k = j
# case 3(a): If new point is dominated by k(k>=1) solutions in the archive
if(count > 0):
p = 1/(1+exp(-deldom))
ran2 = random.random()
# case 3(a).1: Set point of the archive corresponding to deldom as current point with probability = p
if(p >= ran2):
current = copy.deepcopy(amosaParams.dd_archive[k])
func_current = copy.deepcopy(
amosaParams.dd_func_archive[k])
flag = 1
pos = k
# case 3(a).2: Set new point as current point
else:
current = copy.deepcopy(newsol)
func_current = copy.deepcopy(func_new)
flag = 0
# case 3(b): If new point is non-dominating with respect to the point in the archive
elif(count == 0 and duplicate == 0):
# If current point resides in the archive then remove the current point
if (flag == 1):
amosaParams.dd_archive.pop(pos)
amosaParams.dd_func_archive.pop(pos)
amosaParams.i_archivesize = amosaParams.i_archivesize - 1
area2 = copy.deepcopy(amosaParams.dd_func_archive)
archive1 = copy.deepcopy(amosaParams.dd_archive)
k = 0
# If newsol dominates some other sols in archive then remove them all
amosaParams.i_archivesize = len(amosaParams.dd_archive)
amosaParams.dd_archive = []
amosaParams.dd_func_archive = []
for j in range(amosaParams.i_archivesize):
isdom = is_dominated(func_new, area2[j], amosaParams)
if isdom:
k = k+1
else:
amosaParams.dd_archive.append((archive1[j]))
amosaParams.dd_func_archive.append(area2[j])
if(k > 0):
amosaParams.i_archivesize = len(amosaParams.dd_archive)
# edited position for cluster
# Performing clustering if archive size if greater than soft limit
if(amosaParams.i_archivesize > amosaParams.i_softl):
clustering(amosaParams)
amosaParams.i_archivesize = amosaParams.i_archivesize + 1
m = amosaParams.i_archivesize - 1
# Adding the newsol to the archive
amosaParams.dd_archive.append(newsol)
amosaParams.dd_func_archive.append(func_new)
# actural clustering done
current = copy.deepcopy(newsol)
func_current = copy.deepcopy(func_new)
flag = 1
pos = m
# case 2 : Current and newsol are non-dominating to each-other-------
else:
count = 0
deldom = 0.0
consoleprint(2,i)
for j in range(amosaParams.i_archivesize):
isdom = is_dominated(
amosaParams.dd_func_archive[j], func_new, amosaParams)
if(isdom):
count = count + 1
amount = find_unsign_dom(
amosaParams.dd_func_archive[j], func_new, amosaParams)
deldom = deldom + amount
# case 2(a) : New point is dominated by k(k>=1) points in the archive
if(count > 0):
expp = float()
try:
expp = exp(deldom/t)
except OverflowError:
expp = math.inf
p = 1.0/(1.0 + expp)
ran2 = random.random()
if(p >= ran2):
current = copy.deepcopy(newsol)
func_current = copy.deepcopy(func_new)
flag = 0
# case 2(b) : New point is non-dominating with respect to all the points in the archive
elif(count == 0):
area2 = copy.deepcopy(amosaParams.dd_func_archive)
archive1 = copy.deepcopy(amosaParams.dd_archive)
k = 0
h = 0
amosaParams.i_archivesize = len(amosaParams.dd_archive)
amosaParams.dd_archive = []
amosaParams.dd_func_archive = []
for j in range(amosaParams.i_archivesize):
isdom = is_dominated(func_new, area2[j], amosaParams)
if(isdom):
k = k+1
else:
d_func_archive = copy.deepcopy(area2[j])
amosaParams.dd_func_archive.append(d_func_archive)
d_archive = copy.deepcopy(archive1[j])
amosaParams.dd_archive.append(d_archive)
h = h + 1
if(k > 0):
amosaParams.i_archivesize = len(amosaParams.dd_archive)
# clustering
if(amosaParams.i_archivesize > amosaParams.i_softl):
clustering(amosaParams)
m = amosaParams.i_archivesize
amosaParams.i_archivesize = amosaParams.i_archivesize + 1
d_archive = copy.deepcopy(newsol)
amosaParams.dd_archive.append(d_archive)
d_func_archive = copy.deepcopy(func_new)
amosaParams.dd_func_archive.append(d_func_archive)
current = copy.deepcopy(newsol)
func_current = copy.deepcopy(func_new)
flag = 1
pos = m
if(amosaParams.i_no_offunc == 3):
x1 = []
x2 = []
x3 = []
for i in range(len(amosaParams.dd_archive)):
x1.append(amosaParams.dd_func_archive[i][0])
x2.append(amosaParams.dd_func_archive[i][1])
x3.append(amosaParams.dd_func_archive[i][2])
real_time_graph_data.append([x1, x2, x3])
if(amosaParams.i_no_offunc == 2):
x1 = []
x2 = []
for i in range(len(amosaParams.dd_archive)):
x1.append(amosaParams.dd_func_archive[i][0])
x2.append(amosaParams.dd_func_archive[i][1])
real_time_graph_data.append([x1, x2])
t = round(t * amosaParams.d_alpha, 10)
tt=tt+1
if(amosaParams.i_no_offunc == 3):
real_time_plot(real_time_graph_data)
if(amosaParams.i_no_offunc == 2):
real_time_plot(real_time_graph_data)
# with open('saplot.out','w+') as fp:
obj1 = []
obj2 = []
obj3 = []
with open('objective_values.txt', 'w+') as fp:
for i in range(amosaParams.i_archivesize):
fp.write('\n')
#if(amosaParams.dd_func_archive[i][0]<2.5 and amosaParams.dd_func_archive[i][1]<2.5 and amosaParams.dd_func_archive[i][2]<2.5 ):#debug
for h in range(amosaParams.i_no_offunc):
fp.write("\t" + str(amosaParams.dd_func_archive[i][h]))
if h == 0:
obj1.append(amosaParams.dd_func_archive[i][h])
elif h == 1:
obj2.append(amosaParams.dd_func_archive[i][h])
elif h == 2:
obj3.append(amosaParams.dd_func_archive[i][h])
with open('decision_values.txt', 'w+') as fp:
for i in range(amosaParams.i_archivesize):
fp.write('\n')
for h in range(amosaParams.i_totalno_var):
fp.write("\t" + str(amosaParams.dd_archive[i][h]))
if amosaParams.i_no_offunc == 2:
plt.plot(obj1, obj2, 'ro')
plt.show()
elif amosaParams.i_no_offunc == 3:
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.scatter3D(obj1, obj2, obj3)
plt.show()