Evolife
Evolife has been developed to study Genetic algorithms, Natural evolution and behavioural ecology.
Evolife_Batch.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2""" @brief Run Evolife without any display.
3"""
4
5#============================================================================#
6# EVOLIFE http://evolife.telecom-paris.fr Jean-Louis Dessalles #
7# Telecom Paris 2022-04-11 www.dessalles.fr #
8# -------------------------------------------------------------------------- #
9# License: Creative Commons BY-NC-SA #
10#============================================================================#
11# Documentation: https://evolife.telecom-paris.fr/Classes #
12#============================================================================#
13
14
15
18
19
20
21import os
22import os.path
23import sys
24
25from time import sleep
26
27
28from Evolife.Graphics import Simulation_Thread # Thread to run the simulation in parallel
29
30from Evolife.Tools.Tools import error
31from Evolife.Graphics.Curves import Curves, EvolifeColourID # names of curves
32
33
34
35
38
40 """ Launches Evolife in a non-interactive way.
41 Useful for repetitive simulation to explore parameter space.
42 """
43
44 def __init__(self, SimulationStep, Obs):
45 """ Stores Obs as observer
46 and SimulationStep as the function that processes one step of the simulation.
47 Creates curves from Obs's CurveNames.
48 """
49 self.BestResult = None # Best result returned from the simulation
50 self.Results = []
51 self.Curves = Curves() # Stores curves
52 self.Curves.Curvenames(Obs.get_info('CurveNames'))
53 self.simulation = None # name of the simulation thread
54 self.Obs = Obs # simulation observer
55 self.OneStep = SimulationStep # function that launches one step of the simulation
56
57
58 def Simulation_stop(self):
59 """ Stops the simulation thread
60 """
61 if self.simulation is not None:
62 self.simulation.stop()
63
64 def Simulation_launch(self,functioning_mode):
65 """ (re)starts the simulation thread
66 """
67 self.Simulation_stop()
68 self.simulation = Simulation_Thread.Simulation(self.OneStep, functioning_mode, self.ReturnFromThread)
69 self.simulation.start()
70
71 def ReturnFromThread(self, Best):
72 """ The simulation thread returns the best current phenotype
73 """
74 if Best == 'Buzy?':
75 # this should never happen in batch mode
76 error("Evolife_Batch","Inexistent buzy mode")
77 self.BestResult = Best
78 if self.Obs.Visible():
79 self.Process_graph_orders(Best)
80 if self.Obs.Over():
81 return -1 # Stops the simulation thread
82 else:
83 return 0
84
85 def Process_graph_orders(self, BestPhenotype):
86 """ Retrieves plot orders from observer as a list of (CurveId, Point)
87 and add points to curves accordingly
88 """
89 for (CurveId, Point) in self.Obs.get_info('PlotOrders'):
90 try:
91 self.Curves.Curves[EvolifeColourID(CurveId)[0]].add(Point)
92 except IndexError:
93 error("Evolife_Batch: unknown curve ID")
94
95 def Destruction(self, event=None):
96 """ Stops the simulation and dumps data into output file
97 """
98 self.Simulation_stop()
99 x_values_ignored = self.Obs.get_info('ResultOffset') # call first to make parameter 'relevant'
100 self.Curves.Curvenames(self.Obs.get_info('CurveNames')) # stores curve names - may have been updated
101 self.Curves.dump(self.Obs.get_info('ResultFile'), self.Obs.get_info('ResultHeader'), x_values_ignored)
102
103
104
107
108def Start(SimulationStep, Obs):
109 """ SimulationStep is a function that performs a simulation step
110 Obs is the observer that stores statistics
111 """
112 # No display, batch mode
113 Evolife = Evolife_Batch(SimulationStep, Obs)
114 Evolife.Simulation_launch(True)
115 while True:
116 sleep(5)
117 if os.path.exists('stop'):
118 Evolife.Simulation_stop()
119 # os.remove('stop')
120 if not Evolife.simulation.isAlive():
121 break
122 Evolife.Destruction()
123
124
125
126
127__author__ = 'Dessalles'
Stores a list of 'Curves'.
Definition: Curves.py:273
Simulation in batch mode (no vizualisation) #.
def Simulation_stop(self)
Stops the simulation thread.
def Process_graph_orders(self, BestPhenotype)
Retrieves plot orders from observer as a list of (CurveId, Point) and add points to curves accordingl...
def Destruction(self, event=None)
Stops the simulation and dumps data into output file.
def Simulation_launch(self, functioning_mode)
(re)starts the simulation thread
def ReturnFromThread(self, Best)
The simulation thread returns the best current phenotype.
Stores data that can be used to plot curves and stored into a file.
Definition: Curves.py:1
def EvolifeColourID(Colour_designation, default=(4, 'red'))
Recognizes Colour_designation as a number, a name, a (R,V,B) tuple or a #RRVVBB pattern.
Definition: Curves.py:70
def Start(SimulationStep, Obs)
SimulationStep is a function that performs a simulation step Obs is the observer that stores statisti...
Various functions.
Definition: Tools.py:1
def error(ErrMsg, Explanation='')
Definition: Tools.py:182