/// <summary>
        /// Returns a solution found using best-placement.
        /// </summary>
        /// <returns></returns>
        protected override IRoute DoSolve(OsmSharp.Tools.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.Tools.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.Tools.Output.OutputStreamHost.ReportProgress(stagnation,_stagnation,
                //    "OsmSharp.Tools.Math.TSP.EdgeAssemblyGenetic.EdgeAssemblyCrossOverSolver",
                //    "Solving using EAX...");
            }

            List<int> result = new List<int>(best.Genomes);
            result.Insert(0, 0);
            //return new SimpleAsymmetricRoute(result, true);
            return DynamicAsymmetricRoute.CreateFrom(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Solves the problem using a GA.
        /// </summary>
        /// <param name="problem"></param>
        /// <returns></returns>
        protected override IRoute DoSolve(OsmSharp.Tools.Math.TSP.Problems.IProblem problem)
        {
            //int population_size = 10;
            //if (problem.Size < 100)
            //{
            //    population_size = System.Math.Max(problem.Size * 3, 10);
            //    if (problem.Size < 10)
            //    {
            //        population_size = 1;
            //    }
            //}
            //if (problem.Size < 1000)
            //{
            //    population_size = problem.Size / 4;
            //}

            // create the settings.
            SolverSettings settings = new SolverSettings(
                _stagnation_count,
                _population,
                1000000000,
                _eltism,
                _cross,
                _mutation);

            //List<IMutationOperation<List<int>, GeneticProblem, Fitness>> mutators = new List<IMutationOperation<int,GeneticProblem,Fitness>>();
            ////mutators.Add(new DefaultMutationOperation());
            ////mutators.Add(new BestPlacementMutationOperation());
            //mutators.Add(new BestDetailedPlacementMutationOperation());
            //List<double> probabilities = new List<double>();
            //probabilities.Add(1);
            ////probabilities.Add(0.5);
            ////probabilities.Add(0.3);

            //CombinedMutation<List<int>, GeneticProblem, Fitness> mutation = new CombinedMutation<int,GeneticProblem,Fitness>(
            //    StaticRandomGenerator.Get(),
            //    mutators,
            //    probabilities);

            ////SequentialContructiveCrossoverOperator cross_over = new SequentialContructiveCrossoverOperator();
            //BestDetailedPlacementCrossOverOperation cross_over = new BestDetailedPlacementCrossOverOperation();
            ////BestPlacementCrossOverOperation cross_over = new BestPlacementCrossOverOperation();
            ////EdgeRecombinationCrossOverOperation cross_over = new EdgeRecombinationCrossOverOperation();

            //BestPlacementGenerationOperation generation = new BestPlacementGenerationOperation();
            ////RandomGenerationOperation generation = new RandomGenerationOperation();
            ISelector<List<int>, GeneticProblem, Fitness> selector = new RandomSelector<List<int>, GeneticProblem, Fitness>();
            //ISelector<List<int>, GeneticProblem, Fitness> selector = new TournamentBasedSelector<List<int>, GeneticProblem, Fitness>(75, 0.01);
            solver =
                new Solver<List<int>, GeneticProblem, Fitness>(
                    new GeneticProblem(problem),
                    settings,
                    selector,
                    _mutation_operation,
                    _cross_over_operation,
                    _generation_operation,
                    new FitnessCalculator(),
                    true, false);

            solver.NewFittest += new Solver<List<int>, GeneticProblem, Fitness>.NewFittestDelegate(solver_NewFittest);
            solver.NewGeneration += new Solver<List<int>, GeneticProblem, Fitness>.NewGenerationDelegate(solver_NewGeneration);
            List<int> result = new List<int>(solver.Start(null).Genomes);
            result.Insert(0, 0);
            return new SimpleAsymmetricRoute(result, true);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Executes a solver procedure.
        /// </summary>
        /// <param name="problem"></param>
        /// <returns></returns>
        internal override MaxTimeSolution Solve(MaxTimeProblem problem)
        {
            _customers = problem.Customers;

            //float[] solutions = OsmSharp.Tools.Math.VRP.Core.BestPlacement.CheapestInsertionHelper.CalculateBestValues(
            //    problem, _customers);

            generations = 0;

            _max_generations = 10000000;

            // calculate one tsp solution.
            //Tools.Math.TSP.ISolver tsp_solver = new OsmSharp.Tools.Math.TSP.EdgeAssemblyGenetic.EdgeAssemblyCrossOverSolver(_population, _stagnation,
            //         new OsmSharp.Tools.Math.TSP.Genetic.Solver.Operations.Generation._3OptGenerationOperation(),
            //          new OsmSharp.Tools.Math.TSP.Genetic.Solver.Operations.CrossOver.EdgeAssemblyCrossover(30,
            //                 OsmSharp.Tools.Math.TSP.Genetic.Solver.Operations.CrossOver.EdgeAssemblyCrossover.EdgeAssemblyCrossoverSelectionStrategyEnum.SingleRandom,
            //                 true));
            //IRoute tsp_solution = tsp_solver.Solve(new OsmSharp.Routing.VRP.NoDepot.MaxTime.TSPPlacement.TSPProblem(
            //    problem));
            // initialize the generation.
            IGenerationOperation<MaxTimeSolution, MaxTimeProblem, Fitness> generation =
                //new SolverGenerationOperation(new TSPPlacement.TSPPlacementSolver<ResolvedType>(
                //    this.Router, this.Max, this.DeliveryTime, tsp_solution));
                new OsmSharp.Routing.VRP.NoDepot.MaxTime.Genetic.Generation.RandomBestPlacement();
            //new SolverGenerationOperation(new CheapestInsertionSolverWithImprovements<ResolvedType>(
            //    this.Router, this.Max, this.DeliveryTime, 5, 0.1f, true, 0.1f, false, 1f, null, null));
            //new SolverGenerationOperation(new CheapestInsertionSolverWithImprovements<ResolvedType>(
            //    this.Router, this.Max, this.DeliveryTime, 5, 0.1f, true, 0.1f, true, 1f));

            // initialize the crossover.
            ICrossOverOperation<MaxTimeSolution, MaxTimeProblem, Fitness> cross_over =
                new OsmSharp.Routing.VRP.NoDepot.MaxTime.Genetic.CrossOver.RouteExchangeOperation();

            // initialize the mutation.
            //IMutationOperation<MaxTimeSolution, MaxTimeProblem, Fitness> mutation =
            //    new VehicleMutation();

            List<IMutationOperation<MaxTimeSolution, MaxTimeProblem, Fitness>> mutators =
                new List<IMutationOperation<MaxTimeSolution, MaxTimeProblem, Fitness>>();
            mutators.Add(new VehicleMutation());
            //mutators.Add(new ThreeOptMutation());
            //mutators.Add(new RedivideRouteMutation());
            mutators.Add(new RoutePartExchangeMutation());
            if (_probabilities == null)
            {
                _probabilities = new List<double>();
                _probabilities.Add(0.2);
                _probabilities.Add(0.6);
                _probabilities.Add(0.2);
            }

            CombinedMutation<MaxTimeSolution, MaxTimeProblem, Fitness> mutation = new CombinedMutation<MaxTimeSolution, MaxTimeProblem, Fitness>(
                StaticRandomGenerator.Get(),
                mutators,
                _probabilities);

            SolverSettings settings = new SolverSettings(_stagnation, _population, _max_generations,
                _elitism_percentage, _cross_percentage, _mutation_percentage);
            MaxTimeProblem genetic_problem = problem;// new MaxTimeProblem(max,  problem, solutions);
            Solver<MaxTimeSolution, MaxTimeProblem, Fitness> solver =
                new Solver<MaxTimeSolution, MaxTimeProblem, Fitness>(genetic_problem, settings,
                new TournamentBasedSelector<MaxTimeSolution, MaxTimeProblem, Fitness>(5, 0.5),
                mutation, //new ThreeOptMutation(),
                cross_over, // new RouteExchangeOperation(), //new RouteExchangeOperation(), //new RouteExchangeAndVehicleOperation(), // Order1CrossoverOperation()
                generation, //new RandomBestPlacement(),//new RandomGeneration(), //new RandomBestPlacement(),
                new FitnessCalculator());
            solver.NewFittest += new Solver<MaxTimeSolution, MaxTimeProblem, Fitness>.NewFittestDelegate(solver_NewFittest);
            //solver.NewGeneration += new Solver<MaxTimeSolution, Problem, Fitness>.NewGenerationDelegate(solver_NewGeneration);
            Individual<MaxTimeSolution, MaxTimeProblem, Fitness> solution = solver.Start(null);
            //this.solver_NewFittest(solution);

            MaxTimeSolution routes = solution.Genomes;

            long ticks_after = DateTime.Now.Ticks;

            StringBuilder sizes = new StringBuilder();
            foreach (int size in routes.Sizes)
            {
                sizes.Append(size);
                sizes.Append(" ");
            }

            StringBuilder weights = new StringBuilder();
            foreach (double weight in solution.Fitness.Weights)
            {
                weights.Append(weight.ToString(CultureInfo.InvariantCulture));
                weights.Append(" ");
            }

            return routes;
        }