/// <summary> /// Perform the mutation operation /// </summary> /// <param name="probability">Mutation probability</param> /// <param name="solution">The solution to mutate</param> private void DoMutation(double probability, Solution solution) { XReal x = new XReal(solution); for (int var = 0; var < solution.Variable.Length; var++) { if (JMetalRandom.NextDouble() < probability) { double rand = JMetalRandom.NextDouble(); double tmp; if (rand <= 0.5) { tmp = Delta(x.GetUpperBound(var) - x.GetValue(var), perturbation.Value); tmp += x.GetValue(var); } else { tmp = Delta(x.GetLowerBound(var) - x.GetValue(var), perturbation.Value); 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); } } }
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> /// DoMutation method /// </summary> /// <param name="realProbability"></param> /// <param name="binaryProbability"></param> /// <param name="solution"></param> private void DoMutation(double realProbability, double binaryProbability, Solution solution) { double rnd, delta1, delta2, mut_pow, deltaq; double y, yl, yu, val, xy; XReal x = new XReal(solution); Binary binaryVariable = (Binary)solution.Variable[1]; // Polynomial mutation applied to the array real for (int var = 0; var < x.Size(); var++) { if (JMetalRandom.NextDouble() <= realProbability) { y = x.GetValue(var); yl = x.GetLowerBound(var); yu = x.GetUpperBound(var); delta1 = (y - yl) / (yu - yl); delta2 = (yu - y) / (yu - yl); rnd = JMetalRandom.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, (distributionIndex + 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, (distributionIndex + 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.SetValue(var, y); } } // BitFlip mutation applied to the binary part for (int i = 0; i < binaryVariable.NumberOfBits; i++) { if (JMetalRandom.NextDouble() < binaryProbability) { binaryVariable.Bits.Flip(i); } } }
/// <summary> /// Perform the mutation operation /// </summary> /// <param name="probability">Mutation probability</param> /// <param name="solution">The solution to mutate</param> private void DoMutation(Solution solution) { double rnd, delta1, delta2, mut_pow, deltaq; double y, yl, yu, val, xy; mutationProbability = mutationProbability - solution.NumberofReplace * 0.001; if (mutationProbability <= 0.1) { mutationProbability = 0.1; } XReal x = new XReal(solution); for (int var = 0; var < solution.NumberOfVariables(); var++) { if (JMetalRandom.NextDouble() <= mutationProbability) { y = x.GetValue(var); yl = x.GetLowerBound(var); yu = x.GetUpperBound(var); delta1 = (y - yl) / (yu - yl); delta2 = (yu - y) / (yu - yl); rnd = JMetalRandom.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, (distributionIndex + 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, (distributionIndex + 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.SetValue(var, y); } } }
/// <summary> /// Perform the mutation operation /// </summary> /// <param name="probability">Mutation probability</param> /// <param name="solution">The solution to mutate</param> private void DoMutation(double probability, Solution solution) { XReal v = new XReal(solution); try { if ((solution.Type.GetType() == typeof(BinarySolutionType)) || (solution.Type.GetType() == typeof(BinaryRealSolutionType))) { for (int i = 0; i < solution.Variable.Length; i++) { for (int j = 0; j < ((Binary)solution.Variable[i]).NumberOfBits; j++) { if (JMetalRandom.NextDouble() < probability) { ((Binary)solution.Variable[i]).Bits.Flip(j); } } } for (int i = 0; i < solution.Variable.Length; i++) { ((Binary)solution.Variable[i]).Decode(); } } else { // Integer representation for (int i = 0; i < solution.Variable.Length; i++) { if (JMetalRandom.NextDouble() < probability) { int value = JMetalRandom.Next( (int)v.GetLowerBound(i), (int)v.GetUpperBound(i)); v.SetValue(i, value); } } } } catch (Exception ex) { Logger.Log.Error("Error in " + this.GetType().FullName + ".DoMutation()", ex); Console.WriteLine("Error in " + this.GetType().FullName + ".DoMutation()"); throw new Exception("Exception in " + this.GetType().FullName + ".DoMutation()"); } }
/// <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); }