/// <summary>
        /// Constructor for genetic algorithm.
        /// Genetic algorithms imitate natural biological processes,
        /// </summary>
        /// <param name="population">Init population. </param>
        /// <param name="fitness">Fitness.</param>
        /// <param name="selection">Selection operator.</param>
        /// <param name="xover">Xover operator.</param>
        /// <param name="mutation">Mutation operator.</param>
        /// <param name="elitizmus">Elitizmus.</param>
        /// <param name="termination">Termination GA.</param>
        /// <param name="executor">Executor.</param>
        /// <param name="mutationProbability">Mutation probability.</param>
        /// <param name="xoverProbability">Xover probability.</param>
        public GeneticAlgorithm(IPopulation population,
                                IFitness fitness,
                                ISelection selection,
                                IXover xover,
                                IMutation mutation,
                                IElite elitizmus,
                                ITermination termination,
                                IExecutor executor,
                                float mutationProbability,
                                float xoverProbability)
        {
            Population       = population;
            this.fitness     = fitness;
            this.selection   = selection;
            this.xover       = xover;
            this.mutation    = mutation;
            this.elitizmus   = elitizmus;
            this.executor    = executor;
            this.termination = termination;

            // base probability
            this.xoverProbability    = xoverProbability;
            this.mutationProbability = mutationProbability;
            TimeEvolving             = TimeSpan.Zero;
            CurrentGenerationsNumber = 1;
        }
예제 #2
0
        public static ParameterSelection CreateSelection()
        {
            Console.Clear();

            string[]     names       = GetNames();
            string       expression  = GetExpression();
            var          bounds      = GetBounds();
            float        upperBound  = bounds.upper;
            float        lowerBound  = bounds.lower;
            bool         adaptiveOn  = IsAdaptive();
            ISelection   selection   = GetSelection();
            ICrossover   crossover   = GetCrossover();
            IMutation    mutation    = GetMutation();
            ITermination termination = GetTermination();

            Console.Clear();

            return(new ParameterSelection
            {
                Variables = names,
                Expression = expression,
                LowerBound = lowerBound,
                UpperBound = upperBound,
                AdaptiveOn = adaptiveOn,
                Selection = selection,
                Crossover = crossover,
                Mutation = mutation,
                Termination = termination
            });
        }
예제 #3
0
        /// <summary>Initializes a new instance of the <see cref="GeneticAlgorithm"/> class.</summary>
        /// <param name="population">The chromosomes population.</param>
        /// <param name="selection">The selection operator.</param>
        /// <param name="crossover">The crossover operator.</param>
        /// <param name="mutation">The mutation operator.</param>
        /// <param name="reinsertion">The reinsertion operator.</param>
        /// <param name="termination">The termination operator.</param>

        public DynamoGeneticAlgorithm(
            IPopulation population,
            ISelection selection,
            ICrossover crossover,
            IMutation mutation,
            IReinsertion reinsertion,
            ITermination termination)
        {
            ExceptionHelper.ThrowIfNull("population", population);
            ExceptionHelper.ThrowIfNull("selection", selection);
            ExceptionHelper.ThrowIfNull("crossover", crossover);
            ExceptionHelper.ThrowIfNull("mutation", mutation);
            ExceptionHelper.ThrowIfNull("reinsertion", reinsertion);
            ExceptionHelper.ThrowIfNull("termination", termination);

            Population  = population;
            Selection   = selection;
            Crossover   = crossover;
            Mutation    = mutation;
            Reinsertion = reinsertion;
            Termination = termination;

            SelectionSize        = 0;
            CrossoverProbability = (float)DefaultCrossoverProbability;
            MutationProbability  = (float)DefaultMutationProbability;

            TimeEvolving      = TimeSpan.Zero;
            OperatorsStrategy = new DefaultOperatorsStrategy();

            State = GeneticAlgorithmState.NotStarted;
        }
