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); }
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]); }