Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2994-AutoDiffForIntervals/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/MOCMAEvolutionStrategy.cs @ 18183

Last change on this file since 18183 was 17209, checked in by gkronber, 5 years ago

#2994: merged r17132:17198 from trunk to branch

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