예제 #4
0
    private void PrepareComboBoxes()
    {
        PrepareEditComboBox(
            cmbSelection,
            btnEditSelection,
            SelectionService.GetSelectionNames,
            SelectionService.GetSelectionTypeByName,
            SelectionService.CreateSelectionByName,
            () => m_selection,
            (i) => m_selection = i);

        PrepareEditComboBox(
            cmbCrossover,
            btnEditCrossover,
            CrossoverService.GetCrossoverNames,
            CrossoverService.GetCrossoverTypeByName,
            CrossoverService.CreateCrossoverByName,
            () => m_crossover,
            (i) => m_crossover = i);

        PrepareEditComboBox(
            cmbMutation,
            btnEditMutation,
            MutationService.GetMutationNames,
            MutationService.GetMutationTypeByName,
            MutationService.CreateMutationByName,
            () => m_mutation,
            (i) => m_mutation = i);

        PrepareEditComboBox(
            cmbTermination,
            btnEditTermination,
            () => {
            return(TerminationService.GetTerminationNames()
                   .Where(t => !t.Equals("And", StringComparison.OrdinalIgnoreCase) && !t.Equals("Or", StringComparison.OrdinalIgnoreCase)).ToList());
        },
            TerminationService.GetTerminationTypeByName,
            TerminationService.CreateTerminationByName,
            () => m_termination,
            (i) => m_termination = i);

        PrepareEditComboBox(
            cmbTermination1,
            btnEditReinsertion,
            ReinsertionService.GetReinsertionNames,
            ReinsertionService.GetReinsertionTypeByName,
            ReinsertionService.CreateReinsertionByName,
            () => m_reinsertion,
            (i) => m_reinsertion = i);

        PrepareEditComboBox(
            cmbGenerationStrategy,
            btnEditGenerationStrategy,
            PopulationService.GetGenerationStrategyNames,
            PopulationService.GetGenerationStrategyTypeByName,
            PopulationService.CreateGenerationStrategyByName,
            () => m_generationStrategy,
            (i) => m_generationStrategy = i);
    }
예제 #5
0
    private void PrepareComboBoxes()
    {
        PrepareEditComboBox(
            cmbSelection,
            btnEditSelection,
            SelectionService.GetSelectionNames,
            SelectionService.GetSelectionTypeByName,
            SelectionService.CreateSelectionByName,
            () => m_selection,
            (i) => m_selection = i);

        PrepareEditComboBox(
            cmbCrossover,
            btnEditCrossover,
            CrossoverService.GetCrossoverNames,
            CrossoverService.GetCrossoverTypeByName,
            CrossoverService.CreateCrossoverByName,
            () => m_crossover,
            (i) => m_crossover = i);

        PrepareEditComboBox(
            cmbMutation,
            btnEditMutation,
            MutationService.GetMutationNames,
            MutationService.GetMutationTypeByName,
            MutationService.CreateMutationByName,
            () => m_mutation,
            (i) => m_mutation = i);

        PrepareEditComboBox(
            cmbTermination,
            btnEditTermination,
            () =>
        {
            return(TerminationService.GetTerminationNames());
        },
            TerminationService.GetTerminationTypeByName,
            TerminationService.CreateTerminationByName,
            () => m_termination,
            (i) => m_termination = i);

        PrepareEditComboBox(
            cmbReinsertion,
            btnEditReinsertion,
            ReinsertionService.GetReinsertionNames,
            ReinsertionService.GetReinsertionTypeByName,
            ReinsertionService.CreateReinsertionByName,
            () => m_reinsertion,
            (i) => m_reinsertion = i);

        PrepareEditComboBox(
            cmbGenerationStrategy,
            btnEditGenerationStrategy,
            PopulationService.GetGenerationStrategyNames,
            PopulationService.GetGenerationStrategyTypeByName,
            PopulationService.CreateGenerationStrategyByName,
            () => m_generationStrategy,
            (i) => m_generationStrategy = i);
    }
예제 #6
0
        /// <summary>
        /// Creates the termination.
        /// </summary>
        /// <param name="name">Termination name</param>
        /// <param name="param"></param>
        /// <returns>The termination</returns>
        public ITermination CreateTermination(string name, object param)
        {
            ITermination termination = Activator.CreateInstance(terminations[name]) as ITermination;

            termination.InitializeTerminationCondition(param);

            return(termination);
        }
