Exemple #1
0
        public void Evolve_ManyGenerations_Fast()
        {
            int movesAhead = 10;
            int boardSize = 10;
            var selection = new EliteSelection();
            var crossover = new OrderedCrossover();
            var mutation = new TworsMutation();
            var chromosome = new CheckersChromosome(movesAhead, boardSize);
            var fitness = new CheckersFitness(new CheckersBoard(boardSize));

            var population = new Population(40, 40, chromosome);

            var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);
            ga.GenerationRan += delegate
            {
                if (ga.Population.GenerationsNumber % 100 == 0)
                {
                    fitness.Update(ga.Population.BestChromosome as CheckersChromosome);
                }
            };

            ga.Start();
            var firstFitness = ((CheckersChromosome)ga.Population.BestChromosome).Fitness;

            ga.Termination = new GenerationNumberTermination(2001);

            TimeAssert.LessThan(100000, () =>
            {
                ga.Start();
            });

            var lastFitness = ((CheckersChromosome)ga.Population.BestChromosome).Fitness;

            Assert.LessOrEqual(firstFitness, lastFitness);
        }
		public void Clone_NoArgs_Cloned()
		{
			var target = new CheckersChromosome (2, 10);

			var actual = target.Clone () as CheckersChromosome;
			Assert.IsFalse (Object.ReferenceEquals (target, actual));
			Assert.AreEqual(2, actual.Moves.Count);
		}
Exemple #3
0
 /// <summary>
 /// Update the specified checkersChromosome.
 /// </summary>
 /// <param name="checkersChromosome">Checkers chromosome.</param>
 public void Update(CheckersChromosome checkersChromosome)
 {
     if (checkersChromosome.Fitness > 0)
     {
         var move = checkersChromosome.Moves.First();
         Board.MovePiece(move);
     }
 }
 public void Evaluate_ChromosomeForwardMove_Fitness05()
 {
     var target = new CheckersFitness(new CheckersBoard(8));
     var chromosome = new CheckersChromosome(2, 8);
     chromosome.Moves.Clear();
     chromosome.Moves.Add(new CheckersMove(new CheckersPiece(CheckersPlayer.PlayerOne) { CurrentSquare = new CheckersSquare(1, 2) }, new CheckersSquare(2, 3)));
     Assert.AreEqual(0.5, target.Evaluate(chromosome));
 }
        public void Evaluate_ChromosomeForwardMoveAndCanCaptureAnotherOne_Fitness2()
        {
            var board = new CheckersBoard(8);
            var target = new CheckersFitness(board);
            var move = new CheckersMove(new CheckersPiece(CheckersPlayer.PlayerOne) { CurrentSquare = new CheckersSquare(3, 2) }, new CheckersSquare(4, 3));
            Assert.IsTrue(board.MovePiece(move));

            move = new CheckersMove(new CheckersPiece(CheckersPlayer.PlayerTwo) { CurrentSquare = new CheckersSquare(6, 5) }, new CheckersSquare(5, 4));
            Assert.IsTrue(board.MovePiece(move));

            var chromosome = new CheckersChromosome(2, 8);
            chromosome.Moves.Clear();
            chromosome.Moves.Add(new CheckersMove(new CheckersPiece(CheckersPlayer.PlayerOne) { CurrentSquare = new CheckersSquare(4, 3) }, new CheckersSquare(6, 5)));
            target.Update(chromosome);

            Assert.AreEqual(2, target.Evaluate(chromosome));
        }
 /// <summary>
 /// Update the specified checkersChromosome.
 /// </summary>
 /// <param name="checkersChromosome">Checkers chromosome.</param>
 public void Update(CheckersChromosome checkersChromosome)
 {
     if (checkersChromosome.Fitness > 0)
     {
         var move = checkersChromosome.Moves.First();
         Board.MovePiece (move);
     }
 }