Ejemplo n.º 1
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="lowerBound">Lowerbound value</param>
        /// <param name="upperBound">Upperbound value</param>
        public Real(double lowerBound, double upperBound)
        {
            lowerBound_ = lowerBound;
            upperBound_ = upperBound;
            value_      = PseudoRandom.Instance().NextDouble() * (upperBound - lowerBound) + lowerBound;

            //Contract.Requires((value_ >= lowerBound_) && (value_ <= upperBound_));
        } //Real
Ejemplo n.º 2
0
        public override object execute(object obj)
        {
            // <pex>
            if (obj == (object)null)
            {
                throw new ArgumentNullException("obj");
            }
            if (obj != (object)null && !(obj is SolutionSet))
            {
                throw new ArgumentException("complex reason", "obj");
            }
            // </pex>

            SolutionSet solutionSet = (SolutionSet)obj;
            Solution    solution1;
            Solution    solution2;


            ///// OJO, FALTA CONTROLAR SI EL SOLUTION ESTA VACIO O TIENE UN SOLO ELEMENTO
            int sol1 = PseudoRandom.Instance().Next(0, solutionSet.size() - 1);
            int sol2 = PseudoRandom.Instance().Next(0, solutionSet.size() - 1);
            int sol3 = PseudoRandom.Instance().Next(0, solutionSet.size() - 1);

            solution1 = solutionSet[sol1];
            solution2 = solutionSet[sol2];

            while (sol1 == sol2)
            {
                sol2      = PseudoRandom.Instance().Next(0, solutionSet.size() - 1);
                solution2 = solutionSet[sol2];
            }

            int result = comparator_.Compare(solution1, solution2);

            if (result == -1)
            {
                return(solution1);
            }
            else if (result == 1)
            {
                return(solution2);
            }
            else
            {
                if (PseudoRandom.Instance().NextDouble() < 0.5)
                {
                    return(solution1);
                }
                else
                {
                    return(solution2);
                }
            }
        }
Ejemplo n.º 3
0
 public static PseudoRandom Instance()
 {
   if (PseudoRandom_ == null)
   {
     lock (mutex_)
     {
       if (PseudoRandom_ == null)
         PseudoRandom_ = new PseudoRandom();
     }
   }
   return PseudoRandom_;
 }
Ejemplo n.º 4
0
 public static PseudoRandom Instance()
 {
     if (PseudoRandom_ == null)
     {
         lock (mutex_)
         {
             if (PseudoRandom_ == null)
             {
                 PseudoRandom_ = new PseudoRandom();
             }
         }
     }
     return(PseudoRandom_);
 }
Ejemplo n.º 5
0
        static public void execute(int[] perm, int size)
        {
            // <pex>
            if (perm == (int[])null)
            {
                throw new ArgumentNullException("perm");
            }
            if (perm.Length < 2)
            {
                throw new ArgumentException("perm.Length < 2", "perm");
            }
            // </pex>
            int[]  index = new int[size];
            bool[] flag  = new bool[size];

            for (int n = 0; n < size; n++)
            {
                index[n] = n;
                flag[n]  = true;
            }

            int num = 0;

            while (num < size)
            {
                int start = PseudoRandom.Instance().Next(0, size - 1);
                //int start = int(size*nd_uni(&rnd_uni_init));
                while (true)
                {
                    if (flag[start])
                    {
                        perm[num]   = index[start];
                        flag[start] = false;
                        num++;
                        break;
                    }
                    if (start == (size - 1))
                    {
                        start = 0;
                    }
                    else
                    {
                        start++;
                    }
                }
            }
            // while
        } // randomPermutation
