Fitness function for TSP task (Travaling Salasman Problem)
Inheritance: IFitnessFunction
Example #1
0
        // Worker thread
        void SearchSolution()
        {
            // create fitness function
            TSPFitnessFunction fitnessFunction = new TSPFitnessFunction(map);

            // create population
            Population population = new Population(populationSize,
                                                   (greedyCrossover) ? new TSPChromosome(map) : new PermutationChromosome(citiesCount),
                                                   fitnessFunction,
                                                   (selectionMethod == 0) ? (ISelectionMethod) new EliteSelection() :
                                                   (selectionMethod == 1) ? (ISelectionMethod) new RankSelection() :
                                                   (ISelectionMethod) new RouletteWheelSelection()
                                                   );

            // iterations
            int i = 1;

            // path
            double[,] path = new double[citiesCount + 1, 2];

            // loop
            while (!needToStop)
            {
                // run one epoch of genetic algorithm
                population.RunEpoch();

                // display current path
                ushort[] bestValue = ((PermutationChromosome)population.BestChromosome).Value;

                for (int j = 0; j < citiesCount; j++)
                {
                    path[j, 0] = map[bestValue[j], 0];
                    path[j, 1] = map[bestValue[j], 1];
                }
                path[citiesCount, 0] = map[bestValue[0], 0];
                path[citiesCount, 1] = map[bestValue[0], 1];

                mapControl.UpdateDataSeries("path", path);

                // set current iteration's info
                SetText(currentIterationBox, i.ToString());
                SetText(pathLengthBox, fitnessFunction.PathLength(population.BestChromosome).ToString());

                // increase current iteration
                i++;

                //
                if ((iterations != 0) && (i > iterations))
                {
                    break;
                }
            }

            // enable settings controls
            EnableControls(true);
        }
        // Worker thread
        void SearchSolution()
        {
            TSPFitnessFunction fitnessFunction = new TSPFitnessFunction(map);
            Population         population      = new Population(populationSize, new TSPChromosome(map), fitnessFunction,
                                                                (selectionMethod == 0) ? new EliteSelection() : (selectionMethod == 1) ? new RankSelection()
                : (ISelectionMethod) new RouletteWheelSelection());

            int i = 1;

            double[,] path = new double[citiesCount + 1, 2];

            while (!needToStop)
            {
                RILogManager.Default?.SendDebug("Running Epoch " + i);
                population.RunEpoch();
                ushort[] bestValue = ((PermutationChromosome)population.BestChromosome)?.Value;

                for (int j = 0; j < citiesCount; j++)
                {
                    if (bestValue != null && map != null)
                    {
                        path[j, 0] = map[bestValue[j], 0];
                        path[j, 1] = map[bestValue[j], 1];
                    }
                }
                if (bestValue != null && map != null)
                {
                    path[citiesCount, 0] = map[bestValue[0], 0];
                    path[citiesCount, 1] = map[bestValue[0], 1];
                }

                mapControl?.UpdateDataSeries("path", path);
                SetText(currentIterationBox, i.ToString("N0"));

                i++;
                if ((iterations != 0) && (i > iterations))
                {
                    break;
                }
            }

            EnableControls(true);
        }
Example #3
0
        // Worker thread
        void SearchSolution()
        {
            // create fitness function
            TSPFitnessFunction fitnessFunction = new TSPFitnessFunction(map);

            // create population
            Population population = new Population(populationSize,
                (greedyCrossover) ? new TSPChromosome(map) : new PermutationChromosome(citiesCount),
                fitnessFunction,
                (selectionMethod == 0) ? (ISelectionMethod)new EliteSelection() :
                (selectionMethod == 1) ? (ISelectionMethod)new RankSelection() :
                (ISelectionMethod)new RouletteWheelSelection()
                );

            // iterations
            int i = 1;

            // path
            double[,] path = new double[citiesCount + 1, 2];

            // loop
            while (!needToStop)
            {
                // run one epoch of genetic algorithm
                population.RunEpoch();

                // display current path
                ushort[] bestValue = ((PermutationChromosome)population.BestChromosome).Value;

                for (int j = 0; j < citiesCount; j++)
                {
                    path[j, 0] = map[bestValue[j], 0];
                    path[j, 1] = map[bestValue[j], 1];
                }
                path[citiesCount, 0] = map[bestValue[0], 0];
                path[citiesCount, 1] = map[bestValue[0], 1];

                mapControl.UpdateDataSeries("path", path);

                // set current iteration's info
                SetText(currentIterationBox, i.ToString());
                SetText(pathLengthBox, fitnessFunction.PathLength(population.BestChromosome).ToString());

                // increase current iteration
                i++;

                //
                if ((iterations != 0) && (i > iterations))
                    break;
            }

            // enable settings controls
            EnableControls(true);
        }