This repository was archived by the owner on Sep 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGOMEA4Test.py
More file actions
197 lines (159 loc) · 5.67 KB
/
Copy pathGOMEA4Test.py
File metadata and controls
197 lines (159 loc) · 5.67 KB
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
import population as pop
import population as pop2
import linkageTree as lt
import numpy as np
import decoder as dc
from numpy.random import randint
import time
import random
def getDonor(population, x):
numbers = list(range(0, len(population)))
numbers.remove(x)
donorIndex = random.choice(numbers) # excluded the right extremity of the interval
return population[donorIndex]
def secondCheck(element, population, val):
useless, elemSol = dc.getFitnessAndStats(element, val[3], val[1], val[4])
elemSol.sort()
for x in population:
fit, stat = dc.getFitnessAndStats(x, val[3], val[1], val[4])
stat.sort()
if stat == elemSol:
return False
return True
def greedyRecomb(sol, donor, subset, values, population, forcedImprovement, superiorDonor):
accepted = 0
discarted = 0
bestElem = sol.copy()
for cluster in subset:
solFit = dc.getFitness(sol, values[3], values[1], values[4])
newSol = sol.copy()
for element in cluster:
if not forcedImprovement:
newSol[element] = donor[element]
else:
newSol[element] = superiorDonor[element]
newSolFit = dc.getFitness(newSol, values[3], values[1], values[4])
bestFit = solFit
# print(howManyOfThePopChanged(sol, newSol))
# print(newSolFit, " new")
if newSolFit > solFit:
accepted += 1
# we add a second check to see if the solution resulting would be the same, we discarted because same element
if not pop.checkIfElemInPopulation(newSol, population):
#if not pop.checkIfElemInPopulation(newSol, population) and secondCheck(newSol, population, values):
sol = newSol
bestFit = newSolFit
bestElem = newSol.copy()
else:
discarted += 1
#print("Accepted : ", accepted, " Discarted : ", discarted)
return sol, bestFit, bestElem
def terminated(counter, notProgress):
if counter > 30 or notProgress > 1:
return True
return False
# those are all checking functions --------------
def solInPop(sol, pop):
for x in pop:
if sol == x:
print("trovato")
print(x)
print(sol)
# count how many elements changed from two populations
def howManyOfThePopChanged(pop, newPop):
number = 0
for x in range(0, len(pop)):
if pop[x] != newPop[x]:
number += 1
return number
# ----------------------------------------------
def printStat(population, val):
# [goodsNumber, bidsNumber, dummyNumber, bidsValue, bids]
for x in population:
fit, stat = dc.getFitnessAndStats(x, val[3], val[1], val[4])
print(round(fit, 2), " ", x, " ", stat)
def GOMEA():
forcedImprovement = False
counter = 0
# values = [goodsNumber, bidsNumber, dummyNumber, bidsValue, bids]
population, values = pop.population(100, "problemInstances/L2-50-100.txt", -1)
bestFit = 0
bestElem = []
stationaryCounter = 0
#printStat(population, values)
notProgress = 0
# --
flag = False
trovato = -1
while not terminated(counter, notProgress):
if stationaryCounter > 6:
forcedImprovement = True
#print("Forced improvement !!!!!!!!!!!!!!!!!")
lastRoundPopulation = population.copy()
# ----------------
lT = lt.getLinkageTree(population)
# to create the random linkage tree comment up and uncomment down:
#a, b = pop.population(10, "L3-20-20.txt", -1)
#lT = lt.getLinkageTree(a)
# -----------------
#for li in lT[:-1]:
# print(li)
for x in range(0, len(population)):
for subset in lT[:-1]: # avoiding the root of the tree
donor = getDonor(population, x)
population[x], fit, elem = greedyRecomb(population[x], donor, subset, values, population, forcedImprovement, bestElem)
if bestFit < fit:
bestFit = fit
bestElem = elem.copy()
stationaryCounter = 0
stationaryCounter += 1
counter += 1
# --
if bestFit > 3082.75 and flag == False:
trovato = counter
flag = True
#print(counter, " : ", bestFit, " time: ", round(time.time() - startTime, 2))
#print(counter, " : ", bestFit,)
numberOfChange = howManyOfThePopChanged(lastRoundPopulation, population)
if numberOfChange == 0:
notProgress += 1
else:
notProgress = 0
#print(numberOfChange, " elements have changed since last generation")
#printStat(population, values)
#return population, bestFit, time.time() - startTime, values
return population, bestFit, values, trovato, counter
store = []
for x in range(0, 3):
popol, bestFit, val, trovato, counter = GOMEA()
Flag = True
for x in popol:
if round(dc.getFitness(x, val[3], val[1], val[4]), 2) != 3082.78:
Flag = False
break
store.append([counter, trovato, Flag])
for x in store:
print (x)
a = 0
b = 0
notFound = 0
for x in store:
if x[1] != -1:
a += x[0]
b += x[1]
print(x)
else:
notFound += 1
print(a/len(store), " ", b/len(store), "not Found: ", notFound)
#pop, bestFit, val, trovato, counter = GOMEA()
#pop, bestFit, val, trovato, counter = GOMEA()
'''print(bestFit, " : ", round(time, 2))
print()
# controlli sulla popolazione:
for x in pop:
print(dc.getFitness(x, val[3], val[1], val[4]))#, " : ", x)
for x in range(0, len(pop) - 1):
for j in range(x + 1, len(pop)):
if pop[x] == pop[j]:
print("are the same")
'''