Skip to content

Instantly share code, notes, and snippets.

@DM-
Created August 31, 2012 18:05
Show Gist options
  • Select an option

  • Save DM-/3556645 to your computer and use it in GitHub Desktop.

Select an option

Save DM-/3556645 to your computer and use it in GitHub Desktop.
import random
def avg(x):
if type(x) != int:
return sum(x)/float(len(x))
else:
return x
class liver(object):
"""containes genes and probability of life"""
def __init__(self, genes=0):
self.genes = genes
self.age = 0
if genes == 0:
self.genes = []
for i in xrange(10):
self.genes.append(random.gauss(50,20))
if self.genes[i] > 100:
self.genes[i] = 100
if self.genes[i] < 0:
self.genes[i] = 0
self.prob = avg(self.genes)
def mutate(self):
for i,o in enumerate(self.genes):
o+=random.gauss(0,5)
if o > 100:
o=100
if o < 0 :
o=0
self.genes[i]=o
self.prob = avg(self.genes)
def age(self,x):
self.age+=x
class Land(object):
"""Land of livers"""
def __init__(self, numlive=10):
self.popcount = numlive
self.pop=[]
for i in xrange(self.popcount):
self.pop.append(liver())
self.avg = self.findavg()
self.best = self.findrank(1)
self.worst = self.findrank(self.popcount)
def findrank(self,rank):
a = sorted(self.pop, key= lambda live : live.prob)
return a[-rank]
def findavg(self):
ave = 0
for i in self.pop:
ave+= i.prob
return ave/float(self.popcount)
def upstats(self):
self.avg = self.findavg()
self.best = self.findrank(1)
self.worst = self.findrank(self.popcount)
def upstate(self):
for i in xrange(self.popcount):
self.pop[i]=liver(self.best.genes)
self.pop[i].mutate()
#fucks sake, there's a bug in either ^ or V and it's annoying as fuck.
def update(self):
self.upstats()
self.upstate()
def sst(self):
print self.avg
print self.best.prob
topia = Land()
x=10
while x > 0:
topia.upstats()
print(str(topia.best.prob)+" is the best, "+str(topia.avg)+" is the avg, "+str(topia.worst.prob)+" is the worst")
topia.update()
x-=0
if __name__ == '_ _main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment