public void ValiadateTravelingSalesmanProblemFitnessCalculator_ComparerCheck()
 {
     var calculator = new TravelingSalesmanProblemFitnessCalculator(new List<TravelingSalesmanCity>());
     var compResult = calculator.Compare(1000, 2000);
     Assert.AreEqual(1000.0, compResult.FitnessA);
     Assert.AreEqual(2000.0, compResult.FitnessB);
     Assert.IsTrue(compResult.IsAFitter);
     Assert.IsFalse(compResult.IsBFitter);
     Assert.IsFalse(compResult.AreEqual);
 }
        public void ValiadateTravelingSalesmanProblemFitnessCalculator_WithKnownDistance()
        {
            var cities = new List<TravelingSalesmanCity>() {
                new TravelingSalesmanCity('A', 0, 0),
                new TravelingSalesmanCity('B', 0, 10),
                new TravelingSalesmanCity('C', 10, 10),
                new TravelingSalesmanCity('D', 10, 0)
            };

            var calculator = new TravelingSalesmanProblemFitnessCalculator(cities);
            var distance = calculator.Calculate(new Chromosomes.PermutationChromosome<char>("ABCD".ToCharArray()));
            Assert.AreEqual(40, distance);
        }
        public void SolveTravelingSalesmanProblem()
        {
            var citiesCoordinates = new List<TravelingSalesmanCity>()
            {
                new TravelingSalesmanCity('A', 0, 0),
                new TravelingSalesmanCity('B', 10, 100),
                new TravelingSalesmanCity('C', 20, 20),
                new TravelingSalesmanCity('D', 60, 20),
                new TravelingSalesmanCity('E', 30, 10),
                new TravelingSalesmanCity('F', 50, 70),
                new TravelingSalesmanCity('G', 70, 30),
                new TravelingSalesmanCity('H', 30, 30),
                new TravelingSalesmanCity('I', 50, 80)
            };

            var random = new RandomNumberGenerators.Default();

            var genePool = new Chromosomes.GenePool<char>();
            genePool.AddRange(new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I' });

            var chromosomeFactory = new Chromosomes.PermutationChromosomeFactory<int>(random, genePool, 9); // chromosome as a permutation of 1-9 numbers
            var fitnessCalc = new TravelingSalesmanProblemFitnessCalculator(citiesCoordinates);
            var mutationStrategy = new MutationStrategies.SingleGeneMutation(random, 0.02); // mutation rate fixed to 2%
            var crossoverStrategy = new CrossoverStrategies.DoubleCut(random);
            var selectionStrategy = new SelectionStrategies.RouletteWheel(random);
            var migrationStrategy = new MigrationStrategies.Random(random, 0.05); // random migration with chance of occurence 5%
            var populationStrategy = new PopulationStrategies.MultiPopulation(10, 50, migrationStrategy); // 10 population with fixed size of 50

            // need to pass something telling that fitness is a total distance so the shortest the better

            var evolution = new Evolution(random,
                chromosomeFactory,
                populationStrategy,
                mutationStrategy,
                crossoverStrategy,
                selectionStrategy);

            do
            {
                evolution.Step();
                if (evolution.TopFitness == 1.0)
                {
                    return;
                }
            }
            while (evolution.Generation < 1000);
            Assert.Fail();
        }