Пример #1
0
        public void DoMutation(double probability, Solution solution)
        {
            XReal x = new XReal(solution);

            for (int var = 0; var < solution.DecisionVariables.Length; var++)
            {
                if (PseudoRandom.Instance().NextDouble() < probability)
                {
                    double rand = PseudoRandom.Instance().NextDouble();
                    double tmp = (rand - 0.5)*_perturbation;

                    tmp += x.GetValue(var);

                    if (tmp < x.GetLowerBound(var))
                    {
                        tmp = x.GetLowerBound(var);
                    }
                    else if (tmp > x.GetUpperBound(var))
                    {
                        tmp = x.GetUpperBound(var);
                    }

                    x.SetValue(var, tmp);
                }
            }
        }
Пример #2
0
        public override void Evaluate(Solution solution)
        {
            XReal x = new XReal(solution);

            double[] f = new double[NumberOfObjectives];
            f[0] = x.GetValue(0);
            double g = (EvalG(x));
            double h = EvalH(f[0], g);
            f[1] = h*g;

            solution.Objective[0] = f[0];
            solution.Objective[1] = f[1];
        }
Пример #3
0
 private void DoMutation(double probability, Solution solution)
 {
     XReal x = new XReal(solution);
     for (int var = 0; var < solution.NumberOfVariables; var++)
     {
         if (PseudoRandom.Instance().NextDouble() <= probability)
         {
             double y = x.GetValue(var);
             double yl = x.GetLowerBound(var);
             double yu = x.GetUpperBound(var);
             double delta1 = (y - yl)/(yu - yl);
             double delta2 = (yu - y)/(yu - yl);
             double rnd = PseudoRandom.Instance().NextDouble();
             double mutPow = 1.0/(EtaMDefault + 1.0);
             double xy;
             double deltaq;
             double val;
             if (rnd <= 0.5)
             {
                 xy = 1.0 - delta1;
                 val = 2.0*rnd + (1.0 - 2.0*rnd)*(Math.Pow(xy, (_distributionIndex + 1.0)));
                 deltaq = Math.Pow(val, mutPow) - 1.0;
             }
             else
             {
                 xy = 1.0 - delta2;
                 val = 2.0*(1.0 - rnd) + 2.0*(rnd - 0.5)*(Math.Pow(xy, (_distributionIndex + 1.0)));
                 deltaq = 1.0 - (Math.Pow(val, mutPow));
             }
             y = y + deltaq*(yu - yl);
             if (y < yl)
             {
                 y = yl;
             }
             if (y > yu)
             {
                 y = yu;
             }
             x.SetValue(var, y);
         }
     }
 }
