Exemplo n.º 1
0
    BTGenotype genotypeByFullMethod(int maxDepth)
    {
        BTNode     root = subTreeFullMethod(1, maxDepth, 0);
        BTGenotype ans  = new BTGenotype(root);

        return(ans);
    }
Exemplo n.º 2
0
    public void setBT(BTGenotype btGenotype, int _treeIndex)
    {
        treeIndex = _treeIndex;
        BehaviorTree tree = gameObject.AddComponent <BehaviorTree>();

        if (tree != null)
        {
            //Debug.Log("Creando árbol con index " + _treeIndex);
            tree.StartWhenEnabled = false;
            tree.GetBehaviorSource().HasSerialized = false;
            EntryTask entryTask = new EntryTask();
            entryTask.NodeData = new NodeData();
            tree.GetBehaviorSource().EntryTask = entryTask;
            //Debug.Log("Árbol con index " + _treeIndex + ": " + btGenotype.ToString());
            Task rootTask = getBTfromGenotype(btGenotype.getRoot());
            rootTask.NodeData = new NodeData();
            tree.GetBehaviorSource().RootTask      = rootTask;
            tree.GetBehaviorSource().HasSerialized = true;
            tree.RestartWhenComplete = true;
            tree.EnableBehavior();
        }
        else
        {
            Debug.Log("No se pudo crear el árbol con index " + _treeIndex);
        }
    }
Exemplo n.º 3
0
    BTGenotype getCrossover(BTGenotype p1, BTGenotype p2)
    {
        BTGenotype parent1 = p1.getCopy();
        BTGenotype parent2 = p2.getCopy();

        /*Debug.Log("*******Performing Crossover*******");
         * Debug.Log(parent1.ToString());
         * Debug.Log(parent2.ToString());*/
        BTNode n1 = parent1.getRandomSubTree();
        BTNode n2 = parent2.getRandomSubTree();

        //Debug.Log("N1: " + n1.ToString() + " N2: " + n2.ToString());
        parent1.replaceSubTreeWithRoot(n1.index, n2);
        parent2.replaceSubTreeWithRoot(n2.index, n1);
        parent1.restructureBTIndexes();
        parent2.restructureBTIndexes();
        parent1.pruneGenotype(conditionals, actions);
        parent2.pruneGenotype(conditionals, actions);

        /*Debug.Log("*****Crossover Result*****");
         * Debug.Log(parent1.ToString());
         * Debug.Log(parent2.ToString());*/
        if (Random.Range(0, 100) < 50)
        {
            return(parent1);
        }
        return(parent2);
    }
Exemplo n.º 4
0
    public BTGenotype getCopy()
    {
        BTNode     newRoot = getCopyAux(root);
        BTGenotype ans     = new BTGenotype(newRoot);

        ans.maxDepthAllowed = maxDepthAllowed;
        return(ans);
    }
Exemplo n.º 5
0
    public BTGenotype getNextTree()
    {
        BTGenotype ans = population[obtainableTree];

        numberOfTrees[obtainableTree]++;
        obtainableTree++;
        obtainableTree %= populationSize;
        return(ans);
    }
Exemplo n.º 6
0
    void recoverInitialPopulationFromFile()
    {
        string strPopulation = txtPopulation.text;

        string[] lines = Regex.Split(strPopulation, "\n");
        for (int i = 0; i < populationSize; i++)
        {
            string[] nodesStr = lines[i].Split('_');
            population[i] = new BTGenotype(n, nodesStr, maxInitialDepth * 2);
            //Debug.Log(population[i].ToString());
        }
    }
Exemplo n.º 7
0
    void recoverPopulation()
    {
        string populationStr = PlayerPrefs.GetString("populationStr");

        string[] lines = Regex.Split(populationStr, "\n");
        for (int i = 0; i < populationSize; i++)
        {
            string[] nodesStr = lines[i].Split('_');
            population[i] = new BTGenotype(n, nodesStr, maxInitialDepth * 2);
            //Debug.Log(population[i].ToString());
        }
    }
Exemplo n.º 8
0
 public int CompareTo(BTGenotype other)
 {
     if (other == null)
     {
         return(1);
     }
     if (fitness > other.getFitness())
     {
         return(1);
     }
     if (fitness < other.getFitness())
     {
         return(-1);
     }
     return(0);
 }
Exemplo n.º 9
0
    void populationMutation()
    {
        int totalOfNodes = 0;

        for (int i = 0; i < populationSize; i++)
        {
            totalOfNodes += population[i].numberOfNodes();
        }
        int nodesToModify = (int)(totalOfNodes * mutationFactor);

        for (int i = 0; i < nodesToModify; i++)
        {
            int        index        = Random.Range(0, populationSize);
            BTGenotype genotype     = population[index];
            BTNode     nodeToChange = genotype.getRandomSubTree();
            string     newTaskName  = "";
            if (nodeToChange.isLeafNode())
            {
                int rand = Random.Range(0, 100);
                if (rand < 15)
                {
                    newTaskName = randomTerminalTaskName();
                }
                else
                {
                    if (isActionTask(nodeToChange.taskName))
                    {
                        newTaskName = randomActionTaskName();
                    }
                    else
                    {
                        newTaskName = randomConditionalTaskName();
                    }
                }
            }
            else
            {
                newTaskName = randomInternalTaskName();
            }
            nodeToChange.setTaskName(newTaskName);
        }
    }
Exemplo n.º 10
0
    BTGenotype getFitnessProportionate(List <BTGenotype> group)
    {
        double     r     = Random.Range(0.0f, 1.0f);
        BTGenotype ans   = null;
        double     total = 0.0f;

        for (int i = 0; i < group.Count; i++)
        {
            total += group[i].getFitness();
            if (total > r)
            {
                ans = group[i];
            }
        }
        if (ans == null)
        {
            ans = group[group.Count - 1];
        }
        return(ans);
    }
Exemplo n.º 11
0
    void evolvePopulation()
    {
        normalisePopulationFitness();

        List <BTGenotype> list = new List <BTGenotype>();

        for (int j = 0; j < populationSize; j++)
        {
            list.Add(population[j]);
        }
        list.Sort((a, b) => - 1 * a.CompareTo(b));
        for (int j = 0; j < populationSize; j++)
        {
            population[j] = list[j];
        }

        // create two groups to pick genotypes based on fitness
        List <BTGenotype> group1 = new List <BTGenotype>();
        List <BTGenotype> group2 = new List <BTGenotype>();
        int i = 0;

        for (; i < populationSize * 0.2; i++)
        {
            group1.Add(population[i]);
        }
        for (; i < populationSize; i++)
        {
            group2.Add(population[i]);
        }

        BTGenotype [] temp = new BTGenotype[populationSize];
        for (int j = 0; j < populationSize; j++)
        {
            BTGenotype parent1 = getRandomParent(group1, group2);
            BTGenotype parent2 = getRandomParent(group1, group2);
            temp[j] = getCrossover(parent1, parent2);
        }
        population = temp;
    }