Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
0
 public TestGeneticAlgorithm(
     double mutationProbability,
     double crossoverPropability,
     int minPopulationSize,
     int maxPopulationSize,
     IMutation mutationOperator,
     ICrossover crossoverOperator,
     IFitnessEvaluator <TestWorkload> fitnessEvaluator,
     IInitialPopulationCreator initialPopulationCreator,
     ITerminationCondition <TestState> terminationCondition,
     ISelectionStrategy selectionStrategy,
     IReinsertionStrategy reinsertionStrategy,
     IDeploymentChromosomeFactory chromosomeFactory,
     TestState currentState,
     TestWorkload workload,
     IRandomProvider randomProvider)
     : base(
         mutationProbability,
         crossoverPropability,
         minPopulationSize,
         maxPopulationSize,
         mutationOperator,
         crossoverOperator,
         fitnessEvaluator,
         initialPopulationCreator,
         terminationCondition,
         selectionStrategy,
         reinsertionStrategy,
         chromosomeFactory,
         currentState,
         workload,
         randomProvider)
 {
 }
Ejemplo n.º 3
0
 public GeneticAlgorithm(
     double mutationProbability,
     double crossoverPropability,
     int minPopulationSize,
     int maxPopulationSize,
     IMutation mutationOperator,
     ICrossover crossoverOperator,
     IFitnessEvaluator <TWorkload> fitnessEvaluator,
     IInitialPopulationCreator initialPopulationCreator,
     ITerminationCondition <TState> terminationCondition,
     ISelectionStrategy selectionStrategy,
     IReinsertionStrategy reinsertionStrategy,
     IDeploymentChromosomeFactory chromosomeFactory,
     TState currentState,
     TWorkload workload,
     IRandomProvider randomProvider)
 {
     MutationProbability       = mutationProbability;
     CrossoverProbability      = crossoverPropability;
     MinPopulationSize         = minPopulationSize;
     MaxPopulationSize         = maxPopulationSize;
     MutationOperator          = mutationOperator;
     CrossoverOperator         = crossoverOperator;
     FitnessEvaluator          = fitnessEvaluator;
     InitialPopulationCreator  = initialPopulationCreator;
     TerminationCondition      = terminationCondition;
     SelectionStrategyStrategy = selectionStrategy;
     ReinsertionStrategy       = reinsertionStrategy;
     ChromosomeFactory         = chromosomeFactory;
     CurrentState   = currentState;
     Workload       = workload;
     RandomProvider = randomProvider;
 }
Ejemplo n.º 4
0
        public GeneticAlgorithmBuilder <U> SetFitnessEvaluator(IFitnessEvaluator <U> fitnessEvaluator)
        {
            Contract.Requires <ArgumentNullException>(fitnessEvaluator != null, "Argument cannot be null.");

            FitnessEvaluator = fitnessEvaluator;

            return(this);
        }
Ejemplo n.º 5
0
        public Selector(IFitnessEvaluator <string> fitnessEvaluator, int count, int total)
        {
            Contract.Requires <ArgumentNullException>(fitnessEvaluator != null);
            Contract.Requires <ArgumentOutOfRangeException>(count > 0);
            Contract.Requires <ArgumentOutOfRangeException>(total > 0);

            FitnessEvaluator = fitnessEvaluator;
            Count            = count;
            Total            = total;
        }
        public UniqueChromosomeSelector(IFitnessEvaluator <string> fitnessEvaluator, int count, int total)
        {
            if (fitnessEvaluator == null)
            {
                throw new System.ArgumentNullException(nameof(fitnessEvaluator));
            }

            FitnessEvaluator = fitnessEvaluator;
            Count            = count;
            Total            = total;
        }
Ejemplo n.º 7
0
        public StringSinglePointCrossOver(Random random, IFitnessEvaluator <string> evaluator)
        {
            if (random == null)
            {
                throw new ArgumentNullException($"{nameof(random)}");
            }

            if (evaluator == null)
            {
                throw new ArgumentNullException($"{nameof(evaluator)}");
            }

            Random    = random;
            Evaluator = evaluator;
        }
Ejemplo n.º 8
0
        public Mutator(Func <State <string>, int> mutatorCount, IFitnessEvaluator <string> evaluator)
        {
            if (mutatorCount == null)
            {
                throw new ArgumentNullException(nameof(mutatorCount));
            }

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

            MutatorCount = mutatorCount;
            Evaluator    = evaluator;
        }
Ejemplo n.º 9
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;
        }
Ejemplo n.º 10
0
 public void SetUp()
 {
     _mutationProbability         = 1;
     _crossoverProbability        = 1;
     _minPopulationSize           = 2;
     _maxPopulationSize           = 4;
     _mutationOperator            = A.Fake <IMutation>();
     _crossoverOperator           = A.Fake <ICrossover>();
     _fitnessEvaluator            = A.Fake <IFitnessEvaluator <TestWorkload> >();
     _initialPopulationCreator    = A.Fake <IInitialPopulationCreator>();
     _terminationCondition        = A.Fake <ITerminationCondition <TestState> >();
     _selectionStrategy           = A.Fake <ISelectionStrategy>();
     _reinsertionStrategy         = A.Fake <IReinsertionStrategy>();
     _deploymentChromosomeFactory = A.Fake <IDeploymentChromosomeFactory>();
     _currentState   = new TestState();
     _workload       = new TestWorkload();
     _randomProvider = new DefaultRandomProvider();
 }
Ejemplo n.º 11
0
 public GeneticAlgorithm(
     IPopulationFactory <TGeneSequence> populationFactory,
     ITermination <TGeneSequence> termination,
     IFitnessEvaluator <TGeneSequence> fitnessEvaluator,
     ISelection <TGeneSequence> preservationSelection,
     ISelection <TGeneSequence> parentSelection,
     ICrossover <TGeneSequence> crossover,
     IMutation <TGeneSequence> mutation,
     int initialPopulation)
 {
     _populationFactory     = populationFactory;
     _termination           = termination;
     _fitnessEvaluator      = fitnessEvaluator;
     _preservationSelection = preservationSelection;
     _parentSelection       = parentSelection;
     _crossover             = crossover;
     _mutation          = mutation;
     _initialPopulation = initialPopulation;
 }
Ejemplo n.º 12
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);
            }
        }
Ejemplo n.º 13
0
 public SinglePointCrossOver(Random random, IFitnessEvaluator <string> evaluator)
 {
     Random    = random;
     Evaluator = evaluator;
 }
Ejemplo n.º 14
0
 public SinglePointCrossOver(Random random, IFitnessEvaluator <string> evaluator, Func <State <string>, int> crossOverCount)
 {
     Random         = random;
     Evaluator      = evaluator;
     CrossOverCount = crossOverCount;
 }
Ejemplo n.º 15
0
 public Optimiser(IFitnessEvaluator fitnessEvaluator)
 {
     _fitnessEvaluator = fitnessEvaluator;
 }
Ejemplo n.º 16
0
 public Optimiser(IFitnessEvaluator fitnessEvaluator)
 {
     _fitnessEvaluator = fitnessEvaluator;
 }