2""" @brief Simple trial-and-error learning mechanism.
5#============================================================================#
6# EVOLIFE http://evolife.telecom-paris.fr Jean-Louis Dessalles
20if __name__ ==
'__main__':
22 sys.path.append(
'../..')
27from random
import random, randint
33 """ General functions: Closer, Perturbate, Limitate, Decrease
36 """ Definitions of Closer, Perturbate, Limitate, Decrease
39 self.
Closer =
lambda x, Target, Attractiveness: ((100.0 - Attractiveness) * x + Attractiveness * Target) / 100
41 self.
Perturbate =
lambda x, Amplitude: x + (2 * random() - 1) * Amplitude
43 self.
Limitate =
lambda x, Min, Max: min(max(x,Min), Max)
45 self.
Decrease =
lambda x, MaxX, MinY: max(MinY, (100 - x * ((100.0 - MinY)/ MaxX)))
51 """ memory buffer with limited length
54 if self.
past: features = list(self.
past[0][0].keys())
57 Res +=
','.join([f
"{f[0]}:{b[0][f]:0.1f}" for f
in features
if f ==
'Signal'])
58 Res += f
": {b[1]:0.1f} -- "
62 """ defines learning capabilities
64 def __init__(self, Features, MemorySpan=5, AgeMax=100, Infancy=0, Imitation=0, Speed=3,
65 JumpProbability=0, Conservatism=0, LearningSimilarity=10, toric=False, Start=-1):
66 """ Features : Dictionary or list of features that will be learned
67 MemorySpan: size of memory
68 Scores : memory of past benefits
69 AgeMax: Max age before resetting
70 Performance : stores current performances
71 Infancy : percentage of lifetime when the learner is considered a child
72 Imitation : forced similarity wiht neighbouring values when learning continuous function
73 Speed : learning speed
74 JumpProbability: Probability of jumping far
from last value
75 Conservatism: Importance
in % of immediate past solutions
76 LearningSimilarity = LearningSimilarity
77 Influence of neighbouring feature values when retrieving best past feature value.
78 Between 0.1 (
or so)
and 100.
79 Influence of NeighbVal on Val
is LearningSimilarity / abs(Val - NeighbVal)
80 10 means that a feature that differs by 30 contributes up to 1/3 of its value.
81 0.1
or so would cancel the effect of neighbouring feature values.
83 If
True, learning space
is circular (toric): maximal feature values are next to smallest values.
84 Start: Features are created random (-1)
or all-zero (0)
or all-100 (1)
99 self.
Reset(Newborn=
False)
102 """ Initializes Feature values to random values (if Start == -1)
103 Age set to random value if Newborn
is False (useful at start)
108 if self.
Start == -1
or Newborn: Features[F] = randint(0,100)
109 else: Features[F] = 100 * self.
Start
114 """ adult if age larger than AgeMax*Infancy/100
119 """ reads or sets feature value
121 if F
is None: F = list(self.
Features.keys())[0]
122 if Value
is not None: self.
Features[F] = Value
126 if self.
Toric:
return (x % Max)
127 else:
return Gbl.Limitate(x, Min, Max)
131 """ The individual moves its own feature closer to its models' features
134 TrueModels = [m
for m
in models
if m.adult()]
136 ModelValues = list(map(
lambda x: x.feature(Feature), TrueModels))
137 Avg = float(sum(ModelValues)) / len(ModelValues)
143 """ Retrieves the best (or the second best) solution so far
145 if len(self.
Scores) == 0: Best =
None
149 past = self.
Scores.retrieve()
150 Best = max(past, key =
lambda x: x[1])
155 Best = max(past, key =
lambda x: x[1])
159 """ Alternative to bestRecord that aggregates similar feature values
161 if len(self.
Scores) == 0:
return None
163 for i, (B1, Perf1)
in enumerate(self.
Scores.retrieve()):
164 for j, (B2, Perf2)
in enumerate(self.
Scores.retrieve()):
168 Perf1 = (Perf1 + Perf2 / (1 + dist)) * (1 + dist)/(2 + dist)
169 Best[B1[Feature]] = Perf1
170 return max(Best, key=Best.get)
173 """ Averaging past scores
176 return sum([p[1]
for p
in self.
Scores.retrieve()]) / len(self.
Scores)
180 """ A looser has full experience and bad results
184 def explore(self, Feature, Speed, Bottom=0, Top=100):
185 """ the individual changes its feature values
189 except (TypeError, IndexError): Best = self.
Features[Feature]
190 Target = self.
Limitate(Gbl.Perturbate(Best, Speed), Bottom, Top)
194 def Learns(self, neighbours=None, Speed=None, hot=False, BottomValue=0, TopValue=100):
195 """ Learns by randomly changing current value.
196 Starting point depends on previous success and on neighbours.
197 If
'hot' is true, perturbation
is larger
for children
206 if self.
Age == 1:
return False
209 FeatureNames = list(self.
Features.keys())
212 for F
in FeatureNames: self.
feature(F, self.
imitate(neighbours, F))
215 if Speed
is None: Speed = self.
Speed
216 if hot
and not self.
adult():
217 LearningSpeed = Gbl.Decrease(self.
Age, self.
Infancy, Speed)
218 else: LearningSpeed = Speed
221 for F
in FeatureNames:
223 self.
feature(F, self.
explore(F, LearningSpeed, Bottom=BottomValue, Top=TopValue))
234 def __str__(self):
return str(self.
Features)
243if __name__ ==
"__main__":
245 print(Learner.__doc__ +
'\n\n')
249 raw_input(
'[Return]')
252__author__ =
'Dessalles'
General functions: Closer, Perturbate, Limitate, Decrease.
def __init__(self)
Definitions of Closer, Perturbate, Limitate, Decrease.
defines learning capabilities
def loser(self)
A looser has full experience and bad results.
def feature(self, F, Value=None)
reads or sets feature value
def bestFeatureRecord(self, Feature)
Alternative to bestRecord that aggregates similar feature values.
def wins(self, Points)
stores a benefit
def imitate(self, models, Feature)
The individual moves its own feature closer to its models' features.
def avgRecord(self)
Averaging past scores.
def Reset(self, Newborn=True)
Initializes Feature values to random values (if Start == -1) Age set to random value if Newborn is Fa...
def Learns(self, neighbours=None, Speed=None, hot=False, BottomValue=0, TopValue=100)
Learns by randomly changing current value.
def Limitate(self, x, Min, Max)
def bestRecord(self, second=False)
Retrieves the best (or the second best) solution so far.
def __init__(self, Features, MemorySpan=5, AgeMax=100, Infancy=0, Imitation=0, Speed=3, JumpProbability=0, Conservatism=0, LearningSimilarity=10, toric=False, Start=-1)
Features : Dictionary or list of features that will be learned MemorySpan: size of memory Scores : me...
def explore(self, Feature, Speed, Bottom=0, Top=100)
the individual changes its feature values
def adult(self)
adult if age larger than AgeMax*Infancy/100
memory buffer with limited length