2""" @brief Groups are lists of Individuals.
4Reproduction, selection and behavioural games
5take place within the group.
25if __name__ ==
'__main__':
26 sys.path.append(
'../..')
27 from Evolife.Scenarii.MyScenario
import InstantiateScenario
28 InstantiateScenario(
'Cooperation')
31from random
import randint, sample, shuffle
38 """ A group is mainly a list of individuals
50 for individual
in range(Size):
57 """ returns an available ID
59 IDs = [m.ID for m
in self.
members]
60 for ii
in range(100000):
61 if Prefix
is not None: ID =
'%s%d' % (Prefix, ii)
62 else: ID =
'%d_%d' % (self.
ID, ii)
63 if ID
not in IDs:
return ID
67 """ Calls the 'Individual' class
72 """ Returns the Numberth individual
74 try:
return self.
members[Number]
75 except IndexError:
error(
'Group',
'selecting non-existent individual')
77 def isMember(self, indiv):
return indiv
in self.members
79 def update_(self, flagRanking = False, display=False):
80 """ updates various facts about the group
86 if self.
size == 0:
return 0
91 self.
ranking.sort(key=
lambda x: x.score(),reverse=
True)
99 """ Updates various statistics about the group.
100 Calls 'observation' for each member
109 """ lists agents' locations
111 return [(A.ID, A.location())
for A
in self.
members]
114 """ This function is called at the beginning of each year.
115 Individuals get older each year
117 for m
in self.
members: m.aging()
120 """ suppress one specified individual of the group
126 """ tells a member it should die and then removes it from the group
128 indiv = self.whoIs(memberNbr)
131 return self.
members.pop(memberNbr)
134 """ calls 'remove_' with indiv's index in the group
139 """ synonymous to 'remove_member'
144 """ insert a new member in the group
146 if newcomer
is not None:
155 """ printing a sorted list of individuals, one per line
157 if self.
ranking:
return ">\n".join([
"%s" % ind
for ind
in self.
ranking]) +
"\n"
158 else:
return "\n".join([
"%s" % ind
for ind
in self.
members]) +
"\n"
163 """ class Group: list of individuals that interact and reproduce.
164 Same as Group + reproduction + calls to Scenario functions.
167 def createIndividual(self, Newborn=True):
168 """ calls the 'EvolifeIndividual' class
169 calls 'Scenario.new_agent'
174 if not Newborn: self.
Scenario.new_agent(Indiv,
None)
178 """ loads given DNAs into individuals
184 m.DNAfill([int(n)
for n
in self.Start.pop(0).split()])
186 def update_(self, flagRanking = False, display=False):
187 """ updates various facts about the group + positions
189 size = Group.update_(self, flagRanking=flagRanking)
194 for m
in self.
members: m.checkNetwork(membershipFunction=self.
isMember)
198 """ reproduction within the group
199 creates individuals from couples returned by
'Scenario.couples'
200 by calling
'hybrid' on the parents
' genome
201 and then by mutating the individual
and inserting into the group
210 if child
is not None:
211 child.hybrid(C[0],C[1])
214 if self.
Scenario.new_agent(child, C):
218 """ This function is called at the beginning of each year
219 It calls Scenario.season
221 Group.season(self, year)
225 """ kills or weakens one specified individual of the group
228 indiv = self.
whoIs(memberNbr)
234 """ calls Group.remove_ and also Scenario.remove_agent
236 indiv = self.whoIs(memberNbr)
238 return Group.remove_(self, memberNbr)
241 """ Calls Scenario.life_game
248 """ computes an average individual in the group
250 Avg_DNA = [int(round(B)) for B
in self.
Examiner.storages[
'DNA'].average]
256 """ returns the phenotype of the best or representative individual
265if __name__ ==
"__main__":
267 print(Group.__doc__ +
'\n')
271 raw_input(
'[Return to continue]')
274 MyGroup.update_(flagRanking =
True)
276 print(
"%.02f" % (sum([1.0*i.score()
for i
in MyGroup.members])/gr_size))
277 print(MyGroup.Examiner)
278 MyGroup.reproduction(MyScenario.Parameter(
'ReproductionRate'))
279 while MyGroup.size > gr_size:
280 MyGroup.kill(randint(0,MyGroup.size-1))
282 raw_input(
'[Return to terminate]')
284__author__ =
'Dessalles'
class Group: list of individuals that interact and reproduce.
def reproduction(self)
reproduction within the group creates individuals from couples returned by 'Scenario....
def createIndividual(self, Newborn=True)
calls the 'EvolifeIndividual' class calls 'Scenario.new_agent'
def remove_(self, memberNbr)
calls Group.remove_ and also Scenario.remove_agent
def season(self, year)
This function is called at the beginning of each year It calls Scenario.season.
def get_best(self)
returns the phenotype of the best or representative individual
def kill(self, memberNbr)
kills or weakens one specified individual of the group
def update_(self, flagRanking=False, display=False)
updates various facts about the group + positions
def uploadDNA(self, Start)
loads given DNAs into individuals
def get_average(self)
computes an average individual in the group
def life_game(self)
Calls Scenario.life_game.
A group is mainly a list of individuals.
def season(self, year)
This function is called at the beginning of each year.
def kill(self, memberNbr)
suppress one specified individual of the group
def createIndividual(self, ID=None, Newborn=True)
Calls the 'Individual' class.
def receive(self, newcomer)
insert a new member in the group
def isMember(self, indiv)
def update_(self, flagRanking=False, display=False)
updates various facts about the group
def statistics(self)
Updates various statistics about the group.
def free_ID(self, Prefix=None)
returns an available ID
def remove_(self, memberNbr)
tells a member it should die and then removes it from the group
def extract(self, indiv)
synonymous to 'remove_member'
def remove_member(self, indiv)
calls 'remove_' with indiv's index in the group
def __init__(self, Scenario, ID=1, Size=100)
def whoIs(self, Number)
Returns the Numberth individual.
def positions(self)
lists agents' locations
Individual + genome + phenome + social links.
class Individual: basic individual.
Groups several storages in different slots with different names.
An Individual has a genome, several genes, a score and behaviours.
Gets data from various modules and stores them for display and statistics.