/// <summary> /// Initializes a new instance of the <see cref="Population"/> class. /// </summary> /// /// <param name="size">Initial size of population.</param> /// <param name="ancestor">Ancestor chromosome to use for population creatioin.</param> /// <param name="fitnessFunction">Fitness function to use for calculating /// chromosome's fitness values.</param> /// <param name="selectionMethod">Selection algorithm to use for selection /// chromosome's to new generation.</param> /// /// <remarks>Creates new population of specified size. The specified ancestor /// becomes first member of the population and is used to create other members /// with same parameters, which were used for ancestor's creation.</remarks> /// /// <exception cref="ArgumentException">Too small population's size was specified. The /// exception is thrown in the case if <paramref name="size"/> is smaller than 2.</exception> /// public QueuePopulation(int size, IChromosome ancestor, IFitnessFunction fitnessFunction, ISelectionMethod selectionMethod) { if (size < 2) throw new ArgumentException("Too small population's size was specified."); this.fitnessFunction = fitnessFunction; this.selectionMethod = selectionMethod; this.size = size; // add ancestor to the population ancestor.Evaluate(fitnessFunction); population.Add(ancestor.Clone()); // add more chromosomes to the population for (int i = 1; i < size; i++) { // create new chromosome IChromosome c = ancestor.CreateNew(); // calculate it's fitness c.Evaluate(fitnessFunction); // add it to population population.Add(c); } }
/// <summary> /// Initializes a new instance of the <see cref="Population"/> class. /// </summary> /// /// <param name="size">Initial size of population.</param> /// <param name="ancestor">Ancestor chromosome to use for population creatioin.</param> /// <param name="fitnessFunction">Fitness function to use for calculating /// chromosome's fitness values.</param> /// <param name="selectionMethod">Selection algorithm to use for selection /// chromosome's to new generation.</param> /// /// <remarks>Creates new population of specified size. The specified ancestor /// becomes first member of the population and is used to create other members /// with same parameters, which were used for ancestor's creation.</remarks> /// /// <exception cref="ArgumentException">Too small population's size was specified. The /// exception is thrown in the case if <paramref name="size"/> is smaller than 2.</exception> /// public Population(int size, IChromosome ancestor, IFitnessFunction fitnessFunction, ISelectionMethod selectionMethod) { if (size < 2) { throw new ArgumentException("Too small population's size was specified."); } this.fitnessFunction = fitnessFunction; this.selectionMethod = selectionMethod; this.size = size; // add ancestor to the population ancestor.Evaluate(fitnessFunction); population.Add(ancestor.Clone()); // add more chromosomes to the population for (var i = 1; i < size; i++) { // create new chromosome var c = ancestor.CreateNew(); // calculate it's fitness c.Evaluate(fitnessFunction); // add it to population population.Add(c); } }
public IA(Game game, int players) : base(game) { rndSeedGen = new Random(); rndControl = new Random(); rndMovControl = new Random(); this.comidas = null; this.jugadores = null; this.numWeights = HIDDEN_UNITS0 * (INPUT_UNITS + 1) + HIDDEN_UNITS1 * (HIDDEN_UNITS0 + 1) + OUTPUT_UNITS * (HIDDEN_UNITS1 + 1); redes = new ActivationNetwork[players]; for (int i = 0; i < redes.Length; i++) { redes[i] = new ActivationNetwork(new SigmoidFunction(400), INPUT_UNITS, HIDDEN_UNITS0, HIDDEN_UNITS1, OUTPUT_UNITS); } inputVector = new double[INPUT_UNITS]; outputVector = new double[OUTPUT_UNITS]; doneEvents = new ManualResetEvent[players]; for (int i = 0; i < players; i++) { doneEvents[i] = new ManualResetEvent(false); } //Se puede jugar con los parametros de los rangos para modificar la evolucion de las redes //Tambien se puede modificar el metodo de seleccion. chromosomeGenerator = new UniformGenerator(new Range(-10f, 10f), rndSeedGen.Next(-100, 100)); mutationAdditionGenerator = new UniformGenerator(new Range(-8f, 8f), rndSeedGen.Next(-100, 100)); mutationMultiplierGenerator = new UniformGenerator(new Range(-8f, 8f), rndSeedGen.Next(-100, 100)); fitnessFunction = new GameFitnessFunction(); selectionMethod = new EliteSelection(); padre = new gameChromosome(chromosomeGenerator, mutationMultiplierGenerator, mutationAdditionGenerator, numWeights); poblacion = new Population(WorldGame.JUGADORES, padre, fitnessFunction, selectionMethod); }
public GeneticAlgorithm(int selectedFitnessFunction, int populationSize, int iterationsNumber, int selectedMethod) { this.selectedMethod = selectedMethod; ISelectionMethod selectionMethot = GetSelectionMethod(); DoubleArrayChromosome chromosome = new DoubleArrayChromosome(new AForge.Math.Random.StandardGenerator(), new AForge.Math.Random.StandardGenerator(), new AForge.Math.Random.StandardGenerator(), 2); FitnessFunction fitnessfunction = new FitnessFunction(selectedFitnessFunction); Population population = new Population(populationSize, chromosome, fitnessfunction, selectionMethot); double[,] tmp = new double[iterationsNumber, 2]; this.result = new double[iterationsNumber, 2]; int i; for (i = 0; i < iterationsNumber; i++) { population.RunEpoch(); DoubleArrayChromosome bestChromosome = (DoubleArrayChromosome)population.BestChromosome; double x = (double)bestChromosome.Value.GetValue(0); double y = (double)bestChromosome.Value.GetValue(1); tmp[i, 0] = x; tmp[i, 1] = y; if (fitnessfunction.Evaluate(bestChromosome) == 100) { break; } } this.result = new double[i, 2]; for (int j = 0; j < i; j++) { result[j, 0] = tmp[j, 0]; result[j, 1] = tmp[j, 1]; } }
/// <summary> /// Resize population to the new specified size. /// </summary> /// /// <param name="newPopulationSize">New size of population.</param> /// <param name="membersSelector">Selection algorithm to use in the case /// if population should get smaller.</param> /// /// <remarks><para>The method does resizing of population. In the case if population /// should grow, it just adds missing number of random members. In the case if /// population should get smaller, the specified selection method is used to /// reduce the population.</para></remarks> /// /// <exception cref="ArgumentException">Too small population's size was specified. The /// exception is thrown in the case if <paramref name="newPopulationSize"/> is smaller than 2.</exception> /// public void Resize(int newPopulationSize, ISelectionMethod membersSelector) { if (newPopulationSize < 2) { throw new ArgumentException("Too small new population's size was specified."); } if (newPopulationSize > size) { // population is growing, so add new rundom members // Note: we use population.Count here instead of "size" because // population may be bigger already after crossover/mutation. So // we just keep those members instead of adding random member. var toAdd = newPopulationSize - population.Count; for (var i = 0; i < toAdd; i++) { // create new chromosome var c = population[0].CreateNew(); // calculate it's fitness c.Evaluate(fitnessFunction); // add it to population population.Add(c); } } else { // do selection membersSelector.ApplySelection(population, newPopulationSize); } size = newPopulationSize; }
/// <summary> /// Initializes a new instance of the <see cref="EvolutionaryLearning"/> class. /// </summary> /// /// <param name="activationNetwork">Activation network to be trained.</param> /// <param name="populationSize">Size of genetic population.</param> /// <param name="chromosomeGenerator">Random numbers generator used for initialization of genetic /// population representing neural network's weights and thresholds (see <see cref="DoubleArrayChromosome.chromosomeGenerator"/>).</param> /// <param name="mutationMultiplierGenerator">Random numbers generator used to generate random /// factors for multiplication of network's weights and thresholds during genetic mutation /// (ses <see cref="DoubleArrayChromosome.mutationMultiplierGenerator"/>.)</param> /// <param name="mutationAdditionGenerator">Random numbers generator used to generate random /// values added to neural network's weights and thresholds during genetic mutation /// (see <see cref="DoubleArrayChromosome.mutationAdditionGenerator"/>).</param> /// <param name="selectionMethod">Method of selection best chromosomes in genetic population.</param> /// <param name="crossOverRate">Crossover rate in genetic population (see /// <see cref="Population.CrossoverRate"/>).</param> /// <param name="mutationRate">Mutation rate in genetic population (see /// <see cref="Population.MutationRate"/>).</param> /// <param name="randomSelectionRate">Rate of injection of random chromosomes during selection /// in genetic population (see <see cref="Population.RandomSelectionPortion"/>).</param> /// public EvolutionaryLearning(ActivationNetwork activationNetwork, int populationSize, IRandomNumberGenerator chromosomeGenerator, IRandomNumberGenerator mutationMultiplierGenerator, IRandomNumberGenerator mutationAdditionGenerator, ISelectionMethod selectionMethod, double crossOverRate, double mutationRate, double randomSelectionRate) { // Check of assumptions during debugging only Debug.Assert(activationNetwork != null); Debug.Assert(populationSize > 0); Debug.Assert(chromosomeGenerator != null); Debug.Assert(mutationMultiplierGenerator != null); Debug.Assert(mutationAdditionGenerator != null); Debug.Assert(selectionMethod != null); Debug.Assert(crossOverRate >= 0.0 && crossOverRate <= 1.0); Debug.Assert(mutationRate >= 0.0 && crossOverRate <= 1.0); Debug.Assert(randomSelectionRate >= 0.0 && randomSelectionRate <= 1.0); // networks's parameters this.network = activationNetwork; this.numberOfNetworksWeights = CalculateNetworkSize(activationNetwork); // population parameters this.populationSize = populationSize; this.chromosomeGenerator = chromosomeGenerator; this.mutationMultiplierGenerator = mutationMultiplierGenerator; this.mutationAdditionGenerator = mutationAdditionGenerator; this.selectionMethod = selectionMethod; this.crossOverRate = crossOverRate; this.mutationRate = mutationRate; this.randomSelectionRate = randomSelectionRate; }
public AdaptiveSelector(ISelectionMethod initialGoal, String formattedMatrixString) { _templateMatrix = new CpeMatrix(); _templateMatrix.LoadCpeMatrix(formattedMatrixString); _currentGoal = initialGoal; _selectCurrentCpe = new MaintainMethod(); }
public void Start() { //FilePath = "C:\\Users\\JKMT\\Desktop\\OptimizationShooting.txt"; leftRules = new List <EvaluationTreeRuleBase>(PopulationSize); rightRules = new List <EvaluationTreeRuleBase>(PopulationSize); for (int i = 0; i < PopulationSize; i++) { leftRules.Add(FuzzyShootingUtil.CreateRandomEvaluationTreeRuleBase()); rightRules.Add(FuzzyShootingUtil.CreateRandomEvaluationTreeRuleBase()); } IsTrialRunning = false; TrialCount = 0; GenerationCount = 1; TrialController = Trial.GetComponent <TrialController>(); OrganismFactory = new EvaluationTreeRuleBaseFactory(FuzzyShootingUtil.InputSets, FuzzyShootingUtil.TorqueSet); GeneCopier = new RuleCopier(); SelectionMethod = new StochasticUniversalSamplingSelection(0.11f, 0.29f); CrossoverMethod = new UniformCrossover(); GeneticAlgorithm = new GeneticAlgorithm <EvaluationTreeRuleBase, Rule>(SelectionMethod, CrossoverMethod, OrganismFactory, GeneCopier); }
public AdaptiveSelector(ISelectionMethod initialGoal, String formattedMatrixString) { _templateMatrix = new CpeMatrix(); _templateMatrix.LoadCpeMatrix(formattedMatrixString); _currentGoal = initialGoal; _selectCurrentCpe = new MaintainMethod(); }
public WAPPopulation(int size, WAPChromosome ancestor, IFitnessFunction Func, ISelectionMethod sel) : base(size, ancestor, Func, sel) { AddAlternativeRate = 0.01; DropConditionRate = 0.6; rand = new Random(); parallelism = false; epoch = 0; }
public GeneticSolver(int populationSize, int iterationsCount, ISelectionMethod selectionMethod, double mutationRate, double crossoverRate, double randomSelectionPortion, bool diversityCheck, bool logging) { _populationSize = populationSize; _iterationsCount = iterationsCount; _selectionMethod = selectionMethod; _mutationRate = mutationRate; _crossoverRate = crossoverRate; _randomSelectionPortion = randomSelectionPortion; _diversityCheck = diversityCheck; _logging = logging; }
public void pruebaSeleccion() { for (int i = 0; i < 100; i++) { var cromosoma = new CocheCromosoma(); cromosoma.Generate(); cromosoma.Evaluate(fitness); cromosomas.Add(cromosoma); } seleccion = new CocheSeleccion(); Console.WriteLine(this.cromosomas.Count); seleccion.ApplySelection(this.cromosomas, 20); Console.WriteLine(this.cromosomas.Count); }
private void button1_Click(object sender, EventArgs e) { //ShortArrayChromosome[] individos = new ShortArrayChromosome[nPopulacao]; //for (int i = 0; i < individos.Length; i++) //{ // individos[i] = new ShortArrayChromosome(nRainhas, nRainhas); // individos[i].Generate(); // Console.WriteLine(i + " : " + individos[i].ToString()); //} configuraAlgoritimo(); int selecao = selecaoBox.SelectedIndex; ISelectionMethod metodoDeSelecao = (selecao == 0) ? (ISelectionMethod) new RouletteWheelSelection() : (selecao == 1) ? (ISelectionMethod) new EliteSelection() : (ISelectionMethod) new RankSelection(); AvaliadorDeRainhas avaliador = new AvaliadorDeRainhas(); Population populacao = new Population(nPopulacao, new ShortArrayChromosome(nRainhas, nRainhas - 1), avaliador, metodoDeSelecao); populacao.CrossoverRate = crossoverRate; populacao.MutationRate = mutationRate; //populacao.AutoShuffling = true; int iteracao = 0; int pararEm = nParada; while (iteracao < nGeracoes) { //populacao.Shuffle(); //populacao.Selection(); //populacao.Crossover(); //populacao.Mutate(); populacao.RunEpoch(); //MessageBox.Show("iteração: " + iteracao + "\n Avaliacao Media: " + populacao.FitnessAvg + "\n Melhor individo: " + populacao.BestChromosome.ToString()); if (nParada > 0 && iteracao == pararEm) { atualizaDadosPara(iteracao, populacao); MessageBox.Show("Visualização\nGeração: " + iteracao + "\n OK para Continuar"); pararEm += nParada; } if (populacao.BestChromosome.Fitness == nRainhas) { break; } iteracao++; } //constroiTabuleiroNoConsole((ShortArrayChromosome)populacao.BestChromosome); atualizaDadosPara(iteracao, populacao); }
public PopulationNovelty(int size, NoveltyChromosome ancestor, IFitnessFunction fitnessFunction, IDistanceFunction distanceFunction, ISelectionMethod selectionMethod, int kNearestNeighbours) : base(size, ancestor, fitnessFunction, selectionMethod) { if (kNearestNeighbours < 2) { throw new ArgumentException("Too small nearest neighbours was specified."); } _distanceFunction = distanceFunction; _kNearestNeighbours = kNearestNeighbours; }
/// <summary> /// creates a new population from seralization (returned from serializePopulation()) /// ancesstor is set to the first of the deserialized chromosomes /// </summary> /// <param name="popSerialization"></param> public static MultiThreadEvaluationPopulation <T> desrializePopulation( List <string> popSerialization, ChromosomeGenerator deserializer, IFitnessFunction fitnessFunction, ISelectionMethod selectionMethod, CustomThreadPool pool, bool allowDuplicateChromosomes) { IChromosome ancestor = deserializer(popSerialization[0]); MultiThreadEvaluationPopulation <T> newpop = new MultiThreadEvaluationPopulation <T>(0, (T)ancestor, fitnessFunction, selectionMethod, pool, allowDuplicateChromosomes); newpop.size = popSerialization.Count; for (int cc = 0; cc < popSerialization.Count; ++cc) { newpop.AddChromosome(deserializer(popSerialization[cc])); } return(newpop); }
#pragma warning disable CS0436 // Type conflicts with imported type /// <summary> /// Perform migration between two populations. /// </summary> /// /// <param name="anotherPopulation">Population to do migration with.</param> /// <param name="numberOfMigrants">Number of chromosomes from each population to migrate.</param> /// <param name="migrantsSelector">Selection algorithm used to select chromosomes to migrate.</param> /// /// <remarks><para>The method performs migration between two populations - current and the /// <paramref name="anotherPopulation">specified one</paramref>. During migration /// <paramref name="numberOfMigrants">specified number</paramref> of chromosomes is choosen from /// each population using <paramref name="migrantsSelector">specified selection algorithms</paramref> /// and put into another population replacing worst members there.</para></remarks> /// public void Migrate(Population anotherPopulation, int numberOfMigrants, ISelectionMethod migrantsSelector) #pragma warning restore CS0436 // Type conflicts with imported type { int currentSize = this.size; int anotherSize = anotherPopulation.Size; // create copy of current population List <IChromosome> currentCopy = new List <IChromosome>( ); for (int i = 0; i < currentSize; i++) { currentCopy.Add(population[i].Clone( )); } // create copy of another population List <IChromosome> anotherCopy = new List <IChromosome>( ); for (int i = 0; i < anotherSize; i++) { anotherCopy.Add(anotherPopulation.population[i].Clone( )); } // apply selection to both populations' copies - select members to migrate migrantsSelector.ApplySelection(currentCopy, numberOfMigrants); migrantsSelector.ApplySelection(anotherCopy, numberOfMigrants); // sort original populations, so the best chromosomes are in the beginning population.Sort( ); anotherPopulation.population.Sort( ); // remove worst chromosomes from both populations to free space for new members population.RemoveRange(currentSize - numberOfMigrants, numberOfMigrants); anotherPopulation.population.RemoveRange(anotherSize - numberOfMigrants, numberOfMigrants); // put migrants to corresponding populations population.AddRange(anotherCopy); anotherPopulation.population.AddRange(currentCopy); // find best chromosomes in each population FindBestChromosome( ); anotherPopulation.FindBestChromosome( ); }
/// <summary> /// Initializes a new instance of the <see cref="EvolutionaryLearning"/> class. /// </summary> /// /// <param name="activationNetwork">Activation network to be trained.</param> /// <param name="populationSize">Size of genetic population.</param> /// /// <remarks><para>This version of constructor is used to create genetic population /// for searching optimal neural network's weight using default set of parameters, which are: /// <list type="bullet"> /// <item>Selection method - elite;</item> /// <item>Crossover rate - 0.75;</item> /// <item>Mutation rate - 0.25;</item> /// <item>Rate of injection of random chromosomes during selection - 0.20;</item> /// <item>Random numbers generator for initializing new chromosome - /// <c>UniformGenerator( new Range( -1, 1 ) )</c>;</item> /// <item>Random numbers generator used during mutation for genes' multiplication - /// <c>ExponentialGenerator( 1 )</c>;</item> /// <item>Random numbers generator used during mutation for adding random value to genes - /// <c>UniformGenerator( new Range( -0.5f, 0.5f ) )</c>.</item> /// </list></para> /// /// <para>In order to have full control over the above default parameters, it is possible to /// used extended version of constructor, which allows to specify all of the parameters.</para> /// </remarks> /// public EvolutionaryLearning(ActivationNetwork activationNetwork, int populationSize) { // Check of assumptions during debugging only Debug.Assert(activationNetwork != null); Debug.Assert(populationSize > 0); // networks's parameters this.network = activationNetwork; this.numberOfNetworksWeights = CalculateNetworkSize(activationNetwork); // population parameters this.populationSize = populationSize; this.chromosomeGenerator = new UniformGenerator(new Range(-1, 1)); this.mutationMultiplierGenerator = new ExponentialGenerator(1); this.mutationAdditionGenerator = new UniformGenerator(new Range(-0.5f, 0.5f)); this.selectionMethod = new EliteSelection(); this.crossOverRate = 0.75; this.mutationRate = 0.25; this.randomSelectionRate = 0.2; }
/// <summary> /// Initializes a new instance of the <see cref="Population"/> class. /// </summary> /// /// <param name="size">Initial size of population.</param> /// <param name="ancestor">Ancestor chromosome to use for population creatioin.</param> /// <param name="fitnessFunction">Fitness function to use for calculating /// chromosome's fitness values.</param> /// <param name="selectionMethod">Selection algorithm to use for selection /// chromosome's to new generation.</param> /// /// <remarks>Creates new population of specified size. The specified ancestor /// becomes first member of the population and is used to create other members /// with same parameters, which were used for ancestor's creation.</remarks> /// public Population(int size, IChromosome ancestor, IFitnessFunction fitnessFunction, ISelectionMethod selectionMethod) { this.fitnessFunction = fitnessFunction; this.selectionMethod = selectionMethod; this.size = size; // add ancestor to the population ancestor.Evaluate(fitnessFunction); population.Add(ancestor.Clone( )); // add more chromosomes to the population for (int i = 1; i < size; i++) { // create new chromosome IChromosome c = ancestor.CreateNew( ); // calculate it's fitness c.Evaluate(fitnessFunction); // add it to population population.Add(c); } }
/// <summary> /// Initializes a new instance of the <see cref="EvolutionaryLearning"/> class. /// </summary> /// /// <param name="activationNetwork">Activation network to be trained.</param> /// <param name="populationSize">Size of genetic population.</param> /// <param name="chromosomeGenerator">Random numbers generator used for initialization of genetic /// population representing neural network's weights and thresholds (see <see cref="DoubleArrayChromosome.chromosomeGenerator"/>).</param> /// <param name="mutationMultiplierGenerator">Random numbers generator used to generate random /// factors for multiplication of network's weights and thresholds during genetic mutation /// (ses <see cref="DoubleArrayChromosome.mutationMultiplierGenerator"/>.)</param> /// <param name="mutationAdditionGenerator">Random numbers generator used to generate random /// values added to neural network's weights and thresholds during genetic mutation /// (see <see cref="DoubleArrayChromosome.mutationAdditionGenerator"/>).</param> /// <param name="selectionMethod">Method of selection best chromosomes in genetic population.</param> /// <param name="crossOverRate">Crossover rate in genetic population (see /// <see cref="Population.CrossoverRate"/>).</param> /// <param name="mutationRate">Mutation rate in genetic population (see /// <see cref="Population.MutationRate"/>).</param> /// <param name="randomSelectionRate">Rate of injection of random chromosomes during selection /// in genetic population (see <see cref="Population.RandomSelectionPortion"/>).</param> /// public EvolutionaryLearning( ActivationNetwork activationNetwork, int populationSize, IRandomNumberGenerator chromosomeGenerator, IRandomNumberGenerator mutationMultiplierGenerator, IRandomNumberGenerator mutationAdditionGenerator, ISelectionMethod selectionMethod, double crossOverRate, double mutationRate, double randomSelectionRate ) { // Check of assumptions during debugging only Debug.Assert( activationNetwork != null ); Debug.Assert( populationSize > 0 ); Debug.Assert( chromosomeGenerator != null ); Debug.Assert( mutationMultiplierGenerator != null ); Debug.Assert( mutationAdditionGenerator != null ); Debug.Assert( selectionMethod != null ); Debug.Assert( crossOverRate >= 0.0 && crossOverRate <= 1.0 ); Debug.Assert( mutationRate >= 0.0 && crossOverRate <= 1.0 ); Debug.Assert( randomSelectionRate >= 0.0 && randomSelectionRate <= 1.0 ); // networks's parameters this.network = activationNetwork; this.numberOfNetworksWeights = CalculateNetworkSize( activationNetwork ); // population parameters this.populationSize = populationSize; this.chromosomeGenerator = chromosomeGenerator; this.mutationMultiplierGenerator = mutationMultiplierGenerator; this.mutationAdditionGenerator = mutationAdditionGenerator; this.selectionMethod = selectionMethod; this.crossOverRate = crossOverRate; this.mutationRate = mutationRate; this.randomSelectionRate = randomSelectionRate; }
public void SetGoal(ISelectionMethod newGoal) { _currentGoal = newGoal; }
/// <summary> /// Resize population to the new specified size. /// </summary> /// /// <param name="newPopulationSize">New size of population.</param> /// <param name="membersSelector">Selection algorithm to use in the case /// if population should get smaller.</param> /// /// <remarks><para>The method does resizing of population. In the case if population /// should grow, it just adds missing number of random members. In the case if /// population should get smaller, the specified selection method is used to /// reduce the population.</para></remarks> /// /// <exception cref="ArgumentException">Too small population's size was specified. The /// exception is thrown in the case if <paramref name="newPopulationSize"/> is smaller than 2.</exception> /// public void Resize(int newPopulationSize, ISelectionMethod membersSelector) { if (newPopulationSize < 2) throw new ArgumentException("Too small new population's size was specified."); if (newPopulationSize > size) { // population is growing, so add new rundom members // Note: we use population.Count here instead of "size" because // population may be bigger already after crossover/mutation. So // we just keep those members instead of adding random member. int toAdd = newPopulationSize - population.Count; for (int i = 0; i < toAdd; i++) { // create new chromosome IChromosome c = population[0].CreateNew(); // calculate it's fitness c.Evaluate(fitnessFunction); // add it to population population.Add(c); } } else { // do selection membersSelector.ApplySelection(population, newPopulationSize); } size = newPopulationSize; }
/// <summary> /// Perform migration between two populations. /// </summary> /// /// <param name="anotherPopulation">Population to do migration with.</param> /// <param name="numberOfMigrants">Number of chromosomes from each population to migrate.</param> /// <param name="migrantsSelector">Selection algorithm used to select chromosomes to migrate.</param> /// /// <remarks><para>The method performs migration between two populations - current and the /// <paramref name="anotherPopulation">specified one</paramref>. During migration /// <paramref name="numberOfMigrants">specified number</paramref> of chromosomes is choosen from /// each population using <paramref name="migrantsSelector">specified selection algorithms</paramref> /// and put into another population replacing worst members there.</para></remarks> /// public void Migrate(Population anotherPopulation, int numberOfMigrants, ISelectionMethod migrantsSelector) { int currentSize = this.size; int anotherSize = anotherPopulation.Size; // create copy of current population List<IChromosome> currentCopy = new List<IChromosome>(); for (int i = 0; i < currentSize; i++) { currentCopy.Add(population[i].Clone()); } // create copy of another population List<IChromosome> anotherCopy = new List<IChromosome>(); for (int i = 0; i < anotherSize; i++) { anotherCopy.Add(anotherPopulation.population[i].Clone()); } // apply selection to both populations' copies - select members to migrate migrantsSelector.ApplySelection(currentCopy, numberOfMigrants); migrantsSelector.ApplySelection(anotherCopy, numberOfMigrants); // sort original populations, so the best chromosomes are in the beginning population.Sort(); anotherPopulation.population.Sort(); // remove worst chromosomes from both populations to free space for new members population.RemoveRange(currentSize - numberOfMigrants, numberOfMigrants); anotherPopulation.population.RemoveRange(anotherSize - numberOfMigrants, numberOfMigrants); // put migrants to corresponding populations population.AddRange(anotherCopy); anotherPopulation.population.AddRange(currentCopy); // find best chromosomes in each population FindBestChromosome(); anotherPopulation.FindBestChromosome(); }
public IA(Game game, int players) : base(game) { rndSeedGen = new Random(); rndControl = new Random(); rndMovControl = new Random(); this.comidas = null; this.jugadores = null; this.numWeights = HIDDEN_UNITS0 * (INPUT_UNITS + 1) + HIDDEN_UNITS1 * (HIDDEN_UNITS0 + 1) + OUTPUT_UNITS * (HIDDEN_UNITS1 + 1); redes = new ActivationNetwork[players]; for (int i = 0; i < redes.Length; i++) { redes[i] = new ActivationNetwork(new SigmoidFunction(400), INPUT_UNITS, HIDDEN_UNITS0, HIDDEN_UNITS1, OUTPUT_UNITS); } inputVector = new double[INPUT_UNITS]; outputVector = new double[OUTPUT_UNITS]; doneEvents = new ManualResetEvent[players]; for (int i = 0; i < players; i++) doneEvents[i] = new ManualResetEvent(false); //Se puede jugar con los parametros de los rangos para modificar la evolucion de las redes //Tambien se puede modificar el metodo de seleccion. chromosomeGenerator = new UniformGenerator(new Range(-10f, 10f), rndSeedGen.Next(-100, 100)); mutationAdditionGenerator = new UniformGenerator(new Range(-8f, 8f), rndSeedGen.Next(-100, 100)); mutationMultiplierGenerator = new UniformGenerator(new Range(-8f, 8f), rndSeedGen.Next(-100, 100)); fitnessFunction = new GameFitnessFunction(); selectionMethod = new EliteSelection(); padre = new gameChromosome(chromosomeGenerator, mutationMultiplierGenerator, mutationAdditionGenerator, numWeights); poblacion = new Population(WorldGame.JUGADORES, padre, fitnessFunction, selectionMethod); }
public MultiThreadEvaluationPopulation(int size, T ancestor, IFitnessFunction fitnessFunction, ISelectionMethod selectionMethod, CustomThreadPool pool, bool allowDuplicateChromosomes) : base(size, fitnessFunction, selectionMethod) { TasksPool = pool; if (ancestor != null && size > 0) { AddChromosome(ancestor); } TasksPool.waitAllTasks(); // the first evaluation can't be parallel, due to gui input problems for (int s = 1; s < size; ++s) { AddChromosome(this[0].CreateNew()); } allowDupes = allowDuplicateChromosomes; }
/// <summary> /// Initializes a new instance of the <see cref="EvolutionaryLearning"/> class. /// </summary> /// /// <param name="activationNetwork">Activation network to be trained.</param> /// <param name="populationSize">Size of genetic population.</param> /// /// <remarks><para>This version of constructor is used to create genetic population /// for searching optimal neural network's weight using default set of parameters, which are: /// <list type="bullet"> /// <item>Selection method - elite;</item> /// <item>Crossover rate - 0.75;</item> /// <item>Mutation rate - 0.25;</item> /// <item>Rate of injection of random chromosomes during selection - 0.20;</item> /// <item>Random numbers generator for initializing new chromosome - /// <c>UniformGenerator( new Range( -1, 1 ) )</c>;</item> /// <item>Random numbers generator used during mutation for genes' multiplication - /// <c>ExponentialGenerator( 1 )</c>;</item> /// <item>Random numbers generator used during mutation for adding random value to genes - /// <c>UniformGenerator( new Range( -0.5f, 0.5f ) )</c>.</item> /// </list></para> /// /// <para>In order to have full control over the above default parameters, it is possible to /// used extended version of constructor, which allows to specify all of the parameters.</para> /// </remarks> /// public EvolutionaryLearning( ActivationNetwork activationNetwork, int populationSize ) { // Check of assumptions during debugging only Debug.Assert( activationNetwork != null ); Debug.Assert( populationSize > 0 ); // networks's parameters this.network = activationNetwork; this.numberOfNetworksWeights = CalculateNetworkSize( activationNetwork ); // population parameters this.populationSize = populationSize; this.chromosomeGenerator = new UniformGenerator( new Range( -1, 1 ) ); this.mutationMultiplierGenerator = new ExponentialGenerator( 1 ); this.mutationAdditionGenerator = new UniformGenerator( new Range( -0.5f, 0.5f ) ); this.selectionMethod = new EliteSelection( ); this.crossOverRate = 0.75; this.mutationRate = 0.25; this.randomSelectionRate = 0.2; }
public void SetGoal(ISelectionMethod newGoal) { _currentGoal = newGoal; }
static void Main(string[] args) { try { int numLines = 1473; int maxIter = 1500; int[][] examples = new int[numLines][]; readFile("cmc.data", examples); IFitnessFunction f = new FitnessFunction(examples, true); int method = 0, best_method = method; ISelectionMethod[] methods = new ISelectionMethod[]{new RouletteWheelSelection(), new RankSelection(), new EliteSelection()}; WAPChromosome c = new WAPChromosome(5 * 44); WAPChromosome legend = c; legend.Evaluate(f); double bestMR = 0, bestSP = 0, bestDC = 0, bestAA = 0; WAPPopulation pop = new WAPPopulation(100, c, f, methods[0]); pop.parallelism = true; Random rand = new Random(); while (true) { pop.RandomSelectionPortion = rand.NextDouble() * 0.7; pop.MutationRate = 0.1 + rand.NextDouble() * 0.3; pop.SelectionMethod = methods[method]; pop.DropConditionRate = rand.NextDouble() * 0.5; pop.AddAlternativeRate = rand.NextDouble() * 0.5; method = (method + 1) % methods.Length; Console.WriteLine("Entrenando: Metodo:{0}, DC:{1}, AA:{2}, SP:{3}, MR:{4}", method, pop.DropConditionRate, pop.AddAlternativeRate, pop.RandomSelectionPortion, pop.MutationRate); pop.Regenerate(); for (int i = 0; i < maxIter; i++) { pop.RunEpoch(); int prom = 0; for (int j = 0; j < pop.Size; j++) prom += ((WAPChromosome)pop[j]).numRules; prom = prom / pop.Size; //Console.WriteLine("Generacion {0} Mejor Fitness {1} prom: {2}, reglas:{3}", i, pop.BestChromosome.Fitness, pop.FitnessAvg, prom); if (pop.BestChromosome.Fitness > legend.Fitness) { legend = (WAPChromosome)pop.BestChromosome; best_method = method; bestAA = pop.AddAlternativeRate; bestDC = pop.DropConditionRate; bestMR = pop.MutationRate; bestSP = pop.RandomSelectionPortion; } } Console.WriteLine("\n\nMejor Cromosoma de la ultima generacion:"); Console.WriteLine("Fitness {0}", pop.BestChromosome.Fitness); Console.WriteLine("Clasificados {0}", Math.Sqrt(pop.BestChromosome.Fitness) + (double)((WAPChromosome)pop.BestChromosome).numRules / (double)examples.Length / 2.0); Console.WriteLine("Reglas {0}", ((WAPChromosome)pop.BestChromosome).numRules); //Console.WriteLine(legend); Console.WriteLine("\nMejor Cromosoma de todas las generaciones:"); Console.Write("Parm: Metodo:{0}, DC:{1}, AA:{2}, SP:{3}, MR:{4}", best_method, bestDC, bestAA, bestSP, bestMR); Console.WriteLine("Fitness {0}", legend.Fitness); Console.WriteLine("Clasificados {0}", Math.Sqrt(legend.Fitness) + (double)((WAPChromosome)legend).numRules / (double)examples.Length / 2.0); Console.WriteLine("Reglas {0}", legend.numRules); Console.WriteLine(legend); } } catch (Exception e) { Console.WriteLine(e); } }
static void Main(string[] args) { Stopwatch stopwatch = new Stopwatch(); decimal frequency = Stopwatch.Frequency; if (CommandLine.Parser.Default.ParseArguments(args, Options)) { TextWriter reportWriter = Options.OutputFilePath == null ? Console.Out : new StreamWriter(Options.OutputFilePath); VerboseLog("Data parsing ..."); DataParser parser = new DataParser(); List <KnapsackProblemModel> knapsackProblemModels = parser.ParseProblem(Options.InputFiles); Dictionary <int, int> knownResults = null; Dictionary <int, Tuple <int, long> > bruteForceResults = new Dictionary <int, Tuple <int, long> >(); Dictionary <int, Tuple <int, long> > costToRatioHeuristicsResults = new Dictionary <int, Tuple <int, long> >(); Dictionary <int, Tuple <int, long> > branchAndBoundResults = new Dictionary <int, Tuple <int, long> >(); Dictionary <int, Tuple <int, long> > dynamicByCostResults = new Dictionary <int, Tuple <int, long> >(); Dictionary <int, Tuple <int, long> > fptasResults = new Dictionary <int, Tuple <int, long> >(); Dictionary <int, Tuple <int, long> > geneticResults = new Dictionary <int, Tuple <int, long> >(); if (Options.ResultFiles != null) { knownResults = parser.ParseResults(Options.ResultFiles); } VerboseLog("Done."); IKnapsackSolver bruteForceSolver = new BruteForceSolver(); IKnapsackSolver ratioHeuristicSolver = new RatioHeuristicSolver(); IKnapsackSolver branchAndBoundSolver = new BranchAndBoundSolver(); IKnapsackSolver dynamicByCostSolve = new DynamicByCostSolver(); IKnapsackSolver fptasSolver = null; IKnapsackSolver geneticSolver = null; if (Options.FPTAS) { fptasSolver = new FPTASSolver(Options.FPTASAccuracy); } if (Options.Genetics) { ISelectionMethod selectionMethod = null; switch (Options.SelectionMethod) { case "roulette": selectionMethod = new RouletteWheelSelection(); break; case "rank": selectionMethod = new RankSelection(); break; case "elitary": selectionMethod = new EliteSelection(); break; default: Console.WriteLine("Wrong selection method for genetics"); break; } if (selectionMethod == null) { return; } if (Options.GeneticMetaoptimization) { //Random selection portion for (int i = 0; i < 100; i++) { double randomSelectionPortion = (i * 0.001) + 0; //Crossover rate for (int j = 0; j < 1; j++) { double crossoverRate = (j * 0.03) + 0.22; //Mutation rate for (int k = 0; k < 1; k++) { double mutationRate = (k * 0.04) + 0.87; geneticSolver = new GeneticSolver(Options.PopulationSize, Options.IterationsCount, selectionMethod, mutationRate, crossoverRate, randomSelectionPortion, false, false); geneticResults.Clear(); foreach (KnapsackProblemModel problem in knapsackProblemModels) { int result = 0; try { stopwatch.Restart(); result = geneticSolver.Solve(problem); stopwatch.Stop(); } catch (Exception ex) { } geneticResults.Add(problem.ProblemId, new Tuple <int, long>(result, stopwatch.ElapsedTicks)); } decimal totalTime = 0; decimal totalError = 0; foreach (KnapsackProblemModel problem in knapsackProblemModels) { int problemId = problem.ProblemId; Tuple <int, long> result = geneticResults[problemId]; totalTime += (result.Item2 / frequency); totalError += CalculateRelativeError(knownResults[problemId], result.Item1); } decimal averageError = totalError / knapsackProblemModels.Count; reportWriter.WriteLine(randomSelectionPortion + "," + crossoverRate + "," + mutationRate + "," + totalTime + "," + averageError); } } } } geneticSolver = new GeneticSolver(Options.PopulationSize, Options.IterationsCount, selectionMethod, Options.MutationRate, Options.CrossoverRate, Options.RandomSelectionPortion, Options.DiversityCheck, true); } VerboseLog("Solving JIT instance"); KnapsackProblemModel jitProblem = new KnapsackProblemModel(-1, 100, new List <Item> { new Item(18, 114, 0), new Item(42, 136, 1), new Item(88, 192, 2), new Item(3, 223, 3) }); bruteForceSolver.Solve(jitProblem); ratioHeuristicSolver.Solve(jitProblem); branchAndBoundSolver.Solve(jitProblem); dynamicByCostSolve.Solve(jitProblem); if (fptasSolver != null) { fptasSolver.Solve(jitProblem); } if (geneticSolver != null) { geneticSolver.Solve(jitProblem); } VerboseLog("Calculation started"); foreach (KnapsackProblemModel problem in knapsackProblemModels) { VerboseLog("Solving problem:"); VerboseLog(problem); int knownResult = -1; if (knownResults != null) { knownResult = knownResults[problem.ProblemId]; VerboseLog("Result should be: " + knownResult); } if (Options.BruteForce) { VerboseLog("Brute force solver ..."); stopwatch.Restart(); int result = bruteForceSolver.Solve(problem); stopwatch.Stop(); bruteForceResults.Add(problem.ProblemId, new Tuple <int, long>(result, stopwatch.ElapsedTicks)); if (knownResult != -1 && result != knownResult) { Console.WriteLine("ERROR - Brute force algorithm not accurate for problem " + problem); Environment.Exit(1); } } if (Options.BranchAndBound) { VerboseLog("Branch and bound solver ..."); stopwatch.Restart(); int result = branchAndBoundSolver.Solve(problem); stopwatch.Stop(); branchAndBoundResults.Add(problem.ProblemId, new Tuple <int, long>(result, stopwatch.ElapsedTicks)); if (knownResult != -1 && result != knownResult) { Console.WriteLine("ERROR - Branch and bound algorithm not accurate for problem " + problem); Environment.Exit(1); } } if (Options.DynamicByCost) { VerboseLog("Dynamic by cost solver ..."); stopwatch.Restart(); int result = dynamicByCostSolve.Solve(problem); stopwatch.Stop(); dynamicByCostResults.Add(problem.ProblemId, new Tuple <int, long>(result, stopwatch.ElapsedTicks)); if (knownResult != -1 && result != knownResult) { Console.WriteLine("ERROR - Dynamic by cost algorithm not accurate for problem " + problem); Environment.Exit(1); } } if (Options.CostToRatioHeuristics) { VerboseLog("Ratio heuristics solver ..."); stopwatch.Restart(); int result = ratioHeuristicSolver.Solve(problem); stopwatch.Stop(); costToRatioHeuristicsResults.Add(problem.ProblemId, new Tuple <int, long>(result, stopwatch.ElapsedTicks)); } if (Options.FPTAS) { VerboseLog("FPTAS solver ..."); if (fptasSolver != null) { stopwatch.Restart(); int result = fptasSolver.Solve(problem); stopwatch.Stop(); fptasResults.Add(problem.ProblemId, new Tuple <int, long>(result, stopwatch.ElapsedTicks)); } } if (Options.Genetics) { VerboseLog("Genetics solver ..."); if (geneticSolver != null) { stopwatch.Restart(); int result = geneticSolver.Solve(problem); stopwatch.Stop(); geneticResults.Add(problem.ProblemId, new Tuple <int, long>(result, stopwatch.ElapsedTicks)); } } VerboseLog("Problem solved."); } reportWriter.Write("Problem ID;Items count"); if (knownResults != null) { reportWriter.Write(";Known result"); } if (Options.BruteForce) { reportWriter.Write(";Brute force result;Time [s]"); if (knownResults != null) { reportWriter.Write(";Relative error"); } } if (Options.CostToRatioHeuristics) { reportWriter.Write(";Cost to weight ration heuristics result;Time [s]"); if (knownResults != null) { reportWriter.Write(";Relative error"); } } if (Options.BranchAndBound) { reportWriter.Write(";Branch and bound result;Time [s]"); if (knownResults != null) { reportWriter.Write(";Relative error"); } } if (Options.DynamicByCost) { reportWriter.Write(";Dynamic programming by cost result;Time [s]"); if (knownResults != null) { reportWriter.Write(";Relative error"); } } if (Options.FPTAS) { reportWriter.Write(";FPTAS result;Time [s]"); if (knownResults != null) { reportWriter.Write(";Relative error;Max possible error"); } } if (Options.Genetics) { reportWriter.Write(";Genetics result;Time [s]"); if (knownResults != null) { reportWriter.Write(";Relative error"); } } reportWriter.WriteLine(); foreach (KnapsackProblemModel problem in knapsackProblemModels) { var problemId = problem.ProblemId; reportWriter.Write(problemId); reportWriter.Write(";" + problem.Items.Count); if (knownResults != null) { reportWriter.Write(";" + knownResults[problemId]); } if (Options.BruteForce) { Tuple <int, long> bruteForceResult = bruteForceResults[problemId]; reportWriter.Write(";" + bruteForceResult.Item1 + ";" + bruteForceResult.Item2 / frequency); if (knownResults != null) { reportWriter.Write(";" + CalculateRelativeError(knownResults[problemId], bruteForceResult.Item1)); } } if (Options.CostToRatioHeuristics) { Tuple <int, long> heuristicsResult = costToRatioHeuristicsResults[problemId]; reportWriter.Write(";" + heuristicsResult.Item1 + ";" + heuristicsResult.Item2 / frequency); if (knownResults != null) { reportWriter.Write(";" + CalculateRelativeError(knownResults[problemId], heuristicsResult.Item1)); } } if (Options.BranchAndBound) { Tuple <int, long> heuristicsResult = branchAndBoundResults[problemId]; reportWriter.Write(";" + heuristicsResult.Item1 + ";" + heuristicsResult.Item2 / frequency); if (knownResults != null) { reportWriter.Write(";" + CalculateRelativeError(knownResults[problemId], heuristicsResult.Item1)); } } if (Options.DynamicByCost) { Tuple <int, long> heuristicsResult = dynamicByCostResults[problemId]; reportWriter.Write(";" + heuristicsResult.Item1 + ";" + heuristicsResult.Item2 / frequency); if (knownResults != null) { reportWriter.Write(";" + CalculateRelativeError(knownResults[problemId], heuristicsResult.Item1)); } } if (Options.FPTAS) { Tuple <int, long> heuristicsResult = fptasResults[problemId]; reportWriter.Write(";" + heuristicsResult.Item1 + ";" + heuristicsResult.Item2 / frequency); if (knownResults != null) { reportWriter.Write(";" + CalculateRelativeError(knownResults[problemId], heuristicsResult.Item1)); reportWriter.Write(";" + ((FPTASSolver)fptasSolver).GetMaximumError(problem)); } } if (Options.Genetics) { Tuple <int, long> heuristicsResult = geneticResults[problemId]; reportWriter.Write(";" + heuristicsResult.Item1 + ";" + heuristicsResult.Item2 / frequency); if (knownResults != null) { reportWriter.Write(";" + CalculateRelativeError(knownResults[problemId], heuristicsResult.Item1)); } } reportWriter.WriteLine(); } if (Options.Genetics) { decimal totalTime = 0; decimal totalError = 0; foreach (KnapsackProblemModel problem in knapsackProblemModels) { int problemId = problem.ProblemId; Tuple <int, long> result = geneticResults[problemId]; totalTime += (result.Item2 / frequency); totalError += CalculateRelativeError(knownResults[problemId], result.Item1); } decimal averageError = totalError / knapsackProblemModels.Count; reportWriter.WriteLine("Aggregate results"); reportWriter.WriteLine("Aggregate time"); reportWriter.WriteLine(totalTime); reportWriter.WriteLine("Average error"); reportWriter.WriteLine(averageError); } } else { Environment.Exit(1); } Environment.Exit(0); }
public MinFuncTournamentSelection(int kInNetIndividulas) { _kInNetIndividulas = kInNetIndividulas; _rouletteWeelSelection = new MinFuncRouletteWeelSelection(); }