public Agent GetRandomRoulleteAgentFromList(List <Agent> agents, SpecieType agentType)
        {
            int index = _geneticEvolution.Roulette(agents.Where(a => a.Type == agentType)
                                                   .Select(a => a.GetFitness(ApplicationSettings.ScoreForEatingGoodFood, ApplicationSettings.ScoreForEatingBadFood, ApplicationSettings.ScoreForExisting)).ToArray());

            return(agents[index]);
        }
Пример #2
0
 public Food(Vector2 position, SpecieType specieType, Color color, int foodValue)
 {
     FoodValue = foodValue;
     Position  = position;
     Radius    = 5;
     Color     = color;
     Type      = specieType;
 }
        public void DuplicateAgent(SpecieType agentType, Agent fromAgent)
        {
            int    hiddenNeurons = fromAgent.GetHiddenNeurons();
            Agent  agent         = CreateNewAgent(hiddenNeurons, agentType);
            string agentBrain    = fromAgent.GetBrainAsBits();

            agent.InsertNewBrainAsBits(agentBrain);
            agents.Add(agent);
        }
        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);
        }
Пример #5
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);
        }
Пример #6
0
 public Agent(Vector2 position, int eyesCount, int hiddenNeurons, SpecieType agentType, Color color, List <SpecieType> eatsOtherSpecies, Random rand, AgentSettings agentSettings = AgentSettings.None)
 {
     _maxEnergy           = 100;
     Energy               = 100;
     Position             = position;
     Radius               = 10;
     _energyDecay         = 0.35;
     Color                = color;
     _eyesCount           = eyesCount;
     _eyeSees             = new BasePopulation[_eyesCount];
     _eyeLength           = 20;
     _eyesRadius          = Math.PI / 2;
     _distanceBetweenEyes = _eyesRadius / _eyesCount;
     _rotationSpeed       = 0.2f;
     _thrustSpeed         = 3;
     _network             = new Network();
     _network.InitializeNetwork(_eyesCount * 2 + 1, hiddenNeurons, 1, 0.3f, rand);
     //_network.InitializeNetwork(_eyesCount + 1, hiddenNeurons, 1, 0.3f, rand);
     Type             = agentType;
     EatsOtherSpecies = eatsOtherSpecies;
     _agentSettings   = agentSettings;
 }
 Agent CreateNewAgent(int hiddenNeurons, SpecieType agentType)
 {
     return(new Agent(new Vector2(ApplicationSettings.Random.Next() % ApplicationSettings.SpawningSpace.Width, ApplicationSettings.Random.Next() % ApplicationSettings.SpawningSpace.Height),
                      ApplicationSettings.NumberOfEyes, hiddenNeurons, agentType, _agentsColor[(int)agentType], agentType == SpecieType.Herbivore ? typeOneEats : typeTwoEats, ApplicationSettings.Random, ApplicationSettings.AgentSettings));
 }