Evolife
Evolife has been developed to study Genetic algorithms, Natural evolution and behavioural ecology.
Individual.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2""" @brief An Individual has a genome, several genes, a score and behaviours. """
3
4#============================================================================#
5# EVOLIFE http://evolife.telecom-paris.fr Jean-Louis Dessalles #
6# Telecom Paris 2022-04-11 www.dessalles.fr #
7# -------------------------------------------------------------------------- #
8# License: Creative Commons BY-NC-SA #
9#============================================================================#
10# Documentation: https://evolife.telecom-paris.fr/Classes #
11#============================================================================#
12
13
14
17
18
19import sys
20if __name__ == '__main__': # for tests
21 sys.path.append('../..')
22 from Evolife.Scenarii.MyScenario import InstantiateScenario
23 InstantiateScenario('SexRatio')
24
25
26from random import randint
27
28from Evolife.Genetics.Genome import Genome
29from Evolife.Ecology.Phenotype import Phenome
30from Evolife.Social.Alliances import Follower
31
33 """ class Individual: basic individual.
34 Just sets ID and age.
35 """
36
37 def __init__(self, Scenario, ID=None, Newborn = True):
38 self.Scenario = Scenario
39 if not Newborn:
40 # Aged individuals are created when initializing a population
41 AgeMax = self.Scenario.Parameter('AgeMax', Default=100)
42 self.age = randint(1,AgeMax)
43 else: self.age = 0
44 if ID:
45 self.ID = ID
46 else:
47 self.ID = 'A' + str(randint(0,9999)) # permanent identification in the population
48 self.location = None # location in a multi-dimensional space
49 self.__score = 0
50 self.LifePoints = 0 # some individuals may be more resistant than others
51
52 def aging(self, step=1):
53 """ Increments the individual's age
54 """
55 self.age += step
56 return self.age
57
58 def accident(self, loss=1):
59 """ The victim suffers from a loss of life points
60 """
61 self.LifePoints -= loss
62
63 def dead(self):
64 """ An individual is dead if it is too old or has lost all its 'LifePoints'
65 """
66 if self.LifePoints < 0: return True
67 AgeMax = self.Scenario.Parameter('AgeMax', Default=0)
68 if AgeMax and (self.age > AgeMax): return True
69 return False
70
71 def dies(self):
72 """ Action to be performed when dying
73 """
74 pass
75
76 def score(self, bonus=0, FlagSet=False):
77 """ Sets score or adds points to score, depending on FlagSet - Returns score value
78 """
79 if FlagSet: self.__score = bonus
80 else: self.__score += bonus
81 return self.__score
82
83 def signature(self):
84 """ returns age and score
85 """
86 return [self.age, self.__score]
87
88 def observation(self, GroupExaminer):
89 """ stores individual's signature in 'GroupExaminer'
90 """
91 GroupExaminer.store('Properties',self.signature())
92
93 def display(self, erase=False):
94 """ can be used to display individuals
95 """
96 pass
97
98 def __bool__(self):
99 # to avoid problems when testing instances (as it would call __len__ in Alliances)
100 return True
101
102 def __str__(self):
103 # printing one individual
104 return "ID: " + str(self.ID) + "\tage: " + str(self.age) + "\tscore: " \
105 + "%.02f" % self.score()
106
107
108
109
111 """ Individual + genome + phenome + social links
112 """
113 def __init__(self, Scenario, ID=None, Newborn=True, MaxFriends=0):
114 """ Merely calls parent classes' constructors
115 """
116 Individual.__init__(self, Scenario, ID=ID, Newborn=Newborn)
117 if not Newborn:
118 Genome.__init__(self, self.ScenarioScenarioScenarioScenario)
119 Genome.update(self) # gene values are read from DNA
120 else:
121 Genome.__init__(self, self.ScenarioScenarioScenarioScenario) # newborns are created with blank DNA
122 Phenome.__init__(self, self.ScenarioScenarioScenarioScenario, FlagRandom=True)
123 Follower.__init__(self, MaxFriends)
124
125 def observation(self, GroupExaminer):
126 """ stores genome, phenome, social links and location into GroupExaminer
127 """
128 Individual.observation(self, GroupExaminer)
129 GroupExaminer.store('Genomes', Genome.signature(self))
130 GroupExaminer.store('DNA', list(self.get_DNA()), Numeric=True)
131 GroupExaminer.store('Phenomes', Phenome.signature(self))
132 GroupExaminer.store('Network', (self.ID, [T.ID for T in Follower.signature(self)]), Numeric=False)
133 GroupExaminer.store('Field', (self.ID, self.location), Numeric=False)
134
135 def dies(self):
136 """ Action to be performed when dying
137 """
138 Follower.detach(self)
139 Individual.dies(self)
140
141 def __str__(self):
142 # printing one individual
143 return Individual.__str__(self) + "\tPhen: " + Phenome.__str__(self)
144
145
146
147
150
151if __name__ == "__main__":
152 print(__doc__)
153 print(Individual.__doc__ + '\n\n')
154 John_Doe = Individual(7)
155 print("John_Doe:\n")
156 print(John_Doe)
157 raw_input('[Return]')
158
159
160__author__ = 'Dessalles'
Individual + genome + phenome + social links.
Definition: Individual.py:110
def dies(self)
Action to be performed when dying
Definition: Individual.py:135
def observation(self, GroupExaminer)
stores genome, phenome, social links and location into GroupExaminer
Definition: Individual.py:125
def __init__(self, Scenario, ID=None, Newborn=True, MaxFriends=0)
Merely calls parent classes' constructors.
Definition: Individual.py:113
class Individual: basic individual.
Definition: Individual.py:32
def dies(self)
Action to be performed when dying
Definition: Individual.py:71
def display(self, erase=False)
can be used to display individuals
Definition: Individual.py:93
def aging(self, step=1)
Increments the individual's age.
Definition: Individual.py:52
def observation(self, GroupExaminer)
stores individual's signature in 'GroupExaminer'
Definition: Individual.py:88
def dead(self)
An individual is dead if it is too old or has lost all its 'LifePoints'.
Definition: Individual.py:63
def score(self, bonus=0, FlagSet=False)
Sets score or adds points to score, depending on FlagSet - Returns score value.
Definition: Individual.py:76
def accident(self, loss=1)
The victim suffers from a loss of life points.
Definition: Individual.py:58
def signature(self)
returns age and score
Definition: Individual.py:83
class Phenome: set of non inheritable characteristics
Definition: Phenotype.py:61
class Genome: list of genes carried by individuals
Definition: Genome.py:42
Augmented version of Friends for asymmetrical links - replaces 'Alliances'.
Definition: Alliances.py:320
Definition of phenotype as non inheritable characters.
Definition: Phenotype.py:1
Definition of genes as DNA segments having semantics.
Definition: Genome.py:1
Individuals inherit this class which determines who is friend with whom.
Definition: Alliances.py:1