Example #1
0
        /// <summary>
        ///     Setup and solve the TSP.
        /// </summary>
        public void Solve()
        {
            IGenerateRandom rnd     = new MersenneTwisterGenerateRandom();
            var             builder = new StringBuilder();

            InitCities(rnd);

            IPopulation pop = InitPopulation(rnd);

            IScoreFunction score = new TSPScore(_cities);

            _genetic = new BasicEA(pop, score);

            _genetic.AddOperation(0.9, new SpliceNoRepeat(Cities / 3));
            _genetic.AddOperation(0.1, new MutateShuffle());

            int    sameSolutionCount = 0;
            int    iteration         = 1;
            double lastSolution      = double.MaxValue;

            while (sameSolutionCount < MaxSameSolution)
            {
                _genetic.Iteration();

                double thisSolution = _genetic.LastError;

                builder.Length = 0;
                builder.Append("Iteration: ");
                builder.Append(iteration++);
                builder.Append(", Best Path Length = ");
                builder.Append(thisSolution);

                Console.WriteLine(builder.ToString());

                if (Math.Abs(lastSolution - thisSolution) < 1.0)
                {
                    sameSolutionCount++;
                }
                else
                {
                    sameSolutionCount = 0;
                }

                lastSolution = thisSolution;
            }

            Console.WriteLine("Good solution found:");
            var best = (IntegerArrayGenome)_genetic.BestGenome;

            DisplaySolution(best);
            _genetic.FinishTraining();
        }
        /// <summary>
        ///     Setup and solve the TSP.
        /// </summary>
        public void Solve()
        {
            IGenerateRandom rnd = new MersenneTwisterGenerateRandom();
            var builder = new StringBuilder();

            InitCities(rnd);

            IPopulation pop = InitPopulation(rnd);

            IScoreFunction score = new TSPScore(_cities);

            _genetic = new BasicEA(pop, score);

            _genetic.AddOperation(0.9, new SpliceNoRepeat(Cities / 3));
            _genetic.AddOperation(0.1, new MutateShuffle());

            int sameSolutionCount = 0;
            int iteration = 1;
            double lastSolution = double.MaxValue;

            while (sameSolutionCount < MaxSameSolution)
            {
                _genetic.Iteration();

                double thisSolution = _genetic.LastError;

                builder.Length = 0;
                builder.Append("Iteration: ");
                builder.Append(iteration++);
                builder.Append(", Best Path Length = ");
                builder.Append(thisSolution);

                Console.WriteLine(builder.ToString());

                if (Math.Abs(lastSolution - thisSolution) < 1.0)
                {
                    sameSolutionCount++;
                }
                else
                {
                    sameSolutionCount = 0;
                }

                lastSolution = thisSolution;
            }

            Console.WriteLine("Good solution found:");
            var best = (IntegerArrayGenome)_genetic.BestGenome;
            DisplaySolution(best);
            _genetic.FinishTraining();
        }