Example #1
0
 public Evolver(IPopulationGenerator <T> generator,
                int populationSize,
                BreederBase <T> breeder,
                MutatorBase <T> mutator,
                Func <T, double> fitnessFunc) : this(GeneratePopulation(generator, populationSize), breeder, mutator, fitnessFunc)
 {
 }
Example #2
0
        public Simulation(
            ISimulationParameters simulationParams,
            IMutator mutator,
            IItemSelector itemSelector,
            IPopulationLogger logger,
            IEvaluator evaluator,
            ICrossoverSelector selector,
            ICrossBreeder breeder,
            IPopulationGenerator populationGenerator)
        {
            ObjectValidator.IfNullThrowException(simulationParams, nameof(simulationParams));
            ObjectValidator.IfNullThrowException(mutator, nameof(mutator));
            ObjectValidator.IfNullThrowException(itemSelector, nameof(itemSelector));
            ObjectValidator.IfNullThrowException(logger, nameof(logger));
            ObjectValidator.IfNullThrowException(evaluator, nameof(evaluator));
            ObjectValidator.IfNullThrowException(selector, nameof(selector));
            ObjectValidator.IfNullThrowException(breeder, nameof(breeder));
            ObjectValidator.IfNullThrowException(populationGenerator, nameof(populationGenerator));

            _mutator              = mutator;
            _itemSelector         = itemSelector;
            _logger               = logger;
            _evaluator            = evaluator;
            _selector             = selector;
            _breeder              = breeder;
            _populationGenerator  = populationGenerator;
            _simulationParameters = simulationParams;
        }
Example #3
0
        public Population(int count, IPopulationGenerator <T> generator, IFitnessEvaluator <T> evaluator)
        {
            if (count <= 0)
            {
                throw new ArgumentException($"{nameof(count)} must be greater than 0. Actual: {count}.");
            }

            if (generator == null)
            {
                throw new System.ArgumentNullException(nameof(generator));
            }

            if (evaluator == null)
            {
                throw new System.ArgumentNullException(nameof(evaluator));
            }

            TargetSize = count;

            // TODO: This really shouldn't be here. We should instead pass the
            // population
            for (var i = 0; i < count; i++)
            {
                var value      = generator.Generate();
                var fitness    = evaluator.ComputeFitness(value);
                var chromosome = new Chromosome <T>(value, fitness);

                Chromosomes.Add(chromosome);
            }
        }
Example #4
0
 public GeneticSearchEngine(GeneticSearchOptions options, IPopulationGenerator populationGenerator, IChildrenGenerator childrenGenerator, IEnvironment environment)
 {
     this.options       = options;
     searchContext      = new SearchContext(options.IncludeAllHistory, environment);
     engine             = new InternalEngine(populationGenerator, childrenGenerator, options);
     runAndPauseManager = new RunAndPauseManager(pauseTimeout);
 }
 public InternalEngine(IPopulationGenerator populationGenerator, IChildrenGenerator childrenGenerator,
                       GeneticSearchOptions options)
 {
     this.populationGenerator = populationGenerator;
     this.childrenGenerator   = childrenGenerator;
     this.options             = options;
 }
 public NumberVectorTests()
 {
     mutationManager     = new IntUniformMutationManager(0, 100);
     evaluator           = new BasicEvaluator();
     populationGenerator = new IntVectorChromosomePopulationGenerator(VECTOR_SIZE, 0, 1, mutationManager, evaluator);
     crossoverManager    = new SinglePointCrossoverManager <int>(mutationManager, evaluator);
 }
Example #7
0
        public GeneticSearchEngineBuilder(int populationSize, int maxGenerations, ICrossoverManager crossoverManager,
                                          IPopulationGenerator populationGenerator)
        {
            this.populationSize      = populationSize;
            this.crossoverManager    = crossoverManager;
            this.populationGenerator = populationGenerator;

            stopManagers.Add(new StopAtGeneration(maxGenerations));
        }
Example #8
0
 public GeneticAlgorithm(int _genome_size, IProcreator <T> _procreator, ISelector <T> _selector, IPopulationGenerator <T> _generator, Goal _goal, Random _random, string _name)
 {
     procreator  = _procreator;
     selector    = _selector;
     generator   = _generator;
     goal        = _goal;
     genome_size = _genome_size;
     name        = _name;
     random      = _random;
 }
