SHARE
TWEET

flib

phillip1882 Jul 13th, 2012 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2. def FLIBgen():
  3.    size = random.randint(2,26)
  4.    count = 0
  5.    FLIB = ""
  6.    while count < size:
  7.       FLIB = FLIB +(str(random.randint(0,1)) +chr(random.randint(65,65+size-1))+
  8.       str(random.randint(0,1)) +chr(random.randint(65,65+size-1)) )
  9.       count += 1
  10.    return FLIB
  11.  
  12. def execution(flib,inputval):
  13.    state = "A"
  14.    outputval = ""
  15.    count = 0
  16.    while count < len(inputval):
  17.       outputval =outputval +flib[(ord(state) -65) *4 +2*int(inputval[count])]
  18.       state = flib[(ord(state) -65) *4 +2*int(inputval[count]) +1]
  19.       count += 1
  20.    return outputval
  21.  
  22. def mutation(flib):
  23.    index = random.randint(0,len(flib)-1)
  24.    if index&1 == 1:
  25.       statenum = int(len(flib)/4)
  26.       state = random.randint(0,statenum-1)
  27.       flib = flib[:index] +chr(state+65) +flib[index+1:]
  28.    else:
  29.       state = str(random.randint(0,1))
  30.       flib = flib[:index] +state +flib[index+1:]
  31.    return flib
  32.  
  33. def crossover(flib1, flib2):
  34.    #this could be better; currently appends two flibs, random length
  35.    size1 = len(flib1)
  36.    size2 = len(flib2)
  37.    crossize1 = random.randint(0,size1-1)
  38.    crosspos1 = random.randint(0,size1-crossize1-1)  
  39.    crossize2 = random.randint(0,size2-1)
  40.    crosspos2 = random.randint(0,size2-crossize2-1)
  41.    newflib = flib1[crosspos1:crosspos1+crossize1]+flib2[crosspos2:crosspos2+crossize2]
  42.    return newflib
  43.    
  44. def score(inputval,outputval):
  45.    value1 = int(inputval,2)
  46.    value2 = int(outputval,2)>>1
  47.    size = len(inputval)
  48.    result = bin(value1^value2^(1<<size)).count('0')-1
  49.    return result/size
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