Example #1
0
        static void Main(string[] args)
        {
            List<RealAxis> R = new List<RealAxis>();
            List<DiscreteAxis> D = new List<DiscreteAxis>();
            List<BinaryAxis> B = new List<BinaryAxis>();
            for(int i = 0; i < 3; i++)
            {
                R.Add(new RealAxis(-32.768, 32.768));
            }
            Space S = new Space(R, D, B);

            Map<Space, Individual, double> M = new Map<Space, Individual, double>(S, false, 0.5, ME2Functions.RandomIndividual,
                MapFunctions.MapNeighbors, ME2Functions.Ackley, ME2Functions.ApplyFitness, ME2Functions.RouletteSelection);

            Individual Best = M.MappedSpace.ElementAt(0);
            foreach(Individual potentialBest in M.MappedSpace)
            {
                if (potentialBest.Fitness > Best.Fitness)
                    Best = potentialBest;
            }
            Console.WriteLine(Best);

            Explore<Space, Individual, double> E1 = new Explore<Space, Individual, double>(S, false, M.MappedSpace, ExploitFunctions.ExploitNeighbors,
                    ME2Functions.NormalMutate, ME2Functions.Ackley, ME2Functions.ApplyFitness, ExploreFunctions.ApplyExplorePOM, ExploreFunctions.ExploreTP,
                    ME2Functions.TotalFitness, 100);

            Best = E1.Optimized.ElementAt(0);
            foreach (Individual potentialBest in E1.Optimized)
            {
                if (potentialBest.Fitness > Best.Fitness)
                    Best = potentialBest;
            }
            Console.WriteLine(Best);

            Exploit<Space, Individual, double> E2 = new Exploit<Space, Individual, double>(S, false, E1.Optimized, ExploitFunctions.ExploitNeighbors,
                    ME2Functions.NormalMutate, ME2Functions.Ackley, ME2Functions.ApplyFitness, ExploitFunctions.ApplyExploitPOM, ExploitFunctions.ExploitTP,
                    ME2Functions.TotalFitness, 100);

            Best = E2.Optimized.First();
            foreach (Individual potentialBest in E2.Optimized)
            {
                if (potentialBest.Fitness > Best.Fitness)
                    Best = potentialBest;
            }
            Console.WriteLine(Best);
        }
 public static double NormalizedCityBlockDistance(Space SearchSpace, Individual Source, Individual Destination)
 {
     double ncbd = 0.0;
     for (int i = 0; i < Source.Size; i++)
     {
         if (Source[i] is bool)
         {
             ncbd += 1.0;
         }
         else if (Source[i] is int)
         {
             ncbd += (double)(Math.Abs((int)Source[i] - (int)Destination[i]) / ((DiscreteAxis)SearchSpace[i]).RangeProper);
         }
         else if (Source[i] is double)
         {
             ncbd += (Math.Abs((double)Source[i] - (double)Destination[i]) / ((RealAxis)SearchSpace[i]).RangeProper);
         }
     }
     return ncbd;
 }
        public static Individual NormalMutate(Space SearchSpace, Individual IndividualToMutate, Func<Individual, bool, double> Fitness, bool maximize, Action<Individual, double> ApplyFitness)
        {
            // First mutate sigma
            double tau = 1.0 / (2.0 * Math.Sqrt(IndividualToMutate.Size)); // Learning rate inversely proportional to the sqrt of genotype size
            double NT = Normal.Sample(ME2Functions._rng, 0, tau);

            // The value below CANNOT BE USED as sigma
            // Sigma is this value * the allele value
            IndividualToMutate.MutationStep = IndividualToMutate.MutationStep * Math.Exp(NT);

            // Now create a mutant genotype using the original genotype of the Individual
            var resultingGenotype = new List<object>();
            double probability = 0.0;
            for (int i = 0; i < IndividualToMutate.Size; i++)
            {
                // Check whether to mutate this allele
                probability = ContinuousUniform.Sample(ME2Functions._rng, 0.0, 1.0);
                if (probability < IndividualToMutate.MutationProbability)
                {
                    // Allele will be mutated
                    if (IndividualToMutate[i] is bool)
                    {
                        resultingGenotype.Add(!(bool)IndividualToMutate[i]);
                    }
                    else if (IndividualToMutate[i] is int)
                    {
                        double newAllele = (double)IndividualToMutate[i];
                        double NS = Normal.Sample(ME2Functions._rng, 0, Math.Abs(newAllele * IndividualToMutate.MutationStep));
                        newAllele += NS;
                        int minValue = ((DiscreteAxis)SearchSpace[i]).Min;
                        int maxValue = ((DiscreteAxis)SearchSpace[i]).Max;
                        if(newAllele < minValue)
                        {
                            newAllele = minValue;
                        }
                        else if(newAllele > maxValue)
                        {
                            newAllele = maxValue;
                        }
                        resultingGenotype.Add((int)newAllele);
                    }
                    else
                    {
                        double newAllele = (double)IndividualToMutate[i];
                        double NS = Normal.Sample(ME2Functions._rng, 0, Math.Abs(newAllele * IndividualToMutate.MutationStep));
                        newAllele += NS;
                        double minValue = ((RealAxis)SearchSpace[i]).Min;
                        double maxValue = ((RealAxis)SearchSpace[i]).Max;
                        if(newAllele < minValue)
                        {
                            newAllele = minValue;
                        }
                        else if(newAllele > maxValue)
                        {
                            newAllele = maxValue;
                        }
                        resultingGenotype.Add(newAllele);
                    }
                }
                else
                {
                    // Allele will not be mutated
                    resultingGenotype.Add(IndividualToMutate[i]);
                }
            }

            // Return an Individual with the mutant genotype
            Individual mutant = new Individual();
            mutant.Genotype = resultingGenotype;
            mutant.MutationProbability = IndividualToMutate.MutationProbability;
            mutant.MutationStep = IndividualToMutate.MutationStep;
            double newFitness = Fitness(mutant, maximize);
            ApplyFitness(mutant, newFitness);
            return mutant;
        }
 public static Individual RandomIndividual(Space SearchSpace)
 {
     Individual randomIndividual = new Individual();
     for (int i = 0; i < SearchSpace.Size; i++)
     {
         if (i >= 0 && i < SearchSpace.RealAxes)
         {
             randomIndividual.Genotype.Add(ContinuousUniform.Sample(ME2Functions._rng, (double)((RealAxis)SearchSpace[i]).Min, (double)((RealAxis)SearchSpace[i]).Max));
         }
         else if (i >= SearchSpace.RealAxes && i < SearchSpace.DiscreteAxes)
         {
             randomIndividual.Genotype.Add(DiscreteUniform.Sample(ME2Functions._rng, (int)((DiscreteAxis)SearchSpace[i]).Min, (int)((DiscreteAxis)SearchSpace[i]).Max));
         }
         else
         {
             bool randomBool = Convert.ToBoolean(ME2Functions._rng.Next(0, 2));
             randomIndividual.Genotype.Add(randomBool);
         }
     }
     randomIndividual.MutationStep = 1.0;
     randomIndividual.MutationProbability = 1.0;
     return randomIndividual;
 }