Example #1
0
        public NervousSystem()
        {
            this.inputGridLength = ConfigurationManager.Instance.inputGridSize;
            this.nodes           = new List <Node>(inputGridLength * inputGridLength);
            this.outputNodes     = new List <Node>();
            this.connections     = new List <Connection>();
            // Adding the input nodes
            int count = 0;

            for (int i = 0; i < inputGridLength * inputGridLength; i++)
            {
                Node anIn = new Node('i', count);
                nodes.Add(anIn);
                count++;
            }
            // Health Input node
            nodes.Add(new Node('i', count));
            count++;
            // Adding the output nodes
            for (int i = 0; i < 6; i++)
            {
                Node anOut = new Node('o', count);
                count++;
                nodes.Add(anOut);
                outputNodes.Add(anOut);
            }
            // Add a single random connection.
            NeuroEvolution.MutateByConnection(this);
        }
Example #2
0
        /// <summary>
        /// Create a new colony based off a single parent for now.
        ///
        /// Mutate each nervous system by the proper probabilities before passing it on.
        /// </summary>
        /// <param name="spawnRadius"></param>
        /// <param name="singleParent"></param>
        /// <param name="randomChoice"></param>
        public void spawnColony(int spawnRadius, Colony singleParent, bool randomChoice = false)
        {
            int        l = ConfigurationManager.Instance.Chunk_Diameter * ConfigurationManager.Instance.World_Diameter;
            Vector2Int c;

            // Choose a center location in this colony
            if (randomChoice)
            {
                c = new Vector2Int(UnityEngine.Random.Range(spawnRadius / 2, l / 2 - spawnRadius / 2),
                                   UnityEngine.Random.Range(spawnRadius / 2, l / 2 - spawnRadius / 2));
            }
            else
            {
                c = new Vector2Int(l / 4, l / 4);
                if (colonyId == 1)
                {
                    c.y += l / 2;
                }
                else if (colonyId == 2)
                {
                    c.y += l / 2;
                    c.x += l / 2;
                }
                else if (colonyId == 3)
                {
                    c.x += l / 2;
                }
            }


            // Spawn all ants
            for (int i = 0; i < ConfigurationManager.Instance.antsPerColony; i++)
            {
                NervousSystem newNS;
                if (singleParent.bestAnt == null)
                {
                    newNS = new NervousSystem(singleParent.colony[i].GetComponent <Ant>().getNervousSystem().nodes,
                                              singleParent.colony[i].GetComponent <Ant>().getNervousSystem().connections);
                }
                else
                {
                    newNS = new NervousSystem(singleParent.bestAnt.GetComponent <Ant>().getNervousSystem().nodes,
                                              singleParent.bestAnt.GetComponent <Ant>().getNervousSystem().connections);
                }
                for (int k = 0; k < UnityEngine.Random.Range(1, 5); k++)
                {
                    if (UnityEngine.Random.Range(0f, 1f) < ConfigurationManager.Instance.connectionMutationRate)
                    {
                        NeuroEvolution.MutateByConnection(newNS);
                    }
                    if (UnityEngine.Random.Range(0f, 1f) < ConfigurationManager.Instance.nodeMutationRate)
                    {
                        NeuroEvolution.MutateByConnection(newNS);
                    }
                }

                spawnAnt(c, spawnRadius, newNS, false);
            }

            NervousSystem newNSqueen = new NervousSystem(singleParent.queen.GetComponent <Ant>().getNervousSystem().nodes,
                                                         singleParent.queen.GetComponent <Ant>().getNervousSystem().connections);

            for (int k = 0; k < UnityEngine.Random.Range(1, 5); k++)
            {
                // Spawning the queen
                if (UnityEngine.Random.Range(0f, 1f) < ConfigurationManager.Instance.connectionMutationRate)
                {
                    NeuroEvolution.MutateByConnection(newNSqueen);
                }
                if (UnityEngine.Random.Range(0f, 1f) < ConfigurationManager.Instance.nodeMutationRate)
                {
                    NeuroEvolution.MutateByNode(newNSqueen);
                }
            }
            spawnAnt(c, spawnRadius, newNSqueen, true);
        }