Beispiel #1
0
        public void GeneratePopulation()
        {
            if (HistoryHelper.Generations.Count != 0)
            {
                HistoryHelper.Generations = new List <Population>();
            }

            CalculatedPrecision = Math.Pow(10, -Precision);
            CurrentPopulation   = new Population();

            double pom1 = (bRange - aRange);
            double pom2 = pom1 / CalculatedPrecision;

            double log = Math.Log(pom2 + 1, 2);

            L = (int)Math.Ceiling(log);

            for (int i = 0; i < PopulationSize; i++)
            {
                double xreal = Math.Truncate(
                    GenMathBase.RandomNumberBetween(rand.NextDouble(), aRange, bRange) *
                    Math.Pow(10, Precision)) /
                               Math.Pow(10, Precision);
                int    xint     = GenMathBase.XrealToXint(xreal, L, aRange, bRange);
                string xbin     = GenMathBase.XintToXbin(xint, L);
                int    newXint  = GenMathBase.XbinToXint(xbin);
                double newXreal = GenMathBase.XintToXreal(newXint, L, aRange, bRange, Precision);

                Individual indv = new Individual();
                indv.IndvData.xReal = xreal;
                indv.IndvData.xInt  = xint;
                indv.IndvData.xBin  = xbin;
                indv.IndvData.Fx    = GenMathBase.CalculateFx(indv.IndvData.xReal);

                CurrentPopulation.ListOfIndividuals.Add(indv);
            }


            //HistoryHelper.StartPopulation = JsonConvert.DeserializeObject<Population>(JsonConvert.SerializeObject(CurrentPopulation));
        }
Beispiel #2
0
        private void Mutation()
        {
            int degreeOfParallelism = Environment.ProcessorCount;

            Parallel.For(0, degreeOfParallelism, workerId =>
            {
                var max = CurrentPopulation.ListOfIndividuals.Count * (workerId + 1) / degreeOfParallelism;

                for (int i = CurrentPopulation.ListOfIndividuals.Count * workerId / degreeOfParallelism; i < max; i++)
                {
                    for (int j = 0; j < CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.xBin.Count(); j++)
                    {
                        double r = GenMathBase.RandomNumberBetween(rand.NextDouble(), 0, 1);
                        if (r < Pm)
                        {
                            if (CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.xBin.ElementAt(j) == '1')
                            {
                                StringBuilder sb = new StringBuilder(CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.xBin);
                                sb[j]            = '0';
                                CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.xBin        = sb.ToString();
                                CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.MutationPos = CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.MutationPos + j + " ";
                            }
                            else
                            {
                                StringBuilder sb = new StringBuilder(CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.xBin);
                                sb[j]            = '1';
                                CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.xBin        = sb.ToString();
                                CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.MutationPos = CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.MutationPos + j + " ";
                            }
                        }
                    }
                }
            });

            /*for(int i=0;i<CurrentPopulation.ListOfIndividuals.Count;i++)
             * {
             *
             *  for (int j = 0; j < CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.xBin.Count(); j++)
             *  {
             *      double r = GenMathBase.RandomNumberBetween(rand.NextDouble(), 0, 1);
             *      if (r < Pm)
             *      {
             *
             *          if (CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.xBin.ElementAt(j) == '1')
             *          {
             *              StringBuilder sb = new StringBuilder(CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.xBin);
             *              sb[j] = '0';
             *              CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.xBin = sb.ToString();
             *              CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.MutationPos = CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.MutationPos + j + " ";
             *          }
             *          else
             *          {
             *              StringBuilder sb = new StringBuilder(CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.xBin);
             *              sb[j] = '1';
             *              CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.xBin = sb.ToString();
             *              CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.MutationPos = CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.MutationPos + j + " ";
             *          }
             *      }
             *  }
             * }*/

            foreach (Individual indv in CurrentPopulation.ListOfIndividuals)
            {
                indv.IndvData.xInt  = GenMathBase.XbinToXint(indv.IndvData.xBin);
                indv.IndvData.xReal = GenMathBase.XintToXreal(indv.IndvData.xInt, L, aRange, bRange, Precision);
                indv.IndvData.Fx    = GenMathBase.CalculateFx(indv.IndvData.xReal);
            }

            //HistoryHelper.PopulationAfterMutation = JsonConvert.DeserializeObject<Population>(JsonConvert.SerializeObject(CurrentPopulation));
        }