SHARE
TWEET

Untitled

a guest Feb 23rd, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #GEN.py
  2.  
  3. from __future__ import division
  4. from random import random
  5. randstr=lambda n:map(lambda x: round(random())==1,range(n))
  6.  
  7. def nums(bibit):
  8.     small=map(lambda x:x+1,range(9))
  9.     large=[25,50,75,100]
  10.     out=[[],[]]
  11.     n=2
  12.     for i in [0,1]:
  13.         if bibit[i]:n=n+(i+1)
  14.         else:None
  15.     for i in range(n):
  16.         r=int(round(random()*9.98-.49))
  17.         out[0].append(small[r%len(small)])
  18.         del small[r%len(small)]
  19.     for i in range(6-n):
  20.         r=int(round(random()*9.98-.49))
  21.         out[1].append(large[r%len(large)])
  22.         del large[r%len(large)]
  23.     return out
  24.  
  25. def evalu(nbits,obits,numbers):
  26.     stri=''
  27.     nu=[]
  28.     o=[]
  29.     for i in range(int(len(nbits)/3)):
  30.         if not len(numbers[0])+len(numbers[1]): break
  31.         n=(lambda: (1 if nbits[i*3] else 0)+(2 if nbits[i*3+1] else 0))()
  32.         if (nbits[i*3+2] or not len(numbers[0])) and len(numbers[1]):
  33.             nu.append(numbers[1][n%len(numbers[1])])
  34.             del numbers[1][n%len(numbers[1])]
  35.         else:
  36.             print numbers
  37.             print len(numbers[0])
  38.             nu.append(numbers[0][n%len(numbers[0])])
  39.             del numbers[0][n%len(numbers[0])]
  40.     for i in range(int(len(obits)/3)):
  41.         if not len(numbers[0])+len(numbers[1]): break
  42.         n=(lambda: (1 if obits[i*3] else 0)+(2 if obits[i*3+1] else 0))()
  43.         if (obits[i*3+2] or not len(numbers[0])) and len(numbers[1]):
  44.             nu.append(numbers[1][n%len(numbers[1])])
  45.             del numbers[1][n%len(numbers[1])]
  46.         else:
  47.             print numbers
  48.             print len(numbers[0])
  49.             nu.append(numbers[0][n%len(numbers[0])])
  50.             del numbers[0][n%len(numbers[0])]
  51.     for i in range(len(nu)-1):
  52.         stri=stri+str(nu[i])+o[i]
  53.     stri=stri+nu[len(nu)-1]
  54.     return stri
  55. class gen():
  56.     def __init__(self,popsize,goal,numbers,mergetype,fitnesstest):
  57.         self.goal=goal
  58.         self.numbers=numbers
  59.         self.population=self.prodpop(popsize)
  60.         self.mergetype=mergetype
  61.     def prodpop(self,size):
  62.         out=[]
  63.         return map(lambda x: (lambda y:[y,[evalu(y[0:12],y[12:24],self.numbers)],0,0])(randstr(size)),range(size))
  64.     def getfitness(self,val,goal):
  65.         dif=abs(val-goal)
  66.         if dif == 0:
  67.             return 10
  68.         if dif < 10:
  69.             return 5
  70.         elif dif < 100:
  71.             return 3
  72.         elif dif < 200:
  73.             return 2
  74. #for i in [[0,1],[0,0],[1,0],[1,1]]:
  75. #    print nums(i)
  76. #    print int(round(random()*1000))
  77. a=gen(24,random()*1000,nums([1,0]),0,0)
  78.  
  79.  
  80.  
  81.  
  82. ##METAGEN.py
  83.  
  84. from random import random
  85. from gen import makegen
  86. gen=0
  87. tests=
  88. [[1000,(40,25,5,4)],
  89.  [900,(40,25,5,4,8)],
  90.  [100,(40,25,5,4,10,50)]
  91.     ]
  92. PopulationSize=10
  93. randstr=lambda:map(lambda x: round(random())==1,range(PopulationSize))
  94. Pop=map(lambda x:[randstr(),0,0],range(PopulationSize)) # [Values,Fitness,Inherited Fitness]
  95.  
  96. while gen < 5:
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top