2""" @brief A population is a set of semi-permeable groups.
5#============================================================================#
6# EVOLIFE http://evolife.telecom-paris.fr Jean-Louis Dessalles
23if __name__ ==
'__main__':
24 sys.path.append(
'../..')
25 from Evolife.Scenarii.MyScenario
import InstantiateScenario
26 InstantiateScenario(
'Cooperation',
'../Evolife')
28from random
import randint, choice
38 def __init__(self, Scenario, Observer):
39 """ Creation of the groups - calls createGroup
48 nb_groups = self.
Scenario.Parameter(
'NumberOfGroups', Default=1)
49 group_size = self.
popSize // nb_groups
51 while (nb_groups > 0):
54 self.statistics(Display=
True)
57 """ Calls class 'Group'
62 """ random selection of an individual in the population
64 (group, winner) = self.lottery()
65 return group.whoIs(winner)
68 """ random selection of an individual by number in the population
70 winner = randint(0,self.popSize-1)
72 if gr.size > winner:
return (gr,winner)
73 else: winner -= gr.size
74 error(f
"Population: wrong population size: {self.popSize}")
77 """ increments 'year' and calls Oberver's season and groups' season
84 """ migration between groups of some percentage of individuals
86 if len(self.
groups) < 2
or self.
Scenario.Parameter(
'MigrationRate', Default=0) == 0:
88 migrants = int(self.
Scenario.Parameter(
'MigrationRate') * self.
popSize/100.0 + 0.5)
90 (gr_out, migrant) = self.
lottery()
92 gr_in.receive(gr_out.remove_(migrant))
96 """ groups that are too big are split in two,
97 and groups that are too small are dispersed
105 effectif = int(gr.size/2.0 + .5)
108 newgroup.receive(gr.remove_(randint(0,gr.size-1)))
111 self.
groups.append(newgroup)
116 if self.
Scenario.Parameter(
'GroupMinSize', Default=0) ==0:
return
119 if gr.size < self.
Scenario.Parameter(
'GroupMinSize'):
123 for dummy
in list(gr):
125 gr_in = choice(self.
groups)
129 gr_in.receive(gr.remove_(0))
133 """ randomly kills individuals until size is reached
137 while self.popSize > self.Scenario.Parameter(
'PopulationSize'):
138 (gr,Unfortunate) = self.lottery()
139 if gr.kill(Unfortunate)
is not None:
141 self.update(display=
True)
143 def update(self, flagRanking = False, display=False):
144 """ updates groups and looks for empty groups
148 for gr
in self.groups:
149 gr.location = self.popSize
150 grsize = gr.update_(flagRanking, display=display)
151 if grsize == 0: toBeRemoved.append(gr)
152 self.popSize += grsize
153 for gr
in toBeRemoved: self.groups.remove(gr)
154 if self.popSize == 0:
error(
"Population is empty")
155 self.best_score = max([gr.best_score
for gr
in self.groups])
159 """ Updates statistics about the population
161 self.update(display=Display)
162 self.Observer.reset()
164 self.Observer.open_()
165 for gr
in self.groups:
167 self.Observer.store(gr.Examiner)
168 self.Observer.close_()
171 """ One year of life.
172 Calls 'limit',
'migration',
'group_splitting',
'season',
'statistics'
182 self.group_splitting()
184 if self.Observer.Visible():
185 self.statistics(Complete=
True, Display=
True)
189 except Exception
as Msg:
190 error(
"Population", str(Msg))
194 """ iterates over all individuals in the population
196 for gr
in self.groups:
201 """ calling 'display' for all individuals in the population
203 for i
in self.members(): i.display()
208 return "\n Population Statistics:\n" + \
209 "> Popul: %d members\tbest: %d\tavg: %.02f\tyear: %d\n" \
210 % (self.Observer.Statistics[
'Properties'][
'length'],
211 self.Observer.Statistics[
'Properties'][
'best'][1],
212 self.Observer.Statistics[
'Properties'][
'average'][1], self.year) + \
213 "\n".join([
"group %d: %d members\tbest: %d\tavg: %.02f" \
214 % (i, grObs.storages[
'Properties'].length, grObs.storages[
'Properties'].best[1],
215 grObs.storages[
'Properties'].average[1]) \
216 for (i,grObs)
in enumerate(self.Observer.storage)]) +
"\n"
222 """ Population + reproduction + call to Scenario life_game
225 """ Creation of groups """
226 Population.__init__(self, Scenario, Evolife_Obs)
228 if self.
Scenario.Parameter(
'StartFromFile', Default=0):
229 StartFile = open(
'EvoStart.gen',
'r')
230 self.
Observer.TextDisplay(
'Retrieving population from EvoStart.gen\n')
231 Genomes = StartFile.readlines()
234 for gr
in self.
groups: gr.uploadDNA(Genomes)
240 """ This version of 'createGroup' calls the 'EvolifeGroup' class instead of the 'Group' class
245 """ launches reproduction in groups
252 """ Calls local 'life_game' in groups
258 """ Population's 'one_year' + calls to 'reproduction' and 'life_game'
263 Res = Population.one_year(self)
267if __name__ ==
"__main__":
269 print(Population.__doc__ +
'\n\nTest:\n')
276if __name__ ==
"__main__":
278 Obs = Meta_Observer(
'PopObs')
285 raw_input(
'[Return]')
287__author__ =
'Dessalles'
class Group: list of individuals that interact and reproduce.
A group is mainly a list of individuals.
Population + reproduction + call to Scenario life_game.
def one_year(self)
Population's 'one_year' + calls to 'reproduction' and 'life_game'.
def life_game(self)
Calls local 'life_game' in groups.
def reproduction(self)
launches reproduction in groups
def createGroup(self, ID=0, Size=0)
This version of 'createGroup' calls the 'EvolifeGroup' class instead of the 'Group' class.
def __init__(self, Scenario, Evolife_Obs)
Creation of groups.
List of Groups Minimal version
def group_splitting(self)
groups that are too big are split in two, and groups that are too small are dispersed
def season(self)
increments 'year' and calls Oberver's season and groups' season
def createGroup(self, ID=0, Size=0)
Calls class 'Group'.
def lottery(self)
random selection of an individual by number in the population
def migration(self)
migration between groups of some percentage of individuals
def selectIndividual(self)
random selection of an individual in the population
Groups are lists of Individuals.
Gets data from various modules and stores them for display and statistics.
def one_year(self)
One year of life.
def limit(self)
(gr_in,dummy) = self.lottery() # choosing where to go
def update(self, flagRanking=False, display=False)
updates groups and looks for empty groups
def members(self)
iterates over all individuals in the population
def statistics(self, Complete=True, Display=False)
Updates statistics about the population.
def display(self)
calling 'display' for all individuals in the population