Пример #4
0
 private double EvalG(XReal x)
 {
     double g = 0.0;
     for (int i = 1; i < x.GetNumberOfDecisionVariables(); i++)
     {
         g += x.GetValue(i);
     }
     double constante = (9.0/(NumberOfVariables - 1));
     g = constante*g;
     g = g + 1.0;
     return g;
 }
        public override Object Execute(Object obj)
        {
            var parameters = (Object[]) obj;
            var current = (Solution) parameters[0];
            var parent = parameters[1] as Solution[];

            if (parent == null)
            {
                throw new Exception("parents in passed object do not exist");
            }

            if ((Array.Find(ValidTypes, n => n == parent[0].SolutionType.GetType()) == null) ||
                (Array.Find(ValidTypes, n => n == parent[1].SolutionType.GetType()) == null) ||
                (Array.Find(ValidTypes, n => n == parent[2].SolutionType.GetType()) == null))
            {
                throw new Exception("the solutions are not of the right type. The type should be 'Real' or 'ArrayReal', but"
                                    + parent[0].SolutionType.GetType() + "and"
                                    + parent[1].SolutionType.GetType() + "and"
                                    + parent[2].SolutionType.GetType() + "are obtained");
            }

            int numberOfVariables = current.NumberOfVariables;
            int jrand = PseudoRandom.Instance().Next(numberOfVariables - 1);

            var child = new Solution(current);

            XReal xParent0 = new XReal(parent[0]);
            XReal xParent1 = new XReal(parent[1]);
            XReal xParent2 = new XReal(parent[2]);
            XReal xCurrent = new XReal(current);
            XReal xChild = new XReal(child);

            switch (DeVariant)
            {
                case "rand/1/bin":
                case "best/1/bin":

                    for (var j = 0; j < numberOfVariables; j++)
                    {
                        if (PseudoRandom.Instance().NextDouble() < Cr || j == jrand)
                        {
                            double value = xParent2.GetValue(j) + F*(xParent0.GetValue(j) -
                                                                     xParent1.GetValue(j));

                            if (value < xChild.GetLowerBound(j))
                            {
                                value = xChild.GetLowerBound(j);
                            }
                            if (value > xChild.GetUpperBound(j))
                            {
                                value = xChild.GetUpperBound(j);
                            }

                            xChild.SetValue(j, value);
                        }
                        else
                        {
                            double value = xCurrent.GetValue(j);
                            xChild.SetValue(j, value);
                        }
                    }

                    break;
                case "rand/1/exp":
                case "best/1/exp":

                    for (var j = 0; j < numberOfVariables; j++)
                    {
                        if (PseudoRandom.Instance().NextDouble() < Cr || j == jrand)
                        {
                            double value = xParent2.GetValue(j) + F*(xParent0.GetValue(j) -
                                                                     xParent1.GetValue(j));

                            if (value < xChild.GetLowerBound(j))
                            {
                                value = xChild.GetLowerBound(j);
                            }
                            if (value > xChild.GetUpperBound(j))
                            {
                                value = xChild.GetUpperBound(j);
                            }

                            xChild.SetValue(j, value);
                        }
                        else
                        {
                            Cr = 0.0;
                            double value = xCurrent.GetValue(j);
                            xChild.SetValue(j, value);
                        }
                    }

                    break;

                case "current-to-rand/1":
                case "current-to-best/1":

                    for (var j = 0; j < numberOfVariables; j++)
                    {
                        if (PseudoRandom.Instance().NextDouble() < Cr || j == jrand)
                        {
                            double value = xCurrent.GetValue(j) + K*(xParent2.GetValue(j) -
                                                                     xCurrent.GetValue(j)) +
                                           F*(xParent0.GetValue(j) - xParent1.GetValue(j));

                            if (value < xChild.GetLowerBound(j))
                            {
                                value = xChild.GetLowerBound(j);
                            }
                            if (value > xChild.GetUpperBound(j))
                            {
                                value = xChild.GetUpperBound(j);
                            }

                            xChild.SetValue(j, value);
                        }
                    }

                    break;

                case "current-to-rand/1/bin":
                case "current-to-best/1/bin":

                    for (var j = 0; j < numberOfVariables; j++)
                    {
                        if (PseudoRandom.Instance().NextDouble() < Cr || j == jrand)
                        {
                            double value = xCurrent.GetValue(j) + K*(xParent2.GetValue(j) -
                                                                     xCurrent.GetValue(j)) +
                                           F*(xParent0.GetValue(j) - xParent1.GetValue(j));

                            if (value < xChild.GetLowerBound(j))
                            {
                                value = xChild.GetLowerBound(j);
                            }
                            if (value > xChild.GetUpperBound(j))
                            {
                                value = xChild.GetUpperBound(j);
                            }

                            xChild.SetValue(j, value);
                        }
                        else
                        {
                            Cr = 0.0;
                            double value = xCurrent.GetValue(j);
                            xChild.SetValue(j, value);
                        }
                    }

                    break;

                default:
                    throw new Exception("Unknown DE variant (" + DeVariant + ")");
            }

            return child;
        }
Пример #6
0
        public Solution[] DoCrossover(double probability,
                                      Solution parent1,
                                      Solution parent2)
        {
            Solution[] offSpring = new Solution[2];

            offSpring[0] = new Solution(parent1);
            offSpring[1] = new Solution(parent2);

            XReal x1 = new XReal(parent1);
            XReal x2 = new XReal(parent2);
            XReal offs1 = new XReal(offSpring[0]);
            XReal offs2 = new XReal(offSpring[1]);

            int numberOfVariables = x1.GetNumberOfDecisionVariables();

            if (PseudoRandom.Instance().NextDouble() <= probability)
            {
                int i;
                for (i = 0; i < numberOfVariables; i++)
                {
                    double upperValue = x1.GetUpperBound(i);
                    double lowerValue = x1.GetLowerBound(i);
                    double valueX1 = x1.GetValue(i);
                    double valueX2 = x2.GetValue(i);

                    double max;
                    double min;

                    if (valueX2 > valueX1)
                    {
                        max = valueX2;
                        min = valueX1;
                    }
                    else
                    {
                        max = valueX1;
                        min = valueX2;
                    }

                    double range = max - min;
                    // Ranges of the new alleles ;

                    double minRange = min - range*_alpha;
                    double maxRange = max + range*_alpha;

                    double random = PseudoRandom.Instance().NextDouble();
                    double valueY1 = minRange + random*(maxRange - minRange);

                    random = PseudoRandom.Instance().NextDouble();
                    double valueY2 = minRange + random*(maxRange - minRange);

                    if (valueY1 < lowerValue)
                    {
                        offs1.SetValue(i, lowerValue);
                    }
                    else if (valueY1 > upperValue)
                    {
                        offs1.SetValue(i, upperValue);
                    }
                    else
                    {
                        offs1.SetValue(i, valueY1);
                    }

                    if (valueY2 < lowerValue)
                    {
                        offs2.SetValue(i, lowerValue);
                    }
                    else if (valueY2 > upperValue)
                    {
                        offs2.SetValue(i, upperValue);
                    }
                    else
                    {
                        offs2.SetValue(i, valueY2);
                    }
                }
            }

            return offSpring;
        }
