[13852] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
[14087] | 7 | * Implementation based on the GDE3 implementation in jMetal Framework https://github.com/jMetal/jMetal
|
---|
[13852] | 8 | *
|
---|
| 9 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 10 | * it under the terms of the GNU General Public License as published by
|
---|
| 11 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 12 | * (at your option) any later version.
|
---|
| 13 | *
|
---|
| 14 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 17 | * GNU General Public License for more details.
|
---|
| 18 | *
|
---|
| 19 | * You should have received a copy of the GNU General Public License
|
---|
| 20 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 21 | */
|
---|
| 22 | #endregion
|
---|
| 23 | using System;
|
---|
[13749] | 24 | using System.Linq;
|
---|
| 25 | using System.Collections.Generic;
|
---|
| 26 | using HeuristicLab.Analysis;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Data;
|
---|
| 30 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 31 | using HeuristicLab.Operators;
|
---|
| 32 | using HeuristicLab.Optimization;
|
---|
| 33 | using HeuristicLab.Optimization.Operators;
|
---|
| 34 | using HeuristicLab.Parameters;
|
---|
| 35 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 36 | using HeuristicLab.PluginInfrastructure;
|
---|
| 37 | using HeuristicLab.Problems.MultiObjectiveTestFunctions;
|
---|
| 38 | using HeuristicLab.Random;
|
---|
| 39 | using System.Threading;
|
---|
[13756] | 40 | using HeuristicLab.Algorithms.GDE3;
|
---|
[13749] | 41 |
|
---|
[14705] | 42 | namespace HeuristicLab.Algoritms.GDE3 {
|
---|
[13749] | 43 |
|
---|
[14705] | 44 | [Item("Generalized Differential Evolution (GDE3)", "A generalized differential evolution algorithm.")]
|
---|
| 45 | [StorableClass]
|
---|
| 46 | [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 400)]
|
---|
| 47 | public class GDE3 : BasicAlgorithm {
|
---|
| 48 | public override Type ProblemType {
|
---|
| 49 | get { return typeof(MultiObjectiveBasicProblem<RealVectorEncoding>); }
|
---|
| 50 | }
|
---|
| 51 | public new MultiObjectiveTestFunctionProblem Problem {
|
---|
| 52 | get { return (MultiObjectiveTestFunctionProblem)base.Problem; }
|
---|
| 53 | set { base.Problem = value; }
|
---|
| 54 | }
|
---|
[13749] | 55 |
|
---|
[14705] | 56 | public ILookupParameter<DoubleMatrix> BestKnownFrontParameter {
|
---|
| 57 | get {
|
---|
| 58 | return (ILookupParameter<DoubleMatrix>)Parameters["BestKnownFront"];
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
[13749] | 61 |
|
---|
[14705] | 62 | private readonly IRandom _random = new MersenneTwister();
|
---|
| 63 | private int evals;
|
---|
| 64 | private double IGDSumm;
|
---|
[13749] | 65 |
|
---|
[14705] | 66 | #region ParameterNames
|
---|
| 67 | private const string MaximumGenerationsParameterName = "Maximum Generations";
|
---|
| 68 | private const string MaximumEvaluationsParameterName = "Maximum Evaluations";
|
---|
| 69 | private const string CrossoverProbabilityParameterName = "CrossoverProbability";
|
---|
| 70 | private const string PopulationSizeParameterName = "PopulationSize";
|
---|
| 71 | private const string ScalingFactorParameterName = "ScalingFactor";
|
---|
[13849] | 72 |
|
---|
[14705] | 73 | #endregion
|
---|
[13749] | 74 |
|
---|
[14705] | 75 | #region ParameterProperties
|
---|
| 76 | public IFixedValueParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 77 | get { return (IFixedValueParameter<IntValue>)Parameters[MaximumGenerationsParameterName]; }
|
---|
| 78 | }
|
---|
| 79 | public IFixedValueParameter<IntValue> MaximumEvaluationsParameter {
|
---|
| 80 | get { return (IFixedValueParameter<IntValue>)Parameters[MaximumEvaluationsParameterName]; }
|
---|
| 81 | }
|
---|
| 82 | private ValueParameter<IntValue> PopulationSizeParameter {
|
---|
| 83 | get { return (ValueParameter<IntValue>)Parameters[PopulationSizeParameterName]; }
|
---|
| 84 | }
|
---|
| 85 | public ValueParameter<DoubleValue> CrossoverProbabilityParameter {
|
---|
| 86 | get { return (ValueParameter<DoubleValue>)Parameters[CrossoverProbabilityParameterName]; }
|
---|
| 87 | }
|
---|
| 88 | public ValueParameter<DoubleValue> ScalingFactorParameter {
|
---|
| 89 | get { return (ValueParameter<DoubleValue>)Parameters[ScalingFactorParameterName]; }
|
---|
| 90 | }
|
---|
| 91 | #endregion
|
---|
[13749] | 92 |
|
---|
[14705] | 93 | #region Properties
|
---|
| 94 | public int MaximumGenerations {
|
---|
| 95 | get { return MaximumGenerationsParameter.Value.Value; }
|
---|
| 96 | set { MaximumGenerationsParameter.Value.Value = value; }
|
---|
| 97 | }
|
---|
[13749] | 98 |
|
---|
[14705] | 99 | public int MaximumEvaluations {
|
---|
| 100 | get { return MaximumEvaluationsParameter.Value.Value; }
|
---|
| 101 | set { MaximumEvaluationsParameter.Value.Value = value; }
|
---|
| 102 | }
|
---|
[14087] | 103 |
|
---|
[14705] | 104 | public Double CrossoverProbability {
|
---|
| 105 | get { return CrossoverProbabilityParameter.Value.Value; }
|
---|
| 106 | set { CrossoverProbabilityParameter.Value.Value = value; }
|
---|
| 107 | }
|
---|
| 108 | public Double ScalingFactor {
|
---|
| 109 | get { return ScalingFactorParameter.Value.Value; }
|
---|
| 110 | set { ScalingFactorParameter.Value.Value = value; }
|
---|
| 111 | }
|
---|
| 112 | public IntValue PopulationSize {
|
---|
| 113 | get { return PopulationSizeParameter.Value; }
|
---|
| 114 | set { PopulationSizeParameter.Value = value; }
|
---|
| 115 | }
|
---|
| 116 | #endregion
|
---|
[13749] | 117 |
|
---|
[14705] | 118 | #region ResultsProperties
|
---|
| 119 | private double ResultsBestQuality {
|
---|
| 120 | get { return ((DoubleValue)Results["Best Quality"].Value).Value; }
|
---|
| 121 | set { ((DoubleValue)Results["Best Quality"].Value).Value = value; }
|
---|
| 122 | }
|
---|
[13749] | 123 |
|
---|
[14705] | 124 | private double ResultsIGDMean {
|
---|
| 125 | get { return ((DoubleValue)Results["IGDMeanValue"].Value).Value; }
|
---|
| 126 | set { ((DoubleValue)Results["IGDMeanValue"].Value).Value = value; }
|
---|
| 127 | }
|
---|
[13849] | 128 |
|
---|
[14705] | 129 | private double ResultsIGDBest {
|
---|
| 130 | get { return ((DoubleValue)Results["IGDBestValue"].Value).Value; }
|
---|
| 131 | set { ((DoubleValue)Results["IGDBestValue"].Value).Value = value; }
|
---|
| 132 | }
|
---|
[13849] | 133 |
|
---|
[14705] | 134 | private double ResultsIGDWorst {
|
---|
| 135 | get { return ((DoubleValue)Results["IGDWorstValue"].Value).Value; }
|
---|
| 136 | set { ((DoubleValue)Results["IGDWorstValue"].Value).Value = value; }
|
---|
| 137 | }
|
---|
[13849] | 138 |
|
---|
[14705] | 139 | private double ResultsInvertedGenerationalDistance {
|
---|
| 140 | get { return ((DoubleValue)Results["InvertedGenerationalDistance"].Value).Value; }
|
---|
| 141 | set { ((DoubleValue)Results["InvertedGenerationalDistance"].Value).Value = value; }
|
---|
| 142 | }
|
---|
[13749] | 143 |
|
---|
[14705] | 144 | private double ResultsHypervolume {
|
---|
| 145 | get { return ((DoubleValue)Results["HyperVolumeValue"].Value).Value; }
|
---|
| 146 | set { ((DoubleValue)Results["HyperVolumeValue"].Value).Value = value; }
|
---|
| 147 | }
|
---|
[13749] | 148 |
|
---|
[14705] | 149 | private DoubleMatrix ResultsBestFront {
|
---|
| 150 | get { return (DoubleMatrix)Results["Best Front"].Value; }
|
---|
| 151 | set { Results["Best Front"].Value = value; }
|
---|
| 152 | }
|
---|
[13749] | 153 |
|
---|
[14705] | 154 | private int ResultsEvaluations {
|
---|
| 155 | get { return ((IntValue)Results["Evaluations"].Value).Value; }
|
---|
| 156 | set { ((IntValue)Results["Evaluations"].Value).Value = value; }
|
---|
| 157 | }
|
---|
| 158 | private int ResultsGenerations {
|
---|
| 159 | get { return ((IntValue)Results["Generations"].Value).Value; }
|
---|
| 160 | set { ((IntValue)Results["Generations"].Value).Value = value; }
|
---|
| 161 | }
|
---|
| 162 | private double ResultsGenerationalDistance {
|
---|
| 163 | get { return ((DoubleValue)Results["GenerationalDistance"].Value).Value; }
|
---|
| 164 | set { ((DoubleValue)Results["GenerationalDistance"].Value).Value = value; }
|
---|
| 165 | }
|
---|
[13749] | 166 |
|
---|
[14705] | 167 | private double ResultsSpacing {
|
---|
| 168 | get { return ((DoubleValue)Results["Spacing"].Value).Value; }
|
---|
| 169 | set { ((DoubleValue)Results["Spacing"].Value).Value = value; }
|
---|
| 170 | }
|
---|
[13756] | 171 |
|
---|
[14705] | 172 | private double ResultsCrowding {
|
---|
| 173 | get { return ((DoubleValue)Results["Crowding"].Value).Value; }
|
---|
| 174 | set { ((DoubleValue)Results["Crowding"].Value).Value = value; }
|
---|
| 175 | }
|
---|
[13749] | 176 |
|
---|
[14705] | 177 | #endregion
|
---|
[13749] | 178 |
|
---|
[14705] | 179 | [StorableConstructor]
|
---|
| 180 | protected GDE3(bool deserializing) : base(deserializing) { }
|
---|
[13749] | 181 |
|
---|
[14705] | 182 | protected GDE3(GDE3 original, Cloner cloner)
|
---|
| 183 | : base(original, cloner) {
|
---|
| 184 | }
|
---|
[13749] | 185 |
|
---|
[14705] | 186 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 187 | return new GDE3(this, cloner);
|
---|
| 188 | }
|
---|
[13749] | 189 |
|
---|
[14705] | 190 | public GDE3() {
|
---|
| 191 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumGenerationsParameterName, "", new IntValue(1000)));
|
---|
| 192 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluationsParameterName, "", new IntValue(Int32.MaxValue)));
|
---|
| 193 | Parameters.Add(new ValueParameter<IntValue>(PopulationSizeParameterName, "The size of the population of solutions.", new IntValue(100)));
|
---|
| 194 | Parameters.Add(new ValueParameter<DoubleValue>(CrossoverProbabilityParameterName, "The value for crossover rate", new DoubleValue(0.5)));
|
---|
| 195 | Parameters.Add(new ValueParameter<DoubleValue>(ScalingFactorParameterName, "The value for scaling factor", new DoubleValue(0.5)));
|
---|
| 196 | Parameters.Add(new LookupParameter<DoubleMatrix>("BestKnownFront", "The currently best known Pareto front"));
|
---|
| 197 | }
|
---|
[13749] | 198 |
|
---|
[14705] | 199 | protected override void Run(CancellationToken cancellationToken) {
|
---|
| 200 | // Set up the results display
|
---|
| 201 | Results.Add(new Result("Generations", new IntValue(0)));
|
---|
| 202 | Results.Add(new Result("Evaluations", new IntValue(0)));
|
---|
| 203 | Results.Add(new Result("Best Front", new DoubleMatrix()));
|
---|
| 204 | Results.Add(new Result("Crowding", new DoubleValue(0)));
|
---|
| 205 | Results.Add(new Result("InvertedGenerationalDistance", new DoubleValue(0)));
|
---|
| 206 | Results.Add(new Result("GenerationalDistance", new DoubleValue(0)));
|
---|
| 207 | Results.Add(new Result("HyperVolumeValue", new DoubleValue(0)));
|
---|
| 208 | Results.Add(new Result("IGDMeanValue", new DoubleValue(0)));
|
---|
| 209 | Results.Add(new Result("IGDBestValue", new DoubleValue(Int32.MaxValue)));
|
---|
| 210 | Results.Add(new Result("IGDWorstValue", new DoubleValue(0)));
|
---|
[13849] | 211 |
|
---|
[14705] | 212 | Results.Add(new Result("Spacing", new DoubleValue(0)));
|
---|
| 213 | Results.Add(new Result("Scatterplot", typeof(IMOFrontModel)));
|
---|
| 214 | var table = new DataTable("Qualities");
|
---|
| 215 | table.Rows.Add(new DataRow("Best Quality"));
|
---|
| 216 | Results.Add(new Result("Qualities", table));
|
---|
[13749] | 217 |
|
---|
[14705] | 218 | //setup the variables
|
---|
| 219 | List<SolutionSet> population;
|
---|
| 220 | List<SolutionSet> offspringPopulation;
|
---|
| 221 | SolutionSet[] parent;
|
---|
| 222 | double IGDSumm = 0;
|
---|
[13749] | 223 |
|
---|
[14705] | 224 | //initialize population
|
---|
| 225 | population = new List<SolutionSet>(PopulationSizeParameter.Value.Value);
|
---|
[13749] | 226 |
|
---|
[14705] | 227 | for(int i = 0; i < PopulationSizeParameter.Value.Value; ++i) {
|
---|
| 228 | var m = createIndividual();
|
---|
| 229 | m.Quality = Problem.Evaluate(m.Population, _random);
|
---|
| 230 | //the test function is constrained
|
---|
| 231 | if(m.Quality.Length > Problem.Objectives) {
|
---|
| 232 | m.OverallConstrainViolation = m.Quality[Problem.Objectives];
|
---|
| 233 | } else {
|
---|
| 234 | m.OverallConstrainViolation = 0;
|
---|
| 235 | }
|
---|
| 236 | population.Add(m);
|
---|
| 237 | }
|
---|
[13749] | 238 |
|
---|
[14705] | 239 | this.initProgress();
|
---|
| 240 | int generations = 1;
|
---|
[13749] | 241 |
|
---|
[14705] | 242 | while(ResultsEvaluations < MaximumEvaluationsParameter.Value.Value
|
---|
| 243 | && !cancellationToken.IsCancellationRequested) {
|
---|
| 244 | var populationSize = PopulationSizeParameter.Value.Value;
|
---|
[13749] | 245 |
|
---|
[14705] | 246 | // Create the offSpring solutionSet
|
---|
| 247 | offspringPopulation = new List<SolutionSet>(PopulationSizeParameter.Value.Value * 2);
|
---|
[13749] | 248 |
|
---|
[14705] | 249 | for(int i = 0; i < populationSize; i++) {
|
---|
| 250 | // Obtain parents. Two parameters are required: the population and the
|
---|
| 251 | // index of the current individual
|
---|
| 252 | parent = selection(population, i);
|
---|
[13756] | 253 |
|
---|
[14705] | 254 | SolutionSet child;
|
---|
| 255 | // Crossover. The parameters are the current individual and the index of the array of parents
|
---|
| 256 | child = reproduction(population[i], parent);
|
---|
[13849] | 257 |
|
---|
[14705] | 258 | child.Quality = Problem.Evaluate(child.Population, _random);
|
---|
[13756] | 259 |
|
---|
[14705] | 260 | this.updateProgres();
|
---|
[13849] | 261 |
|
---|
[14705] | 262 | //the test function is constrained
|
---|
| 263 | if(child.Quality.Length > Problem.Objectives) {
|
---|
| 264 | child.OverallConstrainViolation = child.Quality[Problem.Objectives];
|
---|
| 265 | } else {
|
---|
| 266 | child.OverallConstrainViolation = 0;
|
---|
| 267 | }
|
---|
[13849] | 268 |
|
---|
[14705] | 269 | // Dominance test
|
---|
| 270 | int result;
|
---|
| 271 | result = compareDomination(population[i], child);
|
---|
[13849] | 272 |
|
---|
[14705] | 273 | if(result == -1) { // Solution i dominates child
|
---|
| 274 | offspringPopulation.Add(population[i]);
|
---|
| 275 | } else if(result == 1) { // child dominates
|
---|
| 276 | offspringPopulation.Add(child);
|
---|
| 277 | } else { // the two solutions are non-dominated
|
---|
| 278 | offspringPopulation.Add(child);
|
---|
| 279 | offspringPopulation.Add(population[i]);
|
---|
| 280 | }
|
---|
[13756] | 281 | }
|
---|
[13749] | 282 |
|
---|
[14705] | 283 | // Ranking the offspring population
|
---|
| 284 | List<SolutionSet>[] ranking = computeRanking(offspringPopulation);
|
---|
| 285 | population = crowdingDistanceSelection(ranking);
|
---|
| 286 | generations++;
|
---|
| 287 | ResultsGenerations = generations;
|
---|
| 288 | displayResults(population);
|
---|
| 289 | }
|
---|
| 290 | }
|
---|
[13756] | 291 |
|
---|
[14705] | 292 | public override bool SupportsPause { get { return false; } } // XXX does it actually support pause?
|
---|
[13756] | 293 |
|
---|
[14705] | 294 | private void displayResults(List<SolutionSet> population) {
|
---|
| 295 | List<SolutionSet>[] rankingFinal = computeRanking(population);
|
---|
[13756] | 296 |
|
---|
[14705] | 297 | int objectives = Problem.Objectives;
|
---|
| 298 | var optimalfront = Problem.TestFunction.OptimalParetoFront(objectives);
|
---|
[13756] | 299 |
|
---|
[14705] | 300 | double[][] opf = new double[0][];
|
---|
| 301 | if(optimalfront != null) {
|
---|
| 302 | opf = optimalfront.Select(s => s.ToArray()).ToArray();
|
---|
| 303 | }
|
---|
[13749] | 304 |
|
---|
[14705] | 305 | //compute the final qualities and population
|
---|
| 306 | double[][] qualitiesFinal = new double[rankingFinal[0].Count][];
|
---|
| 307 | double[][] populationFinal = new double[rankingFinal[0].Count][];
|
---|
[13849] | 308 |
|
---|
[14705] | 309 | for(int i = 0; i < rankingFinal[0].Count; ++i) {
|
---|
| 310 | qualitiesFinal[i] = new double[Problem.Objectives];
|
---|
| 311 | populationFinal[i] = new double[Problem.Objectives];
|
---|
| 312 | for(int j = 0; j < Problem.Objectives; ++j) {
|
---|
| 313 | populationFinal[i][j] = rankingFinal[0][i].Population[j];
|
---|
| 314 | qualitiesFinal[i][j] = rankingFinal[0][i].Quality[j];
|
---|
[13756] | 315 | }
|
---|
[14705] | 316 | }
|
---|
| 317 | IEnumerable<double[]> en = qualitiesFinal;
|
---|
| 318 | IEnumerable<double[]> frontVectors = NonDominatedSelect.selectNonDominatedVectors(qualitiesFinal, Problem.TestFunction.Maximization(objectives), true);
|
---|
| 319 | //update the results
|
---|
[13756] | 320 |
|
---|
[14705] | 321 | ResultsEvaluations = this.evals;
|
---|
| 322 | ResultsBestFront = new DoubleMatrix(MultiObjectiveTestFunctionProblem.To2D(qualitiesFinal));
|
---|
| 323 | ResultsCrowding = Crowding.Calculate(qualitiesFinal, Problem.TestFunction.Bounds(objectives));
|
---|
| 324 | GenerationalDistanceCalculator distance = new GenerationalDistanceCalculator();
|
---|
| 325 | ResultsInvertedGenerationalDistance = distance.CalculateGenerationalDistance(qualitiesFinal, opf, Problem.Objectives);
|
---|
| 326 | ResultsHypervolume = Hypervolume.Calculate(frontVectors, Problem.TestFunction.ReferencePoint(objectives), Problem.TestFunction.Maximization(objectives));
|
---|
| 327 | ResultsGenerationalDistance = GenerationalDistance.Calculate(qualitiesFinal, optimalfront, 1);
|
---|
| 328 | Results["Scatterplot"].Value = new MOSolution(qualitiesFinal, populationFinal, opf, objectives);
|
---|
| 329 | ResultsSpacing = Spacing.Calculate(qualitiesFinal);
|
---|
[13756] | 330 |
|
---|
[14705] | 331 | if(ResultsIGDBest > ResultsInvertedGenerationalDistance) {
|
---|
| 332 | ResultsIGDBest = ResultsInvertedGenerationalDistance;
|
---|
| 333 | }
|
---|
| 334 | if(ResultsIGDWorst < ResultsInvertedGenerationalDistance) {
|
---|
| 335 | ResultsIGDWorst = ResultsInvertedGenerationalDistance;
|
---|
| 336 | }
|
---|
| 337 | this.IGDSumm += ResultsInvertedGenerationalDistance;
|
---|
| 338 | ResultsIGDMean = this.IGDSumm / ResultsGenerations;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | private int getWorstIndex(List<SolutionSet> SolutionsList) {
|
---|
| 342 | int result = 0;
|
---|
| 343 |
|
---|
| 344 | if((SolutionsList == null) || SolutionsList.Count == 0) {
|
---|
| 345 | result = 0;
|
---|
| 346 | } else {
|
---|
| 347 | SolutionSet worstKnown = SolutionsList[0],
|
---|
| 348 | candidateSolution;
|
---|
| 349 | int flag;
|
---|
| 350 | for(int i = 1; i < SolutionsList.Count; i++) {
|
---|
| 351 | candidateSolution = SolutionsList[i];
|
---|
| 352 | flag = compareDomination(worstKnown, candidateSolution);
|
---|
| 353 | if(flag == -1) {
|
---|
| 354 | result = i;
|
---|
| 355 | worstKnown = candidateSolution;
|
---|
| 356 | }
|
---|
[13749] | 357 | }
|
---|
[14705] | 358 | }
|
---|
| 359 | return result;
|
---|
| 360 | }
|
---|
[13756] | 361 |
|
---|
[14705] | 362 | private SolutionSet createIndividual() {
|
---|
| 363 | var dim = Problem.ProblemSize;
|
---|
| 364 | var lb = Problem.Bounds[0, 0];
|
---|
| 365 | var ub = Problem.Bounds[0, 1];
|
---|
| 366 | var range = ub - lb;
|
---|
| 367 | var v = new double[Problem.ProblemSize];
|
---|
| 368 | SolutionSet solutionObject = new SolutionSet(PopulationSizeParameter.Value.Value);
|
---|
[13749] | 369 |
|
---|
[14705] | 370 | for(int i = 0; i < Problem.ProblemSize; ++i) {
|
---|
| 371 | v[i] = _random.NextDouble() * range + lb;
|
---|
[13749] | 372 |
|
---|
[14705] | 373 | }
|
---|
| 374 | solutionObject.createSolution(v);
|
---|
| 375 | return solutionObject;
|
---|
| 376 | }
|
---|
[13749] | 377 |
|
---|
[14705] | 378 | private SolutionSet createEmptyIndividual() {
|
---|
| 379 | SolutionSet solutionObject = new SolutionSet(PopulationSizeParameter.Value.Value);
|
---|
| 380 | var n = new RealVector(Problem.ProblemSize);
|
---|
| 381 | solutionObject.Population = n;
|
---|
| 382 | return solutionObject;
|
---|
| 383 | }
|
---|
[13749] | 384 |
|
---|
[14705] | 385 | private void initProgress() {
|
---|
| 386 | this.evals = PopulationSizeParameter.Value.Value;
|
---|
| 387 | }
|
---|
[13749] | 388 |
|
---|
[14705] | 389 | private void updateProgres() {
|
---|
| 390 | this.evals++;
|
---|
| 391 | }
|
---|
[13749] | 392 |
|
---|
[14705] | 393 | private SolutionSet[] selection(List<SolutionSet> population, int i) {
|
---|
| 394 | SolutionSet[] parents = new SolutionSet[3];
|
---|
| 395 | int r0, r1, r2;
|
---|
| 396 | //assure the selected vectors r0, r1 and r2 are different
|
---|
| 397 | do {
|
---|
| 398 | r0 = _random.Next(0, PopulationSizeParameter.Value.Value);
|
---|
| 399 | } while(r0 == i);
|
---|
| 400 | do {
|
---|
| 401 | r1 = _random.Next(0, PopulationSizeParameter.Value.Value);
|
---|
| 402 | } while(r1 == i || r1 == r0);
|
---|
| 403 | do {
|
---|
| 404 | r2 = _random.Next(0, PopulationSizeParameter.Value.Value);
|
---|
| 405 | } while(r2 == i || r2 == r0 || r2 == r1);
|
---|
[13749] | 406 |
|
---|
[14705] | 407 | parents[0] = population[r0];
|
---|
| 408 | parents[1] = population[r1];
|
---|
| 409 | parents[2] = population[r2];
|
---|
[13756] | 410 |
|
---|
[14705] | 411 | return parents;
|
---|
| 412 | }
|
---|
[13749] | 413 |
|
---|
[14705] | 414 | private SolutionSet reproduction(SolutionSet parent, SolutionSet[] parentsSolutions) {
|
---|
| 415 | var individual = createEmptyIndividual();
|
---|
| 416 | double rnbr = _random.Next(0, Problem.ProblemSize);
|
---|
| 417 | for(int m = 0; m < Problem.ProblemSize; m++) {
|
---|
| 418 | if(_random.NextDouble() < CrossoverProbabilityParameter.Value.Value || m == rnbr) {
|
---|
| 419 | double value;
|
---|
| 420 | value = parentsSolutions[2].Population[m] +
|
---|
| 421 | ScalingFactorParameter.Value.Value * (parentsSolutions[0].Population[m] - parentsSolutions[1].Population[m]);
|
---|
| 422 | //check the problem upper and lower bounds
|
---|
| 423 | if(value > Problem.Bounds[0, 1]) value = Problem.Bounds[0, 1];
|
---|
| 424 | if(value < Problem.Bounds[0, 0]) value = Problem.Bounds[0, 0];
|
---|
| 425 | individual.Population[m] = value;
|
---|
| 426 | } else {
|
---|
| 427 | double value;
|
---|
| 428 | value = parent.Population[m];
|
---|
| 429 | individual.Population[m] = value;
|
---|
[13749] | 430 | }
|
---|
[14705] | 431 | }
|
---|
| 432 | return individual;
|
---|
| 433 | }
|
---|
[13749] | 434 |
|
---|
[14705] | 435 | private List<SolutionSet> crowdingDistanceSelection(List<SolutionSet>[] ranking) {
|
---|
| 436 | List<SolutionSet> population = new List<SolutionSet>();
|
---|
| 437 | int rankingIndex = 0;
|
---|
| 438 | while(populationIsNotFull(population)) {
|
---|
| 439 | if(subFrontFillsIntoThePopulation(ranking, rankingIndex, population)) {
|
---|
| 440 | addRankedSolutionToPopulation(ranking, rankingIndex, population);
|
---|
| 441 | rankingIndex++;
|
---|
| 442 | } else {
|
---|
| 443 | crowdingDistanceAssignment(ranking[rankingIndex]);
|
---|
| 444 | addLastRankedSolutionToPopulation(ranking, rankingIndex, population);
|
---|
[13749] | 445 | }
|
---|
[14705] | 446 | }
|
---|
| 447 | return population;
|
---|
| 448 | }
|
---|
[13749] | 449 |
|
---|
[14705] | 450 | private void addLastRankedSolutionToPopulation(List<SolutionSet>[] ranking, int rankingIndex, List<SolutionSet> population) {
|
---|
| 451 | List<SolutionSet> currentRankedFront = ranking[rankingIndex];
|
---|
| 452 | //descending sort and add the front with highest crowding distance to the population
|
---|
| 453 | currentRankedFront.Sort((x, y) => -x.CrowdingDistance.CompareTo(y.CrowdingDistance));
|
---|
| 454 | int i = 0;
|
---|
| 455 | while(population.Count < PopulationSizeParameter.Value.Value) {
|
---|
| 456 | population.Add(currentRankedFront[i]);
|
---|
| 457 | i++;
|
---|
| 458 | }
|
---|
| 459 | }
|
---|
[13749] | 460 |
|
---|
[14705] | 461 | private void crowdingDistanceAssignment(List<SolutionSet> rankingSubfront) {
|
---|
| 462 | int size = rankingSubfront.Count;
|
---|
[13749] | 463 |
|
---|
[14705] | 464 | if(size == 0)
|
---|
| 465 | return;
|
---|
[13749] | 466 |
|
---|
[14705] | 467 | if(size == 1) {
|
---|
| 468 | rankingSubfront[0].CrowdingDistance = double.PositiveInfinity;
|
---|
| 469 | return;
|
---|
| 470 | }
|
---|
[13749] | 471 |
|
---|
[14705] | 472 | if(size == 2) {
|
---|
| 473 | rankingSubfront[0].CrowdingDistance = double.PositiveInfinity;
|
---|
| 474 | rankingSubfront[1].CrowdingDistance = double.PositiveInfinity;
|
---|
| 475 | return;
|
---|
| 476 | }
|
---|
[13749] | 477 |
|
---|
[14705] | 478 | //Use a new SolutionSet to evite alter original solutionSet
|
---|
| 479 | List<SolutionSet> front = new List<SolutionSet>(size);
|
---|
| 480 | for(int i = 0; i < size; i++) {
|
---|
| 481 | front.Add(rankingSubfront[i]);
|
---|
| 482 | }
|
---|
[13749] | 483 |
|
---|
[14705] | 484 | for(int i = 0; i < size; i++)
|
---|
| 485 | front[i].CrowdingDistance = 0.0;
|
---|
[13749] | 486 |
|
---|
[14705] | 487 | double objetiveMaxn;
|
---|
| 488 | double objetiveMinn;
|
---|
| 489 | double distance;
|
---|
[13749] | 490 |
|
---|
[14705] | 491 | for(int i = 0; i < Problem.Objectives; i++) {
|
---|
| 492 | // Sort the front population by the objective i
|
---|
| 493 | front.Sort((x, y) => x.Quality[i].CompareTo(y.Quality[i]));
|
---|
| 494 | objetiveMinn = front[0].Quality[i];
|
---|
| 495 | objetiveMaxn = front[front.Count - 1].Quality[i];
|
---|
[13749] | 496 |
|
---|
[14705] | 497 | //Set crowding distance for the current front
|
---|
| 498 | front[0].CrowdingDistance = double.PositiveInfinity;
|
---|
| 499 | front[size - 1].CrowdingDistance = double.PositiveInfinity;
|
---|
[13749] | 500 |
|
---|
[14705] | 501 | for(int j = 1; j < size - 1; j++) {
|
---|
| 502 | distance = front[j + 1].Quality[i] - front[j - 1].Quality[i];
|
---|
| 503 | distance = distance / (objetiveMaxn - objetiveMinn);
|
---|
| 504 | distance += front[j].CrowdingDistance;
|
---|
| 505 | front[j].CrowdingDistance = distance;
|
---|
[13749] | 506 | }
|
---|
[14705] | 507 | }
|
---|
| 508 | }
|
---|
[13749] | 509 |
|
---|
[14705] | 510 | private void addRankedSolutionToPopulation(List<SolutionSet>[] ranking, int rankingIndex, List<SolutionSet> population) {
|
---|
| 511 | foreach(SolutionSet solution in ranking[rankingIndex]) {
|
---|
| 512 | population.Add(solution);
|
---|
| 513 | }
|
---|
| 514 | }
|
---|
[13749] | 515 |
|
---|
[14705] | 516 | private bool subFrontFillsIntoThePopulation(List<SolutionSet>[] ranking, int rankingIndex, List<SolutionSet> population) {
|
---|
| 517 | return ranking[rankingIndex].Count < (PopulationSizeParameter.Value.Value - population.Count);
|
---|
| 518 | }
|
---|
[13749] | 519 |
|
---|
[14705] | 520 | private bool populationIsNotFull(List<SolutionSet> population) {
|
---|
| 521 | return population.Count < PopulationSizeParameter.Value.Value;
|
---|
| 522 | }
|
---|
[13749] | 523 |
|
---|
[14705] | 524 | private List<SolutionSet>[] computeRanking(List<SolutionSet> tmpList) {
|
---|
| 525 | // dominateMe[i] contains the number of solutions dominating i
|
---|
| 526 | int[] dominateMe = new int[tmpList.Count];
|
---|
[13749] | 527 |
|
---|
[14705] | 528 | // iDominate[k] contains the list of solutions dominated by k
|
---|
| 529 | List<int>[] iDominate = new List<int>[tmpList.Count];
|
---|
[13749] | 530 |
|
---|
[14705] | 531 | // front[i] contains the list of individuals belonging to the front i
|
---|
| 532 | List<int>[] front = new List<int>[tmpList.Count + 1];
|
---|
[13749] | 533 |
|
---|
[14705] | 534 | // flagDominate is an auxiliar encodings.variable
|
---|
| 535 | int flagDominate;
|
---|
[13749] | 536 |
|
---|
[14705] | 537 | // Initialize the fronts
|
---|
| 538 | for(int i = 0; i < front.Length; i++) {
|
---|
| 539 | front[i] = new List<int>();
|
---|
| 540 | }
|
---|
[13749] | 541 |
|
---|
[14705] | 542 | //-> Fast non dominated sorting algorithm
|
---|
| 543 | // Contribution of Guillaume Jacquenot
|
---|
| 544 | for(int p = 0; p < tmpList.Count; p++) {
|
---|
| 545 | // Initialize the list of individuals that i dominate and the number
|
---|
| 546 | // of individuals that dominate me
|
---|
| 547 | iDominate[p] = new List<int>();
|
---|
| 548 | dominateMe[p] = 0;
|
---|
| 549 | }
|
---|
| 550 | for(int p = 0; p < (tmpList.Count - 1); p++) {
|
---|
| 551 | // For all q individuals , calculate if p dominates q or vice versa
|
---|
| 552 | for(int q = p + 1; q < tmpList.Count; q++) {
|
---|
| 553 | flagDominate = compareConstraintsViolation(tmpList[p], tmpList[q]);
|
---|
| 554 | if(flagDominate == 0) {
|
---|
| 555 | flagDominate = compareDomination(tmpList[p], tmpList[q]);
|
---|
| 556 | }
|
---|
| 557 | if(flagDominate == -1) {
|
---|
| 558 | iDominate[p].Add(q);
|
---|
| 559 | dominateMe[q]++;
|
---|
| 560 | } else if(flagDominate == 1) {
|
---|
| 561 | iDominate[q].Add(p);
|
---|
| 562 | dominateMe[p]++;
|
---|
| 563 | }
|
---|
| 564 | }
|
---|
| 565 | // If nobody dominates p, p belongs to the first front
|
---|
| 566 | }
|
---|
| 567 | for(int i = 0; i < tmpList.Count; i++) {
|
---|
| 568 | if(dominateMe[i] == 0) {
|
---|
| 569 | front[0].Add(i);
|
---|
| 570 | tmpList[i].Rank = 0;
|
---|
| 571 | }
|
---|
| 572 | }
|
---|
[13749] | 573 |
|
---|
[14705] | 574 | //Obtain the rest of fronts
|
---|
| 575 | int k = 0;
|
---|
[13749] | 576 |
|
---|
[14705] | 577 | while(front[k].Count != 0) {
|
---|
| 578 | k++;
|
---|
| 579 | foreach(var it1 in front[k - 1]) {
|
---|
| 580 | foreach(var it2 in iDominate[it1]) {
|
---|
| 581 | int index = it2;
|
---|
| 582 | dominateMe[index]--;
|
---|
| 583 | if(dominateMe[index] == 0) {
|
---|
| 584 | front[k].Add(index);
|
---|
| 585 | tmpList[index].Rank = k;
|
---|
[13749] | 586 | }
|
---|
[14705] | 587 | }
|
---|
| 588 | }
|
---|
| 589 | }
|
---|
| 590 | //<-
|
---|
[13749] | 591 |
|
---|
[14705] | 592 | var rankedSubpopulation = new List<SolutionSet>[k];
|
---|
| 593 | //0,1,2,....,i-1 are front, then i fronts
|
---|
| 594 | for(int j = 0; j < k; j++) {
|
---|
| 595 | rankedSubpopulation[j] = new List<SolutionSet>(front[j].Count);
|
---|
| 596 | foreach(var it1 in front[j]) {
|
---|
| 597 | rankedSubpopulation[j].Add(tmpList[it1]);
|
---|
[13749] | 598 | }
|
---|
[14705] | 599 | }
|
---|
| 600 | return rankedSubpopulation;
|
---|
| 601 | }
|
---|
[13749] | 602 |
|
---|
[14705] | 603 | private int compareDomination(SolutionSet solution1, SolutionSet solution2) {
|
---|
| 604 | int dominate1; // dominate1 indicates if some objective of solution1
|
---|
| 605 | // dominates the same objective in solution2. dominate2
|
---|
| 606 | int dominate2; // is the complementary of dominate1.
|
---|
[13749] | 607 |
|
---|
[14705] | 608 | dominate1 = 0;
|
---|
| 609 | dominate2 = 0;
|
---|
[13749] | 610 |
|
---|
[14705] | 611 | int flag; //stores the result of the comparison
|
---|
[13749] | 612 |
|
---|
[14705] | 613 | // Test to determine whether at least a solution violates some constraint
|
---|
| 614 | if(needToCompareViolations(solution1, solution2)) {
|
---|
| 615 | return compareConstraintsViolation(solution1, solution2);
|
---|
| 616 | }
|
---|
[13849] | 617 |
|
---|
[14705] | 618 | // Equal number of violated constraints. Applying a dominance Test then
|
---|
| 619 | double value1, value2;
|
---|
| 620 | for(int i = 0; i < Problem.Objectives; i++) {
|
---|
| 621 | value1 = solution1.Quality[i];
|
---|
| 622 | value2 = solution2.Quality[i];
|
---|
| 623 | if(value1 < value2) {
|
---|
| 624 | flag = -1;
|
---|
| 625 | } else if(value2 < value1) {
|
---|
| 626 | flag = 1;
|
---|
| 627 | } else {
|
---|
| 628 | flag = 0;
|
---|
| 629 | }
|
---|
[13749] | 630 |
|
---|
[14705] | 631 | if(flag == -1) {
|
---|
| 632 | dominate1 = 1;
|
---|
| 633 | }
|
---|
[13749] | 634 |
|
---|
[14705] | 635 | if(flag == 1) {
|
---|
| 636 | dominate2 = 1;
|
---|
[13749] | 637 | }
|
---|
[14705] | 638 | }
|
---|
[13849] | 639 |
|
---|
[14705] | 640 | if(dominate1 == dominate2) {
|
---|
| 641 | return 0; //No one dominate the other
|
---|
| 642 | }
|
---|
| 643 | if(dominate1 == 1) {
|
---|
| 644 | return -1; // solution1 dominate
|
---|
| 645 | }
|
---|
| 646 | return 1; // solution2 dominate
|
---|
| 647 | }
|
---|
[13849] | 648 |
|
---|
[14705] | 649 | private bool needToCompareViolations(SolutionSet solution1, SolutionSet solution2) {
|
---|
| 650 | bool needToCompare;
|
---|
| 651 | needToCompare = (solution1.OverallConstrainViolation < 0) || (solution2.OverallConstrainViolation < 0);
|
---|
[13849] | 652 |
|
---|
[14705] | 653 | return needToCompare;
|
---|
| 654 | }
|
---|
[13849] | 655 |
|
---|
[14705] | 656 | private int compareConstraintsViolation(SolutionSet solution1, SolutionSet solution2) {
|
---|
| 657 | int result;
|
---|
| 658 | double overall1, overall2;
|
---|
| 659 | overall1 = solution1.OverallConstrainViolation;
|
---|
| 660 | overall2 = solution2.OverallConstrainViolation;
|
---|
| 661 |
|
---|
| 662 | if((overall1 < 0) && (overall2 < 0)) {
|
---|
| 663 | if(overall1 > overall2) {
|
---|
| 664 | result = -1;
|
---|
| 665 | } else if(overall2 > overall1) {
|
---|
| 666 | result = 1;
|
---|
| 667 | } else {
|
---|
| 668 | result = 0;
|
---|
[13849] | 669 | }
|
---|
[14705] | 670 | } else if((overall1 == 0) && (overall2 < 0)) {
|
---|
| 671 | result = -1;
|
---|
| 672 | } else if((overall1 < 0) && (overall2 == 0)) {
|
---|
| 673 | result = 1;
|
---|
| 674 | } else {
|
---|
| 675 | result = 0;
|
---|
| 676 | }
|
---|
| 677 | return result;
|
---|
[13749] | 678 | }
|
---|
[14705] | 679 | }
|
---|
[13749] | 680 | }
|
---|
| 681 |
|
---|
| 682 |
|
---|
| 683 |
|
---|