public Agent GenerateAgent(SpecieType agentType, bool isCrossoverAgent, List <Agent> agents)
        {
            Agent agent;

            if (isCrossoverAgent)
            {
                if (agents.Count(a => a.Type == agentType) <= 1)
                {
                    GenerateAgent(agentType, false, agents);
                    return(null);
                }

                Agent parent0 = GetRandomRoulleteAgentFromList(agents, agentType);
                Agent parent1 = GetRandomRoulleteAgentFromList(agents.Except(new List <Agent> {
                    parent0
                }).ToList(), agentType);

                string brainOfParent0 = parent0.GetBrainAsBits();
                string brainOfParent1 = parent1.GetBrainAsBits();

                string smaller = brainOfParent0.Length < brainOfParent1.Length ? brainOfParent0 : brainOfParent1;
                string bigger  = brainOfParent0.Length > brainOfParent1.Length ? brainOfParent0 : brainOfParent1;

                int hiddenNeurons;

                if (ApplicationSettings.Random.Next() % 2 == 0)
                {//append it
                    smaller       = smaller + bigger.Substring(smaller.Length);
                    hiddenNeurons = brainOfParent0.Length > brainOfParent1.Length ? parent0.GetHiddenNeurons() : parent1.GetHiddenNeurons();
                }
                else
                {//cut it
                    bigger        = bigger.Substring(0, smaller.Length);
                    hiddenNeurons = brainOfParent0.Length < brainOfParent1.Length ? parent0.GetHiddenNeurons() : parent1.GetHiddenNeurons();
                }
                brainOfParent0 = smaller;
                brainOfParent1 = bigger;

                string networkAsBits = _geneticEvolution.Reproduce(brainOfParent0, brainOfParent1);

                //hiddenNeurons += ApplicationSettings.Random.Next() % 100 < 10 ? 1 : 0;//Chance to mutate an extra hidden neuron
                //hiddenNeurons += ApplicationSettings.Random.Next() % 100 < 10 ? -1 : 0;//Chance to mutate and lose a hidden neuron

                agent = CreateNewAgent(hiddenNeurons, agentType);

                agent.InsertNewBrainAsBits(networkAsBits);
            }
            else
            {
                agent = CreateNewAgent(ApplicationSettings.HiddenNeurons, agentType);
            }
            return(agent);
        }
Example #2
0
        public void GenerateAgent(SpecieType agentType, bool isCrossoverAgent)
        {
            Agent agent;

            if (isCrossoverAgent)
            {
                if (agents.Count(a => a.Type == agentType) <= 1)
                {
                    GenerateAgent(agentType, false);
                    return;
                }
                int indexParent0 = _geneticEvolution.Roulette(agents.Where(a => a.Type == agentType)
                                                              .Select(a => a.GetFitness(ApplicationSettings.ScoreForEatingGoodFood, ApplicationSettings.ScoreForEatingBadFood, ApplicationSettings.ScoreForExisting)).ToArray());
                int indexParent1 = _geneticEvolution.Roulette(agents.Where(a => a.Type == agentType)
                                                              .Select(a => a.GetFitness(ApplicationSettings.ScoreForEatingGoodFood, ApplicationSettings.ScoreForEatingBadFood, ApplicationSettings.ScoreForExisting)).ToArray());
                string brainOfAgent0 = agents[indexParent0].GetBrainAsBits();
                string brainOfAgent1 = agents[indexParent1].GetBrainAsBits();

                string smaller = brainOfAgent0.Length > brainOfAgent1.Length ? brainOfAgent0 : brainOfAgent1;
                string bigger  = brainOfAgent0.Length < brainOfAgent1.Length ? brainOfAgent0 : brainOfAgent1;

                int hiddenNeurons;

                if (ApplicationSettings.Random.Next() % 2 == 0)
                {//append it
                    smaller       = smaller + bigger.Substring(smaller.Length);
                    hiddenNeurons = brainOfAgent0.Length > brainOfAgent1.Length ? agents[indexParent0].GetHiddenNeurons() : agents[indexParent1].GetHiddenNeurons();
                }
                else
                {//cut it
                    bigger        = bigger.Substring(0, smaller.Length);
                    hiddenNeurons = brainOfAgent0.Length < brainOfAgent1.Length ? agents[indexParent0].GetHiddenNeurons() : agents[indexParent1].GetHiddenNeurons();
                }
                brainOfAgent0 = smaller;
                brainOfAgent1 = bigger;

                string networkAsBits = _geneticEvolution.Reproduce(brainOfAgent0, brainOfAgent1);

                agent = new Agent(new Vector2(ApplicationSettings.Random.Next() % ApplicationSettings.SpawningSpace.Width, ApplicationSettings.Random.Next() % ApplicationSettings.SpawningSpace.Height),
                                  numberOfEyes, hiddenNeurons, agentType, _agentsColor[(int)agentType], agentType == SpecieType.Herbivore ? typeOneEats : typeTwoEats, ApplicationSettings.Random, ApplicationSettings.AgentSettings);

                agent.InsertNewBrainAsBits(networkAsBits);
            }
            else
            {
                agent = new Agent(new Vector2(ApplicationSettings.Random.Next() % ApplicationSettings.SpawningSpace.Width, ApplicationSettings.Random.Next() % ApplicationSettings.SpawningSpace.Height),
                                  numberOfEyes, 5, agentType, _agentsColor[(int)agentType], agentType == SpecieType.Herbivore ? typeOneEats : typeTwoEats, ApplicationSettings.Random, ApplicationSettings.AgentSettings);
            }
            agents.Add(agent);
        }