/// <summary> /// Runs of the aMOCell2 algorithm. /// </summary> /// <returns>a <code>SolutionSet</code> that is a set of non dominated solutions /// as a result of the algorithm execution</returns> public override SolutionSet Execute() { //Init the param int populationSize = -1, archiveSize = -1, maxEvaluations = -1, evaluations = -1; Operator mutationOperator, crossoverOperator, selectionOperator; SolutionSet currentSolutionSet; CrowdingArchive archive; SolutionSet[] neighbors; Neighborhood neighborhood; IComparer <Solution> dominance = new DominanceComparator(), crowding = new CrowdingComparator(); Distance distance = new Distance(); //Read the params JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "populationSize", ref populationSize); JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "archiveSize", ref archiveSize); JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "maxEvaluations", ref maxEvaluations); //Read the operators mutationOperator = Operators["mutation"]; crossoverOperator = Operators["crossover"]; selectionOperator = Operators["selection"]; //Initialize the variables currentSolutionSet = new SolutionSet(populationSize); archive = new CrowdingArchive(archiveSize, this.Problem.NumberOfObjectives); evaluations = 0; neighborhood = new Neighborhood(populationSize); neighbors = new SolutionSet[populationSize]; //Create the initial population for (int i = 0; i < populationSize; i++) { Solution solution = new Solution(this.Problem); this.Problem.Evaluate(solution); this.Problem.EvaluateConstraints(solution); currentSolutionSet.Add(solution); solution.Location = i; evaluations++; } while (evaluations < maxEvaluations) { for (int ind = 0; ind < currentSolutionSet.Size(); ind++) { Solution individual = new Solution(currentSolutionSet.Get(ind)); Solution[] parents = new Solution[2]; Solution[] offSpring; neighbors[ind] = neighborhood.GetEightNeighbors(currentSolutionSet, ind); neighbors[ind].Add(individual); //parents parents[0] = (Solution)selectionOperator.Execute(neighbors[ind]); if (archive.Size() > 0) { parents[1] = (Solution)selectionOperator.Execute(archive); } else { parents[1] = (Solution)selectionOperator.Execute(neighbors[ind]); } //Create a new solution, using genetic operators mutation and crossover offSpring = (Solution[])crossoverOperator.Execute(parents); mutationOperator.Execute(offSpring[0]); //->Evaluate solution and constraints this.Problem.Evaluate(offSpring[0]); this.Problem.EvaluateConstraints(offSpring[0]); evaluations++; int flag = dominance.Compare(individual, offSpring[0]); if (flag == 1) { // OffSpring[0] dominates offSpring[0].Location = individual.Location; currentSolutionSet.Replace(offSpring[0].Location, offSpring[0]); archive.Add(new Solution(offSpring[0])); } else if (flag == 0) { //Both two are non-dominated neighbors[ind].Add(offSpring[0]); Ranking rank = new Ranking(neighbors[ind]); for (int j = 0; j < rank.GetNumberOfSubfronts(); j++) { distance.CrowdingDistanceAssignment(rank.GetSubfront(j), this.Problem.NumberOfObjectives); } bool deleteMutant = true; int compareResult = crowding.Compare(individual, offSpring[0]); if (compareResult == 1) { //The offSpring[0] is better deleteMutant = false; } if (!deleteMutant) { offSpring[0].Location = individual.Location; currentSolutionSet.Replace(offSpring[0].Location, offSpring[0]); archive.Add(new Solution(offSpring[0])); } else { archive.Add(new Solution(offSpring[0])); } } } } Result = archive; return(archive); }
public override SolutionSet Execute() { double contrDE = 0; double contrSBX = 0; double contrBLXA = 0; double contrPol = 0; double[] contrReal = new double[3]; contrReal[0] = contrReal[1] = contrReal[2] = 0; IComparer <Solution> dominance = new DominanceComparator(); IComparer <Solution> crowdingComparator = new CrowdingComparator(); Distance distance = new Distance(); Operator selectionOperator; //Read parameter values JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "maxEvaluations", ref maxEvaluations); JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "populationSize", ref populationSize); JMetalCSharp.Utils.Utils.GetIndicatorsFromParameters(this.InputParameters, "indicators", ref indicators); //Init the variables population = new SolutionSet(populationSize); evaluations = 0; selectionOperator = Operators["selection"]; Offspring[] getOffspring; int N_O; // number of offpring objects getOffspring = (Offspring[])GetInputParameter("offspringsCreators"); N_O = getOffspring.Length; contribution = new double[N_O]; contributionCounter = new int[N_O]; contribution[0] = (double)(populationSize / (double)N_O) / (double)populationSize; for (int i = 1; i < N_O; i++) { contribution[i] = (double)(populationSize / (double)N_O) / (double)populationSize + (double)contribution[i - 1]; } // Create the initial solutionSet Solution newSolution; for (int i = 0; i < populationSize; i++) { newSolution = new Solution(Problem); Problem.Evaluate(newSolution); Problem.EvaluateConstraints(newSolution); evaluations++; newSolution.Location = i; population.Add(newSolution); } while (evaluations < maxEvaluations) { // Create the offSpring solutionSet // Create the offSpring solutionSet offspringPopulation = new SolutionSet(2); Solution[] parents = new Solution[2]; int selectedSolution = JMetalRandom.Next(0, populationSize - 1); Solution individual = new Solution(population.Get(selectedSolution)); int selected = 0; bool found = false; Solution offSpring = null; double rnd = JMetalRandom.NextDouble(); for (selected = 0; selected < N_O; selected++) { if (!found && (rnd <= contribution[selected])) { if ("DE" == getOffspring[selected].Id) { offSpring = getOffspring[selected].GetOffspring(population, selectedSolution); contrDE++; } else if ("SBXCrossover" == getOffspring[selected].Id) { offSpring = getOffspring[selected].GetOffspring(population); contrSBX++; } else if ("BLXAlphaCrossover" == getOffspring[selected].Id) { offSpring = getOffspring[selected].GetOffspring(population); contrBLXA++; } else if ("PolynomialMutation" == getOffspring[selected].Id) { offSpring = ((PolynomialMutationOffspring)getOffspring[selected]).GetOffspring(individual); contrPol++; } else { Console.Error.WriteLine("Error in NSGAIIARandom. Operator " + offSpring + " does not exist"); Logger.Log.Error("NSGAIIARandom. Operator " + offSpring + " does not exist"); } offSpring.Fitness = (int)selected; currentCrossover = selected; found = true; } } Problem.Evaluate(offSpring); offspringPopulation.Add(offSpring); evaluations += 1; // Create the solutionSet union of solutionSet and offSpring union = ((SolutionSet)population).Union(offspringPopulation); // Ranking the union Ranking ranking = new Ranking(union); int remain = populationSize; int index = 0; SolutionSet front = null; 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.Get(k)); } //Decrement remain remain = remain - front.Size(); //Obtain the next front index++; if (remain > 0) { front = ranking.GetSubfront(index); } } // 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); front.Sort(new CrowdingComparator()); for (int k = 0; k < remain; k++) { population.Add(front.Get(k)); } remain = 0; } } // Return the first non-dominated front Ranking rank = new Ranking(population); Result = rank.GetSubfront(0); return(Result); }
/// <summary> /// Execute the algorithm /// </summary> /// <returns></returns> public override SolutionSet Execute() { int populationSize = -1, archiveSize = -1, maxEvaluations = -1, evaluations = -1, feedBack = -1; Operator mutationOperator, crossoverOperator, selectionOperator; SolutionSet currentPopulation; CrowdingArchive archive; SolutionSet[] neighbors; Neighborhood neighborhood; IComparer <Solution> dominance = new DominanceComparator(); IComparer <Solution> crowdingComparator = new CrowdingComparator(); Distance distance = new Distance(); //Read the parameters JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "populationSize", ref populationSize); JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "archiveSize", ref archiveSize); JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "maxEvaluations", ref maxEvaluations); JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "feedBack", ref feedBack); //Read the operators mutationOperator = Operators["mutation"]; crossoverOperator = Operators["crossover"]; selectionOperator = Operators["selection"]; //Initialize the variables //Initialize the population and the archive currentPopulation = new SolutionSet(populationSize); archive = new CrowdingArchive(archiveSize, this.Problem.NumberOfObjectives); evaluations = 0; neighborhood = new Neighborhood(populationSize); neighbors = new SolutionSet[populationSize]; //Create the comparator for check dominance dominance = new DominanceComparator(); //Create the initial population for (int i = 0; i < populationSize; i++) { Solution individual = new Solution(this.Problem); this.Problem.Evaluate(individual); this.Problem.EvaluateConstraints(individual); currentPopulation.Add(individual); individual.Location = i; evaluations++; } while (evaluations < maxEvaluations) { for (int ind = 0; ind < currentPopulation.Size(); ind++) { Solution individual = new Solution(currentPopulation.Get(ind)); Solution[] parents = new Solution[2]; Solution[] offSpring; neighbors[ind] = neighborhood.GetEightNeighbors(currentPopulation, ind); neighbors[ind].Add(individual); //parents parents[0] = (Solution)selectionOperator.Execute(neighbors[ind]); parents[1] = (Solution)selectionOperator.Execute(neighbors[ind]); //Create a new individual, using genetic operators mutation and crossover offSpring = (Solution[])crossoverOperator.Execute(parents); mutationOperator.Execute(offSpring[0]); //->Evaluate individual an his constraints this.Problem.Evaluate(offSpring[0]); this.Problem.EvaluateConstraints(offSpring[0]); evaluations++; //<-Individual evaluated int flag = dominance.Compare(individual, offSpring[0]); if (flag == 1) { //The new individuals dominate offSpring[0].Location = individual.Location; currentPopulation.Replace(offSpring[0].Location, offSpring[0]); archive.Add(new Solution(offSpring[0])); } else if (flag == 0) { //The individuals are non-dominates neighbors[ind].Add(offSpring[0]); offSpring[0].Location = -1; Ranking rank = new Ranking(neighbors[ind]); for (int j = 0; j < rank.GetNumberOfSubfronts(); j++) { distance.CrowdingDistanceAssignment(rank.GetSubfront(j), this.Problem.NumberOfObjectives); } Solution worst = neighbors[ind].Worst(crowdingComparator); if (worst.Location == -1) { //The worst is the offspring archive.Add(new Solution(offSpring[0])); } else { offSpring[0].Location = worst.Location; currentPopulation.Replace(offSpring[0].Location, offSpring[0]); archive.Add(new Solution(offSpring[0])); } } } //Store a portion of the archive into the population distance.CrowdingDistanceAssignment(archive, this.Problem.NumberOfObjectives); for (int j = 0; j < feedBack; j++) { if (archive.Size() > j) { int r = JMetalRandom.Next(0, currentPopulation.Size() - 1); if (r < currentPopulation.Size()) { Solution individual = archive.Get(j); individual.Location = r; currentPopulation.Replace(r, new Solution(individual)); } } } } Result = archive; return(archive); }
/// <summary> /// Runs of the Paes algorithm. /// </summary> /// <returns>a <code>SolutionSet</code> that is a set of non dominated solutions /// as a result of the algorithm execution </returns> public override SolutionSet Execute() { int bisections = -1, archiveSize = -1, maxEvaluations = -1, evaluations; AdaptiveGridArchive archive; Operator mutationOperator; IComparer <Solution> dominance; //Read the params JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "biSections", ref bisections); JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "archiveSize", ref archiveSize); JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "maxEvaluations", ref maxEvaluations); //Read the operators mutationOperator = this.Operators["mutation"]; //Initialize the variables evaluations = 0; archive = new AdaptiveGridArchive(archiveSize, bisections, this.Problem.NumberOfObjectives); dominance = new DominanceComparator(); //-> Create the initial solution and evaluate it and his constraints Solution solution = new Solution(this.Problem); this.Problem.Evaluate(solution); this.Problem.EvaluateConstraints(solution); evaluations++; // Add it to the archive archive.Add(new Solution(solution)); //Iterations.... do { // Create the mutate one Solution mutatedIndividual = new Solution(solution); mutationOperator.Execute(mutatedIndividual); this.Problem.Evaluate(mutatedIndividual); this.Problem.EvaluateConstraints(mutatedIndividual); evaluations++; // Check dominance int flag = dominance.Compare(solution, mutatedIndividual); if (flag == 1) { //If mutate solution dominate solution = new Solution(mutatedIndividual); archive.Add(mutatedIndividual); } else if (flag == 0) { //If none dominate the other if (archive.Add(mutatedIndividual)) { solution = Test(solution, mutatedIndividual, archive); } } } while (evaluations < maxEvaluations); Result = archive; return(archive); }
public override SolutionSet Execute() { double contrDE = 0; double contrSBX = 0; double contrPol = 0; double contrTotalDE = 0; double contrTotalSBX = 0; double contrTotalPol = 0; double[] contrReal = new double[3]; contrReal[0] = contrReal[1] = contrReal[2] = 0; IComparer <Solution> dominance = new DominanceComparator(); IComparer <Solution> crowdingComparator = new CrowdingComparator(); Distance distance = new Distance(); Operator selectionOperator; //Read parameter values JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "maxEvaluations", ref maxEvaluations); JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "populationSize", ref populationSize); //Init the variables population = new SolutionSet(populationSize); evaluations = 0; selectionOperator = Operators["selection"]; Offspring[] getOffspring; int N_O; // number of offpring objects getOffspring = ((Offspring[])GetInputParameter("offspringsCreators")); N_O = getOffspring.Length; contribution = new double[N_O]; contributionCounter = new int[N_O]; contribution[0] = (double)(populationSize / (double)N_O) / (double)populationSize; for (int i = 1; i < N_O; i++) { contribution[i] = (double)(populationSize / (double)N_O) / (double)populationSize + (double)contribution[i - 1]; } for (int i = 0; i < N_O; i++) { Console.WriteLine(getOffspring[i].Configuration()); Console.WriteLine("Contribution: " + contribution[i]); } // Create the initial solutionSet Solution newSolution; for (int i = 0; i < populationSize; i++) { newSolution = new Solution(Problem); Problem.Evaluate(newSolution); Problem.EvaluateConstraints(newSolution); evaluations++; newSolution.Location = i; population.Add(newSolution); } while (evaluations < maxEvaluations) { // Create the offSpring solutionSet offspringPopulation = new SolutionSet(populationSize); Solution[] parents = new Solution[2]; for (int i = 0; i < (populationSize / 1); i++) { if (evaluations < maxEvaluations) { Solution individual = new Solution(population.Get(JMetalRandom.Next(0, populationSize - 1))); int selected = 0; bool found = false; Solution offSpring = null; double rnd = JMetalRandom.NextDouble(); for (selected = 0; selected < N_O; selected++) { if (!found && (rnd <= contribution[selected])) { if ("DE" == getOffspring[selected].Id) { offSpring = getOffspring[selected].GetOffspring(population, i); contrDE++; } else if ("SBXCrossover" == getOffspring[selected].Id) { offSpring = getOffspring[selected].GetOffspring(population); contrSBX++; } else if ("PolynomialMutation" == getOffspring[selected].Id) { offSpring = ((PolynomialMutationOffspring)getOffspring[selected]).GetOffspring(individual); contrPol++; } else { Logger.Log.Error("Error in NSGAIIAdaptive. Operator " + offSpring + " does not exist"); Console.WriteLine("Error in NSGAIIAdaptive. Operator " + offSpring + " does not exist"); } offSpring.Fitness = (int)selected; found = true; } } Problem.Evaluate(offSpring); offspringPopulation.Add(offSpring); evaluations += 1; } } // Create the solutionSet union of solutionSet and offSpring union = ((SolutionSet)population).Union(offspringPopulation); // Ranking the union Ranking ranking = new Ranking(union); int remain = populationSize; int index = 0; SolutionSet front = null; 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.Get(k)); } //Decrement remain remain = remain - front.Size(); //Obtain the next front index++; if (remain > 0) { front = ranking.GetSubfront(index); } } // 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); front.Sort(new CrowdingComparator()); for (int k = 0; k < remain; k++) { population.Add(front.Get(k)); } remain = 0; } // CONTRIBUTION CALCULATING PHASE // First: reset contribution counter for (int i = 0; i < N_O; i++) { contributionCounter[i] = 0; } // Second: determine the contribution of each operator for (int i = 0; i < population.Size(); i++) { if ((int)population.Get(i).Fitness != -1) { contributionCounter[(int)population.Get(i).Fitness] += 1; } population.Get(i).Fitness = -1; } contrTotalDE += contributionCounter[0]; contrTotalSBX += contributionCounter[1]; contrTotalPol += contributionCounter[2]; int minimumContribution = 2; int totalContributionCounter = 0; for (int i = 0; i < N_O; i++) { if (contributionCounter[i] < minimumContribution) { contributionCounter[i] = minimumContribution; } totalContributionCounter += contributionCounter[i]; } if (totalContributionCounter == 0) { for (int i = 0; i < N_O; i++) { contributionCounter[i] = 10; } } // Third: calculating contribution contribution[0] = contributionCounter[0] * 1.0 / (double)totalContributionCounter; for (int i = 1; i < N_O; i++) { contribution[i] = contribution[i - 1] + 1.0 * contributionCounter[i] / (double)totalContributionCounter; } } // Return the first non-dominated front Ranking rank = new Ranking(population); Result = rank.GetSubfront(0); return(Result); }
/** * Runs of the GDE3 algorithm. * @return a <code>SolutionSet</code> that is a set of non dominated solutions * as a result of the algorithm execution * @throws JMException */ public override SolutionSet Execute() { int populationSize = -1; int maxIterations = -1; int evaluations; int iterations; SolutionSet population; SolutionSet offspringPopulation; Distance distance; IComparer <Solution> dominance; Operator selectionOperator; Operator crossoverOperator; distance = new Distance(); dominance = new DominanceComparator(); Solution[] parent; //Read the parameters JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "populationSize", ref populationSize); JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "maxIterations", ref maxIterations); selectionOperator = this.Operators["selection"]; crossoverOperator = this.Operators["crossover"]; //Initialize the variables population = new SolutionSet(populationSize); evaluations = 0; iterations = 0; // Create the initial solutionSet Solution newSolution; for (int i = 0; i < populationSize; i++) { newSolution = new Solution(this.Problem); this.Problem.Evaluate(newSolution); this.Problem.EvaluateConstraints(newSolution); evaluations++; population.Add(newSolution); } // Generations ... while (iterations < maxIterations) { // Create the offSpring solutionSet offspringPopulation = new SolutionSet(populationSize * 2); for (int i = 0; i < populationSize; i++) { // Obtain parents. Two parameters are required: the population and the // index of the current individual parent = (Solution[])selectionOperator.Execute(new object[] { population, i }); Solution child; // Crossover. Two parameters are required: the current individual and the // array of parents child = (Solution)crossoverOperator.Execute(new object[] { population.Get(i), parent }); this.Problem.Evaluate(child); this.Problem.EvaluateConstraints(child); evaluations++; // Dominance test int result; result = dominance.Compare(population.Get(i), child); if (result == -1) { // Solution i dominates child offspringPopulation.Add(population.Get(i)); } else if (result == 1) { // child dominates offspringPopulation.Add(child); } else { // the two solutions are non-dominated offspringPopulation.Add(child); offspringPopulation.Add(population.Get(i)); } } // Ranking the offspring population Ranking ranking = new Ranking(offspringPopulation); int remain = populationSize; int index = 0; SolutionSet front = null; population.Clear(); // Obtain the next front front = ranking.GetSubfront(index); while ((remain > 0) && (remain >= front.Size())) { //Assign crowding distance to individuals distance.CrowdingDistanceAssignment(front, this.Problem.NumberOfObjectives); //Add the individuals of this front for (int k = 0; k < front.Size(); k++) { population.Add(front.Get(k)); } // for //Decrement remain remain = remain - front.Size(); //Obtain the next front index++; if (remain > 0) { front = ranking.GetSubfront(index); } } // remain is less than front(index).size, insert only the best one if (remain > 0) { // front contains individuals to insert while (front.Size() > remain) { distance.CrowdingDistanceAssignment(front, this.Problem.NumberOfObjectives); front.Remove(front.IndexWorst(new CrowdingComparator())); } for (int k = 0; k < front.Size(); k++) { population.Add(front.Get(k)); } remain = 0; } iterations++; } // Return the first non-dominated front Ranking rnk = new Ranking(population); this.Result = rnk.GetSubfront(0); return(this.Result); }