/// <summary> /// /// </summary> /// <param name="x"></param> /// <returns></returns> public double EvalG(XReal x) { double g = 0.0; for (int i = 1; i < x.GetNumberOfDecisionVariables(); i++) { g += x.GetValue(i); } double constant = (9.0 / (NumberOfVariables - 1)); g = constant * g; g = g + 1.0; return(g); }
/// <summary> /// /Update the speed of each particle /// </summary> /// <param name="iter"></param> /// <param name="miter"></param> private void ComputeSpeed(int iter, int miter) { double r1, r2, W, C1, C2; double wmax, wmin; XReal bestGlobal; for (int i = 0; i < swarmSize; i++) { XReal particle = new XReal(particles.Get(i)); XReal bestParticle = new XReal(best[i]); //Select a global best for calculate the speed of particle i, bestGlobal Solution one, two; int pos1 = JMetalRandom.Next(0, Leaders.Size() - 1); int pos2 = JMetalRandom.Next(0, Leaders.Size() - 1); one = Leaders.Get(pos1); two = Leaders.Get(pos2); if (crowdingDistanceComparator.Compare(one, two) < 1) { bestGlobal = new XReal(one); } else { bestGlobal = new XReal(two); //Params for velocity equation } r1 = JMetalRandom.NextDouble(r1Min, r1Max); r2 = JMetalRandom.NextDouble(r2Min, r2Max); C1 = JMetalRandom.NextDouble(C1Min, C1Max); C2 = JMetalRandom.NextDouble(C2Min, C2Max); W = JMetalRandom.NextDouble(WMin, WMax); wmax = WMax; wmin = WMin; for (int var = 0; var < particle.GetNumberOfDecisionVariables(); var++) { //Computing the velocity of this particle speed[i][var] = VelocityConstriction(ConstrictionCoefficient(C1, C2) * (InertiaWeight(iter, miter, wmax, wmin) * speed[i][var] + C1 * r1 * (bestParticle.GetValue(var) - particle.GetValue(var)) + C2 * r2 * (bestGlobal.GetValue(var) - particle.GetValue(var))), deltaMax, deltaMin, var, i); } } }
/// <summary> /// Returns the value of the ZDT1 function G. /// </summary> /// <param name="x">Solution</param> /// <returns></returns> private double EvalG(XReal x) { double g = 0; double tmp; for (int i = 1; i < x.GetNumberOfDecisionVariables(); i++) { tmp = x.GetValue(i); g += tmp; } double constant = (9.0 / (NumberOfVariables - 1)); g = constant * g; g = g + 1; return(g); }
/// <summary> /// Returns the distance between two solutions in the search space. /// </summary> /// <param name="solutionI">The first <code>Solution</code>.</param> /// <param name="solutionJ">The second <code>Solution</code>.</param> /// <returns>The distance between solutions.</returns> public 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); } //-> Return the euclidean distance return(Math.Sqrt(distance)); }
private Solution DoMutation(double probability, Solution parent) { Solution current = new Solution(parent); Solution child; child = new Solution(current); XReal xCurrent = new XReal(current); XReal xChild = new XReal(child); int numberOfVariables = xCurrent.GetNumberOfDecisionVariables(); randStdNormal = new double[numberOfVariables]; for (int j = 0; j < numberOfVariables; j++) { double value; // value = xParent2.GetValue(j) + f * (xParent0.GetValue(j) - xParent1.GetValue(j)); double u1 = JMetalRandom.NextDouble(0, 1); double u2 = JMetalRandom.NextDouble(0, 1); if (JMetalRandom.NextDouble() <= probability) { randStdNormal[j] = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2); //random normal(0,1) value = xCurrent.GetValue(j) + zeta * xCurrent.GetStdDev(j) * randStdNormal[j]; if (value < xChild.GetLowerBound(j)) { value = xChild.GetLowerBound(j); //value = JMetalRandom.NextDouble(xChild.GetLowerBound(j), xChild.GetUpperBound(j)); } if (value > xChild.GetUpperBound(j)) { value = xChild.GetUpperBound(j); //value = JMetalRandom.NextDouble(xChild.GetLowerBound(j), xChild.GetUpperBound(j)); } xChild.SetValue(j, value); } } return(child); }
/// <summary> /// Update the position of each particle /// </summary> private void ComputeNewPositions() { for (int i = 0; i < swarmSize; i++) { XReal particle = new XReal(particles.Get(i)); for (int var = 0; var < particle.GetNumberOfDecisionVariables(); var++) { particle.SetValue(var, particle.GetValue(var) + speed[i][var]); if (particle.GetValue(var) < this.Problem.LowerLimit[var]) { particle.SetValue(var, this.Problem.LowerLimit[var]); speed[i][var] = speed[i][var] * ChVel1; } if (particle.GetValue(var) > this.Problem.UpperLimit[var]) { particle.SetValue(var, this.Problem.UpperLimit[var]); speed[i][var] = speed[i][var] * ChVel2; } } } }
/// <summary> /// Perform the crossover operation. /// </summary> /// <param name="probability">Crossover probability</param> /// <param name="parent1">The first parent</param> /// <param name="parent2">The second parent</param> /// <returns>An array containing the two offsprings</returns> private Solution[] DoCrossover(double probability, 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; 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 (JMetalRandom.NextDouble() <= probability) { for (i = 0; i < numberOfVariables; i++) { valueX1 = x1.GetValue(i); valueX2 = x2.GetValue(i); if (JMetalRandom.NextDouble() <= 0.5) { if (Math.Abs(valueX1 - valueX2) > EPS) { if (valueX1 < valueX2) { y1 = valueX1; y2 = valueX2; } else { y1 = valueX2; y2 = valueX1; } yL = x1.GetLowerBound(i); yu = x1.GetUpperBound(i); rand = JMetalRandom.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))); } 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))); } 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 (JMetalRandom.NextDouble() <= 0.5) { offs1.SetValue(i, c2); offs2.SetValue(i, c1); } else { offs1.SetValue(i, c1); offs2.SetValue(i, c2); } } else { offs1.SetValue(i, valueX1); offs2.SetValue(i, valueX2); } } else { offs1.SetValue(i, valueX2); offs2.SetValue(i, valueX1); } } } return(offSpring); }
private IntergenSolution[] DoCrossover(double probability, IntergenSolution parent1, IntergenSolution parent2) { IntergenSolution[] offSpring = new IntergenSolution[2]; offSpring[0] = new IntergenSolution(parent1); offSpring[1] = new IntergenSolution(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 (JMetalRandom.NextDouble() <= probability) { for (i = 0; i < numberOfVariables; i++) { valueX1 = x1.GetValue(i); valueX2 = x2.GetValue(i); if (JMetalRandom.NextDouble() <= 0.5) { if (Math.Abs(valueX1 - valueX2) > EPS) { if (valueX1 < valueX2) { y1 = valueX1; y2 = valueX2; } else { y1 = valueX2; y2 = valueX1; } yL = x1.GetLowerBound(i); yu = x1.GetUpperBound(i); rand = JMetalRandom.NextDouble(); beta = 1.0 + (2.0 * (y1 - yL) / (y2 - y1)); //Console.WriteLine("Beta1" + beta); alpha = 2.0 - Math.Pow(beta, -(distributionIndex + 1.0)); if (rand <= (1.0 / alpha)) { betaq = Math.Pow((rand * alpha), (1.0 / (distributionIndex + 1.0))); /* if (double.IsNaN(betaq)) * { * Console.WriteLine("NAN BETAq"); * } */ } else { betaq = Math.Pow((1.0 / (2.0 - rand * alpha)), (1.0 / (distributionIndex + 1.0))); /* var betax = Math.Pow((1.0 / (2.0 - rand * alpha)), (1.0 / (distributionIndex + 1.0))); * if (double.IsNaN(betax)) * { * Console.WriteLine("NAN BETAx"); * } * if (double.IsNaN(betaq)) * { * Console.WriteLine("NAN BETAq"); * } */ } /* * if (double.IsNaN(betaq)) * { * Console.WriteLine("NAN BETAq"); * } */ 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))); /*if (double.IsNaN(betaq)) * { * Console.WriteLine("NAN BETAq"); * } */ } else { betaq = Math.Pow((1.0 / (2.0 - rand * alpha)), (1.0 / (distributionIndex + 1.0))); /*if (double.IsNaN(betaq)) * { * Console.WriteLine("NAN BETAq"); * }*/ } 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 (double.IsNaN(c2) || double.IsNaN(c1)) * { * Console.WriteLine("Setting NAN"); * } */ if (JMetalRandom.NextDouble() <= 0.5) { offs1.SetValue(i, c2); offs2.SetValue(i, c1); } else { offs1.SetValue(i, c1); offs2.SetValue(i, c2); } } else { offs1.SetValue(i, valueX1); offs2.SetValue(i, valueX2); } } else { offs1.SetValue(i, valueX2); offs2.SetValue(i, valueX1); } } } /* * var o1 = offSpring[0]; * XReal values = new XReal(o1); * for (int u = 0; u < values.Size(); u++) * { * * if (double.IsNaN(values.GetValue(u))) * { * * Console.WriteLine("crossover NAN1"); * } * } * * var s = offSpring[1]; * XReal values2 = new XReal(s); * for (int u = 0; u < values2.Size(); u++) * { * if (double.IsNaN(values2.GetValue(u))) * { * Console.WriteLine("crossover NAN2"); * } * } */ return(offSpring); }
/// <summary> /// Executes the operation /// </summary> /// <param name="obj">An object containing an array of three parents</param> /// <returns>An object containing the offSprings</returns> public override object Execute(object obj) { object[] parameters = (object[])obj; Solution current = (Solution)parameters[0]; Solution[] parent = (Solution[])parameters[1]; Solution child; if (!(VALID_TYPES.Contains(parent[0].Type.GetType()) && VALID_TYPES.Contains(parent[1].Type.GetType()) && VALID_TYPES.Contains(parent[2].Type.GetType()))) { Logger.Log.Error("Exception in " + this.GetType().FullName + ".Execute()"); throw new Exception("Exception in " + this.GetType().FullName + ".Execute()"); } int jrand; 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); int numberOfVariables = xParent0.GetNumberOfDecisionVariables(); jrand = JMetalRandom.Next(0, numberOfVariables - 1); // STEP 4. Checking the DE variant if ((deVariant == "rand/1/bin") || (deVariant == "best/1/bin")) { for (int j = 0; j < numberOfVariables; j++) { if (JMetalRandom.NextDouble(0, 1) < cr || j == jrand) { double value; 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; value = xCurrent.GetValue(j); xChild.SetValue(j, value); } } } else if ((deVariant == "rand/1/exp") || (deVariant == "best/1/exp")) { for (int j = 0; j < numberOfVariables; j++) { if (JMetalRandom.NextDouble(0, 1) < cr || j == jrand) { double value; 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; double value; value = xCurrent.GetValue(j); xChild.SetValue(j, value); } } } else if ((deVariant == "current-to-rand/1") || (deVariant == "current-to-best/1")) { for (int j = 0; j < numberOfVariables; j++) { double value; 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 if ((deVariant == "current-to-rand/1/bin") || (deVariant == "current-to-best/1/bin")) { for (int j = 0; j < numberOfVariables; j++) { if (JMetalRandom.NextDouble(0, 1) < cr || j == jrand) { double value; 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 { double value; value = xCurrent.GetValue(j); xChild.SetValue(j, value); } } } else if ((deVariant == "current-to-rand/1/exp") || (deVariant == "current-to-best/1/exp")) { for (int j = 0; j < numberOfVariables; j++) { if (JMetalRandom.NextDouble(0, 1) < cr || j == jrand) { double value; 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; value = xCurrent.GetValue(j); xChild.SetValue(j, value); } } } else { Logger.Log.Error("Exception in " + this.GetType().FullName + ".Execute()"); throw new Exception("Exception in " + this.GetType().FullName + ".Execute()"); } return(child); }
/// <summary> /// Perform the crossover operation. /// </summary> /// <param name="probability">Crossover probability</param> /// <param name="parent1">The first parent</param> /// <param name="parent2">The second parent</param> /// <returns></returns> private Solution[] DoCrossover(double?probability, Solution parent1, Solution parent2) { Solution[] offSpring = new Solution[2]; offSpring[0] = new Solution(parent1); offSpring[1] = new Solution(parent2); double rand; double valueY1; double valueY2; double valueX1; double valueX2; double upperValue; double lowerValue; XReal x1 = new XReal(parent1); XReal x2 = new XReal(parent2); XReal offs1 = new XReal(offSpring[0]); XReal offs2 = new XReal(offSpring[1]); int numberOfVaribales = x1.GetNumberOfDecisionVariables(); if (JMetalRandom.NextDouble() <= probability) { for (int i = 0; i < numberOfVaribales; i++) { double max; double min; double range; double minRange; double maxRange; upperValue = x1.GetUpperBound(i); lowerValue = x1.GetLowerBound(i); valueX1 = x1.GetValue(i); valueX2 = x2.GetValue(i); if (valueX2 > valueX1) { max = valueX2; min = valueX1; } else { max = valueX1; min = valueX2; } range = max - min; minRange = min - range * alpha; maxRange = max + range * alpha; rand = JMetalRandom.NextDouble(); valueY1 = minRange + rand * (maxRange - minRange); rand = JMetalRandom.NextDouble(); valueY2 = minRange + rand * (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); }