/// <summary>
        /// Creates a new solver.
        /// </summary>
        /// <param name="population_size"></param>
        /// <param name="stagnation"></param>
        /// <param name="generation_operation"></param>
        /// <param name="cross_over_operation"></param>
        public EdgeAssemblyCrossOverSolver(int population_size, int stagnation,
            IGenerationOperation<List<int>, GeneticProblem, Fitness> generation_operation,
            ICrossOverOperation<List<int>, GeneticProblem, Fitness> cross_over_operation)
        {
            //_stopped = false;
            _stagnation = stagnation;
            _population_size = population_size;

            _generation_operation = generation_operation;
            _cross_over_operation = cross_over_operation;
        }
        /// <summary>
        /// Creates a new solver.
        /// </summary>
        /// <param name="population_size"></param>
        /// <param name="stagnation"></param>
        /// <param name="generation_operation"></param>
        /// <param name="cross_over_operation"></param>
        public EdgeAssemblyCrossOverSolver(int population_size, int stagnation,
                                           IGenerationOperation <List <int>, GeneticProblem, Fitness> generation_operation,
                                           ICrossOverOperation <List <int>, GeneticProblem, Fitness> cross_over_operation)
        {
            //_stopped = false;
            _stagnation      = stagnation;
            _population_size = population_size;

            _generation_operation = generation_operation;
            _cross_over_operation = cross_over_operation;
        }
Example #3
0
        /// <summary>
        /// Creates a genetic solver.
        /// </summary>
        public GeneticSolver()
        {
            _mutation_operation = new BestDetailedPlacementMutationOperation();
            _cross_over_operation = new SequentialContructiveCrossoverOperator();
            _generation_operation = new BestPlacementGenerationOperation();

            _eltism = 10;
            _mutation = 30;
            _cross = 20;

            _population = 20;
            _stagnation_count = 100;
        }
Example #4
0
        /// <summary>
        /// Creates a genetic solver.
        /// </summary>
        public GeneticSolver()
        {
            _mutation_operation   = new BestDetailedPlacementMutationOperation();
            _cross_over_operation = new SequentialContructiveCrossoverOperator();
            _generation_operation = new BestPlacementGenerationOperation();

            _eltism   = 10;
            _mutation = 30;
            _cross    = 20;

            _population       = 20;
            _stagnation_count = 100;
        }
Example #5
0
 /// <summary>
 /// Creates a new solver.
 /// </summary>
 /// <param name="settings"></param>
 /// <param name="selector"></param>
 /// <param name="combined_operation"></param>
 /// <param name="problem"></param>
 /// <param name="fitness_calculator"></param>
 public Solver(
     ProblemType problem,
     SolverSettings settings,
     ISelector <GenomeType, ProblemType, WeightType> selector,
     IOperation <GenomeType, ProblemType, WeightType> combined_operation,
     IFitnessCalculator <GenomeType, ProblemType, WeightType> fitness_calculator)
 {
     _problem              = problem;
     _settings             = settings;
     _selector             = selector;
     _mutation_operation   = combined_operation;
     _crossOverOperation   = combined_operation;
     _generation_operation = combined_operation;
     _fitness_calculator   = fitness_calculator;
 }
Example #6
0
        /// <summary>
        /// Creates a genetic solver.
        /// </summary>
        /// <param name="population"></param>
        /// <param name="stagnation"></param>
        /// <param name="elitism"></param>
        /// <param name="mutation_operation"></param>
        /// <param name="mutation"></param>
        /// <param name="cross_over_operation"></param>
        /// <param name="cross_over"></param>
        /// <param name="generation_operation"></param>
        public GeneticSolver(int population, int stagnation, int elitism,
                             IMutationOperation <List <int>, GeneticProblem, Fitness> mutation_operation, int mutation,
                             ICrossOverOperation <List <int>, GeneticProblem, Fitness> cross_over_operation, int cross_over,
                             IGenerationOperation <List <int>, GeneticProblem, Fitness> generation_operation)
        {
            _mutation_operation   = mutation_operation;
            _cross_over_operation = cross_over_operation;
            _generation_operation = generation_operation;

            _eltism   = elitism;
            _mutation = mutation;
            _cross    = cross_over;

            _population       = population;
            _stagnation_count = stagnation;
        }