Example #9
0
        private static List <T> GeneratePopulation(IPopulationGenerator <T> generator, int populationSize)
        {
            var result = new List <T>();

            // Generate intial population
            for (int i = 0; i < populationSize; i++)
            {
                result.Add(generator.GenerateIndividual());
            }

            return(result);
        }
Example #10
0
        //public UmEngineWithoutRecombination(IBenchmark benchmark, IPopulationGenerator populationGenerator, IEvaluator evaluator, ILogger logger, IMutator objectMutator, IMutator stdDeviationsMutator, IMutationRuleSupervisor mutationRuleSupervisor, IParentsSelector parentsParentsSelector, ISurvivorsSelector survivorsSelector, IPointsGenerator positivePointsGenerator, IPointsGenerator negativePointsGenerator, ExperimentParameters experimentParameters, Solution[] basePopulation, Solution[] offspringPopulation) : base(benchmark, populationGenerator, evaluator, logger, objectMutator, stdDeviationsMutator, mutationRuleSupervisor, parentsParentsSelector, survivorsSelector, positivePointsGenerator, negativePointsGenerator, experimentParameters, basePopulation, offspringPopulation)
        //{
        //    Benchmark = benchmark;
        //    PopulationGenerator = populationGenerator;
        //    Evaluator = evaluator;
        //    Logger = logger;
        //    ObjectMutator = objectMutator;
        //    StdDeviationsMutator = stdDeviationsMutator;
        //    MutationRuleSupervisor = mutationRuleSupervisor;
        //    ParentsSelector = parentsParentsSelector;
        //    SurvivorsSelector = survivorsSelector;
        //    PositivePointsGenerator = positivePointsGenerator;
        //    NegativePointsGenerator = negativePointsGenerator;
        //    ExperimentParameters = experimentParameters;
        //    BasePopulation = basePopulation;
        //    OffspringPopulation = offspringPopulation;
        //}

        //protected IProcessor<Constraint[]> RedundantConstriantsRemover;

        public UmEngineWithoutRecombination(IBenchmark benchmark, IPopulationGenerator populationGenerator, IEvaluator evaluator, ILogger logger, IMutator objectMutator, IMutator stdDeviationsMutator, IMutationRuleSupervisor mutationRuleSupervisor, IParentsSelector parentsParentsSelector, ISurvivorsSelector survivorsSelector, IProcessor <Constraint[]> redundantConstrainsRemover, ExperimentParameters experimentParameters, Statistics statistics, Solution[] basePopulation, Solution[] offspringPopulation) : base(benchmark, populationGenerator, evaluator, logger, objectMutator, stdDeviationsMutator, mutationRuleSupervisor, parentsParentsSelector, survivorsSelector, redundantConstrainsRemover, experimentParameters, statistics, basePopulation, offspringPopulation)
        {
            //Benchmark = benchmark;
            //PopulationGenerator = populationGenerator;
            //Evaluator = evaluator;
            //Logger = logger;
            //ObjectMutator = objectMutator;
            //StdDeviationsMutator = stdDeviationsMutator;
            //MutationRuleSupervisor = mutationRuleSupervisor;
            //ParentsSelector = parentsParentsSelector;
            //SurvivorsSelector = survivorsSelector;
            //RedundantConstriantsRemover = redundantConstrainsRemover;
            //ExperimentParameters = experimentParameters;
            //BasePopulation = basePopulation;
            //OffspringPopulation = offspringPopulation;
        }
Example #11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GeneticAlgorithmManager"/> class
        /// with given <paramref name="fitnessEvaluator"/> and <paramref name="populationSize"/>.
        /// </summary>
        /// <param name="fitnessEvaluator">Object which has to evaluate fitness values for chromosomes in each population.</param>
        /// <param name="populationGenerator">Object which has to generate populations for each generation.</param>
        /// <param name="populationSize">The number of chromosomes of population used in the algorithm.</param>
        /// <param name="initialChromosomeBrain">The string representation of the initial chromosome.</param>
        public GeneticAlgorithmManager(IFitnessEvaluator fitnessEvaluator, IPopulationGenerator populationGenerator = null, int populationSize = 4, string initialChromosomeBrain = null)
        {
            if (fitnessEvaluator == null)
            {
                throw new ArgumentNullException("fitnessEvaluator");
            }

            PopulationSize = populationSize;
            Generation     = 0;

            this.fitnessEvaluator = fitnessEvaluator;
            this.fitnessEvaluator.EvaluationFinished += FitnessEvaluator_EvaluationFinished;

            this.populationGenerator = populationGenerator ?? new SRMPopulationGenerator(25);

            this.initialChromosomeBrain = initialChromosomeBrain;
        }
