Exemplo n.º 1
0
 /// <summary>
 /// Constructs a new instance of the <see cref="ChromosomeMetadata"/> class.
 /// </summary>
 internal PopulationMetadata()
 {
     Size               = DEFAULT_SIZE;
     SurvivalRate       = DEFAULT_SURVIVAL_RATE;
     DistanceThreshold  = DEFAULT_DISTANCE_THRESHOLD;
     SelectionAlgorithm = GeneticFactory.ConstructSelectionAlgorithm(DEFAULT_SELECTION_ALGORITHM);
 }
 public MRMutation(MutationMethod[] mutationMethods, double mutationProbability, int[][] population, Random random) : base(mutationProbability, population, random)
 {
     Mutations = new List <Mutation>();
     foreach (var method in mutationMethods)
     {
         Mutations.Add(GeneticFactory.CreateMutation(method, null, population, mutationProbability, random));
     }
 }
Exemplo n.º 3
0
 public MACrossover(CrossoverMethod[] crossoverMethods, int startPoint, ConflictResolver resolverConflict, ConflictResolver resolverRandomized, Random random, bool mutateIfSame) : base(resolverConflict, resolverRandomized, random, mutateIfSame)
 {
     _crossovers = new List <Crossover>();
     foreach (var method in crossoverMethods)
     {
         _crossovers.Add(GeneticFactory.CreateCrossover(startPoint, method, null, resolverConflict, resolverRandomized, random, mutateIfSame));
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Parses a TerminationCondition element.
        /// </summary>
        /// <param name="element">The TerminationConition element.</param>
        /// <returns>The parsed termination condition.</returns>
        private static ITerminationCondition ParseTerminationCondition(XElement element)
        {
            var attributes    = element.Attributes();
            var typeAttribute = attributes.FirstOrDefault(attribute => attribute.Name.LocalName == "Type");
            var argAttribute  = attributes.FirstOrDefault(attribute => attribute.Name.LocalName == "Arg");

            var condition = (TerminationCondition)Enum.Parse(typeof(TerminationCondition), typeAttribute.Value);

            return(GeneticFactory.ConstructTerminationCondition(condition, argAttribute?.Value));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Parses a SelectionAlgorithm element.
        /// </summary>
        /// <param name="element">The SelectionAlgorithm element.</param>
        /// <returns>The parsed selection algorithm.</returns>
        private static ISelectionAlgorithm ParseSelectionAlgorithm(XElement element)
        {
            var attributes    = element.Attributes();
            var typeAttribute = attributes.FirstOrDefault(attribute => attribute.Name.LocalName == "Type");
            var argAttribute  = attributes.FirstOrDefault(attribute => attribute.Name.LocalName == "Arg");

            var algorithm = (SelectionAlgorithm)Enum.Parse(typeof(SelectionAlgorithm), typeAttribute.Value);

            return(GeneticFactory.ConstructSelectionAlgorithm(algorithm, argAttribute?.Value));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Adds new crossover operators to the metadata.
        /// </summary>
        /// <param name="crossoverOptions">The crossover options.</param>
        /// <param name="weight">The operator weight.</param>
        internal override void AddCrossoverOperators(NeuralCrossover crossoverOptions, double weight = 1)
        {
            var crossovers = GeneticFactory.ConstructNeuralCrossoverOperators(crossoverOptions, weight);

            if (_crossovers == null)
            {
                _crossovers = crossovers;
            }
            else
            {
                _crossovers.AddRange(crossovers);
            }

            _crossoverF += ComputeWeightedSum(crossovers);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Adds new mutation operators to the metadata.
        /// </summary>
        /// <param name="mutationOptions">The mutation options.</param>
        /// <param name="weight">The operator weight.</param>
        internal override void AddMutationOperators(NeuralMutation mutationOptions, double weight = 1)
        {
            var mutations = GeneticFactory.ConstructNeuralMutationOperators(mutationOptions, weight);

            if (_mutations == null)
            {
                _mutations = mutations;
            }
            else
            {
                _mutations.AddRange(mutations);
            }

            _mutationF += ComputeWeightedSum(mutations);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Constructs a new instance of the <see cref="Creature"/> class.
        /// </summary>
        /// <param name="species">The species this creature belongs to.</param>
        internal Creature(Species species)
        {
            _species = species;

            _chromosomeMetadata = _species.Population.Metadata
                                  .Where(metadata => metadata.Name == _species.ChromosomeFilter || _species.ChromosomeFilter == "*")
                                  .ToList();

            foreach (var chromosomeMetadata in _chromosomeMetadata)
            {
                if (_chromosomes.ContainsKey(chromosomeMetadata.Name))
                {
                    throw new ArgumentException($"Error! Creature already contain a chromosome named {chromosomeMetadata.Name}");
                }

                var chromosome = GeneticFactory.ConstructChromosome(chromosomeMetadata);
                _chromosomes.Add(chromosomeMetadata.Name, chromosome);
                chromosome.Creature = this;
            }
        }
Exemplo n.º 9
0
        public GeneticTSP(int[] order, OptimizationParameters parameters, DelegateFitness.CalcFitness calcFitness, CancellationToken ct, Random random)
        {
            _use2opt = parameters.Use2opt;
            var populationInitialization =
                GeneticFactory.CreatePopulationInitialization(parameters.PopulationInitializationMethod, random);
            var population = populationInitialization.InitializePopulation(order, parameters.PopulationSize, parameters.StartingId);

            _genetic = new BaseGenetic(parameters, population, calcFitness, ct, random);

            if (parameters.StopAfterEpochsWithoutChange)
            {
                _genetic.LoadModule(new TerminationModule(parameters.StopAfterEpochCount));
            }
            if (parameters.EnableCataclysm)
            {
                _genetic.LoadModule(new CataclysmModule(populationInitialization, parameters.DeathPercentage, parameters.CataclysmEpoch));
            }

            _tspModule = new TSPModule();
            _tspModule.LoadCrossoverOperator(_genetic.Crossover);
            _genetic.LoadModule(_tspModule);
        }
        private PermutationChromosome GetMutation(PermutationChromosome chromosome, PermutationMutation mutation)
        {
            var rator = GeneticFactory.ConstructPermutationMutationOperators(mutation).First();

            return(rator.Invoke(chromosome) as PermutationChromosome);
        }
Exemplo n.º 11
0
        private BinaryChromosome GetMutation(BinaryChromosome chromosome, BinaryMutation mutation)
        {
            var rator = GeneticFactory.ConstructBinaryMutationOperators(mutation).First();

            return(rator.Invoke(chromosome) as BinaryChromosome);
        }
Exemplo n.º 12
0
        private BinaryChromosome GetCrossover(BinaryChromosome x, BinaryChromosome y, BinaryCrossover crossover)
        {
            var rator = GeneticFactory.ConstructBinaryCrossoverOperators(crossover).First();

            return(rator.Invoke(x, y) as BinaryChromosome);
        }
        private NeuralChromosome GetCrossover(NeuralChromosome x, NeuralChromosome y, NeuralCrossover crossover)
        {
            var rator = GeneticFactory.ConstructNeuralCrossoverOperators(crossover).First();

            return(rator.Invoke(x, y) as NeuralChromosome);
        }
Exemplo n.º 14
0
 /// <summary>
 /// Gets the default mutation operators.
 /// </summary>
 /// <returns>The default mutation operators.</returns>
 protected override List <IMutation> GetDefaultMutations()
 {
     return(GeneticFactory.ConstructNeuralMutationOperators(DEFAULT_MUTATION_OPTIONS));
 }
Exemplo n.º 15
0
 /// <summary>
 /// Gets the default crossover operators.
 /// </summary>
 /// <returns>The default crossover operators.</returns>
 protected override List <ICrossover> GetDefaultCrossovers()
 {
     return(GeneticFactory.ConstructNeuralCrossoverOperators(DEFAULT_CROSSOVER_OPTIONS));
 }
Exemplo n.º 16
0
        private NeuralChromosome GetMutation(NeuralChromosome chromosome, NeuralMutation mutation)
        {
            var rator = GeneticFactory.ConstructNeuralMutationOperators(mutation).First();

            return(rator.Invoke(chromosome) as NeuralChromosome);
        }
Exemplo n.º 17
0
        private PermutationChromosome GetCrossover(PermutationChromosome x, PermutationChromosome y, PermutationCrossover crossover)
        {
            var rator = GeneticFactory.ConstructPermutationCrossoverOperators(crossover).First();

            return(rator.Invoke(x, y) as PermutationChromosome);
        }