Example #7
0
 /// <summary>
 /// Creates a new solver.
 /// </summary>
 /// <param name="problem"></param>
 /// <param name="settings"></param>
 /// <param name="selector"></param>
 /// <param name="mutation"></param>
 /// <param name="cross_over"></param>
 /// <param name="generation"></param>
 /// <param name="fitness_calculator"></param>
 /// <param name="accept_only_better_cross_over"></param>
 /// <param name="accept_only_better_mutation"></param>
 public Solver(
     ProblemType problem,
     SolverSettings settings,
     ISelector <GenomeType, ProblemType, WeightType> selector,
     IMutationOperation <GenomeType, ProblemType, WeightType> mutation,
     ICrossOverOperation <GenomeType, ProblemType, WeightType> cross_over,
     IGenerationOperation <GenomeType, ProblemType, WeightType> generation,
     IFitnessCalculator <GenomeType, ProblemType, WeightType> fitness_calculator,
     bool accept_only_better_cross_over, bool accept_only_better_mutation)
 {
     _problem                          = problem;
     _settings                         = settings;
     _selector                         = selector;
     _generation_operation             = generation;
     _mutation_operation               = mutation;
     _crossOverOperation               = cross_over;
     _fitness_calculator               = fitness_calculator;
     _accept_only_better_on_cross_over = accept_only_better_cross_over;
     _accept_only_better_on_mutation   = accept_only_better_mutation;
 }
Example #8
0
        /// <summary>
        /// Creates a genetic solver.
        /// </summary>
        /// <param name="population"></param>
        /// <param name="stagnation"></param>
        /// <param name="elitism"></param>
        /// <param name="mutation_operation"></param>
        /// <param name="mutation"></param>
        /// <param name="cross_over_operation"></param>
        /// <param name="cross_over"></param>
        /// <param name="generation_operation"></param>
        public GeneticSolver(int population, int stagnation, int elitism,
            IMutationOperation<List<int>, GeneticProblem, Fitness> mutation_operation, int mutation,
            ICrossOverOperation<List<int>, GeneticProblem, Fitness> cross_over_operation, int cross_over,
            IGenerationOperation<List<int>, GeneticProblem, Fitness> generation_operation)
        {
            _mutation_operation = mutation_operation;
            _cross_over_operation = cross_over_operation;
            _generation_operation = generation_operation;

            _eltism = elitism;
            _mutation = mutation;
            _cross = cross_over;

            _population = population;
            _stagnation_count = stagnation;
        }
