Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Optimization.Operators/3.3/MultiObjective/FastNonDominatedSort.cs @ 16662

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

#2925: merged all changes from trunk to branch (up to r16659)

File size: 4.9 KB
RevLine 
[5143]1#region License Information
2/* HeuristicLab
[16662]3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5143]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
[12123]23using System.Linq;
[5143]24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
[16662]29using HEAL.Attic;
[5143]30
31namespace HeuristicLab.Optimization.Operators {
32  /// <summary>
33  /// FastNonDominatedSort as described in: Deb, Pratap, Agrawal and Meyarivan, "A Fast and Elitist Multiobjective
34  /// Genetic Algorithm: NSGA-II", IEEE Transactions On Evolutionary Computation, Vol. 6, No. 2, April 2002
35  /// </summary>
36  [Item("FastNonDominatedSort", @"FastNonDominatedSort as described in: Deb, Pratap, Agrawal and Meyarivan, ""A Fast and Elitist Multiobjective
37Genetic Algorithm: NSGA-II"", IEEE Transactions On Evolutionary Computation, Vol. 6, No. 2, April 2002")]
[16662]38  [StorableType("0D1919E6-FCDE-4216-831A-E6427C3D986E")]
[11970]39  public class FastNonDominatedSort : SingleSuccessorOperator, IMultiObjectiveOperator {
[5143]40
[12123]41    #region Parameter properties
[5143]42    public IValueLookupParameter<BoolArray> MaximizationParameter {
43      get { return (IValueLookupParameter<BoolArray>)Parameters["Maximization"]; }
44    }
[12123]45    public IValueLookupParameter<BoolValue> DominateOnEqualQualitiesParameter {
[12144]46      get { return (ValueLookupParameter<BoolValue>)Parameters["DominateOnEqualQualities"]; }
[12123]47    }
[5143]48    public IScopeTreeLookupParameter<DoubleArray> QualitiesParameter {
49      get { return (IScopeTreeLookupParameter<DoubleArray>)Parameters["Qualities"]; }
50    }
51    public IScopeTreeLookupParameter<IntValue> RankParameter {
52      get { return (IScopeTreeLookupParameter<IntValue>)Parameters["Rank"]; }
53    }
[12123]54    #endregion
[5143]55
56    [StorableConstructor]
[16662]57    protected FastNonDominatedSort(StorableConstructorFlag _) : base(_) { }
[5143]58    protected FastNonDominatedSort(FastNonDominatedSort original, Cloner cloner) : base(original, cloner) { }
59    public FastNonDominatedSort() {
60      Parameters.Add(new ValueLookupParameter<BoolArray>("Maximization", "Whether each objective is maximization or minimization."));
[12144]61      Parameters.Add(new ValueLookupParameter<BoolValue>("DominateOnEqualQualities", "Flag which determines wether solutions with equal quality values should be treated as dominated."));
[5143]62      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("Qualities", "The qualities of a solution.", 1));
63      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("Rank", "The rank of a solution.", 1));
64    }
65
66    public override IOperation Apply() {
[12123]67      bool dominateOnEqualQualities = DominateOnEqualQualitiesParameter.ActualValue.Value;
[12580]68      bool[] maximization = MaximizationParameter.ActualValue.ToArray();
69      double[][] qualities = QualitiesParameter.ActualValue.Select(x => x.ToArray()).ToArray();
[5143]70      if (qualities == null) throw new InvalidOperationException(Name + ": No qualities found.");
71
72      IScope scope = ExecutionContext.Scope;
73      int populationSize = scope.SubScopes.Count;
74
[15080]75      int[] rank;
76      var fronts = DominationCalculator<IScope>.CalculateAllParetoFronts(scope.SubScopes.ToArray(), qualities, maximization, out rank, dominateOnEqualQualities);
[5143]77
[15080]78      RankParameter.ActualValue = new ItemArray<IntValue>(rank.Select(x => new IntValue(x)));
[5143]79
80      scope.SubScopes.Clear();
81
[15080]82      for (var i = 0; i < fronts.Count; i++) {
[5143]83        Scope frontScope = new Scope("Front " + i);
84        foreach (var p in fronts[i])
[15080]85          frontScope.SubScopes.Add(p.Item1);
[5143]86        if (frontScope.SubScopes.Count > 0)
87          scope.SubScopes.Add(frontScope);
88      }
89      return base.Apply();
90    }
91
92    public override IDeepCloneable Clone(Cloner cloner) {
93      return new FastNonDominatedSort(this, cloner);
94    }
[12123]95
96    [StorableHook(HookType.AfterDeserialization)]
97    private void AfterDeserialization() {
98      // BackwardsCompatibility3.3
99      #region Backwards compatible code, remove with 3.4
[12144]100      if (!Parameters.ContainsKey("DominateOnEqualQualities"))
101        Parameters.Add(new ValueLookupParameter<BoolValue>("DominateOnEqualQualities", "Flag which determines wether solutions with equal quality values should be treated as dominated."));
[12123]102      #endregion
103    }
[5143]104  }
105}
Note: See TracBrowser for help on using the repository browser.