Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/MOCMAEvolutionStrategy.cs @ 17315

Last change on this file since 17315 was 17315, checked in by abeham, 5 years ago

#2521: reverted r17309

File size: 25.4 KB
RevLine 
[15045]1#region License Information
2/* HeuristicLab
[17226]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[15045]4 * and the BEACON Center for the Study of Evolution in Action.
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21#endregion
22
23using System;
24using System.Collections.Generic;
25using System.Linq;
26using System.Threading;
[16807]27using HEAL.Attic;
[15045]28using HeuristicLab.Analysis;
29using HeuristicLab.Common;
30using HeuristicLab.Core;
31using HeuristicLab.Data;
32using HeuristicLab.Encodings.RealVectorEncoding;
33using HeuristicLab.Optimization;
34using HeuristicLab.Parameters;
35using HeuristicLab.Random;
36
37namespace HeuristicLab.Algorithms.MOCMAEvolutionStrategy {
[15226]38  [Item("Multi-Objective CMA Evolution Strategy (MOCMAES)", "A multi objective evolution strategy based on covariance matrix adaptation. Code is based on 'Covariance Matrix Adaptation for Multi - objective Optimization' by Igel, Hansen and Roth")]
[15045]39  [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 210)]
[16723]40  [StorableType("C10264E3-E4C6-4735-8E94-0DC116E8908D")]
[15045]41  public class MOCMAEvolutionStrategy : BasicAlgorithm {
[15176]42    public override Type ProblemType {
[16807]43      get { return typeof(MultiObjectiveProblem<RealVectorEncoding, RealVector>); }
[15045]44    }
[16807]45    public new MultiObjectiveProblem<RealVectorEncoding, RealVector> Problem {
46      get { return (MultiObjectiveProblem<RealVectorEncoding, RealVector>)base.Problem; }
[15045]47      set { base.Problem = value; }
48    }
[15176]49    public override bool SupportsPause {
[15045]50      get { return true; }
51    }
52
[17225]53    public RealVectorEncoding Encoding {
54      get { return Problem.Encoding; }
55    }
56
[15176]57    #region Storable fields
[15045]58    [Storable]
59    private IRandom random = new MersenneTwister();
60    [Storable]
61    private NormalDistributedRandom gauss;
62    [Storable]
63    private Individual[] solutions;
64    [Storable]
65    private double stepSizeLearningRate; //=cp learning rate in [0,1]
66    [Storable]
67    private double stepSizeDampeningFactor; //d
68    [Storable]
[17225]69    private double targetSuccessProbability; // p^target_succ
[15045]70    [Storable]
[17225]71    private double evolutionPathLearningRate; //cc
[15045]72    [Storable]
[17225]73    private double covarianceMatrixLearningRate; //ccov
[15045]74    [Storable]
75    private double covarianceMatrixUnlearningRate;
76    [Storable]
77    private double successThreshold; //ptresh
78    #endregion
79
80    #region ParameterNames
81    private const string MaximumRuntimeName = "Maximum Runtime";
82    private const string SeedName = "Seed";
83    private const string SetSeedRandomlyName = "SetSeedRandomly";
84    private const string PopulationSizeName = "PopulationSize";
85    private const string MaximumGenerationsName = "MaximumGenerations";
86    private const string MaximumEvaluatedSolutionsName = "MaximumEvaluatedSolutions";
87    private const string InitialSigmaName = "InitialSigma";
88    private const string IndicatorName = "Indicator";
89
90    private const string EvaluationsResultName = "Evaluations";
91    private const string IterationsResultName = "Generations";
92    private const string TimetableResultName = "Timetable";
93    private const string HypervolumeResultName = "Hypervolume";
94    private const string GenerationalDistanceResultName = "Generational Distance";
95    private const string InvertedGenerationalDistanceResultName = "Inverted Generational Distance";
96    private const string CrowdingResultName = "Crowding";
97    private const string SpacingResultName = "Spacing";
98    private const string CurrentFrontResultName = "Pareto Front";
99    private const string BestHypervolumeResultName = "Best Hypervolume";
100    private const string BestKnownHypervolumeResultName = "Best known hypervolume";
101    private const string DifferenceToBestKnownHypervolumeResultName = "Absolute Distance to BestKnownHypervolume";
102    private const string ScatterPlotResultName = "ScatterPlot";
103    #endregion
104
105    #region ParameterProperties
[15176]106    public IFixedValueParameter<IntValue> MaximumRuntimeParameter {
[15045]107      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumRuntimeName]; }
108    }
[15176]109    public IFixedValueParameter<IntValue> SeedParameter {
[15045]110      get { return (IFixedValueParameter<IntValue>)Parameters[SeedName]; }
111    }
[15176]112    public FixedValueParameter<BoolValue> SetSeedRandomlyParameter {
[15045]113      get { return (FixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyName]; }
114    }
[15176]115    public IFixedValueParameter<IntValue> PopulationSizeParameter {
[15045]116      get { return (IFixedValueParameter<IntValue>)Parameters[PopulationSizeName]; }
117    }
[15176]118    public IFixedValueParameter<IntValue> MaximumGenerationsParameter {
[15045]119      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumGenerationsName]; }
120    }
[15176]121    public IFixedValueParameter<IntValue> MaximumEvaluatedSolutionsParameter {
[15045]122      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumEvaluatedSolutionsName]; }
123    }
[15176]124    public IValueParameter<DoubleArray> InitialSigmaParameter {
[15045]125      get { return (IValueParameter<DoubleArray>)Parameters[InitialSigmaName]; }
126    }
[15176]127    public IConstrainedValueParameter<IIndicator> IndicatorParameter {
[15045]128      get { return (IConstrainedValueParameter<IIndicator>)Parameters[IndicatorName]; }
129    }
130    #endregion
131
132    #region Properties
[15176]133    public int MaximumRuntime {
[15045]134      get { return MaximumRuntimeParameter.Value.Value; }
135      set { MaximumRuntimeParameter.Value.Value = value; }
136    }
[15176]137    public int Seed {
[15045]138      get { return SeedParameter.Value.Value; }
139      set { SeedParameter.Value.Value = value; }
140    }
[15176]141    public bool SetSeedRandomly {
[15045]142      get { return SetSeedRandomlyParameter.Value.Value; }
143      set { SetSeedRandomlyParameter.Value.Value = value; }
144    }
[15176]145    public int PopulationSize {
[15045]146      get { return PopulationSizeParameter.Value.Value; }
147      set { PopulationSizeParameter.Value.Value = value; }
148    }
[15176]149    public int MaximumGenerations {
[15045]150      get { return MaximumGenerationsParameter.Value.Value; }
151      set { MaximumGenerationsParameter.Value.Value = value; }
152    }
[15176]153    public int MaximumEvaluatedSolutions {
[15045]154      get { return MaximumEvaluatedSolutionsParameter.Value.Value; }
155      set { MaximumEvaluatedSolutionsParameter.Value.Value = value; }
156    }
[15176]157    public DoubleArray InitialSigma {
[15045]158      get { return InitialSigmaParameter.Value; }
159      set { InitialSigmaParameter.Value = value; }
160    }
[15176]161    public IIndicator Indicator {
[15045]162      get { return IndicatorParameter.Value; }
163      set { IndicatorParameter.Value = value; }
164    }
165
[17225]166    public double StepSizeLearningRate {
167      get { return stepSizeLearningRate; }
168    }
169    public double StepSizeDampeningFactor {
170      get { return stepSizeDampeningFactor; }
171    }
172    public double TargetSuccessProbability {
173      get { return targetSuccessProbability; }
174    }
175    public double EvolutionPathLearningRate {
176      get { return evolutionPathLearningRate; }
177    }
178    public double CovarianceMatrixLearningRate {
179      get { return covarianceMatrixLearningRate; }
180    }
181    public double CovarianceMatrixUnlearningRate {
182      get { return covarianceMatrixUnlearningRate; }
183    }
184    public double SuccessThreshold {
185      get { return successThreshold; }
186    }
[15045]187    #endregion
188
189    #region ResultsProperties
[15176]190    private int ResultsEvaluations {
[15045]191      get { return ((IntValue)Results[EvaluationsResultName].Value).Value; }
192      set { ((IntValue)Results[EvaluationsResultName].Value).Value = value; }
193    }
[15176]194    private int ResultsIterations {
[15045]195      get { return ((IntValue)Results[IterationsResultName].Value).Value; }
196      set { ((IntValue)Results[IterationsResultName].Value).Value = value; }
197    }
198    #region Datatable
[15176]199    private DataTable ResultsQualities {
[15045]200      get { return (DataTable)Results[TimetableResultName].Value; }
201    }
[15176]202    private DataRow ResultsBestHypervolumeDataLine {
[15045]203      get { return ResultsQualities.Rows[BestHypervolumeResultName]; }
204    }
[15176]205    private DataRow ResultsHypervolumeDataLine {
[15045]206      get { return ResultsQualities.Rows[HypervolumeResultName]; }
207    }
[15176]208    private DataRow ResultsGenerationalDistanceDataLine {
[15045]209      get { return ResultsQualities.Rows[GenerationalDistanceResultName]; }
210    }
[15176]211    private DataRow ResultsInvertedGenerationalDistanceDataLine {
[15045]212      get { return ResultsQualities.Rows[InvertedGenerationalDistanceResultName]; }
213    }
[15176]214    private DataRow ResultsCrowdingDataLine {
[15045]215      get { return ResultsQualities.Rows[CrowdingResultName]; }
216    }
[15176]217    private DataRow ResultsSpacingDataLine {
[15045]218      get { return ResultsQualities.Rows[SpacingResultName]; }
219    }
[15176]220    private DataRow ResultsHypervolumeDifferenceDataLine {
[15045]221      get { return ResultsQualities.Rows[DifferenceToBestKnownHypervolumeResultName]; }
222    }
223    #endregion
224    //QualityIndicators
[15176]225    private double ResultsHypervolume {
[15045]226      get { return ((DoubleValue)Results[HypervolumeResultName].Value).Value; }
227      set { ((DoubleValue)Results[HypervolumeResultName].Value).Value = value; }
228    }
[15176]229    private double ResultsGenerationalDistance {
[15045]230      get { return ((DoubleValue)Results[GenerationalDistanceResultName].Value).Value; }
231      set { ((DoubleValue)Results[GenerationalDistanceResultName].Value).Value = value; }
232    }
[15176]233    private double ResultsInvertedGenerationalDistance {
[15045]234      get { return ((DoubleValue)Results[InvertedGenerationalDistanceResultName].Value).Value; }
235      set { ((DoubleValue)Results[InvertedGenerationalDistanceResultName].Value).Value = value; }
236    }
[15176]237    private double ResultsCrowding {
[15045]238      get { return ((DoubleValue)Results[CrowdingResultName].Value).Value; }
239      set { ((DoubleValue)Results[CrowdingResultName].Value).Value = value; }
240    }
[15176]241    private double ResultsSpacing {
[15045]242      get { return ((DoubleValue)Results[SpacingResultName].Value).Value; }
243      set { ((DoubleValue)Results[SpacingResultName].Value).Value = value; }
244    }
[15176]245    private double ResultsBestHypervolume {
[15045]246      get { return ((DoubleValue)Results[BestHypervolumeResultName].Value).Value; }
247      set { ((DoubleValue)Results[BestHypervolumeResultName].Value).Value = value; }
248    }
[15176]249    private double ResultsBestKnownHypervolume {
[15045]250      get { return ((DoubleValue)Results[BestKnownHypervolumeResultName].Value).Value; }
251      set { ((DoubleValue)Results[BestKnownHypervolumeResultName].Value).Value = value; }
252    }
[15176]253    private double ResultsDifferenceBestKnownHypervolume {
[15045]254      get { return ((DoubleValue)Results[DifferenceToBestKnownHypervolumeResultName].Value).Value; }
255      set { ((DoubleValue)Results[DifferenceToBestKnownHypervolumeResultName].Value).Value = value; }
256    }
257    //Solutions
[15176]258    private DoubleMatrix ResultsSolutions {
[15045]259      get { return (DoubleMatrix)Results[CurrentFrontResultName].Value; }
260      set { Results[CurrentFrontResultName].Value = value; }
261    }
[15203]262    private ParetoFrontScatterPlot ResultsScatterPlot {
263      get { return (ParetoFrontScatterPlot)Results[ScatterPlotResultName].Value; }
[15045]264      set { Results[ScatterPlotResultName].Value = value; }
265    }
266    #endregion
267
268    #region Constructors
269    public MOCMAEvolutionStrategy() {
270      Parameters.Add(new FixedValueParameter<IntValue>(MaximumRuntimeName, "The maximum runtime in seconds after which the algorithm stops. Use -1 to specify no limit for the runtime", new IntValue(3600)));
271      Parameters.Add(new FixedValueParameter<IntValue>(SeedName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
272      Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
273      Parameters.Add(new FixedValueParameter<IntValue>(PopulationSizeName, "λ (lambda) - the size of the offspring population.", new IntValue(20)));
[17225]274      Parameters.Add(new ValueParameter<DoubleArray>(InitialSigmaName, "The initial sigma can be a single value or a value for each dimension. All values need to be > 0.", new DoubleArray(new[] {0.5})));
[15045]275      Parameters.Add(new FixedValueParameter<IntValue>(MaximumGenerationsName, "The maximum number of generations which should be processed.", new IntValue(1000)));
276      Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluatedSolutionsName, "The maximum number of evaluated solutions that should be computed.", new IntValue(int.MaxValue)));
[17225]277      var set = new ItemSet<IIndicator> {new HypervolumeIndicator(), new CrowdingIndicator(), new MinimalDistanceIndicator()};
[15045]278      Parameters.Add(new ConstrainedValueParameter<IIndicator>(IndicatorName, "The selection mechanism on non-dominated solutions", set, set.First()));
279    }
280
281    [StorableConstructor]
[16723]282    protected MOCMAEvolutionStrategy(StorableConstructorFlag _) : base(_) { }
[15045]283
284    protected MOCMAEvolutionStrategy(MOCMAEvolutionStrategy original, Cloner cloner) : base(original, cloner) {
285      random = cloner.Clone(original.random);
286      gauss = cloner.Clone(original.gauss);
[15089]287      solutions = original.solutions != null ? original.solutions.Select(cloner.Clone).ToArray() : null;
[15045]288      stepSizeLearningRate = original.stepSizeLearningRate;
289      stepSizeDampeningFactor = original.stepSizeDampeningFactor;
290      targetSuccessProbability = original.targetSuccessProbability;
291      evolutionPathLearningRate = original.evolutionPathLearningRate;
292      covarianceMatrixLearningRate = original.covarianceMatrixLearningRate;
293      covarianceMatrixUnlearningRate = original.covarianceMatrixUnlearningRate;
294      successThreshold = original.successThreshold;
295    }
296
[17225]297    public override IDeepCloneable Clone(Cloner cloner) {
298      return new MOCMAEvolutionStrategy(this, cloner);
299    }
[15045]300    #endregion
301
302    #region Initialization
303    protected override void Initialize(CancellationToken cancellationToken) {
[16723]304      if (SetSeedRandomly) Seed = RandomSeedGenerator.GetSeed();
[15045]305      random.Reset(Seed);
306      gauss = new NormalDistributedRandom(random, 0, 1);
307
308      InitResults();
309      InitStrategy();
310      InitSolutions();
311      Analyze();
312
313      ResultsIterations = 1;
314    }
315    private Individual InitializeIndividual(RealVector x) {
316      var zeros = new RealVector(x.Length);
317      var c = new double[x.Length, x.Length];
318      var sigma = InitialSigma.Max();
319      for (var i = 0; i < x.Length; i++) {
320        var d = InitialSigma[i % InitialSigma.Length] / sigma;
321        c[i, i] = d * d;
322      }
323      return new Individual(x, targetSuccessProbability, sigma, zeros, c, this);
324    }
325    private void InitSolutions() {
326      solutions = new Individual[PopulationSize];
327      for (var i = 0; i < PopulationSize; i++) {
[17261]328        var x = new RealVector(Encoding.Length); // Uniform distribution in all dimensions assumed.
[17225]329        var bounds = Encoding.Bounds;
330        for (var j = 0; j < Encoding.Length; j++) {
[15045]331          var dim = j % bounds.Rows;
332          x[j] = random.NextDouble() * (bounds[dim, 1] - bounds[dim, 0]) + bounds[dim, 0];
333        }
334        solutions[i] = InitializeIndividual(x);
335        PenalizeEvaluate(solutions[i]);
336      }
[15205]337      ResultsEvaluations += solutions.Length;
[15045]338    }
339    private void InitStrategy() {
340      const int lambda = 1;
[17225]341      double n = Encoding.Length;
[15045]342      targetSuccessProbability = 1.0 / (5.0 + Math.Sqrt(lambda) / 2.0);
343      stepSizeDampeningFactor = 1.0 + n / (2.0 * lambda);
344      stepSizeLearningRate = targetSuccessProbability * lambda / (2.0 + targetSuccessProbability * lambda);
345      evolutionPathLearningRate = 2.0 / (n + 2.0);
346      covarianceMatrixLearningRate = 2.0 / (n * n + 6.0);
347      covarianceMatrixUnlearningRate = 0.4 / (Math.Pow(n, 1.6) + 1);
348      successThreshold = 0.44;
349    }
350    private void InitResults() {
[17261]351      Results.Add(new Result(IterationsResultName, "The number of generations evaluated", new IntValue(0)));
352      Results.Add(new Result(EvaluationsResultName, "The number of function evaluations performed", new IntValue(0)));
353      Results.Add(new Result(HypervolumeResultName, "The hyper volume of the current front considering the reference point defined in the Problem", new DoubleValue(0.0)));
354      Results.Add(new Result(BestHypervolumeResultName, "The best hyper volume of the current run considering the reference point defined in the Problem", new DoubleValue(0.0)));
355      Results.Add(new Result(BestKnownHypervolumeResultName, "The best known hyper volume considering the reference point defined in the Problem", new DoubleValue(double.NaN)));
356      Results.Add(new Result(DifferenceToBestKnownHypervolumeResultName, "The difference between the current and the best known hyper volume", new DoubleValue(double.NaN)));
357      Results.Add(new Result(GenerationalDistanceResultName, "The generational distance to an optimal Pareto front defined in the Problem", new DoubleValue(double.NaN)));
358      Results.Add(new Result(InvertedGenerationalDistanceResultName, "The inverted generational distance to an optimal Pareto front defined in the Problem", new DoubleValue(double.NaN)));
[15045]359      Results.Add(new Result(CrowdingResultName, "The average crowding value for the current front (excluding infinities)", new DoubleValue(0.0)));
360      Results.Add(new Result(SpacingResultName, "The spacing for the current front (excluding infinities)", new DoubleValue(0.0)));
361
362      var table = new DataTable("QualityIndicators");
363      table.Rows.Add(new DataRow(BestHypervolumeResultName));
364      table.Rows.Add(new DataRow(HypervolumeResultName));
365      table.Rows.Add(new DataRow(CrowdingResultName));
366      table.Rows.Add(new DataRow(GenerationalDistanceResultName));
367      table.Rows.Add(new DataRow(InvertedGenerationalDistanceResultName));
368      table.Rows.Add(new DataRow(DifferenceToBestKnownHypervolumeResultName));
369      table.Rows.Add(new DataRow(SpacingResultName));
[17261]370      Results.Add(new Result(TimetableResultName, "Different quality measures in a time series", table));
[15045]371      Results.Add(new Result(CurrentFrontResultName, "The current front", new DoubleMatrix()));
[17261]372      Results.Add(new Result(ScatterPlotResultName, "A scatter plot displaying the evaluated solutions and (if available) the analytically optimal front", new ParetoFrontScatterPlot()));
[15045]373
[17225]374      var problem = Problem;
[15045]375      if (problem == null) return;
[17225]376      var bkf = problem.BestKnownFront == null ? null : problem.BestKnownFront.ToArray();
377      if (bkf != null && problem.ReferencePoint != null) {
[17315]378        ResultsBestKnownHypervolume = HypervolumeCalculator.CalculateHypervolume(bkf, problem.ReferencePoint, Problem.Maximization);
[15045]379        ResultsDifferenceBestKnownHypervolume = ResultsBestKnownHypervolume;
380      }
[17225]381      ResultsScatterPlot = new ParetoFrontScatterPlot(new double[0][], new double[0][], bkf, Problem.Objectives, Problem.Encoding.Length);
[15045]382    }
383    #endregion
384
385    #region Mainloop
386    protected override void Run(CancellationToken cancellationToken) {
[15335]387      while (ResultsIterations < MaximumGenerations && ResultsEvaluations < MaximumEvaluatedSolutions) {
[15045]388        try {
389          Iterate();
390          ResultsIterations++;
391          cancellationToken.ThrowIfCancellationRequested();
[15176]392        } finally {
[15045]393          Analyze();
394        }
395      }
396    }
397    private void Iterate() {
398      var offspring = solutions.Select(i => {
399        var o = new Individual(i);
400        o.Mutate(gauss);
401        PenalizeEvaluate(o);
402        return o;
403      });
[15205]404      ResultsEvaluations += solutions.Length;
[15045]405      var parents = solutions.Concat(offspring).ToArray();
406      SelectParents(parents, solutions.Length);
407      UpdatePopulation(parents);
408    }
409    protected override void OnExecutionTimeChanged() {
410      base.OnExecutionTimeChanged();
411      if (CancellationTokenSource == null) return;
412      if (MaximumRuntime == -1) return;
413      if (ExecutionTime.TotalSeconds > MaximumRuntime) CancellationTokenSource.Cancel();
414    }
415    #endregion
416
417    #region Evaluation
418    private void PenalizeEvaluate(Individual individual) {
419      if (IsFeasable(individual.Mean)) {
420        individual.Fitness = Evaluate(individual.Mean);
421        individual.PenalizedFitness = individual.Fitness;
422      } else {
423        var t = ClosestFeasible(individual.Mean);
424        individual.Fitness = Evaluate(t);
425        individual.PenalizedFitness = Penalize(individual.Mean, t, individual.Fitness);
426      }
427    }
428    private double[] Evaluate(RealVector x) {
[16807]429      var res = Problem.Evaluate(x, random);
[15045]430      return res;
431    }
432    private double[] Penalize(RealVector x, RealVector t, IEnumerable<double> fitness) {
433      var penalty = x.Zip(t, (a, b) => (a - b) * (a - b)).Sum() * 1E-6;
434      return fitness.Select((v, i) => Problem.Maximization[i] ? v - penalty : v + penalty).ToArray();
435    }
436    private RealVector ClosestFeasible(RealVector x) {
[17225]437      var bounds = Encoding.Bounds;
[15045]438      var r = new RealVector(x.Length);
439      for (var i = 0; i < x.Length; i++) {
440        var dim = i % bounds.Rows;
441        r[i] = Math.Min(Math.Max(bounds[dim, 0], x[i]), bounds[dim, 1]);
442      }
443      return r;
444    }
445    private bool IsFeasable(RealVector offspring) {
[17225]446      var bounds = Encoding.Bounds;
[15045]447      for (var i = 0; i < offspring.Length; i++) {
448        var dim = i % bounds.Rows;
449        if (bounds[dim, 0] > offspring[i] || offspring[i] > bounds[dim, 1]) return false;
450      }
451      return true;
452    }
453    #endregion
454
455    private void SelectParents(IReadOnlyList<Individual> parents, int length) {
[17261]456      //perform a non-dominated sort to assign the rank to every element
[15089]457      int[] ranks;
[17315]458      var fronts = DominationCalculator.CalculateAllParetoFronts(parents.ToArray(), parents.Select(i => i.PenalizedFitness).ToArray(), Problem.Maximization, out ranks);
[15045]459
460      //deselect the highest rank fronts until we would end up with less or equal mu elements
461      var rank = fronts.Count - 1;
462      var popSize = parents.Count;
463      while (popSize - fronts[rank].Count >= length) {
464        var front = fronts[rank];
[15089]465        foreach (var i in front) i.Item1.Selected = false;
[15045]466        popSize -= front.Count;
467        rank--;
468      }
469
[17261]470      //now use the indicator to deselect the approximately worst elements of the last selected front
[15089]471      var front1 = fronts[rank].OrderBy(x => x.Item1.PenalizedFitness[0]).ToList();
[15045]472      for (; popSize > length; popSize--) {
[15089]473        var lc = Indicator.LeastContributer(front1.Select(i => i.Item1).ToArray(), Problem);
474        front1[lc].Item1.Selected = false;
[15045]475        front1.Swap(lc, front1.Count - 1);
476        front1.RemoveAt(front1.Count - 1);
477      }
478    }
479
480    private void UpdatePopulation(IReadOnlyList<Individual> parents) {
481      foreach (var p in parents.Skip(solutions.Length).Where(i => i.Selected))
482        p.UpdateAsOffspring();
483      for (var i = 0; i < solutions.Length; i++)
484        if (parents[i].Selected)
485          parents[i].UpdateAsParent(parents[i + solutions.Length].Selected);
486      solutions = parents.Where(p => p.Selected).ToArray();
487    }
488
489    private void Analyze() {
[17225]490      var qualities = solutions.Select(x => x.Fitness).ToArray();
491
492      //to do check for side effects
493      ResultsScatterPlot = new ParetoFrontScatterPlot(qualities, solutions.Select(x => x.Mean.ToArray()).ToArray(), ResultsScatterPlot.ParetoFront, ResultsScatterPlot.Objectives, ResultsScatterPlot.ProblemSize);
[15045]494      ResultsSolutions = solutions.Select(x => x.Mean.ToArray()).ToMatrix();
495
[17225]496      var problem = Problem as MultiObjectiveProblem<RealVectorEncoding, RealVector>;
[15045]497      if (problem == null) return;
498
[17225]499
500      if (qualities.Length == 0) return;
501      ResultsCrowding = CrowdingCalculator.CalculateCrowding(qualities);
[17261]502      ResultsSpacing = SpacingAnalyzer.CalculateSpacing(qualities);
[17225]503
504
[17261]505      ResultsGenerationalDistance = problem.BestKnownFront != null ? GenerationalDistanceAnalyzer.CalculateGenerationalDistance(qualities, problem.BestKnownFront, 1) : double.NaN;
506      ResultsInvertedGenerationalDistance = problem.BestKnownFront != null ? GenerationalDistanceAnalyzer.CalculateInverseGenerationalDistance(qualities, problem.BestKnownFront, 1) : double.NaN;
[17225]507      ResultsHypervolume = problem.ReferencePoint != null ? HypervolumeCalculator.CalculateHypervolume(qualities, problem.ReferencePoint, Problem.Maximization) : double.NaN;
[15045]508      ResultsBestHypervolume = Math.Max(ResultsHypervolume, ResultsBestHypervolume);
509      ResultsDifferenceBestKnownHypervolume = ResultsBestKnownHypervolume - ResultsBestHypervolume;
510
511      ResultsBestHypervolumeDataLine.Values.Add(ResultsBestHypervolume);
512      ResultsHypervolumeDataLine.Values.Add(ResultsHypervolume);
513      ResultsCrowdingDataLine.Values.Add(ResultsCrowding);
514      ResultsGenerationalDistanceDataLine.Values.Add(ResultsGenerationalDistance);
515      ResultsInvertedGenerationalDistanceDataLine.Values.Add(ResultsInvertedGenerationalDistance);
516      ResultsSpacingDataLine.Values.Add(ResultsSpacing);
517      ResultsHypervolumeDifferenceDataLine.Values.Add(ResultsDifferenceBestKnownHypervolume);
518
[16807]519      Problem.Analyze(solutions.Select(x => x.Mean).ToArray(),
[15045]520        solutions.Select(x => x.Fitness).ToArray(),
521        Results,
522        random);
523    }
524  }
[17225]525}
Note: See TracBrowser for help on using the repository browser.