Evolife
Evolife has been developed to study Genetic algorithms, Natural evolution and behavioural ecology.
Phenotype.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2""" @brief Definition of phenotype as non inheritable characters.
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
20import sys
21if __name__ == '__main__': sys.path.append('../..') # for tests
22
23
24
25import random
26from Evolife.Tools.Tools import error
27
28
29
30class Phene:
31 """ class Phene: define a non-heritable characteristics
32 """
33 MaxPheneValue = 100
34
35 def __init__(self, Name, FlagRandom=True):
36 """ creates a zero-valued or a random characteristics, depending on FlagRandom
37 """
38 self.Name = Name
39 if FlagRandom:
40 self.__value = random.randint(0, self.MaxPheneValue)
41 else:
42 self.__value = 0
43
44 def relative_value(self):
45 """ returns the Phene's value between 0 and 100
46 """
47 return (100.0 * self.__value) / self.MaxPheneValue
48
49 def value(self, Value=None, Levelling = False):
50 """ sets or merely reads the Phene's value, possibly by limiting it to MaxPheneValue
51 """
52 if Value is None: return self.__value
53 if Value <= Phene.MaxPheneValue: self.__value = Value
54 elif Levelling: self.__value = Phene.MaxPheneValue
55 else: error("Phenotype: ", "Maximum value exceeded: %f" % Value)
56 return self.__value
57
58 def __str__(self):
59 return self.Name + '=' + "%d" % self.value()
60
61class Phenome:
62 """ class Phenome: set of non inheritable characteristics
63 """
64 def __init__(self, Scenario, FlagRandom = True):
65 """ creates a dictionary of Phenes as defined by Scenario.phenemap()
66 """
67 self.Scenario = Scenario
68 self.Phenes = {PN:Phene(PN,FlagRandom)
69 for PN in self.Scenario.phenemap()}
70
71 def Phene_value(self, name, Value=None, Levelling=False):
72 """ reads or sets the value of a phene
73 """
74 return self.Phenes[name].value(Value, Levelling)
75
76 def Phene_relative_value(self, name):
77 """ returns a Phene's value between 0 and 100
78 """
79 return self.Phenes[name].relative_value()
80
81 def signature(self):
82 """ returns phene values as a list of relative values
83 """
84 return [self.Phenes[PN].relative_value() for PN in self.Scenario.phenemap()]
85
86 def __str__(self):
87 return 'Phenotype:\n ' + ' <> '.join([Ph.__str__() for Ph in self.Phenes.values()])
88
89
90
91
92if __name__ == "__main__":
93 print(__doc__)
94 from Evolife.Scenarii.MyScenario import InstantiateScenario
95 InstantiateScenario('Signalling')
96 from Evolife.Scenarii.MyScenario import MyScenario
97 Ph = Phenome(FlagRandom = True)
98 print(Ph)
99 raw_input('[Return]')
100
101
102__author__ = 'Dessalles'
class Phene: define a non-heritable characteristics
Definition: Phenotype.py:30
def __init__(self, Name, FlagRandom=True)
creates a zero-valued or a random characteristics, depending on FlagRandom
Definition: Phenotype.py:35
def relative_value(self)
returns the Phene's value between 0 and 100
Definition: Phenotype.py:44
def value(self, Value=None, Levelling=False)
sets or merely reads the Phene's value, possibly by limiting it to MaxPheneValue
Definition: Phenotype.py:49
class Phenome: set of non inheritable characteristics
Definition: Phenotype.py:61
def __init__(self, Scenario, FlagRandom=True)
creates a dictionary of Phenes as defined by Scenario.phenemap()
Definition: Phenotype.py:64
def Phene_relative_value(self, name)
returns a Phene's value between 0 and 100
Definition: Phenotype.py:76
def signature(self)
returns phene values as a list of relative values
Definition: Phenotype.py:81
def Phene_value(self, name, Value=None, Levelling=False)
reads or sets the value of a phene
Definition: Phenotype.py:71
value
value += self.__dna[pos]* 2 ** (end - 1 - pos)
Definition: DNA.py:117
Various functions.
Definition: Tools.py:1
def error(ErrMsg, Explanation='')
Definition: Tools.py:182