コード例 #1
0
 /// <summary>
 /// Creates a new solver.
 /// </summary>
 /// <param name="problem"></param>
 /// <param name="customers"></param>
 public EdgeAssemblyCrossOverSolver(OsmSharp.Math.TSP.Problems.IProblem problem, IList <int> customers)
 {
     //_stopped = false;
     //_customers = customers;
 }
コード例 #2
0
        /// <summary>
        /// Returns a solution found using best-placement.
        /// </summary>
        /// <returns></returns>
        protected override IRoute DoSolve(OsmSharp.Math.TSP.Problems.IProblem problem)
        {
            // create the settings.
            SolverSettings settings = new SolverSettings(
                -1,
                -1,
                1000000000,
                -1,
                -1,
                -1);

            Solver <List <int>, GeneticProblem, Fitness> solver =
                new Solver <List <int>, GeneticProblem, Fitness>(
                    new GeneticProblem(problem),
                    settings,
                    null,
                    null,
                    null,
                    _generation_operation,
                    new FitnessCalculator(),
                    true, false);

            Population <List <int>, GeneticProblem, Fitness> population =
                new Population <List <int>, GeneticProblem, Fitness>(true);

            while (population.Count < _population_size)
            {
                // generate new.
                Individual <List <int>, GeneticProblem, Fitness> new_individual =
                    _generation_operation.Generate(solver);

                // add to population.
                population.Add(new_individual);
            }

            // select each individual once.
            Population <List <int>, GeneticProblem, Fitness> new_population =
                new Population <List <int>, GeneticProblem, Fitness>(true);
            Individual <List <int>, GeneticProblem, Fitness> best = null;
            int stagnation = 0;

            while (stagnation < _stagnation)
            {
                while (new_population.Count < _population_size)
                {
                    // select an individual and the next one.
                    int idx = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(population.Count);
                    Individual <List <int>, GeneticProblem, Fitness> individual1 = population[idx];
                    Individual <List <int>, GeneticProblem, Fitness> individual2 = null;
                    if (idx == population.Count - 1)
                    {
                        individual2 = population[0];
                    }
                    else
                    {
                        individual2 = population[idx + 1];
                    }
                    population.RemoveAt(idx);

                    Individual <List <int>, GeneticProblem, Fitness> new_individual = _cross_over_operation.CrossOver(solver,
                                                                                                                      individual1, individual2);

                    new_individual.CalculateFitness(solver.Problem, solver.FitnessCalculator);
                    if (new_individual.Fitness.CompareTo(
                            individual1.Fitness) < 0)
                    {
                        new_population.Add(new_individual);
                    }
                    else
                    {
                        new_population.Add(individual1);
                    }
                }

                population = new_population;
                population.Sort(solver, solver.FitnessCalculator);

                new_population = new Population <List <int>, GeneticProblem, Fitness>(true);

                if (best == null ||
                    best.Fitness.CompareTo(population[0].Fitness) > 0)
                {
                    stagnation = 0;
                    best       = population[0];
                }
                else
                {
                    stagnation++;
                }

                // report progress.
                OsmSharp.Logging.Log.TraceEvent("EdgeAssemblyCrossOverSolver", Logging.TraceEventType.Information,
                                                string.Format("Solving using EAX: Stagnation {0}.", stagnation));
            }

            var result = new List <int>(best.Genomes);

            result.Insert(0, 0);
            return(DynamicAsymmetricRoute.CreateFrom(result));
        }