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; } } }
public override void evaluate(Solution solution) { // <pex> if (solution == (Solution)null) throw new ArgumentNullException("solution"); if (solution.variable_ == (Variable[])null) throw new ArgumentNullException("solution"); // </pex> Variable[] x = solution.variable_; double[] fx = new double[numberOfObjectives_]; double sum1 = 0.0; sum1 = Math.Sqrt(Math.Pow((x[0].value_ -2),2) + Math.Pow((x[1].value_ -3) ,2)) ; fx[0] = sum1; //for (int var = 0; var < numberOfVariables_; var++) //{ // sum1 += Math.Pow(x[var].value_ // - (1.0 / Math.Sqrt((double)numberOfVariables_)), 2.0); //} //double exp1 = Math.Exp((-1.0) * sum1); //fx[0] = 1 - exp1; //double sum2 = 0.0; //sum2 = (x[0].value_ + x[1].value_ + x[2].value_)*(-2); //fx[1] = sum2; //double sum2 = 0.0; //for (int var = 0; var < numberOfVariables_; var++) //{ // sum2 += Math.Pow(x[var].value_ // + (1.0 / Math.Sqrt((double)numberOfVariables_)), 2.0); //} //double exp2 = Math.Exp((-1.0) * sum2); //fx[1] = 1 - exp2; solution.objective_[0] = fx[0]; //solution.objective_[1] = fx[1]; }
/// <summary> /// Generated new the children population list based on the parents population list. /// This node needs to be used in a loop along with Assign Fitness Function node and /// Sorting node. /// </summary> /// <param name="populationList">Parents population list.</param> /// <param name="lowerLimits">List of lower limits for new generation.</param> /// <param name="upperLimits">List of upper limits for new generation.</param> /// <returns>Generated population list.</returns> public static List<List<double>> GenerationAlgorithm(List<List<double>> populationList, List<double> lowerLimits, List<double> upperLimits) { int numVar = lowerLimits.Count; int numObj = populationList.Count - numVar; //create the NSGA-II algorithm Algorithm algorithm = CreateAlgorithm(numObj, lowerLimits, upperLimits); //change solutions list to solutionSet SolutionSet population = SolutionListToPop(populationList, algorithm, numVar); Operator mutationOperator = algorithm.operators["mutation"]; Operator crossoverOperator = algorithm.operators["crossover"]; Operator selectionOperator = algorithm.operators["selection"]; int populationSize = population.size(); SolutionSet offspringPopulation = new SolutionSet(populationSize); Solution[] parents = new Solution[2]; for (int i = 0; i < (populationSize / 2); i++) { // selection parents[0] = (Solution)selectionOperator.execute(population); parents[1] = (Solution)selectionOperator.execute(population); // crossover Solution[] offSpring = (Solution[])crossoverOperator.execute(parents); // mutation mutationOperator.execute(offSpring[0]); mutationOperator.execute(offSpring[1]); offspringPopulation.@add(offSpring[0]); offspringPopulation.add(offSpring[1]); } return PopToSolutionList(offspringPopulation); }
/// <summary> /// Creates the initial solution lists of variables and objectives. /// The objectives will be assigned after evaluation using /// NSGA_II Assigne Fitness Function Results node. /// </summary> /// <param name="populationSize">Size that will be used to generate population.</param> /// <param name="numObjectives">Number of objectives that you are trying to optimize. If your problem is single objective optimization, set this number to one.</param> /// <param name="lowerLimits">List of lower limits for all variable. The number of lower limits should be the same as the number of variables.</param> /// <param name="upperLimits">List of upper limits for all variable. The number of upper limits should be the same as the number of variables.</param> /// <returns>An initial solution list.</returns> public static List<List<double>> InitialSolutionList(double populationSize, double numObjectives, List<double> lowerLimits, List<double> upperLimits) { //number of varables plus number of objectives int numObj = Convert.ToInt32(numObjectives); int numVar = lowerLimits.Count; Algorithm algorithm = CreateAlgorithm(numObjectives, lowerLimits, upperLimits); //Initializing variables SolutionSet population = new SolutionSet(Convert.ToInt32(populationSize)); // Creating the initial solutionSet Solution newSolution; for (int i = 0; i < populationSize; i++) { newSolution = new Solution(algorithm.problem_); population.add(newSolution); } List<List<double>> test = PopToSolutionList(population); return PopToSolutionList(population); }
//changes solution list to solution set private static SolutionSet SolutionListToPop(List<List<double>> populationList, Algorithm algorithm, int numVar) { int popSize = populationList[0].Count; int numObj = populationList.Count - numVar; SolutionSet population = new SolutionSet(popSize); Solution newSolution; for (int j = 0; j < popSize; j++) { newSolution = new Solution(algorithm.problem_); for (int i = 0; i < numVar; i++) { newSolution.variable_[i].value_ = populationList[i][j]; } population.add(newSolution); } for (int j = 0; j < population.size(); j++) { for (int i = 0; i < numObj; i++) { population[j].objective_[i] = populationList[i + numVar][j]; } } return population; }
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; }
// union public void replace(int position, Solution solution) { if (position > _solutionsList.Count) { _solutionsList.Add(solution); } // if _solutionsList.RemoveAt(position); _solutionsList.Insert(position,solution); } // replace
// SolutionSet /// <summary> /// Adds a new solution into the solution set /// </summary> /// <param name="solution"> /// A <see cref="Solution"/> /// </param> /// <returns> /// A <see cref="System.Boolean"/> /// </returns> public bool add (Solution solution) { if (_solutionsList.Count == capacity_) { return false; // if } else { _solutionsList.Add (solution); return true; } // else }
// NSGAII public override SolutionSet execute() { int populationSize; int maxEvaluations; int evaluations; SolutionSet population; SolutionSet offspringPopulation; SolutionSet union; SolutionSet allTest = new SolutionSet(); Operator mutationOperator; Operator crossoverOperator; Operator selectionOperator; populationSize = (int)inputParameters_["populationSize"]; maxEvaluations = (int)inputParameters_["maxEvaluations"]; // Initializing variables population = new SolutionSet(populationSize); evaluations = 0; //System.Console.WriteLine("Solves Name:" + problem_.problemName_); //System.Console.WriteLine("Pop size : " + populationSize); //System.Console.WriteLine("Max Evals: " + maxEvaluations); // Reading operators mutationOperator = operators["mutation"]; crossoverOperator = operators["crossover"]; selectionOperator = operators["selection"]; //System.Console.WriteLine("Crossover parameters: " + crossoverOperator); //System.Console.WriteLine("Mutation parameters: " + mutationOperator); // Creating the initial solutionSet Solution newSolution; for (int i = 0; i < populationSize; i++) { newSolution = new Solution(problem_); //newSolution.variable_[0] = 1; problem_.evaluate(newSolution); //newSolution.objective_[0] = 10; //Mohammad //slnLst.Add(newSolution); //System.Console.WriteLine ("" + i + ": " + newSolution); //problem_.evaluateConstraints(newSolution); evaluations++; population.add(newSolution); } //for // Main loop while (evaluations < maxEvaluations) { // Creating the offSpring solutionSet offspringPopulation = new SolutionSet(populationSize); Solution[] parents = new Solution[2]; for (int i = 0; i < (populationSize / 2); i++) { if (evaluations < maxEvaluations) { //if ((evaluations % 1000) == 0) // Console.WriteLine("Evals: " + evaluations); // selection parents[0] = (Solution)selectionOperator.execute(population); parents[1] = (Solution)selectionOperator.execute(population); // crossover Solution[] offSpring = (Solution[])crossoverOperator.execute(parents); // mutation mutationOperator.execute(offSpring[0]); mutationOperator.execute(offSpring[1]); //Environment.Exit(0); // evaluation problem_.evaluate(offSpring[0]); problem_.evaluate(offSpring[1]); offspringPopulation.@add(offSpring[0]); offspringPopulation.add(offSpring[1]); evaluations += 2; } // if } // for // Creating the solutionSet union of solutionSet and offSpring union = ((SolutionSet)population).union(offspringPopulation); //Mohammad allTest = ((SolutionSet)allTest).union(union); //System.Console.WriteLine ("Union size:" + union.size ()); // Ranking the union Ranking ranking = new Ranking(union); int remain = populationSize; int index = 0; SolutionSet front = null; //Distance distance = new Distance (); population.clear(); // Obtain the next front front = ranking.getSubfront(index); //* while ((remain > 0) && (remain >= front.size())) { //Assign crowding distance to individuals Distance.crowdingDistanceAssignment(front, problem_.numberOfObjectives_); //Add the individuals of this front for (int k = 0; k < front.size(); k++) { population.@add(front[k]); } // for //Decrement remain remain = remain - front.size(); //Obtain the next front index++; if (remain > 0) { front = ranking.getSubfront(index); } // if } // while // Remain is less than front(index).size, insert only the best one if (remain > 0) { // front contains individuals to insert Distance.crowdingDistanceAssignment(front, problem_.numberOfObjectives_); IComparer comp = new CrowdingDistanceComparator(); front.solutionList_.Sort(comp.Compare); for (int k = 0; k < remain; k++) { population.@add(front[k]); } // for remain = 0; } // if } // while // Return as output parameter the required evaluations outputParameters_["evaluations"] = evaluations; // Return the first non-dominated front Ranking ranking2 = new Ranking(population); //Console.WriteLine(slnLst[0].objective_.GetValue(0)); //File.WriteAllLines("C:/text.txt",slnLst.ConvertAll(Convert.ToString)); return ranking2.getSubfront(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; }
// Solution /// <summary> /// Copy constructor /// </summary> /// <param name="solution"> /// A <see cref="Solution"/> /// </param> public Solution (Solution solution) { // <pex> if (solution == (Solution)null) throw new ArgumentNullException("solution"); if (solution.type_ == (SolutionType)null) throw new ArgumentNullException("solution"); // </pex> problem_ = solution.problem_; type_ = solution.type_; numberOfObjectives_ = solution.numberOfObjectives_; objective_ = new double[numberOfObjectives_]; for (int i = 0; i < numberOfObjectives_; i++) { objective_[i] = solution.objective_[i]; } // for //<- variable_ = type_.copyVariables (solution.variable_); overallConstraintViolation_ = solution.overallConstraintViolation_; numberOfViolatedConstraints_ = solution.numberOfViolatedConstraints_; //distanceToSolutionSet_ = solution.getDistanceToSolutionSet(); crowdingDistance_ = solution.crowdingDistance_; //kDistance_ = solution.getKDistance(); //fitness_ = solution.getFitness(); //marked_ = solution.isMarked (); rank_ = solution.rank_; //location_ = solution.getLocation(); }
public virtual void evaluateConstraints (Solution solution) { }
// Solves public abstract void evaluate (Solution solution);