Exemplo n.º 1
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;
 }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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;
        }
Exemplo n.º 4
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;
        }