def buildGTreeGrowWithConstants(depth, value_callback, value_leaf, max_siblings, max_depth):
    ''' this is the same as the previous function but leaves contain a constant and variable
    the constant is decided by the module of control of randomization
    '''
    random_value = random.choice(value_callback)
    random_value_leaf =  [rc.new_constant(),random.choice(value_leaf)]
    n = py.GTree.GTreeNode(0)

    if depth == max_depth: 
        n.setData(random_value_leaf)
        return n

    if py.Util.randomFlipCoin(0.5) :
        child = buildGTreeGrowWithConstants(depth + 1, value_callback, value_leaf, max_siblings, max_depth)
        child.setParent(n)
        n.addChild(child)
        
        child = buildGTreeGrowWithConstants(depth + 1, value_callback, value_leaf, max_siblings, max_depth)
        child.setParent(n)
        n.addChild(child)
    
    if n.isLeaf() : 
        n.setData(random_value_leaf)
    else :
        n.setData(random_value)
    return n
Esempio n. 2
0
def buildGTreeGrowWithConstants(depth, value_callback, value_leaf, max_depth):
    ''' this is the same as the previous function but leaves contain a constant and variable
    the constant is decided by the module of control of randomization
    '''
    random_value = random.choice(value_callback)
    random_value_leaf = [rc.new_constant(), random.choice(value_leaf)]
    n = py.GTree.GTreeNode(0)

    if depth == max_depth/2:
        n.setData(random_value_leaf)
        return n

    if py.Util.randomFlipCoin(0.5):
        child = buildGTreeGrowWithConstants(depth + 1, value_callback, value_leaf, max_depth)
        child.setParent(n)
        n.addChild(child)

        child = buildGTreeGrowWithConstants(depth + 1, value_callback, value_leaf, max_depth)
        child.setParent(n)
        n.addChild(child)

    if n.isLeaf():
        n.setData(random_value_leaf)
    else:
        n.setData(random_value)
    return n
Esempio n. 3
0
def mutate_tree_with_constants(genome, **args):
    ''' this mutates the constant part, the variable part of the leaves or the function in a node
    mutations add a random number to previous constant
    mutations change function and variable randomly
    '''
    mutations = 0
    allele = genome.getParam("allele")
    for node in genome.getAllNodes():
        if node.isLeaf():
            # potentially change constant in leaf
            if py.Util.randomFlipCoin(args["pmut"]):
                mutations += 1
                constant, variable = node.getData()
                node.setData([rc.mutated_constant(constant), variable])
            # potentially change the variable in leaf
            if py.Util.randomFlipCoin(args["pmut"]):
                mutations += 1
                constant, variable = node.getData()
                node.setData([constant, random.choice(allele[0][1])])
            # potentially change leaf to tree
            if genome.getNodeDepth(node) != genome.getParam("max_depth") and py.Util.randomFlipCoin(args["pmut"] )  :
                mutations += 1
                node.setData(random.choice(allele[0][0]))
                child1 = py.GTree.GTreeNode(0)
                child1.setData([rc.new_constant(), random.choice(allele[0][1])])
                child1.setParent(node)
                node.addChild(child1)
                child2 = py.GTree.GTreeNode(0)
                child2.setData([rc.new_constant(), random.choice(allele[0][1])])
                child2.setParent(node)
                node.addChild(child2)
                genome.processNodes()
        else:
            # potentially change function in node
            if py.Util.randomFlipCoin(args["pmut"]):
                mutations += 1
                node.setData(random.choice(allele[0][0]))
            if genome.getNodeHeight(node) == 1:
                # potentially change node to leaf
                if py.Util.randomFlipCoin(args["pmut"] ):
                    mutations += 1
                    node.setData([rc.new_constant(), random.choice(allele[0][1])])
                    node.getChilds()[:] = []
                    genome.processNodes()
    return mutations
Esempio n. 4
0
def mutate_tree_with_constants(genome, **args):
    ''' this mutates the constant part, the variable part of the leaves or the function in a node
    mutations add a random number to previous constant
    mutations change function and variable randomly
    '''
    mutations = 0
    allele = genome.getParam("allele")
    for node in genome.getAllNodes():
        if node.isLeaf():
            # potentially change constant in leaf
            if py.Util.randomFlipCoin(args["pmut"]):
                mutations += 1
                constant, variable = node.getData()
                node.setData([rc.mutated_constant(constant), variable])
            # potentially change the variable in leaf
            if py.Util.randomFlipCoin(args["pmut"]):
                mutations += 1
                constant, variable = node.getData()
                node.setData([constant, random.choice(allele[0][1])])
            # potentially change leaf to tree
            if py.Util.randomFlipCoin(args["pmut"] / 10):
                mutations += 1
                node = buildGTreeGrowWithConstants(0, allele[0][0], allele[0][1], 1)
                genome.processNodes()
        else:
            # potentially change function in node
            if py.Util.randomFlipCoin(args["pmut"]):
                mutations += 1
                node.setData(random.choice(allele[0][0]))
            if genome.getNodeHeight(node) == 1:
                # potentially change node to leaf
                if py.Util.randomFlipCoin(args["pmut"] / 10):
                    mutations += 1
                    node.setData([rc.new_constant(), random.choice(allele[0][1])])
                    node.getChilds()[:] = []
                    genome.processNodes()
    return mutations