public void GetRandomAgentsBiasFitness_Test() { //Create agents AgentObject agent1 = new CustomAgent(populationManager, new Genome(), 100); AgentObject agent2 = new CustomAgent(populationManager, new Genome(), 300); //Create two species Species species1 = new Species(0, agent1); species1.Members.Add(agent1); species1.Members.Add(agent2); List <AgentObject> resultAgents = populationManager.GetRandomAgentsBiasFitness(1000, species1); //Count values int amountAgents1 = 0; int amountAgents2 = 0; for (int i = 0; i < resultAgents.Count; i++) { if (resultAgents[i] == agent1) { amountAgents1++; } if (resultAgents[i] == agent2) { amountAgents2++; } } Assert.AreEqual(1000, amountAgents1 + amountAgents2); Assert.GreaterOrEqual(amountAgents2, 600); }
public void MutateAgent_Node_Test() { MutationLog mutationLog = new MutationLog(); AgentObject customAgent = new CustomAgent(populationManager, parent2Genome, 10); Assert.AreEqual(6, customAgent.Genome.Nodes.Count); Assert.AreEqual(9, customAgent.Genome.Connections.Count); Dictionary <int, double> weights = new Dictionary <int, double>(); foreach (int key in customAgent.Genome.Connections.Keys) { weights.Add(key, customAgent.Genome.Connections[key].Weight); } //Can fail sometimes, when a disabled connection is selected populationManager.MutateAgent(customAgent, 0f, 1f, 0f, nodeInnovationCounter, connectionInnovationCounter, mutationLog); //1 Node shoudl be added and 2 connections Assert.AreEqual(7, customAgent.Genome.Nodes.Count); Assert.AreEqual(12, nodeInnovationCounter.GetNewNumber()); Assert.AreEqual(11, customAgent.Genome.Connections.Count); Assert.AreEqual(16, connectionInnovationCounter.GetNewNumber()); //WEights should not have changed foreach (int key in weights.Keys) { Assert.AreEqual(weights[key], customAgent.Genome.Connections[key].Weight); } }
public void MutateAgent_Weight_Test() { MutationLog mutationLog = new MutationLog(); AgentObject customAgent = new CustomAgent(populationManager, parent2Genome, 10); Assert.AreEqual(6, customAgent.Genome.Nodes.Count); Assert.AreEqual(9, customAgent.Genome.Connections.Count); Dictionary <int, double> weights = new Dictionary <int, double>(); foreach (int key in customAgent.Genome.Connections.Keys) { weights.Add(key, customAgent.Genome.Connections[key].Weight); } //Mutate weights populationManager.MutateAgent(customAgent, 0f, 0f, 1f, nodeInnovationCounter, connectionInnovationCounter, mutationLog); //Nodes should not have changed Assert.AreEqual(6, customAgent.Genome.Nodes.Count); Assert.AreEqual(9, customAgent.Genome.Connections.Count); Assert.AreEqual(14, connectionInnovationCounter.GetNewNumber()); Assert.AreEqual(11, nodeInnovationCounter.GetNewNumber()); foreach (int key in weights.Keys) { Assert.AreNotEqual(weights[key], customAgent.Genome.Connections[key].Weight); } }
public void GetTotalFitnessForSpecies_Test() { //Create agents AgentObject agent1 = new CustomAgent(populationManager, new Genome(), 100); AgentObject agent2 = new CustomAgent(populationManager, new Genome(), 200); AgentObject agent3 = new CustomAgent(populationManager, new Genome(), 300); AgentObject agent4 = new CustomAgent(populationManager, new Genome(), 400); //Create two species Species species1 = new Species(0, agent1); species1.Members.Add(agent1); species1.Members.Add(agent2); species1.CalculateTotalSharedFitness(); Species species2 = new Species(0, agent3); species2.Members.Add(agent3); species2.Members.Add(agent4); species2.CalculateTotalSharedFitness(); //Create a list of species List <Species> speciesList = new List <Species>(); speciesList.Add(species1); speciesList.Add(species2); float sum = populationManager.GetTotalFitnessForSpecies(speciesList); Assert.AreEqual(500, sum); }
public void GetAmountOffSpringsForSpecies_Test() { //Create agents AgentObject agent1 = new CustomAgent(populationManager, new Genome(), 1); AgentObject agent2 = new CustomAgent(populationManager, new Genome(), 2); AgentObject agent3 = new CustomAgent(populationManager, new Genome(), 3); AgentObject agent4 = new CustomAgent(populationManager, new Genome(), 3); //Create two species Species species1 = new Species(0, agent1); species1.Members.Add(agent1); species1.Members.Add(agent2); species1.CalculateTotalSharedFitness(); Species species2 = new Species(0, agent3); species2.Members.Add(agent3); species2.Members.Add(agent4); species2.CalculateTotalSharedFitness(); //Create a list of species List <Species> speciesList = new List <Species>(); speciesList.Add(species1); speciesList.Add(species2); Dictionary <Species, int> result = populationManager.GetAmountOffSpringsForSpecies(90, new List <Species>(speciesList)); Assert.AreEqual(30, result[species1]); Assert.AreEqual(60, result[species2]); }
public void IsSpeciesAvailableForAgent_Test() { //Initialize population populationManager.CreateInitialPopulation(parent3GenomeSimple, nodeInnovationCounter, connectionInnovationCounter, 100); AgentObject invalidAgent = new CustomAgent(populationManager, parent1Genome, 100); AgentObject invalidAgent2 = new CustomAgent(populationManager, parent2Genome, 100); AgentObject validAgent = new CustomAgent(populationManager, new Genome(parent3GenomeSimple), 100); //Return null for the given genome. Should not match Assert.Null(populationManager.IsSpeciesAvailableForAgent(invalidAgent, populationManager.Species)); Assert.Null(populationManager.IsSpeciesAvailableForAgent(invalidAgent2, populationManager.Species)); Assert.NotNull(populationManager.IsSpeciesAvailableForAgent(validAgent, populationManager.Species)); }
public KeyValuePair <Uri, IUserAgent> GetResult() { if (_inputFields.UserAgent is int) { CustomAgent agent = new CustomAgent(); agent.GetCrawlDelay = (int)intOutValue; return(new KeyValuePair <Uri, IUserAgent>(_inputFields.Domain.ToUri(), agent)); } foreach (Type type in Extensions.GetAgentsType()) { if (string.Compare(type.Name, _inputFields.UserAgent, true) == 0) { var userAgent = (IUserAgent)Activator.CreateInstance(type); userAgent.GetCrawlDelay = (int)intOutValue; return(new KeyValuePair <Uri, IUserAgent>(_inputFields.Domain.ToUri(), userAgent)); } } return(new KeyValuePair <Uri, IUserAgent>()); }
public void PlaceAgentInSpecies_Test() { AgentObject agent1 = new CustomAgent(populationManager, parent1Genome, 1); AgentObject agent2 = new CustomAgent(populationManager, parent1Genome, 2); AgentObject agent3 = new CustomAgent(populationManager, parent2Genome, 3); AgentObject agent4 = new CustomAgent(populationManager, parent2Genome, 4); AgentObject agent5 = new CustomAgent(populationManager, parent3GenomeSimple, 5); Species species1 = new Species(0, agent1); Species species2 = new Species(0, agent5); List <AgentObject> agents = new List <AgentObject> { agent1, agent2, agent3, agent4 }; List <Species> species = new List <Species> { species1, species2 }; //Species one should be updated, //Species two should be deleted //For agent 3 and 4 is a new species required populationManager.PlaceAgentInSpecies(agents, species); Assert.AreEqual(2, species.Count); Assert.AreEqual(species1, species[0]); Assert.AreNotEqual(species2, species[1]); //Check agents Assert.AreEqual(agent1, species[0].RepresentiveAgent); Assert.AreEqual(agent3, species[1].RepresentiveAgent); Assert.AreEqual(2, species[0].Members.Count); Assert.AreEqual(2, species[1].Members.Count); Assert.AreEqual(agent1, species[0].Members[0]); Assert.AreEqual(agent2, species[0].Members[1]); Assert.AreEqual(agent3, species[1].Members[0]); Assert.AreEqual(agent4, species[1].Members[1]); }
public void MutateAgent_Connection_Test() { MutationLog mutationLog = new MutationLog(); AgentObject customAgent = new CustomAgent(populationManager, parent2Genome, 10); Assert.AreEqual(6, customAgent.Genome.Nodes.Count); Assert.AreEqual(9, customAgent.Genome.Connections.Count); Dictionary <int, double> weights = new Dictionary <int, double>(); foreach (int key in customAgent.Genome.Connections.Keys) { weights.Add(key, customAgent.Genome.Connections[key].Weight); } //Mutate connections, test multiple times, because there can be invalid connecionts for (int i = 0; i < 100; i++) { populationManager.MutateAgent(customAgent, 1f, 0f, 0f, nodeInnovationCounter, connectionInnovationCounter, mutationLog); //If a connection occurs, leave the loop if (customAgent.Genome.Connections.Count >= 10) { break; } } //Nodes should not have changed Assert.AreEqual(6, customAgent.Genome.Nodes.Count); Assert.AreEqual(11, nodeInnovationCounter.GetNewNumber()); Assert.AreEqual(10, customAgent.Genome.Connections.Count); Assert.AreEqual(15, connectionInnovationCounter.GetNewNumber()); //WEights should not have changed foreach (int key in weights.Keys) { Assert.AreEqual(weights[key], customAgent.Genome.Connections[key].Weight); } }
public void GetBestGenomeForSpecies_Test() { //Create agents AgentObject agent1 = new CustomAgent(populationManager, new Genome(), 100); AgentObject agent2 = new CustomAgent(populationManager, new Genome(), 200); AgentObject agent3 = new CustomAgent(populationManager, new Genome(), 300); //Create two species Species species1 = new Species(0, agent1); species1.Members.Add(agent1); Species species2 = new Species(0, agent3); species2.Members.Add(agent3); Species species3 = new Species(0, agent2); for (int i = 0; i < 4; i++) { species1.Members.Add(new CustomAgent(populationManager, new Genome(), 50)); species2.Members.Add(new CustomAgent(populationManager, new Genome(), 50)); species3.Members.Add(new CustomAgent(populationManager, new Genome(), 50)); } List <Species> species = new List <Species>() { species1, species2, species3 }; List <Genome> bestGenomes = populationManager.GetBestGenomeForSpecies(species); Assert.AreEqual(2, bestGenomes.Count); Assert.AreEqual(agent1.Genome, bestGenomes[0]); Assert.AreEqual(agent3.Genome, bestGenomes[1]); }
public static CustomAgent ToEntity(this CustomAgentModel model, CustomAgent destination) { return(AutoMapperConfiguration.Mapper.Map(model, destination)); }
public static CustomAgentModel ToModel(this CustomAgent entity) { return(AutoMapperConfiguration.Mapper.Map <CustomAgent, CustomAgentModel>(entity)); }