Example #12
0
        //protected EngineBase(IBenchmark benchmark, IPopulationGenerator populationGenerator, IEvaluator evaluator, ILogger logger, IMutator objectMutator, IMutator stdDeviationsMutator, IMutationRuleSupervisor mutationRuleSupervisor, IParentsSelector parentsParentsSelector, ISurvivorsSelector survivorsSelector, IPointsGenerator positivePointsGenerator, IPointsGenerator negativePointsGenerator, ExperimentParameters experimentParameters, Solution[] basePopulation, Solution[] offspringPopulation)
        //{
        //    Benchmark = benchmark;
        //    PopulationGenerator = populationGenerator;
        //    Evaluator = evaluator;
        //    Logger = logger;
        //    ObjectMutator = objectMutator;
        //    StdDeviationsMutator = stdDeviationsMutator;
        //    MutationRuleSupervisor = mutationRuleSupervisor;
        //    ParentsSelector = parentsParentsSelector;
        //    SurvivorsSelector = survivorsSelector;
        //    PositivePointsGenerator = positivePointsGenerator;
        //    NegativePointsGenerator = negativePointsGenerator;
        //    ExperimentParameters = experimentParameters;
        //    BasePopulation = basePopulation;
        //    OffspringPopulation = offspringPopulation;
        //}

        protected EngineBase(IBenchmark benchmark, IPopulationGenerator populationGenerator, IEvaluator evaluator, ILogger logger, IMutator objectMutator, IMutator stdDeviationsMutator, IMutationRuleSupervisor mutationRuleSupervisor, IParentsSelector parentsParentsSelector, ISurvivorsSelector survivorsSelector, IProcessor <Constraint[]> redundantConstrainsRemover, ExperimentParameters experimentParameters, Statistics statistics, Solution[] basePopulation, Solution[] offspringPopulation)
        {
            Benchmark           = benchmark;
            PopulationGenerator = populationGenerator;
            Evaluator           = evaluator;
            Logger                      = logger;
            ObjectMutator               = objectMutator;
            StdDeviationsMutator        = stdDeviationsMutator;
            MutationRuleSupervisor      = mutationRuleSupervisor;
            ParentsSelector             = parentsParentsSelector;
            SurvivorsSelector           = survivorsSelector;
            RedundantConstriantsRemover = redundantConstrainsRemover;
            ExperimentParameters        = experimentParameters;
            Statistics                  = statistics;
            BasePopulation              = basePopulation;
            OffspringPopulation         = offspringPopulation;
            Stoper                      = new Stopwatch();
        }
Example #13
0
        public Population(int count, IPopulationGenerator <T> generator, IFitnessEvaluator <T> evaluator)
        {
            Contract.Requires <ArgumentOutOfRangeException>(count > 0);
            Contract.Requires <ArgumentNullException>(generator != null);
            Contract.Requires <ArgumentNullException>(evaluator != null);

            TargetSize = count;
            Evaluator  = evaluator;

            // TODO: This really shouldn't be here. We should instead pass the
            // population
            for (var i = 0; i < count; i++)
            {
                var value      = generator.Generate();
                var chromosome = new Chromosome <T>(value);
                chromosome.Fitness = evaluator.ComputeFitness(chromosome.Genes);

                Chromosomes.Add(chromosome);
            }
        }
Example #14
0
        /// <summary>
        /// Test that the genomes in the chromosome are within range, and that they are dispersed.
        /// </summary>
        public static void TestChromosomes <T>(this IPopulationGenerator populationGenerator, int minGenome, int maxGenome)
        {
            var chromosomes    = populationGenerator.GeneratePopulation(20);
            var recivedGenomes = new HashSet <int>();

            foreach (var chromosome in chromosomes)
            {
                var vectorChromosome = (VectorChromosome <T>)chromosome;
                foreach (var genome in vectorChromosome.GetVector())
                {
                    var intGenome = genome.ToInt();
                    Assert.IsTrue(intGenome >= minGenome, $"Got a genome smaller than {minGenome} ({intGenome})");
                    Assert.IsTrue(intGenome <= maxGenome, $"Got a genome bigger than {maxGenome} ({intGenome})");
                    recivedGenomes.Add(intGenome);
                }
            }
            for (int i = minGenome; i < maxGenome; i++)
            {
                Assert.IsTrue(recivedGenomes.Contains(i), $"Didn't get {i}");
            }
        }