예제 #7
0
        /// <inheritdoc/>
        public IService Configure(ITermination termination)
        {
            if (termination is null)
            {
                throw new ArgumentNullException(nameof(termination));
            }

            return(new Service(originationBuilder, terminationBuilder.Configure(termination), executionBuilder, spanBuilder));
        }
            public ITerminationBuilder Configure(ITermination termination)
            {
                if (first == termination)
                {
                    return(this);
                }

                return(new Listed(new[] { first, termination }));
            }
            public ITerminationBuilder Configure(ITermination termination)
            {
                if (terminations.Contains(termination))
                {
                    return(this);
                }

                return(new Listed(terminations.Append(termination)));
            }
        /// <summary>
        /// Creates <see cref="ISpan"/> from <see cref="ITermination"/> and <see cref="Action"/>.
        /// </summary>
        /// <param name="termination">
        /// <see cref="ITermination"/> to terminate in <see cref="ISpan"/>.
        /// </param>
        /// <param name="origination">
        /// <see cref="Action"/> to originate in <see cref="ISpan"/>.
        /// </param>
        /// <returns>
        /// <see cref="ISpan"/> created.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// Thrown if <paramref name="termination"/> is null.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// Thrown if <paramref name="origination"/> is null.
        /// </exception>
        public static ISpan From(this ITermination termination, Action origination)
        {
            if (termination is null)
            {
                throw new ArgumentNullException(nameof(termination));
            }
            if (origination is null)
            {
                throw new ArgumentNullException(nameof(origination));
            }

            return(termination.From(new Origination(origination)));
        }
예제 #11
0
        /// <summary>
        /// Creates <see cref="ISpan"/> from <see cref="IOrigination"/> and <see cref="ITermination"/>.
        /// </summary>
        /// <param name="origination">
        /// <see cref="IOrigination"/> to originate in <see cref="ISpan"/>.
        /// </param>
        /// <param name="termination">
        /// <see cref="ITermination"/> to terminate in <see cref="ISpan"/>.
        /// </param>
        /// <returns>
        /// <see cref="ISpan"/> created.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// Thrown if <paramref name="origination"/> is null.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// Thrown if <paramref name="termination"/> is null.
        /// </exception>
        public static ISpan To(this IOrigination origination, ITermination termination)
        {
            if (origination is null)
            {
                throw new ArgumentNullException(nameof(origination));
            }
            if (termination is null)
            {
                throw new ArgumentNullException(nameof(termination));
            }

            return(new Span(origination, termination));
        }
        public void CreateTerminationByName_ValidName_TerminationCreated()
        {
            ITermination actual = TerminationService.CreateTerminationByName("Fitness Stagnation") as FitnessStagnationTermination;

            Assert.IsNotNull(actual);

            actual = TerminationService.CreateTerminationByName("Fitness Threshold") as FitnessThresholdTermination;
            Assert.IsNotNull(actual);

            actual = TerminationService.CreateTerminationByName("Generation Number") as GenerationNumberTermination;
            Assert.IsNotNull(actual);

            actual = TerminationService.CreateTerminationByName("Time Evolving") as TimeEvolvingTermination;
            Assert.IsNotNull(actual);
        }
        /// <summary>
        /// Binds <see cref="ICycle"/> to <see cref="IOrigination"/> and <see cref="ITermination"/>.
        /// </summary>
        /// <param name="cycle">
        /// <see cref="ICycle"/> to be bound to <paramref name="origination"/> and <paramref name="termination"/>.
        /// </param>
        /// <param name="origination">
        /// <see cref="IOrigination"/> to bind to <see cref="ICycle"/>.
        /// </param>
        /// <param name="termination">
        /// <see cref="ITermination"/> to bind to <see cref="ICycle"/>.
        /// </param>
        /// <returns>
        /// <see cref="ICycle"/> bound.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// Thrown if <paramref name="cycle"/> is null.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// Thrown if <paramref name="origination"/> is null.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// Thrown if <paramref name="termination"/> is null.
        /// </exception>
        public static ICycle Between(this ICycle cycle, IOrigination origination, ITermination termination)
        {
            if (cycle is null)
            {
                throw new ArgumentNullException(nameof(cycle));
            }
            if (origination is null)
            {
                throw new ArgumentNullException(nameof(origination));
            }
            if (termination is null)
            {
                throw new ArgumentNullException(nameof(termination));
            }

            return(cycle.In(origination.To(termination)));
        }
