Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def FLIBgen():
- size = random.randint(2,26)
- count = 0
- FLIB = ""
- while count < size:
- FLIB = FLIB +(str(random.randint(0,1)) +chr(random.randint(65,65+size-1))+
- str(random.randint(0,1)) +chr(random.randint(65,65+size-1)) )
- count += 1
- return FLIB
- def execution(flib,inputval):
- state = "A"
- outputval = ""
- count = 0
- while count < len(inputval):
- outputval =outputval +flib[(ord(state) -65) *4 +2*int(inputval[count])]
- state = flib[(ord(state) -65) *4 +2*int(inputval[count]) +1]
- count += 1
- return outputval
- def mutation(flib):
- index = random.randint(0,len(flib)-1)
- if index&1 == 1:
- statenum = int(len(flib)/4)
- state = random.randint(0,statenum-1)
- flib = flib[:index] +chr(state+65) +flib[index+1:]
- else:
- state = str(random.randint(0,1))
- flib = flib[:index] +state +flib[index+1:]
- return flib
- def crossover(flib1, flib2):
- #this could be better; currently appends two flibs, random length
- size1 = len(flib1)
- size2 = len(flib2)
- crossize1 = random.randint(0,size1-1)
- crosspos1 = random.randint(0,size1-crossize1-1)
- crossize2 = random.randint(0,size2-1)
- crosspos2 = random.randint(0,size2-crossize2-1)
- newflib = flib1[crosspos1:crosspos1+crossize1]+flib2[crosspos2:crosspos2+crossize2]
- return newflib
- def score(inputval,outputval):
- value1 = int(inputval,2)
- value2 = int(outputval,2)>>1
- size = len(inputval)
- result = bin(value1^value2^(1<<size)).count('0')-1
- return result/size
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement