forked from wellingtoncruz/python-algoritmo-genetico
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulation.py
More file actions
176 lines (133 loc) · 5.19 KB
/
population.py
File metadata and controls
176 lines (133 loc) · 5.19 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
import os, subprocess, sys, socket, time, struct, random, traci, random, datetime, copy
import guy, config
class Population:
def __init__(self, brandnew=True):
self.config = config.Config()
self.members = []
self.nextgeneration = []
self.generation = int(0)
if(self.config.GENERATIONLOG):
h = random.randrange(1000,9999)
self.generationlogfile = open(self.config.GENERATIONLOGFILE+str(h), "w")
self.generationmediafile = open("medias.txt"+str(h), "w")
if(brandnew):
for i in range(0, self.config.MAXPOPULATION):
self.members.append(guy.Guy())
self.orderYourGuys()
def introduceYourGuys(self):
for guy in self.members:
guy.introduceYourself()
def evaluateYourGuys(self, members):
if(self.config.SPEEDLOG):
speedlogfile = open(self.config.SPEEDLOGFILE, "w")
print >> speedlogfile, "POPULATION EVALUATION STARTED AT %s" % (datetime.datetime.now())
for i in range(0, len(members)):
if(self.config.SPEEDLOG):
print >> speedlogfile, "\tEVALUATION OF GUY NUM %d STATED AT %s" % (i, datetime.datetime.now())
members[i].evaluateYourself()
print " GUY %i SCORE: %f"%(i, members[i].getScore())
if(self.config.SPEEDLOG):
print >> speedlogfile, "\tEVALUATION OF GUY NUM %d FINISHED AT %s" % (i, datetime.datetime.now())
if(self.config.SPEEDLOG):
print >> speedlogfile, "POPULATION EVALUATION FINISHED AT %s" % (datetime.datetime.now())
return members
def orderYourGuys(self):
if(self.config.DEBUG):
print "MEMBERS LIST BEFORE QUICKSORT"
for guy in self.members:
print guy.getScore()
self.members = self._quicksort_(self.members)
if(self.config.DEBUG):
print "MEMBERS LIST AFTER QUICKSORT"
for guy in self.members:
print guy.getScore()
def selectACouple(self):
candidates = copy.copy(self.members)
couple = []
if(self.config.SELECTIONMETHOD==0):
for x in [0,1]:
roullete = selector = int(0)
for i in range(1, len(candidates)):
roullete += i*2
rand = random.randrange(1, roullete)
if(self.config.DEBUG):
print "MAX VALUE FOR ROULLETE: %d"%(roullete)
print "RANDOM VALUE FOR ROULLETE: %d"%(rand)
for i in range(1, len(candidates)):
selector += i*2
if(rand > selector):
continue
else:
if(self.config.DEBUG):
print "\nSELECTED GUY IS: "
print candidates[i].introduceYourself()
couple.append(candidates.pop(i))
break
elif(self.config.SELECTIONMETHOD==1):
for x in [0,1]:
roullete = selector = float(0)
for i in range(0, len(candidates)):
roullete += candidates[i].getScore()
rand = random.randrange(0, int(roullete))
if(self.config.DEBUG):
print "MAX VALUE FOR ROULLETE: %d"%(roullete)
print "RANDOM VALUE FOR ROULLETE: %d"%(rand)
for i in range(0, len(candidates)):
selector += candidates[i].getScore()
if(rand > selector):
continue
else:
if(self.config.DEBUG):
print "\nSELECTED GUY IS: "
print candidates[i].introduceYourself()
couple.append(candidates.pop(i))
break
return couple
def buildNextGeneration(self):
if(self.config.GENERATIONLOG):
print >> self.generationlogfile, "GENERATION %d SUMMARY:"%(self.generation)
medium = 0.0
for i in range(0, len(self.members)):
#print >> self.generationlogfile, "GUY %i SCORE %f "%(i, self.members[i].getScore())
medium += self.members[i].getScore()
print >> self.generationlogfile, "%d&%f\\\\"%(i, self.members[i].getScore())
print >> self.generationlogfile, "\hline"
#print >> self.generationlogfile, "media: %f"%(medium/len(self.members))
print >> self.generationmediafile, "%s"%(str(medium/len(self.members)).replace(".",","))
self.generation += 1
i = 0
while(i < self.config.MAXPOPULATION):
couple = self.selectACouple()
son = guy.Guy(False, False, couple[0], couple[1])
if(not(son.isEqual(couple[0])) and not(son.isEqual(couple[1]))):
self.nextgeneration.append(son)
i += 1
self.nextgeneration = self.evaluateYourGuys(self.nextgeneration)
self.members = self.members + self.nextgeneration
self.nextgeneration = []
self.orderYourGuys()
self.members = self.members[self.config.MAXPOPULATION:]
print "GENERATION %d SUMMARY"%(self.generation)
for i in range(0, len(self.members)):
g = self.members[i]
print "i: %d s: %f"%(i, g.getScore())
def letsGoDarwin(self):
for i in range(0, self.config.MAXGENERATIONS):
guy = self.getBestEvolutedGuy()
if(guy.getScore() < self.config.VERYGOODGUY):
self.buildNextGeneration()
def getBestEvolutedGuy(self):
return self.members[len(self.members)-1]
def _quicksort_(self, members):
if len(members) <= 1:
return members
less, equal, greater = [], [], []
pivot = members[0]
for guy in members:
if(guy.getScore() < pivot.getScore()):
less.append(guy)
elif(guy.getScore() == pivot.getScore()):
equal.append(guy)
else:
greater.append(guy)
return self._quicksort_(less) + equal + self._quicksort_(greater)