예제 #14
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;
 }
예제 #15
0
        public async Task Evolve(ITermination <TIndividual, TGeneStructure, TGene> termination,
                                 CancellationToken cancellationToken)
        {
            termination.Algorithm = this;
            if (!IsInitialized)
            {
                Initialize();
            }

            while (!termination.ShouldTerminate())
            {
                await EvolveOneGeneration();

                Update();
                OnGenerationProgress(this);
                if (cancellationToken.IsCancellationRequested)
                {
                    break;
                }
            }

            Statistics.Finish();
            OnTermination(this);
        }
예제 #16
0
        public CPSetup(IPopulation population, ISelection selection, ICrossover crossover, IMutation mutation, ITermination termination,
                       string filename = "")
        {
            if (String.IsNullOrEmpty(filename))
            {
                Console.WriteLine("Podaj nazwę pliku z grafem: ");
                var builder = new StringBuilder();
                builder.Append("Data/");
                builder.Append(Console.ReadLine());
                filename = builder.ToString();
            }
            Graph = new Graph(@filename);
            int[] startValues = Graph.OddVertices.ToArray();
            Fitness     = new CPFitness();
            Chromosome  = new CPChromosome(startValues.Length, startValues, population.MaxSize);
            Population  = population;
            Selection   = selection;
            Crossover   = crossover;
            Mutation    = mutation;
            Termination = termination;

            GA             = new GeneticAlgorithm(Population, Fitness, Selection, Crossover, Mutation);
            GA.Termination = Termination;
        }
예제 #17
0
        internal Span(IOrigination origination, ITermination termination)
        {
            Origination = origination;

            Termination = termination;
        }
 public void AddTermination(ITermination termination)
 {
     Terminations.Add(termination);
 }