Пример #7
0
        /** Returns the distance between two solutions in the search space.
           *  @param solutionI The first <code>Solution</code>.
           *  @param solutionJ The second <code>Solution</code>.
           *  @return the distance between solutions.
           * @throws JMException
           */
        public static double DistanceBetweenSolutions(Solution solutionI, Solution solutionJ)
        {
            double distance = 0.0;
            XReal solI = new XReal(solutionI);
            XReal solJ = new XReal(solutionJ);

            double diff; //Auxiliar var
            //-> Calculate the Euclidean distance
            for (int i = 0; i < solI.GetNumberOfDecisionVariables(); i++)
            {
                diff = solI.GetValue(i) - solJ.GetValue(i);
                distance += Math.Pow(diff, 2.0);
            } // for
            //-> Return the euclidean distance
            return Math.Sqrt(distance);
        }
Пример #8
0
        private Solution[] DoCrossover(double probability, Solution parent1, Solution parent2)
        {
            var offSpring = new Solution[2];

            offSpring[0] = new Solution(parent1);
            offSpring[1] = new Solution(parent2);

            int i;
            double rand;
            double y1, y2, yL, yu;
            double c1, c2;
            double alpha, beta, betaq;
            double valueX1, valueX2;
            XReal x1 = new XReal(parent1);
            XReal x2 = new XReal(parent2);
            XReal offs1 = new XReal(offSpring[0]);
            XReal offs2 = new XReal(offSpring[1]);

            int numberOfVariables = x1.GetNumberOfDecisionVariables();

            if (PseudoRandom.Instance().NextDouble() <= probability)
            {
                for (i = 0; i < numberOfVariables; i++)
                {
                    valueX1 = x1.GetValue(i);
                    valueX2 = x2.GetValue(i);
                    if (PseudoRandom.Instance().NextDouble() <= 0.5)
                    {
                        if (Math.Abs(valueX1 - valueX2) > Eps)
                        {
                            if (valueX1 < valueX2)
                            {
                                y1 = valueX1;
                                y2 = valueX2;
                            }
                            else
                            {
                                y1 = valueX2;
                                y2 = valueX1;
                            } // if

                            yL = x1.GetLowerBound(i);
                            yu = x1.GetUpperBound(i);
                            rand = PseudoRandom.Instance().NextDouble();
                            beta = 1.0 + (2.0*(y1 - yL)/(y2 - y1));
                            alpha = 2.0 - Math.Pow(beta, -(_distributionIndex + 1.0));

                            if (rand <= (1.0/alpha))
                            {
                                betaq = Math.Pow((rand*alpha), (1.0/(_distributionIndex + 1.0)));
                            }
                            else
                            {
                                betaq = Math.Pow((1.0/(2.0 - rand*alpha)), (1.0/(_distributionIndex + 1.0)));
                            } // if

                            c1 = 0.5*((y1 + y2) - betaq*(y2 - y1));
                            beta = 1.0 + (2.0*(yu - y2)/(y2 - y1));
                            alpha = 2.0 - Math.Pow(beta, -(_distributionIndex + 1.0));

                            if (rand <= (1.0/alpha))
                            {
                                betaq = Math.Pow((rand*alpha), (1.0/(_distributionIndex + 1.0)));
                            }
                            else
                            {
                                betaq = Math.Pow((1.0/(2.0 - rand*alpha)), (1.0/(_distributionIndex + 1.0)));
                            } // if

                            c2 = 0.5*((y1 + y2) + betaq*(y2 - y1));

                            if (c1 < yL)
                            {
                                c1 = yL;
                            }

                            if (c2 < yL)
                            {
                                c2 = yL;
                            }

                            if (c1 > yu)
                            {
                                c1 = yu;
                            }

                            if (c2 > yu)
                            {
                                c2 = yu;
                            }

                            if (PseudoRandom.Instance().NextDouble() <= 0.5)
                            {
                                offs1.SetValue(i, c2);
                                offs2.SetValue(i, c1);
                            }
                            else
                            {
                                offs1.SetValue(i, c1);
                                offs2.SetValue(i, c2);
                            } // if
                        }
                        else
                        {
                            offs1.SetValue(i, valueX1);
                            offs2.SetValue(i, valueX2);
                        } // if
                    }
                    else
                    {
                        offs1.SetValue(i, valueX2);
                        offs2.SetValue(i, valueX1);
                    } // if
                } // if
            } // if

            return offSpring;
        }