예제 #1
0
        /// <summary>
        /// Constructs a chromosome from metadata.
        /// </summary>
        /// <remarks>This constructor is for neural chromosomes only.</remarks>
        /// <param name="chromosomeType">The chromosome type.</param>
        /// <param name="nodeGenes">The node genes.</param>
        /// <param name="edgeGenes">The edge genes.</param>
        /// <returns></returns>
        public static IChromosome ConstructChromosome(ChromosomeType chromosomeType,
                                                      IList <NeuralChromosome.NodeGene> nodeGenes, IList <NeuralChromosome.EdgeGene> edgeGenes)
        {
            switch (chromosomeType)
            {
            case ChromosomeType.Neural:
                return(new NeuralChromosome(nodeGenes, edgeGenes, "UT", 1, 1, 1, ActivationFunction.Sigmoid));

            default:
                throw new ArgumentException($"Error! Invalid chromosome type.");
            }
        }
예제 #2
0
        /// <summary>Initializes a new instance of the CombinatorialChromosome class.</summary>
        /// <param name="numberOfElements">Number of unique genes in the chromosome.</param>
        public CombinatorialChromosome(int numberOfElements) : base(numberOfElements)
        {
            NumberOfElements = numberOfElements;
            ChromosomeType   = ChromosomeType.CombinatorialChromosome;

            var elements = RandomizationProvider.Current.GetUniqueInts(numberOfElements, 0, numberOfElements);

            for (int i = 0; i < numberOfElements; i++)
            {
                ReplaceGene(i, new Gene(elements[i]));
            }
        }
예제 #3
0
        /// <summary>
        /// Constructs a chromosome from metadata.
        /// </summary>
        /// <remarks>This constructor is for neural chromosomes only.</remarks>
        /// <param name="chromosomeType">The chromosome type.</param>
        /// <param name="inputSize">The input size.</param>
        /// <param name="outputSize">The output size.</param>
        /// <param name="innovationTrackerName">The innovation tracker name.</param>
        /// <returns></returns>
        public static IChromosome ConstructChromosome(ChromosomeType chromosomeType,
                                                      uint inputSize, uint outputSize, string innovationTrackerName)
        {
            switch (chromosomeType)
            {
            case ChromosomeType.Neural:
                return(new NeuralChromosome(inputSize, outputSize, innovationTrackerName));

            default:
                throw new ArgumentException($"Error! Invalid chromosome type.");
            }
        }
예제 #4
0
        /// <summary>
        /// Constructs a chromosome from metadata.
        /// </summary>
        /// <typeparam name="T">The array data type.</typeparam>
        /// <param name="chromosomeType">The chromosome type.</param>
        /// <param name="genes">The chromosome genes genes.</param>
        /// <returns>The chromosome.</returns>
        public static IChromosome ConstructChromosome <T>(ChromosomeType chromosomeType, T[] genes)
        {
            switch (chromosomeType)
            {
            case ChromosomeType.Binary:
                return(new BinaryChromosome(genes as bool[]));

            case ChromosomeType.Permutation:
                return(new PermutationChromosome(genes as uint[]));

            default:
                throw new ArgumentException($"Error! Invalid chromosome type.");
            }
        }
예제 #5
0
        /// <summary>Initializes a new instance of the DoubleChromosome class.</summary>
        /// <param name="minValues">Minimum values.</param>
        /// <param name="maxValues">Maximum values.</param>
        public DoubleChromosome(double[] minValues, double[] maxValues)
        {
            MinValues = minValues;
            MaxValues = maxValues;

            ChromosomeType = ChromosomeType.DoubleChromosome;

            ChromosomeLength = minValues.Count();
            Genes            = new Gene[ChromosomeLength];

            var rnd = RandomizationProvider.Current;

            for (int i = 0; i < ChromosomeLength; i++)
            {
                ReplaceGene(i, new Gene(rnd.GetDouble(minValues[i], maxValues[i])));
            }
        }
예제 #6
0
        /// <summary>Initializes a new instance of the BinaryChromosome class.</summary>
        /// <param name="minValues">Minimum values.</param>
        /// <param name="maxValues">Maximum values.</param>
        /// <param name="totalBits">Total bits.</param>
        /// <param name="fractionDigits">Decimals.</param>
        /// <param name="genes">Genes.</param>
        public BinaryChromosome(double[] minValues, double[] maxValues, int[] totalBits, int[] fractionDigits, double[] genes) : base(totalBits.Sum())
        {
            MinValues      = minValues;
            MaxValues      = maxValues;
            TotalBits      = totalBits;
            FractionDigits = fractionDigits;

            ChromosomeType = ChromosomeType.BinaryChromosome;

            // If values are not supplied, create random values
            if (genes == null)
            {
                genes = new double[minValues.Length];
                var rnd = RandomizationProvider.Current;

                for (int i = 0; i < genes.Length; i++)
                {
                    genes[i] = rnd.GetDouble(minValues[i], maxValues[i]);
                }
            }
            OriginalValueStringRepresentation = string.Join("", BinaryStringRepresentation.ToRepresentation(genes, totalBits, fractionDigits));
            CreateGenes();
        }
예제 #7
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);
        }
예제 #8
0
 public Chromosome(ChromosomeType type, bool value)
 {
     this.type = type;
     this.increase = value;
 }
 /// <summary>
 /// A mutation will change the type of a chromosome
 /// </summary>
 public void Mutate()
 {
     Type = Type == ChromosomeType.OProducer ? ChromosomeType.Oc2Producer : ChromosomeType.OProducer;
 }
 public MyChromosome(ChromosomeType type)
 {
     Type = type;
 }