Exemple #1
0
 public static object GetTermination(DynamoGeneticAlgorithm algorithm)
 {
     if (algorithm.Termination is FitnessStagnationTermination)
     {
         return(algorithm.Termination as FitnessStagnationTermination);
     }
     if (algorithm.Termination is FitnessThresholdTermination)
     {
         return(algorithm.Termination as FitnessThresholdTermination);
     }
     if (algorithm.Termination is GenerationNumberTermination)
     {
         return(algorithm.Termination as GenerationNumberTermination);
     }
     if (algorithm.Termination is TimeEvolvingTermination)
     {
         return(algorithm.Termination as TimeEvolvingTermination);
     }
     if (algorithm.Termination is AndTermination)
     {
         return(algorithm.Termination as AndTermination);
     }
     if (algorithm.Termination is OrTermination)
     {
         return(algorithm.Termination as OrTermination);
     }
     return(null);
 }
Exemple #2
0
 public static object GetCrossover(DynamoGeneticAlgorithm algorithm)
 {
     if (algorithm.Crossover is AlternatingPositionCrossover)
     {
         return(algorithm.Crossover as AlternatingPositionCrossover);
     }
     if (algorithm.Crossover is CutAndSpliceCrossover)
     {
         return(algorithm.Crossover as CutAndSpliceCrossover);
     }
     if (algorithm.Crossover is CycleCrossover)
     {
         return(algorithm.Crossover as CycleCrossover);
     }
     if (algorithm.Crossover is OnePointCrossover)
     {
         return(algorithm.Crossover as OnePointCrossover);
     }
     if (algorithm.Crossover is OrderedCrossover)
     {
         return(algorithm.Crossover as OrderedCrossover);
     }
     if (algorithm.Crossover is OrderBasedCrossover)
     {
         return(algorithm.Crossover as OrderBasedCrossover);
     }
     if (algorithm.Crossover is PartiallyMappedCrossover)
     {
         return(algorithm.Crossover as PartiallyMappedCrossover);
     }
     if (algorithm.Crossover is PositionBasedCrossover)
     {
         return(algorithm.Crossover as PositionBasedCrossover);
     }
     if (algorithm.Crossover is SimulatedBinaryCrossover)
     {
         return(algorithm.Crossover as SimulatedBinaryCrossover);
     }
     if (algorithm.Crossover is ThreeParentCrossover)
     {
         return(algorithm.Crossover as ThreeParentCrossover);
     }
     if (algorithm.Crossover is TwoPointCrossover)
     {
         return(algorithm.Crossover as TwoPointCrossover);
     }
     if (algorithm.Crossover is UniformCrossover)
     {
         return(algorithm.Crossover as UniformCrossover);
     }
     if (algorithm.Crossover is VotingRecombinationCrossover)
     {
         return(algorithm.Crossover as VotingRecombinationCrossover);
     }
     return(null);
 }
Exemple #3
0
 public static object GetSelection(DynamoGeneticAlgorithm algorithm)
 {
     if (algorithm.Selection is EliteSelection)
     {
         return(algorithm.Selection as EliteSelection);
     }
     if (algorithm.Selection is RouletteWheelSelection)
     {
         return(algorithm.Selection as RouletteWheelSelection);
     }
     if (algorithm.Selection is StochasticUniversalSamplingSelection)
     {
         return(algorithm.Selection as StochasticUniversalSamplingSelection);
     }
     if (algorithm.Selection is TournamentSelection)
     {
         return(algorithm.Selection as TournamentSelection);
     }
     return(null);
 }
Exemple #4
0
 public static object GetMutation(DynamoGeneticAlgorithm algorithm)
 {
     if (algorithm.Mutation is DisplacementMutation)
     {
         return(algorithm.Mutation as DisplacementMutation);
     }
     if (algorithm.Mutation is FlipBitMutation)
     {
         return(algorithm.Mutation as FlipBitMutation);
     }
     if (algorithm.Mutation is GaussianMutation)
     {
         return(algorithm.Mutation as GaussianMutation);
     }
     if (algorithm.Mutation is InsertionMutation)
     {
         return(algorithm.Mutation as InsertionMutation);
     }
     if (algorithm.Mutation is PartialShuffleMutation)
     {
         return(algorithm.Mutation as PartialShuffleMutation);
     }
     if (algorithm.Mutation is PolynomialMutation)
     {
         return(algorithm.Mutation as PolynomialMutation);
     }
     if (algorithm.Mutation is ReverseSequenceMutation)
     {
         return(algorithm.Mutation as ReverseSequenceMutation);
     }
     if (algorithm.Mutation is TworsMutation)
     {
         return(algorithm.Mutation as TworsMutation);
     }
     if (algorithm.Mutation is UniformMutation)
     {
         return(algorithm.Mutation as UniformMutation);
     }
     return(null);
 }
Exemple #5
0
 public static double GetMutationProbability(DynamoGeneticAlgorithm algorithm)
 {
     return(algorithm.MutationProbability);
 }
Exemple #6
0
 public static double TimeEvolving(DynamoGeneticAlgorithm algorithm)
 {
     return(algorithm.TimeEvolving.TotalSeconds);
 }
Exemple #7
0
 public static bool HasFinished(DynamoGeneticAlgorithm algorithm)
 {
     return(algorithm.State == GeneticAlgorithmState.TerminationReached);
 }
Exemple #8
0
 public static DynamoGeneticAlgorithm ProduceNewGeneration(DynamoGeneticAlgorithm algorithm, List <object> parents, List <object> offsprings, List <double> offspringsFitness)
 {
     algorithm.ProduceNewGeneration(parents, offsprings, offspringsFitness);
     return(algorithm);
 }
Exemple #9
0
 public static Dictionary <string, List <IChromosome> > ProduceOffsprings(DynamoGeneticAlgorithm algorithm, List <double?> fitness)
 {
     return(algorithm.ProduceOffsprings(fitness));
 }
Exemple #10
0
        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);
        }
Exemple #11
0
 public static double GetCrossoverProbability(DynamoGeneticAlgorithm algorithm)
 {
     return(algorithm.CrossoverProbability);
 }
Exemple #12
0
 public static int NumberOfGenerations(DynamoGeneticAlgorithm algorithm)
 {
     return(NumberOfGenerations((Population)algorithm.Population));
 }
Exemple #13
0
 public static Population GetPopulation(DynamoGeneticAlgorithm algorithm)
 {
     return((Population)algorithm.Population);
 }