2""" @brief Various functions. """
4#============================================================================#
5# EVOLIFE http://evolife.telecom-paris.fr Jean-Louis Dessalles
23from math
import floor, modf, log
27 GrayTable = EvolifeGray.GrayCode()
33 """ Computes a decreasing function of x in [0,M] which sums to 1
34 1/(x+M) normalized for x
in [0,M]
39 if x >= 0:
return 1.0/(x+(1.0*M)/Selection) / log(1+Selection)
40 else:
error(
'Tools: decrease called with negative value')
43 try: D = decreaseTable.get((x, M, Selection),
None)
46 decreaseTable = dict()
47 if D
is not None:
return D
49 if Selection: D = (one_value(x) + one_value(x+1))/2
52 if len(decreaseTable) < 2000:
53 decreaseTable[(x, M, Selection)] = D
63 """ computes what one gets from a maximum of N with probability proba
66 if random.random() < modf(C)[0]:
71 """ computes random uniform variable between 0 and Max
73 if isinstance(Max, int)
and Max > 1:
74 return random.randint(0, Max) <= proba
76 return Max * random.random() <= proba
79 """ draws one one the pie shares y picking a location uniformly
81 if Probabilities == []:
error(
'Calling Fortune Wheel with no probabilities')
82 Lottery = random.uniform(0, sum(Probabilities))
84 for p
in enumerate(Probabilities):
86 if P >= Lottery:
break
92 """ returns x affected by a multiplicative uniform noise
93 between 1-range_/100 and 1+range_/100
96 error("Tools: noise amplitude",str(range_))
97 return x * (1.0 +
percent((2 * random.random() - 1) * range_))
100 """ returns x affected by an additive uniform noise
101 between -range_ and range_
103 return x + ((2 * random.random() - 1) * range_)
105def transpose(Matrix):
106 """ groups ith items
in each list of Matrix
109 #This genial version is much too slow
110 #return reduce(lambda x, y: map(lambda u,v: u+v,x,y),
111 # [map(lambda x: [x],L)
for L
in Matrix])
124 return list(zip(*Matrix))
128 """ converts a number into letters - Useful to list files in correct order
130 A = chr(ord(
'a') + Nb // 676)
131 A += chr(ord(
'a')+ (Nb % 676) // 26)
132 A += chr(ord(
'a')+ Nb % 26)
136 """ converts a number into a padded string
138 return (
"000000" + str(Nb))[-6:]
142 """ computes a polygon function crossing all points in Points
153 if p[0] == found[0]:
return (p[1] - found[1])/2
154 return (found[1] + (x - found[0]) * (p[1] - found[1]) / (p[0] - found[0]))
159 """ Analyses the content of a file and returns all matching occurrences of Pattern
161 Filin = open(FileName,
"r")
162 FContent = Filin.read() +
'\n'
164 R = re.findall(Pattern,FContent,flags=Flag)
168 """ Saves a list of strings into a file
170 Filout = open(FileName,
"w")
171 Filout.write(
'\n'.join(L))
184 print(
"\n\n******** ERROR ************")
186 if Explanation: print(Explanation)
187 print(
"************ ERROR ********\n")
198 print(
"\n-------- WARNING -------- %s %s -------- WARNING --------\n" % (WMsg, Explanation))
202 """ memory buffer with limited length
215 self.
past.append(Item)
225 if self.
past != []:
return self.
past[-1]
229 if self.
past != []:
return self.
past.pop()
237 return str(self.
past)
250 if os.path.exists(
'/usr/local/lib/python/site-packages'):
252 sys.path.append(
'/usr/local/lib/python/site-packages/')
260 return "Boosting with Psyco : %s" % UsePsyco
265if __name__ ==
"__main__":
271 for R
in os.walk(os.path.abspath(
'.')[0:os.path.abspath(
'.').find(
'Evo')]):
272 if os.path.exists(os.path.join(R[0],
'Evolife',
'__init__.py')):
273 sys.path.append(R[0])
279 from Evolife.Scenarii.Parameters
import Parameters
283 S = P.Parameter(
'Selectivity')
284 print(
"selectivity = ", int(S))
285 print([ (x,
decrease(x,M,S))
for x
in range(M)])
286 print(sum([
decrease(x,M,S)
for x
in range(M)]))
297__author__ =
'Dessalles'