Ejemplo n.º 6
0
        public override object execute(object obj)
        {
            Object[] parameters = (Object[])obj;
            Solution current    = (Solution)parameters[0];

            Solution[] parent = (Solution[])parameters[1];

            //if ((Array.Find (validTypes, n => n == parent[0].type_.GetType ()) == null) ||
            //    (Array.Find (validTypes, n => n == parent[1].type_.GetType ()) == null) ||
            //    (Array.Find (validTypes, n => n == parent[2].type_.GetType ()) == null))
            //  //Console.WriteLine ("DiffentialEvolution. Types are incompatible");
            //else
            //  ;
            //Console.WriteLine ("Differential evolution. Types are Compatible");

            int    numberOfVariables = current.numberOfVariables_;
            double jrand             = PseudoRandom.Instance().Next(numberOfVariables);

            Solution child = new Solution(current);

            if (DE_Variant_.CompareTo("rand/1/bin") == 0)
            {
                for (int j = 0; j < numberOfVariables; j++)
                {
                    if (PseudoRandom.Instance().NextDouble() < CR_ || j == jrand)
                    {
                        double val;
                        val = parent[2].variable_[j].value_ + F_ * (parent[0].variable_[j].value_ - parent[1].variable_[j].value_);
                        if (val < child.variable_[j].lowerBound_)
                        {
                            val = child.variable_[j].value_ = child.variable_[j].lowerBound_;
                        }
                        if (val > child.variable_[j].upperBound_)
                        {
                            val = child.variable_[j].value_ = child.variable_[j].upperBound_;
                        }

                        child.variable_[j].value_ = val;
                    }
                    else
                    {
                        child.variable_[j].value_ = current.variable_[j].value_;
                    } // else
                }     // for
            }         // if
            return(child);
        }
Ejemplo n.º 7
0
        } //Binary

        public Binary(int numberOfBits)
        {
            numberOfBits_ = numberOfBits;

            bits_ = new BitArray(numberOfBits_);
            for (int i = 0; i < numberOfBits_; i++)
            {
                if (PseudoRandom.Instance().NextDouble() < 0.5)
                {
                    bits_[i] = true;
                }
                else
                {
                    bits_[i] = false;
                }
            }
        } //Binary
Ejemplo n.º 8
0
        private void doMutation(Solution x)
        {
            double rnd, delta1, delta2, mut_pow, deltaq;
            double y, yl, yu, val, xy;
            double eta_m = distributionIndex_;

            for (int var = 0; var < x.numberOfVariables_; var++)
            {
                if (PseudoRandom.Instance().NextDouble() <= mutationProbability_)
                {
                    y       = x.variable_[var].value_;
                    yl      = x.variable_[var].lowerBound_;
                    yu      = x.variable_[var].upperBound_;
                    delta1  = (y - yl) / (yu - yl);
                    delta2  = (yu - y) / (yu - yl);
                    rnd     = PseudoRandom.Instance().NextDouble();
                    mut_pow = 1.0 / (eta_m + 1.0);
                    if (rnd <= 0.5)
                    {
                        xy     = 1.0 - delta1;
                        val    = 2.0 * rnd + (1.0 - 2.0 * rnd) * (Math.Pow(xy, (eta_m + 1.0)));
                        deltaq = Math.Pow(val, mut_pow) - 1.0;
                    }
                    else
                    {
                        xy     = 1.0 - delta2;
                        val    = 2.0 * (1.0 - rnd) + 2.0 * (rnd - 0.5) * (Math.Pow(xy, (eta_m + 1.0)));
                        deltaq = 1.0 - (Math.Pow(val, mut_pow));
                    }
                    y = y + deltaq * (yu - yl);
                    if (y < yl)
                    {
                        y = yl;
                    }
                    if (y > yu)
                    {
                        y = yu;
                    }
                    x.variable_[var].value_ = y;
                }
            }
        }
Ejemplo n.º 9
0
        Solution[] doCrossover(Solution parent1, Solution parent2)
        {
            Solution[] 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;
            Solution x1    = parent1;
            Solution x2    = parent2;
            Solution offs1 = offSpring[0];
            Solution offs2 = offSpring[1];


            int numberOfVariables = x1.numberOfVariables_;

            if (PseudoRandom.Instance().NextDouble() <= crossoverProbability_)
            {
                for (i = 0; i < numberOfVariables; i++)
                {
                    valueX1 = x1.variable_[i].value_;
                    valueX2 = x2.variable_[i].value_;
                    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.variable_[i].lowerBound_;
                            yu    = x1.variable_[i].upperBound_;
                            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.variable_[i].value_ = c2;
                                offs2.variable_[i].value_ = c1;
                            }
                            else
                            {
                                offs1.variable_[i].value_ = c1;
                                offs2.variable_[i].value_ = c2;
                            }
                            // if
                        }
                        else
                        {
                            offs1.variable_[i].value_ = valueX1;
                            offs2.variable_[i].value_ = valueX2;
                        }
                        // if
                    }
                    else
                    {
                        offs1.variable_[i].value_ = valueX2;
                        offs2.variable_[i].value_ = valueX1;
                    }
                    // if
                }
                // if
            }
            // if
            return(offSpring);
        }