예제 #19
0
파일: Basic.cs 프로젝트: majasinski/Evo
        public static DynamoGeneticAlgorithm CreateGeneticAlgorithm(Population population,
                                                                    [DefaultArgument("Evo.DynamoGeneticAlgorithm.Default()")] object selection,
                                                                    [DefaultArgument("Evo.DynamoGeneticAlgorithm.Default()")] object crossover,
                                                                    [DefaultArgument("Evo.DynamoGeneticAlgorithm.Default()")] object mutation,
                                                                    [DefaultArgument("Evo.DynamoGeneticAlgorithm.Default()")] object termination,
                                                                    [DefaultArgument("Evo.DynamoGeneticAlgorithm.Default()")] int?selectionSize,
                                                                    double crossoverProbability = DynamoGeneticAlgorithm.DefaultCrossoverProbability,
                                                                    double mutationProbability  = DynamoGeneticAlgorithm.DefaultMutationProbability)
        {
            IChromosome    exemplaryChromome = population.CurrentGeneration.Chromosomes[0];
            ChromosomeType chromosomeType    = 0;

            if (exemplaryChromome is BinaryChromosome)
            {
                chromosomeType = ((BinaryChromosome)exemplaryChromome).ChromosomeType;
            }
            else if (exemplaryChromome is CombinatorialChromosome)
            {
                chromosomeType = ((CombinatorialChromosome)exemplaryChromome).ChromosomeType;
            }
            else if (exemplaryChromome is DoubleChromosome)
            {
                chromosomeType = ((DoubleChromosome)exemplaryChromome).ChromosomeType;
            }

            ISelection selectionMethod = null;

            if (selection == null)
            {
                selectionMethod = new EliteSelection();
            }
            else
            {
                if (selection is EliteSelection)
                {
                    selectionMethod = selection as EliteSelection;
                }
                else if (selection is RouletteWheelSelection)
                {
                    selectionMethod = selection as RouletteWheelSelection;
                }
                else if (selection is StochasticUniversalSamplingSelection)
                {
                    selectionMethod = selection as StochasticUniversalSamplingSelection;
                }
                else if (selection is TournamentSelection)
                {
                    selectionMethod = selection as TournamentSelection;
                }
                else
                {
                    throw new CrossoverException("Invalid selection input. A valid object returned by a node of the Selections category should be used.");
                }
            }

            ICrossover crossoverMethod = null;

            if (crossover == null)
            {
                crossoverMethod = new UniformCrossover(0.5f);
            }
            else
            {
                if (crossover is AlternatingPositionCrossover)
                {
                    if (chromosomeType != ChromosomeType.CombinatorialChromosome)
                    {
                        throw new CrossoverException("The Alternating-position Crossover (AP) can be only used with combinatorial chromosomes. The specified individuals are not combinatorial chromosomes.");
                    }
                    crossoverMethod = crossover as AlternatingPositionCrossover;
                }
                else if (crossover is CutAndSpliceCrossover)
                {
                    if (chromosomeType == ChromosomeType.DoubleChromosome)
                    {
                        throw new CrossoverException("The Cut and Splice Crossover (AP) can be only used with binary and combinatorial chromosomes. The specified individuals are double chromosomes.");
                    }
                    crossoverMethod = crossover as CutAndSpliceCrossover;
                }
                else if (crossover is CycleCrossover)
                {
                    if (chromosomeType != ChromosomeType.CombinatorialChromosome)
                    {
                        throw new CrossoverException("The Cycle Crossover (CX) can be only used with combinatorial chromosomes. The specified individuals are not combinatorial chromosomes.");
                    }
                    crossoverMethod = crossover as CycleCrossover;
                }
                else if (crossover is OnePointCrossover)
                {
                    if (chromosomeType == ChromosomeType.DoubleChromosome)
                    {
                        throw new CrossoverException("The One-point Crossover can be only used with binary and combinatorial chromosomes. The specified individuals are double chromosomes.");
                    }
                    crossoverMethod = crossover as OnePointCrossover;
                }
                else if (crossover is OrderBasedCrossover)
                {
                    if (chromosomeType != ChromosomeType.CombinatorialChromosome)
                    {
                        throw new CrossoverException("The Order Based Crossover (OX2) can be only used with combinatorial chromosomes. The specified individuals are not combinatorial chromosomes.");
                    }
                    crossoverMethod = crossover as OrderBasedCrossover;
                }
                else if (crossover is OrderedCrossover)
                {
                    if (chromosomeType != ChromosomeType.CombinatorialChromosome)
                    {
                        throw new CrossoverException("The Ordered Crossover (OX1) can be only used with combinatorial chromosomes. The specified individuals are not combinatorial chromosomes.");
                    }
                    crossoverMethod = crossover as OrderedCrossover;
                }
                else if (crossover is PartiallyMappedCrossover)
                {
                    if (chromosomeType != ChromosomeType.CombinatorialChromosome)
                    {
                        throw new CrossoverException("The Partially-mapped Crossover (PMX) can be only used with combinatorial chromosomes. The specified individuals are not combinatorial chromosomes.");
                    }
                    crossoverMethod = crossover as PartiallyMappedCrossover;
                }
                else if (crossover is PositionBasedCrossover)
                {
                    if (chromosomeType != ChromosomeType.CombinatorialChromosome)
                    {
                        throw new CrossoverException("The Position Based Crossover (POS) can be only used with combinatorial chromosomes. The specified individuals are not combinatorial chromosomes.");
                    }
                    crossoverMethod = crossover as PositionBasedCrossover;
                }
                else if (crossover is SimulatedBinaryCrossover)
                {
                    if (chromosomeType != ChromosomeType.DoubleChromosome)
                    {
                        throw new CrossoverException("The Simulated Binary Crossover (SBX) can be only used with double chromosomes. The specified individuals are not double chromosomes.");
                    }
                    crossoverMethod = crossover as SimulatedBinaryCrossover;
                }
                else if (crossover is ThreeParentCrossover)
                {
                    if (chromosomeType == ChromosomeType.DoubleChromosome)
                    {
                        throw new CrossoverException("The Three-parent Crossover can be only used with binary and combinatorial chromosomes. The specified individuals are double chromosomes.");
                    }
                    crossoverMethod = crossover as ThreeParentCrossover;
                }
                else if (crossover is TwoPointCrossover)
                {
                    if (chromosomeType == ChromosomeType.DoubleChromosome)
                    {
                        throw new CrossoverException("The Two-point Crossover can be only used with binary and combinatorial chromosomes. The specified individuals are double chromosomes.");
                    }
                    crossoverMethod = crossover as TwoPointCrossover;
                }
                else if (crossover is UniformCrossover)
                {
                    if (chromosomeType == ChromosomeType.DoubleChromosome)
                    {
                        throw new CrossoverException("The Uniform Crossover can be only used with binary and combinatorial chromosomes. The specified individuals are double chromosomes.");
                    }
                    crossoverMethod = crossover as UniformCrossover;
                }
                else if (crossover is VotingRecombinationCrossover)
                {
                    if (chromosomeType == ChromosomeType.DoubleChromosome)
                    {
                        throw new CrossoverException("The Voting Recombination Crossover (VR) can be only used with binary and combinatorial chromosomes. The specified individuals are double chromosomes.");
                    }
                    crossoverMethod = crossover as VotingRecombinationCrossover;
                }
                else
                {
                    throw new CrossoverException("Invalid crossover input. A valid object returned by a node of the Crossovers category should be used.");
                }
            }

            IMutation mutationMethod = null;

            if (mutation == null)
            {
                mutationMethod = new FlipBitMutation();
            }
            else
            {
                if (mutation is DisplacementMutation)
                {
                    if (chromosomeType == ChromosomeType.DoubleChromosome)
                    {
                        throw new CrossoverException("The Displacement Mutation can be only used on binary and combinatorial chromosomes. The specified individuals are double chromosomes.");
                    }
                    mutationMethod = mutation as DisplacementMutation;
                }
                else if (mutation is FlipBitMutation)
                {
                    if (chromosomeType != ChromosomeType.BinaryChromosome)
                    {
                        throw new CrossoverException("The Flip Bit Mutation can be only used on binary chromosomes. The specified individuals are not binary chromosomes.");
                    }
                    mutationMethod = mutation as FlipBitMutation;
                }
                else if (mutation is GaussianMutation)
                {
                    if (chromosomeType != ChromosomeType.DoubleChromosome)
                    {
                        throw new CrossoverException("The Gaussian Mutation can be only used on double chromosomes. The specified individuals are not double chromosomes.");
                    }
                    mutationMethod = mutation as GaussianMutation;
                }
                else if (mutation is InsertionMutation)
                {
                    if (chromosomeType == ChromosomeType.DoubleChromosome)
                    {
                        throw new CrossoverException("The Displacement Mutation can be only used on binary and combinatorial chromosomes. The specified individuals are double chromosomes.");
                    }
                    mutationMethod = mutation as InsertionMutation;
                }
                else if (mutation is PartialShuffleMutation)
                {
                    if (chromosomeType == ChromosomeType.DoubleChromosome)
                    {
                        throw new CrossoverException("The Displacement Mutation (PSM) can be only used on binary and combinatorial chromosomes. The specified individuals are double chromosomes.");
                    }
                    mutationMethod = mutation as PartialShuffleMutation;
                }
                else if (mutation is PolynomialMutation)
                {
                    if (chromosomeType != ChromosomeType.DoubleChromosome)
                    {
                        throw new CrossoverException("The Polynomial Mutation can be only used on double chromosomes. The specified individuals are not double chromosomes.");
                    }
                    mutationMethod = mutation as PolynomialMutation;
                }
                else if (mutation is ReverseSequenceMutation)
                {
                    if (chromosomeType == ChromosomeType.DoubleChromosome)
                    {
                        throw new CrossoverException("The Displacement Mutation (RSM) can be only used on binary and combinatorial chromosomes. The specified individuals are double chromosomes.");
                    }
                    mutationMethod = mutation as ReverseSequenceMutation;
                }
                else if (mutation is TworsMutation)
                {
                    if (chromosomeType == ChromosomeType.DoubleChromosome)
                    {
                        throw new CrossoverException("The Displacement Mutation can be only used on binary and combinatorial chromosomes. The specified individuals are double chromosomes.");
                    }
                    mutationMethod = mutation as TworsMutation;
                }
                else if (mutation is UniformMutation)
                {
                    if (chromosomeType != ChromosomeType.BinaryChromosome)
                    {
                        throw new CrossoverException("The Uniform Mutation can be only used on binary chromosomes. The specified individuals are not binary chromosomes.");
                    }
                    mutationMethod = mutation as UniformMutation;
                }
                else
                {
                    throw new CrossoverException("Invalid mutation input. A valid object returned by a node of Mutations category should be used.");
                }
            }

            ElitistReinsertion reinsertionMethod = new ElitistReinsertion();

            ITermination terminationMethod = null;

            if (termination == null)
            {
                terminationMethod = new FitnessStagnationTermination(100);
            }
            else
            {
                if (termination is FitnessStagnationTermination)
                {
                    terminationMethod = termination as FitnessStagnationTermination;
                }
                else if (termination is FitnessThresholdTermination)
                {
                    terminationMethod = termination as FitnessThresholdTermination;
                }
                else if (termination is GenerationNumberTermination)
                {
                    terminationMethod = termination as GenerationNumberTermination;
                }
                else if (termination is TimeEvolvingTermination)
                {
                    terminationMethod = termination as TimeEvolvingTermination;
                }
                else if (termination is AndTermination)
                {
                    terminationMethod = termination as AndTermination;
                }
                else if (termination is OrTermination)
                {
                    terminationMethod = termination as OrTermination;
                }
                else
                {
                    throw new CrossoverException("Invalid termination input. An object returned by nodes of Terminations library should be used.");
                }
            }

            if (selectionSize == null)
            {
                selectionSize = (int)Math.Round(0.5 * population.MaxSize);
            }
            else if (!(selectionSize is int))
            {
                throw new CrossoverException("Defined selection size is not an integer.");
            }
            else if (selectionSize > population.MaxSize)
            {
                throw new CrossoverException("Selection size cannot be greater than population size.");
            }

            var algorithm = new DynamoGeneticAlgorithm(population, selectionMethod, crossoverMethod, mutationMethod, reinsertionMethod, terminationMethod)
            {
                SelectionSize        = (int)selectionSize,
                CrossoverProbability = (float)crossoverProbability,
                MutationProbability  = (float)mutationProbability,

                Timer = Stopwatch.StartNew()
            };

            algorithm.Timer.Start();
            return(algorithm);
        }
        /// <summary>
        /// Adds the termination.
        /// </summary>
        /// <param name="termination">The termination.</param>
        public void AddTermination(ITermination termination)
        {
            ExceptionHelper.ThrowIfNull("termination", termination);

            Terminations.Add(termination);
        }
