/// <summary> /// Create an initial population. /// </summary> /// <param name="rnd">Random number generator.</param> /// <param name="codec">The codec, the type of network to use.</param> /// <returns>The population.</returns> public static IPopulation InitPopulation(IGenerateRandom rnd, RBFNetworkGenomeCODEC codec) { // Create a RBF network to get the length. var network = new RBFNetwork(codec.InputCount, codec.RbfCount, codec.OutputCount); int size = network.LongTermMemory.Length; // Create a new population, use a single species. IPopulation result = new BasicPopulation(PopulationSize, new DoubleArrayGenomeFactory(size)); var defaultSpecies = new BasicSpecies {Population = result}; result.Species.Add(defaultSpecies); // Create a new population of random networks. for (int i = 0; i < PopulationSize; i++) { var genome = new DoubleArrayGenome(size); network.Reset(rnd); Array.Copy(network.LongTermMemory, 0, genome.Data, 0, size); defaultSpecies.Add(genome); } // Set the genome factory to use the double array genome. result.GenomeFactory = new DoubleArrayGenomeFactory(size); return result; }
/// <summary> /// Demonstrate the crossover splice operator. Two offspring will be created by swapping three /// segments of the parents (two cut points). Some genes may repeat. /// </summary> public static void Splice() { Console.WriteLine("Crossover Splice"); // Create a random number generator IGenerateRandom rnd = new MersenneTwisterGenerateRandom(); // Create a new population. IPopulation pop = new BasicPopulation(); pop.GenomeFactory = new IntegerArrayGenomeFactory(10); // Create a trainer with a very simple score function. We do not care // about the calculation of the score, as they will never be calculated. IEvolutionaryAlgorithm train = new BasicEA(pop, new NullScore()); // Create a splice operator, length = 5. Use it 1.0 (100%) of the time. var opp = new Splice(5); train.AddOperation(1.0, opp); // Create two parents, the genes are set to 1,2,3,4,5,7,8,9,10 // and 10,9,8,7,6,5,4,3,2,1. var parents = new IntegerArrayGenome[2]; parents[0] = (IntegerArrayGenome) pop.GenomeFactory.Factor(); parents[1] = (IntegerArrayGenome) pop.GenomeFactory.Factor(); for (int i = 1; i <= 10; i++) { parents[0].Data[i - 1] = i; parents[1].Data[i - 1] = 11 - i; } // Create an array to hold the offspring. var offspring = new IntegerArrayGenome[2]; // Perform the operation opp.PerformOperation(rnd, parents, 0, offspring, 0); // Display the results Console.WriteLine("Parent 1: " + string.Join(",", parents[0].Data)); Console.WriteLine("Parent 2: " + string.Join(",", parents[1].Data)); Console.WriteLine("Offspring 1: " + string.Join(",", offspring[0].Data)); Console.WriteLine("Offspring 2: " + string.Join(",", offspring[1].Data)); }
/// <summary> /// The entry point for this example. If you would like to make this example /// stand alone, then add to its own project and rename to Main. /// </summary> /// <param name="args">Not used.</param> public static void ExampleMain(string[] args) { // Create a new population. IPopulation pop = new BasicPopulation(); ISpecies species = pop.CreateSpecies(); // Create 1000 genomes, assign the score to be the index number. for (int i = 0; i < 1000; i++) { IGenome genome = new IntegerArrayGenome(1); genome.Score = i; genome.AdjustedScore = i; pop.Species[0].Add(genome); } IGenerateRandom rnd = new MersenneTwisterGenerateRandom(); // Create a trainer with a very simple score function. We do not care // about the calculation of the score, as they will never be calculated. // We only care that we are maximizing. IEvolutionaryAlgorithm train = new BasicEA(pop, new NullScore()); // Perform the test for round counts between 1 and 10. for (int roundCount = 1; roundCount <= 10; roundCount++) { var selection = new TournamentSelection(train, roundCount); int sum = 0; int count = 0; for (int i = 0; i < 100000; i++) { int genomeID = selection.PerformSelection(rnd, species); IGenome genome = species.Members[genomeID]; sum += (int) genome.AdjustedScore; count++; } sum /= count; Console.WriteLine("Rounds: " + roundCount + ", Avg Score: " + sum); } }
/// <summary> /// Create an initial random population of random paths through the cities. /// </summary> /// <param name="rnd">The random population.</param> /// <returns>The population</returns> private IPopulation InitPopulation(IGenerateRandom rnd) { IPopulation result = new BasicPopulation(PopulationSize, null); var defaultSpecies = new BasicSpecies(); defaultSpecies.Population = result; for (int i = 0; i < PopulationSize; i++) { IntegerArrayGenome genome = RandomGenome(rnd); defaultSpecies.Add(genome); } result.GenomeFactory = new IntegerArrayGenomeFactory(_cities.Length); result.Species.Add(defaultSpecies); return result; }
/** * Create the initial random population. * * @return The population. */ private IPopulation InitPopulation() { IPopulation result = new BasicPopulation(PlantUniverse.PopulationSize, null); BasicSpecies defaultSpecies = new BasicSpecies(); defaultSpecies.Population = result; for (int i = 0; i < PlantUniverse.PopulationSize; i++) { DoubleArrayGenome genome = RandomGenome(); defaultSpecies.Add(genome); } result.GenomeFactory = new DoubleArrayGenomeFactory(PlantUniverse.GenomeSize); result.Species.Add(defaultSpecies); return result; }
/// <summary> /// Create an initial random population. /// </summary> /// <param name="rnd">A random number generator.</param> /// <param name="eval">The expression evaluator.</param> /// <returns>The new population.</returns> private IPopulation InitPopulation(IGenerateRandom rnd, EvaluateExpression eval) { IPopulation result = new BasicPopulation(PopulationSize, null); var defaultSpecies = new BasicSpecies(); defaultSpecies.Population = result; for (int i = 0; i < PopulationSize; i++) { TreeGenome genome = RandomGenome(rnd, eval); defaultSpecies.Add(genome); } result.GenomeFactory = new TreeGenomeFactory(eval); result.Species.Add(defaultSpecies); return result; }