<jupyter_start><jupyter_text>Representing Progarm by Tree<jupyter_code>from random import random,randint,choice
from copy import deepcopy
from math import log

class fwrapper:
    def __init__(self,function,childcount,name):
        self.function=function
        self.childcount=childcount
        self.name=name
class node:
    def __init__(self,fw,children):
        self.function=fw.function
        self.name=fw.name
        self.children=children
    def evaluate(self,inp):
        results=[n.evaluate(inp) for n in self.children]
        return self.function(results)
    def display(self,indent=0):
        print (' '*indent, self.name)
        for c in self.children:
            c.display(indent+1)
    
class paramnode:
    def __init__(self,idx):
        self.idx=idx
    def evaluate(self,inp):
        return inp[self.idx]
    def display(self,indent=0):
         print('%sp%d' % (' '*indent,self.idx))
    
class constnode:
    def __init__(self,v):
        self.v=v
    def evaluate(self,inp):
        return self.v
    def display(self,indent=0):
         print('%s%d' % (' '*indent,self.v))<jupyter_output><empty_output><jupyter_text>Define Functions<jupyter_code>addw=fwrapper(lambda l:l[0]+l[1],2,'add')
subw=fwrapper(lambda l:l[0]-l[1],2,'subtract')
mulw=fwrapper(lambda l:l[0]*l[1],2,'multiply')
def iffunc(l):
    if l[0]>0: return l[1]
    else: return l[2]
ifw=fwrapper(iffunc,3,'if')

def isgreater(l):
    if l[0]>l[1]: return 1
    else: return 0
gtw=fwrapper(isgreater,2,'isgreater')

flist=[addw,mulw,ifw,gtw,subw]<jupyter_output><empty_output><jupyter_text>Example ProgramBelow is a tree representation of a simple program, involving one if-statement.<jupyter_code>def example(p0, p1):
    if p0 > 3:
        return p1 + 5
    else:
        return p1 - 2
def exampletree(): 
    return node(ifw,[node(gtw,[paramnode(0),constnode(3)]),
                     node(addw,[paramnode(1),constnode(5)]),
                       node(subw,[paramnode(1),constnode(2)])])
tree = exampletree()
tree.evaluate([2,3])
tree.display()<jupyter_output>if
  isgreater
  p0
  3
  add
  p1
  5
  subtract
  p1
  2<jupyter_text>Initial Population<jupyter_code>def makerandomtree(pc,maxdepth=4,fpr=0.5,ppr=0.6): 
    if random()<fpr and maxdepth>0:
        f=choice(flist)
        children=[makerandomtree(pc,maxdepth-1,fpr,ppr)
               for i in range(f.childcount)]
        return node(f,children)
    elif random()<ppr:
        return paramnode(randint(0,pc-1))
    else:
        return constnode(randint(0,10))
random1=makerandomtree(2, 0.9)
random1.evaluate([7,1])
random1.display()
random2=makerandomtree(2)
random2.evaluate([5,3])
random2.display()<jupyter_output>2<jupyter_text>Testing<jupyter_code>def hiddenfunction(x,y):
    return x*y+5

def buildhiddenset(): 
    rows=[]
    for i in range(200):
        x=randint(0,40)
        y=randint(0,40)
        rows.append([x,y,hiddenfunction(x,y)])
    return rows

hiddenset=buildhiddenset()
# measure error using mean absolute error
def scorefunction(tree,s):
    dif=0
    for data in s:
        v=tree.evaluate([data[0],data[1]])
        dif+=abs(v-data[2])
    return dif
scorefunction(random1,hiddenset)<jupyter_output><empty_output><jupyter_text>Mutation<jupyter_code>def mutate(t,pc,probchange=0.1): 
    if random()<probchange:
        return makerandomtree(pc)
    else:
        result=deepcopy(t)
        if isinstance(t,node):
            result.children=[mutate(c,pc,probchange) for c in t.children]
        return result
random2.display()
muttree=mutate(random2,2)
muttree.display()<jupyter_output>2<jupyter_text>Crossover<jupyter_code>def crossover(t1,t2,probswap=0.7,top=1): 
    if random()<probswap and not top:
        return deepcopy(t2)
    else:
        result=deepcopy(t1)
        if isinstance(t1,node) and isinstance(t2,node):
            result.children=[crossover(c,choice(t2.children),probswap,0)
                            for c in t1.children]
        return result
random1=makerandomtree(2)
random1.display()
random2=makerandomtree(2)
random2.display()
cross=crossover(random1,random2)
cross.display()<jupyter_output>add
  if
  p0
   if
   p1
    add
    5
    p1
   p0
  p1
  isgreater
  6
  5<jupyter_text>Environment<jupyter_code>def evolve(pc,popsize,rankfunction,maxgen=500,
                mutationrate=0.1,breedingrate=0.4,pexp=0.7,pnew=0.05):
    # Returns a random number, tending towards lower numbers. 
    # The lower pexp # is, more lower numbers you will get
    def selectindex():
        return int(log(random())/log(pexp))
    # Create a random initial population
    population=[makerandomtree(pc) for i in range(popsize)]
    for i in range(maxgen):
        scores=rankfunction(population)
        print("Generation %i, cost: %i"%(i, scores[0][0]))
        if scores[0][0]==0: break
        # The two best always make it
        newpop=[scores[0][1],scores[1][1]]
        
        # Build the next generation
        while len(newpop)<popsize:
            if random()>pnew: 
                newpop.append(mutate(crossover(scores[selectindex( )][1], 
                                               scores[selectindex( )][1],
                                               probswap=breedingrate),
                                     pc,probchange=mutationrate))
            else:
                # Add a random node to mix things up
                newpop.append(makerandomtree(pc))
        population=newpop 
    return scores[0][1]
def getrankfunction(dataset):
    def rankfunction(population):
        scores=[(scorefunction(t,dataset),t) for t in population] 
        scores.sort(key = lambda i : i[0])
        return scores
    return rankfunction<jupyter_output><empty_output><jupyter_text>Evolution / Training<jupyter_code>rf=getrankfunction(buildhiddenset())
best = evolve(2,500,rf,mutationrate=0.2,breedingrate=0.1,pexp=0.7,pnew=0.2)
best.display( )<jupyter_output>add
  isgreater
   multiply
   0
    multiply
    1
     if
     2
     8
     p0
   isgreater
   p1
    add
     multiply
     p1
     p1
    0
  add
   add
   5
    multiply
    p1
    p0
   isgreater
    subtract
    p1
    p1
    isgreater
    p1
     isgreater
      multiply
      p1
      8
     6
{'fraction_non_alphanumeric': 0.12165214585350113, 'fraction_numerical': 0.028557599225556632, 'mean_word_length': 2.8840852130325816, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '396401', 'license': 'MIT', 'n_tokens_mistral': 2243, 'n_tokens_neox': 2174, 'n_words': 400, 'path': 'models/genetic_programming/tutorial.ipynb', 'repo_name': 'shawlu95/Data_Science_Toolbox', 'size': '6198'}