public void Test_IRRC() { const int cols = 20; const int rows = 15; var me = new IncreasingRowsRandomColumns(rows, cols); var m = new bool[rows, cols]; for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { m[r, c] = false; } } int row, col; int k = 0; while (me.GetNext(out row, out col)) { Assert.IsFalse(m[row, col]); Assert.AreEqual(k++ / cols, row); m[row, col] = true; } Assert.AreEqual(cols * rows, k); foreach (var b in m) { Assert.IsTrue(b); } }
public void Test_IRRC_ZeroMatrix() { const int cols = 0; const int rows = 0; var me = new IncreasingRowsRandomColumns(rows, cols); int r, c; Assert.IsFalse(me.GetNext(out r, out c)); }
/// <summary> /// Creates a population and let it evolve until termination condition is reached. /// </summary> /// <param name="terminationCondition">Evolution termination condition evaluated on each new epoch</param> /// <returns>The best solution found across the evolution</returns> public ISolution EvolveUntil(TerminationCondition terminationCondition) { var rnd = RandomProvider.GetThreadRandom(); var shiftMatrix = MatrixBuilder.Build(this.problem); var enumerator = new IncreasingRowsRandomColumns(this.problem.Days, this.problem.Slots); var constraints = ConstraintsBuilder.Build(this.problem); var chromoProcessor = new ChromosomeProcessor(shiftMatrix, enumerator, constraints); var evaluator = new FitnessEvaluator(problem); var fitnessFunction = new Fitness.FitnessFunction(chromoProcessor, evaluator, shiftMatrix); var chromosomeLength = shiftMatrix.GetNumberOfUnforcedSlots(); const double crossoverProbability = 0.90; const double mutationProbability = 0.05; const int elitismPercentage = 5; epochs = 1; epochsWithoutFitnessImprovement = 0; overallBestFitness = -1; log.Debug("Starting population."); var population = new Population(); for (var i = 0; i < populationSize; i++) { var c = new Double[chromosomeLength]; for (var k = 0; k < chromosomeLength; k++) { c[k] = rnd.NextDouble(); } var ch = new Chromosome(c); population.Solutions.Add(ch); } //create the genetic operators var elite = new Elite(elitismPercentage); var crossover = new Crossover(crossoverProbability, true) { CrossoverType = CrossoverType.SinglePoint }; var mutation = new SwapMutate(mutationProbability); //create the GA itself var ga = new GeneticAlgorithm(population, fitnessFunction.Evaluate); //subscribe to the GAs Generation Complete event ga.OnGenerationComplete += Ga_OnGenerationComplete; //add the operators to the ga process pipeline ga.Operators.Add(elite); ga.Operators.Add(crossover); ga.Operators.Add(mutation); //run the GA ga.Run((pop, currentGeneration, currentEvaluation) => { return(terminationCondition(currentGeneration, epochsWithoutFitnessImprovement, population.MaximumFitness, population.AverageFitness)); }); population.GetTop(1)[0].Evaluate(fitnessFunction.Evaluate); return(SolutionBuilder.Build(overallBestFitness, shiftMatrix, epochs * population.PopulationSize, this.problem.Items)); }