예제 #21
0
        public AlgoritmoGenetico(Problema problema, IPopulation population, ISelection selection,
                                 ICrossover crossover, float crossoverProbability, IMutation mutation, float mutationProbability, ITermination termination, IReinsertion reinsertion) : base(problema)
        {
            ga = new GeneticAlgorithm(population, new FitnessViajante(problema), selection, crossover, mutation)
            {
                CrossoverProbability = crossoverProbability,
                MutationProbability  = mutationProbability,
                Reinsertion          = reinsertion,
                Termination          = termination,
                TaskExecutor         = new SmartThreadPoolTaskExecutor(),
            };

            ga.GenerationRan += Ga_GenerationRan;
        }
예제 #22
0
 private void SetSampleOperatorsToComboxes()
 {
     SetSampleOperatorToCombobox(CrossoverService.GetCrossoverTypes, m_sampleController.CreateCrossover, (c) => m_crossover         = c, cmbCrossover);
     SetSampleOperatorToCombobox(MutationService.GetMutationTypes, m_sampleController.CreateMutation, (c) => m_mutation             = c, cmbMutation);
     SetSampleOperatorToCombobox(SelectionService.GetSelectionTypes, m_sampleController.CreateSelection, (c) => m_selection         = c, cmbSelection);
     SetSampleOperatorToCombobox(TerminationService.GetTerminationTypes, m_sampleController.CreateTermination, (c) => m_termination = c, cmbTermination);
 }