Example #15
0
        //public CmEngineWithRecombination(IBenchmark benchmark, IPopulationGenerator populationGenerator, IEvaluator evaluator, ILogger logger, IMutator objectMutator, IMutator stdDeviationsMutator, IMutationRuleSupervisor mutationRuleSupervisor, IParentsSelector parentsParentsSelector, ISurvivorsSelector survivorsSelector, IPointsGenerator positivePointsGenerator, IPointsGenerator negativePointsGenerator, ExperimentParameters experimentParameters, Solution[] basePopulation, Solution[] offspringPopulation, IRecombiner objectRecombiner, IRecombiner stdDeviationsRecombiner, IMutator rotationsMutator, IRecombiner rotationsRecombiner) : base(benchmark, populationGenerator, evaluator, logger, objectMutator, stdDeviationsMutator, mutationRuleSupervisor, parentsParentsSelector, survivorsSelector, positivePointsGenerator, negativePointsGenerator, experimentParameters, basePopulation, offspringPopulation, objectRecombiner, stdDeviationsRecombiner)
        //{
        //    RotationsMutator = rotationsMutator;
        //    RotationsRecombiner = rotationsRecombiner;
        //}

        public CmEngineWithRecombination(IBenchmark benchmark, IPopulationGenerator populationGenerator, IEvaluator evaluator, ILogger logger, IMutator objectMutator, IMutator stdDeviationsMutator, IMutationRuleSupervisor mutationRuleSupervisor, IParentsSelector parentsParentsSelector, ISurvivorsSelector survivorsSelector, IProcessor <Constraint[]> redundantConstrainsRemover, ExperimentParameters experimentParameters, Statistics statistics, Solution[] basePopulation, Solution[] offspringPopulation, IRecombiner objectRecombiner, IRecombiner stdDeviationsRecombiner, IMutator rotationsMutator, IRecombiner rotationsRecombiner) : base(benchmark, populationGenerator, evaluator, logger, objectMutator, stdDeviationsMutator, mutationRuleSupervisor, parentsParentsSelector, survivorsSelector, redundantConstrainsRemover, experimentParameters, statistics, basePopulation, offspringPopulation, objectRecombiner, stdDeviationsRecombiner)
        {
            RotationsMutator    = rotationsMutator;
            RotationsRecombiner = rotationsRecombiner;
        }
Example #16
0
 public ProblemManager(IPopulationGenerator populationGenerator, bool logPopulation = false)
 {
     PopulationGenerator = populationGenerator;
     LogPopulation = logPopulation;
     MaxIterations = 100;
 }
