예제 #1
0
    public static void PrintTree(BehaviourNode tree, string indent, bool last)
    {
        Debug.Log(indent + "+- " + tree.GetType());

        indent += last ? "   " : "|  ";
        if (tree.getNextNodes() != null)
        {
            for (int i = 0; i < tree.getNextNodes().Count; i++)
            {
                if (tree.getNextNodes()[i] != null)
                {
                    PrintTree(tree.getNextNodes()[i], indent, i == tree.getNextNodes().Count - 1);
                }
            }
        }
    }
 private void iterate(BehaviourNode bn)
 {
     if (bn.getNextNodes() != null && bn.getNextNodes().Count > 0)
     {
         for (int i = 0; i < bn.getNextNodes().Count; i++)
         {
             if (bn.getNextNodes()[i] != null)
             {
                 iterate(bn.getNextNodes()[i]);
             }
             if (UnityEngine.Random.Range(0.0f, 1.0f) > mutationRate)
             {
                 bn.mutate();
             }
         }
     }
 }
예제 #3
0
    public BehaviourNode getNodeAtDepth(int depthOfRandomNode)
    {
        BehaviourNode currentNode = root;

        for (int i = 0; i < depth; i++)
        {
            currentNode = currentNode.getNextNodes() [Random.Range(0, 1)];
        }
        return(currentNode);
    }
예제 #4
0
    //iterate over tree
    public void iterate(BehaviourNode bn, BehaviourNode parent, BotController bc)
    {
        if (bn.getNextNodes() != null)
        {
            //Debug.Log("iterating over children");

            foreach (BehaviourNode child in bn.getNextNodes())
            {
                if (child != null)
                {
                    iterate(child, bn, bc);
                }
            }
        }
        if (parent != null && parent is IFunction)
        {
            ((FunctionNode)parent).fillParams(bn.fire(bc)); // should not be like this!
            //--> if everything is write rewrite fire => fire(bc)
        }
    }
예제 #5
0
    private int calculateHeight(BehaviourNode root)
    {
        int max = 0;

        if (root != null && root.getNextNodes() != null)
        {
            foreach (BehaviourNode childNode in root.getNextNodes())
            {
                if (childNode != null)
                {
                    int height = calculateHeight(childNode);
                    if (height > max)
                    {
                        max = height;
                    }
                }
            }
        }
        return(max + 1);
    }
예제 #6
0
    public override List <BotBehavior> mutate(List <BotBehavior> individuals, float mutationProbability)
    {
        foreach (BotBehavior bh in individuals)
        {
            bool proceedDepth   = UnityEngine.Random.Range(0.0f, 1.0f) <= depthOfMutation;
            bool excuteMutation = UnityEngine.Random.Range(0.0f, 1.0f) <= mutationRate;

            BehaviourNode childNode = bh.getRoot();

            if (excuteMutation)
            {
                int           indexOfRemoval     = 0;
                BehaviourNode parent             = null;
                int           depthOfCurrentNode = 0;
                while (proceedDepth && childNode.getNextNodes() != null && childNode.getNextNodes().Count != 0)
                {
                    //Debug.Log ("Childnode count " + childNode.getNextNodes ().Count);
                    indexOfRemoval = UnityEngine.Random.Range(0, childNode.getNextNodes().Count);
                    parent         = childNode;
                    //Debug.Log ("indexOfRemoval " + indexOfRemoval);
                    childNode = parent.getNextNodes()[indexOfRemoval];
                    depthOfCurrentNode++;
                    proceedDepth = UnityEngine.Random.Range(0.0f, 1.0f) <= depthOfMutation;
                }
                if (parent != null)
                {
                    // remove node and grow new tree starting from this new node
                    int depth = calculateDepth();
                    if (depth > (bh.getDepth() - depthOfCurrentNode) * maxDepthIncrease)
                    {
                        depth = Mathf.FloorToInt((float)((bh.getDepth() - depthOfCurrentNode) * maxDepthIncrease));
                    }
                    bool isTerminal = UnityEngine.Random.Range(0.0f, 1.0f) <= chanceOfTerminal;
                    parent.getNextNodes()[indexOfRemoval] = bh.innerGrowTree(depth, functions, terminals, isTerminal);
                }
            }
        }
        return(individuals);
    }
예제 #7
0
    public void mutate(BehaviourNode current, BehaviourNode parent, int i)
    {
        if (current is IFunction && Random.Range(0, 1) < functionMutationRate && parent != null)
        {
            int           randomFunction = Random.Range(0, functions.Count - 1);
            BehaviourNode function       = (BehaviourNode)System.Activator.CreateInstance(functions[randomFunction]);
            function.setNextNodes(current.getNextNodes());
            parent.getNextNodes()[i] = function;
        }
        else if (Random.Range(0, 1) < terminalMutationRate)
        {
            current.mutate();
        }
        int index = 0;

        foreach (BehaviourNode
                 child in current.getNextNodes())
        {
            index++;
            mutate(child, current, index);
        }
    }
예제 #8
0
    private void iterate(BehaviourNode bn, int indexInParent, BehaviourNode parent, ref BehaviourNode res, ref int resIndex, int count)
    {
        // add self as possible sample
        // check if their are childeren
        // first itterate over childeren


        count++;// new element discovered
        double factor  = bn is IFunction == true ? probabilityFunctionNode : 1 - probabilityFunctionNode;
        bool   replace = UnityEngine.Random.Range(0, count) < factor;

        if (replace)
        {
            res      = parent;
            resIndex = indexInParent;
        }
        if (bn.getNextNodes().Count > 0)
        {
            for (int i = 0; i < bn.getNextNodes().Count; i++)
            {
                iterate(bn.getNextNodes()[i], i, bn, ref res, ref resIndex, count);
            }
        }
    }
예제 #9
0
    public void mutate(float mutationProb)
    {
        Queue <BehaviourNode> toLoop = new Queue <BehaviourNode> ();

        while (toLoop.Count != 0)
        {
            BehaviourNode bn = toLoop.Dequeue();
            if (rollDice(mutationProb))
            {
                bn.mutate();
            }
            foreach (BehaviourNode bhvn in bn.getNextNodes())
            {
                toLoop.Enqueue(bhvn);
            }
        }
    }