Example #1
0
        public override T Mutate <T>()
        {
            var newData = new List <double>(Vector);

            double MutationChance = Manager.GetParameters().GetParameter("extra_MutationChance", 0.5);

            dynamic Params = Manager.GetParameters().JsonParams.parameters;

            int x = 0;

            foreach (var P in Params)
            {
                if ((bool)P.enabled == true)
                {
                    int ListSize = P.listsize != null ? (int)P.listsize : 1;

                    double MutateRange = (double)((double)P.rangeMax - (double)P.rangeMin) / (double)P.rangeAccuracy;

                    if (P.distinct == null || (bool)P.distinct == false)
                    {
                        for (int i = 0; i < ListSize; i++)
                        {
                            if (rand.Next(0, 100) <= MutationChance * 100)
                            {
                                /*double Shift = Math.Round(rand.NextDouble(-MutateRange, MutateRange), (int)Math.Log10((double)P.rangeAccuracy));
                                 * newData[x] += Shift;
                                 * if (newData[x] > (double)P.rangeMax)
                                 *  newData[x] = (double)P.rangeMax;
                                 * if (newData[x] < (double)P.rangeMin)
                                 *  newData[x] = (double)P.rangeMin;*/
                                newData[x] = GetValInRange((double)P.rangeMin, (double)P.rangeMax, (int)P.rangeAccuracy);
                            }
                            x++;
                        }
                    }
                    else
                    {
                        var Allowed = new List <double>();
                        for (int i = (int)P.rangeMin; i <= (int)P.rangeMax; i++)
                        {
                            Allowed.Add(i);
                        }
                        foreach (var X in Vector)
                        {
                            Allowed.Remove(X);
                        }

                        for (int i = 0; i < ListSize; i++)
                        {
                            if (rand.Next(0, 100) <= MutationChance * 100)
                            {
                                double temp = newData[x];
                                newData[x] = Allowed[rand.Next(0, Allowed.Count)];
                                Allowed.Remove(newData[x]);
                                Allowed.Add(temp);
                            }
                            x++;
                        }
                    }
                }
            }

            PopulationMember ret = new BalanceGA(Manager, newData, rand);

            ((BalanceGA)ret).CreatedBy = "Mutation";

            return((T)ret);
        }
Example #2
0
        public override PopulationMember Clone()
        {
            BalanceGA ret = new BalanceGA(Manager, new List <double>(Vector), rand);

            return(ret);
        }
Example #3
0
        public override T Crossover <T>(T b)
        {
            BalanceGA CWith = ((BalanceGA)(object)b);

            var newData = new List <double>(Vector);

            dynamic Params = Manager.GetParameters().JsonParams.parameters;

            int  x = 0;
            int  NonDistinctRangeStart   = -1;
            int  NonDistinctRangeEnd     = -1;
            bool DistinctFlag            = false;
            int  LastNonDistinctAccuracy = 1;

            foreach (var P in Params)
            {
                if ((bool)P.enabled == true)
                {
                    int ListSize = P.listsize != null ? (int)P.listsize : 1;

                    if (P.distinct == null || (bool)P.distinct == false)
                    {
                        if (NonDistinctRangeStart == -1)
                        {
                            NonDistinctRangeStart = x;
                        }

                        LastNonDistinctAccuracy = (int)P.rangeAccuracy;

                        NonDistinctRangeEnd = x + ListSize;
                    }
                    else
                    {
                        DistinctFlag = true;
                        var newSet = new HashSet <double>();
                        foreach (var X in Vector.Skip(x).Take(ListSize))
                        {
                            newSet.Add(X);
                        }
                        foreach (var X in CWith.Vector.Skip(x).Take(ListSize))
                        {
                            newSet.Add(X);
                        }

                        var vec = newSet.ToList();
                        vec.Shuffle();

                        var L = vec.Take(ListSize).ToList();
                        for (int i = 0; i < ListSize; i++)
                        {
                            newData[x + i] = L[i];
                        }
                    }

                    x += ListSize;
                }

                if (NonDistinctRangeStart != -1 && (DistinctFlag || NonDistinctRangeEnd == Vector.Count))
                {
                    if (NonDistinctRangeEnd - NonDistinctRangeStart == 1)
                    {
                        newData[x] = Math.Round((Vector[NonDistinctRangeStart] + CWith.Vector[NonDistinctRangeStart]) / 2, (int)Math.Log10(LastNonDistinctAccuracy));
                    }
                    else
                    {
                        int p1 = rand.Next(NonDistinctRangeStart, NonDistinctRangeEnd - 1);
                        int p2 = rand.Next(p1 + 1, NonDistinctRangeEnd);

                        for (int i = p1; i < p2; i++)
                        {
                            newData[i] = CWith.Vector[i];
                        }
                    }

                    NonDistinctRangeStart = -1;
                    DistinctFlag          = false;
                }
            }

            PopulationMember ret = (T)Activator.CreateInstance(typeof(T), new object[] { Manager, newData, rand });

            ((BalanceGA)ret).CreatedBy = "Crossover";

            return((T)ret);
        }