Example #17
0
 public Core(IObjectiveScoreCalculator scoreCalculator, IPopulationGenerator populationGenerator, ITerminationCondition terminationCondition)
 {
     this.scoreCalculator      = scoreCalculator;
     this.populationGenerator  = populationGenerator;
     this.terminationCondition = terminationCondition;
 }
        public static IEngine GetEngine(ExperimentParameters experimentParameters)
        {
            IEngine engine;

            //BasePopulation
            Solution[]           basePopulation      = new Solution[experimentParameters.BasePopulationSize];
            Solution[]           offspringPopulation = new Solution[experimentParameters.OffspringPopulationSize];
            IBenchmark           benchmark           = BenchmarkFactory.GetBenchmark(experimentParameters);
            IPopulationGenerator populationGenerator = PopulationGeneratorsFactory.GetPopulationGenerator(experimentParameters);

            //Points generators
            //var domain = new Domain2(experimentParameters);
            //IPointsGenerator positivePointsGenerator = new PositivePointsGenerator();
            //var positivePoints =
            //    positivePointsGenerator.GeneratePoints(experimentParameters.NumberOfPositivePoints, benchmark);
            //IPointsGenerator negativePointsGenerator = new NegativePointsGenerator(positivePoints, new CanberraDistanceCalculator());

            ////Evaluator
            //var negativePoints =
            //    negativePointsGenerator.GeneratePoints(experimentParameters.NumberOfNegativePoints, benchmark);
            //IEvaluator evaluator = new Evaluator(experimentParameters, positivePoints, negativePoints);
            IEvaluator evaluator = new Evaluator(experimentParameters);

            //Redundant constraints remover
            var redundantConstraintsRemover = new RedundantConstraintsRemover(new DomainSpaceSampler(), benchmark, experimentParameters);

            //Statistics
            var statistics = new Statistics();

            //Logger
            ILogger logger = null;

            //Selectors
            var parentsSelector   = SelectorsFactory.GetParentsSelector(experimentParameters);
            var survivorsSelector = SelectorsFactory.GetSurvivorsSelector(experimentParameters);

            //Mutation
            var objectMutator          = MutatorsFactory.GetObjectMutator(experimentParameters);
            var stdDeviationsMutator   = MutatorsFactory.GetStdDevsMutator(experimentParameters);
            var mutationRuleSupervisor = MutationSupervisorsFactory.GetMutationRuleSupervisor(experimentParameters);

            if (experimentParameters.TypeOfMutation == ExperimentParameters.MutationType.Correlated)
            {
                if (experimentParameters.UseRecombination)
                {
                    var objectRecombiner    = RecombinersFactory.GetObjectRecombiner(experimentParameters);
                    var stdDevsRecombiner   = RecombinersFactory.GetStdDevsRecombiner(experimentParameters);
                    var rotationsRecombiner = RecombinersFactory.GetRotationsRecombiner(experimentParameters);
                    var rotationsMutator    = MutatorsFactory.GetRotationsMutator(experimentParameters);

                    //engine = new CmEngineWithRecombination(benchmark, populationGenerator, evaluator, logger, objectMutator, stdDeviationsMutator, mutationRuleSupervisor, parentsSelector, survivorsSelector, positivePointsGenerator, negativePointsGenerator, experimentParameters, basePopulation, offspringPopulation, objectRecombiner, stdDevsRecombiner, rotationsMutator, rotationsRecombiner);
                    engine = new CmEngineWithRecombination(benchmark, populationGenerator, evaluator, logger, objectMutator, stdDeviationsMutator, mutationRuleSupervisor, parentsSelector, survivorsSelector, redundantConstraintsRemover, experimentParameters, statistics, basePopulation, offspringPopulation, objectRecombiner, stdDevsRecombiner, rotationsMutator, rotationsRecombiner);
                }
                else
                {
                    var rotationsMutator = MutatorsFactory.GetRotationsMutator(experimentParameters);

                    //engine = new CmEngineWithoutRecombination(benchmark, populationGenerator, evaluator, logger, objectMutator, stdDeviationsMutator, mutationRuleSupervisor, parentsSelector, survivorsSelector, positivePointsGenerator, negativePointsGenerator, experimentParameters, basePopulation, offspringPopulation, rotationsMutator);
                    engine = new CmEngineWithoutRecombination(benchmark, populationGenerator, evaluator, logger, objectMutator, stdDeviationsMutator, mutationRuleSupervisor, parentsSelector, survivorsSelector, redundantConstraintsRemover, experimentParameters, statistics, basePopulation, offspringPopulation, rotationsMutator);
                }
            }
            else
            {
                if (experimentParameters.UseRecombination)
                {
                    var objectRecombiner  = RecombinersFactory.GetObjectRecombiner(experimentParameters);
                    var stdDevsRecombiner = RecombinersFactory.GetStdDevsRecombiner(experimentParameters);

                    //engine = new UmEngineWithRecombination(benchmark, populationGenerator, evaluator, logger, objectMutator, stdDeviationsMutator, mutationRuleSupervisor, parentsSelector, survivorsSelector, positivePointsGenerator, negativePointsGenerator, experimentParameters, basePopulation, offspringPopulation, objectRecombiner, stdDevsRecombiner);
                    engine = new UmEngineWithRecombination(benchmark, populationGenerator, evaluator, logger, objectMutator, stdDeviationsMutator, mutationRuleSupervisor, parentsSelector, survivorsSelector, redundantConstraintsRemover, experimentParameters, statistics, basePopulation, offspringPopulation, objectRecombiner, stdDevsRecombiner);
                }
                else
                {
                    //engine = new UmEngineWithoutRecombination(benchmark, populationGenerator, evaluator, logger, objectMutator, stdDeviationsMutator, mutationRuleSupervisor, parentsSelector, survivorsSelector, positivePointsGenerator, negativePointsGenerator, experimentParameters, basePopulation, offspringPopulation);
                    engine = new UmEngineWithoutRecombination(benchmark, populationGenerator, evaluator, logger, objectMutator, stdDeviationsMutator, mutationRuleSupervisor, parentsSelector, survivorsSelector, redundantConstraintsRemover, experimentParameters, statistics, basePopulation, offspringPopulation);
                }
            }

            return(engine);
        }