예제 #23
0
 public Task EvolveAsync(ITermination <TIndividual, TGeneStructure, TGene> termination)
 {
     _cancellationSource = new CancellationTokenSource();
     return(AsyncRunner = Task.Run(async() => await Evolve(termination, _cancellationSource.Token),
                                   _cancellationSource.Token));
 }
예제 #24
0
        public Configuration(int selectionType, int crossType, int mutaType, int reinsertionType, int terminationType)
        {
            //  Initialize(); //IMPORTANT

            SelectionID = selectionType;
            Selection   = new TournamentSelection();
            if (selectionType == 1)
            {
                Selection = new EliteSelection();
            }
            else if (selectionType == 2)
            {
                Selection = new RouletteWheelSelection();
            }
            else if (selectionType == 3)
            {
                Selection = new StochasticUniversalSamplingSelection();
            }
            else if (selectionType == 4)
            {
                Selection = new TournamentSelection();                          //check options
            }
            CrossoverID = crossType;
            Crossover   = new UniformCrossover(0.5f);
            if (crossType == 1)
            {
                Crossover = new CutAndSpliceCrossover();
            }
            else if (crossType == 2)
            {
                Crossover = new CycleCrossover();
            }
            else if (crossType == 3)
            {
                Crossover = new OnePointCrossover();                      //options swapPoint
            }
            else if (crossType == 4)
            {
                Crossover = new OrderBasedCrossover();
            }
            else if (crossType == 5)
            {
                Crossover = new OrderedCrossover();
            }
            else if (crossType == 6)
            {
                Crossover = new PartiallyMappedCrossover();
            }
            else if (crossType == 7)
            {
                Crossover = new ThreeParentCrossover();
            }
            else if (crossType == 8)
            {
                Crossover = new TwoPointCrossover();                      //options SwapPoint
            }
            else if (crossType == 9)
            {
                Crossover = new UniformCrossover(0.5f);                      //mix probability is 5
            }
            MutationID = mutaType;
            Mutation   = new UniformMutation(true);
            if (mutaType == 1)
            {
                Mutation = new ReverseSequenceMutation();
            }
            else if (mutaType == 2)
            {
                Mutation = new TworsMutation();
            }
            else if (mutaType == 3)
            {
                Mutation = new UniformMutation(true);                     //options
            }
            ReinsertionID = reinsertionType;
            Reinsertion   = null;
            if (reinsertionType == 1)
            {
                Reinsertion = new ElitistReinsertion();
            }
            else if (reinsertionType == 2)
            {
                Reinsertion = new FitnessBasedReinsertion();
            }
            else if (reinsertionType == 3)
            {
                Reinsertion = new PureReinsertion();
            }
            else if (reinsertionType == 4)
            {
                Reinsertion = new UniformReinsertion();
            }
            else if (reinsertionType == 5)
            {
                Reinsertion = null;
            }


            TerminationID = terminationType;

            Termination = new TimeEvolvingTermination();
            if (terminationType == 1)
            {
                Termination = new GenerationNumberTermination();
            }
            else if (terminationType == 2)
            {
                Termination = new TimeEvolvingTermination(new System.TimeSpan(0, 3, 0));
            }
            else if (terminationType == 3)
            {
                Termination = new FitnessStagnationTermination(200);                            //options
            }
            else if (terminationType == 4)
            {
                Termination = new FitnessThresholdTermination();                           // expected fitness
            }
            else if (terminationType == 5)
            {
                Termination = new AndTermination();
            }
            else if (terminationType == 6)
            {
                Termination = new OrTermination();
            }
        }
 internal One(ITermination first)
 {
     this.first = first;
 }
 public ITerminationBuilder Configure(ITermination termination)
 {
     return(new One(termination));
 }
예제 #27
0
        /// <summary>
        /// Adds the termination.
        /// </summary>
        /// <param name="termination">The termination.</param>
        public void AddTermination(ITermination termination)
        {
            ExceptionHelper.ThrowIfNull("termination", termination);

            Terminations.Add(termination);
        }
 internal DisposeToTerminate(ITermination termination)
 {
     this.termination = termination;
 }