Example #9
0
        /// <summary>
        /// Start the solver with an initial population.
        /// </summary>
        /// <param name="initial"></param>
        public Individual <GenomeType, ProblemType, WeightType> Start(Population <GenomeType, ProblemType, WeightType> initial)
        {
            WeightType fitness = default(WeightType);

            Population <GenomeType, ProblemType, WeightType> population = new Population <GenomeType, ProblemType, WeightType>(initial, true);

            OsmSharp.Logging.Log.TraceEvent("OsmSharp.Math.AI.Genetic.SolversSolver<GenomeType, ProblemType, WeightType>", TraceEventType.Information,
                                            "Generating population...");

            while (population.Count < _settings.PopulationSize)
            {
                // generate new.
                IGenerationOperation <GenomeType, ProblemType, WeightType> generation_operation = this.CreateGenerationOperation();
                Individual <GenomeType, ProblemType, WeightType>           new_individual       =
                    generation_operation.Generate(this);

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

                // report population generation.
                OsmSharp.Logging.Log.TraceEvent("OsmSharp.Math.AI.Genetic.SolversSolver<GenomeType, ProblemType, WeightType>", TraceEventType.Information,
                                                "Generating population {0}/{1}...", population.Count, _settings.PopulationSize);
                this.ReportNew(string.Format("Generating population..."), population.Count, _settings.PopulationSize);
            }

            OsmSharp.Logging.Log.TraceEvent("OsmSharp.Math.AI.Genetic.SolversSolver<GenomeType, ProblemType, WeightType>", TraceEventType.Information,
                                            "Done!");

            // sort the initial population.
            population.Sort(this, _fitness_calculator);

            //Tools.Core.Output.OutputTextStreamHost.WriteLine("Average {0}",
            //    _fitness_calculator.AverageFitness(_problem, population));

            int stagnation = 0;

            // start next generation or stop.
            WeightType new_fitness =
                population[0].Fitness;

            // is there improvement?
            if (new_fitness.CompareTo(fitness) > 0)
            { // a new fitness is found!
                // raise a new event here.
                this.RaiseNewFittest(population[0]);


                // set new fitness.
                fitness = new_fitness;
                fittest = population[0];

                // reset the stagnation count.
                stagnation = 0;

                OsmSharp.Logging.Log.TraceEvent("OsmSharp.Math.AI.Genetic.Solvers.Solver<GenomeType, ProblemType, WeightType>", TraceEventType.Information,
                                                "New Fittest {0}", fittest.ToString());
            }

            // start getting genetic!
            int generation_count = 0;

            while (generation_count < _settings.MaxGeneration &&
                   stagnation < _settings.StagnationCount)
            {
                // check if the solving has been cancelled.
                if (this.Cancelled)
                {
                    this.ReportNew(string.Format("Solver cancelled at generation {0}!", generation_count), 1, 1);
                    return(null);
                }
                if (this.Stopped)
                {
                    this.ReportNew(string.Format("Solver stopped at generation {0}!", generation_count), 1, 1);
                    return(fittest);
                }

                // calculate the next generation.
                population =
                    this.NextGeneration(population);

                // get the population fitness.
                population.Sort(this, _fitness_calculator);
                OsmSharp.Logging.Log.TraceEvent("OsmSharp.Math.AI.Genetic.Solvers.Solver<GenomeType, ProblemType, WeightType>", TraceEventType.Information,
                                                "{0}->{1}", population[0].Fitness, population[population.Count - 1].Fitness);

                // start next generation or stop.
                new_fitness =
                    population[0].Fitness;

                // is there improvement?
                if (new_fitness.CompareTo(fitness) < 0)
                { // a new fitness is found!
                    // raise a new event here.
                    this.RaiseNewFittest(population[0]);

                    // set new fitness.
                    fitness = new_fitness;
                    fittest = population[0];

                    OsmSharp.Logging.Log.TraceEvent("OsmSharp.Math.AI.Genetic.Solvers.Solver<GenomeType, ProblemType, WeightType>", TraceEventType.Information,
                                                    "New Fittest {0}-{1} {2}", generation_count, stagnation, fittest.ToString());

                    // reset the stagnation count.
                    stagnation = 0;

                    // report the new fittest.
                    this.ReportNew(string.Format("New Fittest {0}",
                                                 fittest.ToString()), 0, 1);
                }
                else
                {
                    // increase the stagnation count.
                    stagnation++;
                }

                // raise the generation count.
                generation_count++;

                // raise a new generation event.
                this.RaiseNewGeneration(generation_count, stagnation, population);

                // report the new generation.
                this.ReportNew(string.Format("Generation {0}-{1} {2}",
                                             generation_count,
                                             stagnation,
                                             fittest.ToString()), stagnation, _settings.StagnationCount);
                if (stagnation != 0 && stagnation % 1 == 0)
                {
                    OsmSharp.Logging.Log.TraceEvent("OsmSharp.Math.AI.Genetic.Solvers.Solver<GenomeType, ProblemType, WeightType>", TraceEventType.Information,
                                                    "Generation {0}-{1} {2}", generation_count, stagnation, fittest.ToString());
                }
            }

            OsmSharp.Logging.Log.TraceEvent("OsmSharp.Math.AI.Genetic.Solvers.Solver<GenomeType, ProblemType, WeightType>", TraceEventType.Information,
                                            "Result [{0}]:", fitness, fittest.ToString());

            // report the new generation.
            this.ReportNew(string.Format("Evolution finished @ generation {0}: {1}",
                                         generation_count,
                                         fittest.ToString()), generation_count, _settings.StagnationCount);
            OsmSharp.Logging.Log.TraceEvent("OsmSharp.Math.AI.Genetic.Solvers.Solver<GenomeType, ProblemType, WeightType>", TraceEventType.Information,
                                            "Evolution finished @ generation {0}: {1}", generation_count, fittest.ToString());

            return(fittest);
        }