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])])
        else :
            #potentially change function in node
            if py.Util.randomFlipCoin(args["pmut"]):
                mutations += 1
                node.setData(random.choice(allele[0][0]))  
    return mutations
Beispiel #2
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])])
        else:
            #potentially change function in node
            if py.Util.randomFlipCoin(args["pmut"]):
                mutations += 1
                node.setData(random.choice(allele[0][0]))
    return mutations
Beispiel #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
Beispiel #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