2""" @brief This example shows how to use Evolife's graphic system (based on PyQT)
3to run simulations that display images,dots and lines,
and curves.
5 -----------------------------------------------------------------------------------
6 Evolife provides a window system that you can use
for you own simulation.
9 - Re-implement the function
'One_Run' in the
class 'Population'.
10 This function
is repeatedly called by Evolife. It should perform the simulation.
11 - This function typically executes individual behaviour:
in this example,
12 function
'move'. You may just re-write the latter.
15 Evolife gets instructions
for display through the
class 'Observer'.
16 Observer should
return appropriate data to Evolife
's requests, as indicated.
17 This means that the simulation must keep Observer informed of relevant changes.
20 Evolife can be started with various capabilities
for display
21 (curves, dots
and lines, genomes, links...)
as indicated.
22 -----------------------------------------------------------------------------------
26#============================================================================#
27# EVOLIFE http://evolife.telecom-paris.fr Jean-Louis Dessalles
74from random
import randint
77sys.path.append(
'../..')
86 """ Stores all values that should be displayed
87 May also store general information
90 def __init__(self, TimeLimit):
91 Observer.Generic_Observer.__init__(self, TimeLimit=TimeLimit)
94 self.curve(Name=
'Time', Color=
'blue', Legend=
'time step + noise')
100 self.recordInfo(
'WindowLegends',
"""The "field" window shows moving vectors.<br>The "Trajectories" window shows one rotating segment""")
103 """ initial draw: here a green square
105 return [(5, 5,
'green', 5, 95, 5,
'green', 5),
106 (95, 5,
'green', 5, 95, 95,
'green', 15),
107 (95, 95,
'green', 5 , 5, 95,
'green', 5),
108 (5, 95,
'green', 4, 5, 5,
'green', 5), ]
115 """ This function is called each time the window wants to update display
130 elif Slot ==
'Network':
138 return Observer.Generic_Observer.get_data(self, Slot)
151 elif Slot ==
'Pattern':
155 else:
return Observer.Generic_Observer.get_info(self, Slot, default=default)
159 """ class Agent: defines what an individual consists of
162 self.
ID =
"A%d" % IdNb
164 if IdNb % 2: self.
colour = Curves.Shade(IdNb, BaseColour=
'red', Max=100, darkToLight=
False)
165 else: self.
colour = Curves.Shade(IdNb, BaseColour=
'blue', Max=100)
175 """ defines the population of agents
178 """ creates a population of agents
180 self.Pop = [Agent(IdNb) for IdNb
in range(NbAgents)]
187 return {A.ID:A.Location
for A
in self.
Pop}
194 for agent
in self.
Pop: agent.move()
197 self.
Obs.record(list(self.
positions().items()), Window=
'Field')
199 self.
Obs.record((
'ant', (self.
Obs.StepId % 80, 105, 0, 0.13,
'shape=ant.gif')), Window=
'Field')
202 self.
Obs.record((
's1',(40 + 20*(((1+self.
Obs.StepId) % 4)//2), 60 - 20 * ((self.
Obs.StepId % 4)//2),
'brown', 1, 50, 50,
'brown', 3)), Window=
'Trajectories')
206 self.
Obs.curve(
'Time', self.
Obs.StepId + randint(0,20))
210 MC = self.
Obs.get_info(
'MouseClick', erase=
True)
214 self.
Obs.record((
'mouse', MC[1] + (
'blue', 10)),
215 Window=
'Field' if MC[0] ==
'Field_window' else 'Trajectories')
223 Obs.setOutputDir(
'___Results')
224 Obs.recordInfo(
'Background',
'yellow')
226 Obs.recordInfo(
'CurvesWallpaper',
'../Graphics/EvolifeBG.png')
227 Obs.recordInfo(
'TrajectoriesWallpaper',
'../Graphics/EvolifeBG.png')
228 Obs.recordInfo(
'TrajectoriesTitle',
'Trajectory window')
229 Obs.recordInfo(
'FieldTitle',
'Field window')
230 Obs.recordInfo(
'DefaultViews', [
'Field', (
'Trajectories', 300, 240)])
252 Evolife_Window.Start(
256 Options = {
'Run':
True}
261if __name__ ==
"__main__":
268__author__ =
'Dessalles'
class Agent: defines what an individual consists of
Stores all values that should be displayed May also store general information.
def get_data(self, Slot)
This function is called each time the window wants to update display.
def Field_grid(self)
initial draw: here a green square
defines the population of agents
def __init__(self, NbAgents, Observer)
creates a population of agents