Esempio n. 1
0
        private void Selection()
        {
            Population selected = new Population();

            while (selected.ListOfIndividuals.Count != PopulationSize)
            {
                double r = GenMathBase.RandomNumberBetween(rand.NextDouble(), 0, 1);

                Individual indv = CurrentPopulation.ListOfIndividuals.Where(q => q.IndvData.Qx >= r).FirstOrDefault();

                if (indv != null)
                {
                    indv.IndvData.r = r;
                    selected.ListOfIndividuals.Add(indv);
                }
            }
            CurrentPopulation = JsonConvert.DeserializeObject <Population>(JsonConvert.SerializeObject(selected));
            //HistoryHelper.PopulationAfterSelection = JsonConvert.DeserializeObject<Population>(JsonConvert.SerializeObject(CurrentPopulation));
        }
Esempio n. 2
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));
        }
Esempio n. 3
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));
        }
Esempio n. 4
0
        private void Crossover()
        {
            for (int i = 0; i < CurrentPopulation.ListOfIndividuals.Count; i++)
            {
                double r = GenMathBase.RandomNumberBetween(rand.NextDouble(), 0, 1);
                if (r < Pk)
                {
                    CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.isParent = true;
                }
            }

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

            Individual parent1          = null;
            Individual parent2          = null;
            int        firstParentIndex = -1;
            int        parentCount      = 0;

            for (int i = 0; i < CurrentPopulation.ListOfIndividuals.Count; i++)
            {
                if (CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.isParent == true)
                {
                    if (parent1 == null)
                    {
                        parentCount++;
                        if (firstParentIndex == -1)
                        {
                            firstParentIndex = i;
                        }
                        CurrentPopulation.ListOfIndividuals.ElementAt(i).index = i;
                        parent1 = CurrentPopulation.ListOfIndividuals.ElementAt(i);

                        if (parent2 == null && parentCount == CurrentPopulation.ListOfIndividuals.Where(e => e.IndvData.isParent == true).Count())
                        {
                            parent2 = CurrentPopulation.ListOfIndividuals.ElementAt(firstParentIndex);

                            int pc = rand.Next(0, CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.xBin.Count());
                            parent1.IndvData.Pc = pc;

                            string Ls1 = parent1.IndvData.xBin.Substring(0, pc);
                            string Rs2 = parent2.IndvData.xBin.Substring(pc);

                            string newxBin = string.Concat(Ls1, Rs2);

                            CurrentPopulation.ListOfIndividuals.ElementAt(parent1.index).SetXBin(newxBin);

                            parent1 = null;
                            parent2 = null;
                        }
                    }
                    else if (parent1 != null && parent2 == null)
                    {
                        parentCount++;
                        CurrentPopulation.ListOfIndividuals.ElementAt(i).index = i;
                        parent2 = CurrentPopulation.ListOfIndividuals.ElementAt(i);

                        int pc = rand.Next(0, CurrentPopulation.ListOfIndividuals.ElementAt(i).IndvData.xBin.Count());
                        parent1.IndvData.Pc = pc;
                        parent2.IndvData.Pc = pc;

                        string Ls1 = parent1.IndvData.xBin.Substring(0, pc);
                        string Rs1 = parent1.IndvData.xBin.Substring(pc);
                        string Ls2 = parent2.IndvData.xBin.Substring(0, pc);
                        string Rs2 = parent2.IndvData.xBin.Substring(pc);

                        string newxBin  = string.Concat(Ls1, Rs2);
                        string newxBin2 = string.Concat(Ls2, Rs1);

                        CurrentPopulation.ListOfIndividuals.ElementAt(parent1.index).SetXBin(newxBin);
                        CurrentPopulation.ListOfIndividuals.ElementAt(parent2.index).SetXBin(newxBin2);

                        parent1 = null;
                        parent2 = null;
                    }
                }
            }

            if (isElite)
            {
                Eval();
                best = CurrentPopulation.FindBestIndividual();
                int indx = rand.Next(0, CurrentPopulation.ListOfIndividuals.Count - 1);
                CurrentPopulation.ListOfIndividuals[indx] = best;
            }

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