Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Operators/3.3/MultiObjective/FastNonDominatedSort.cs @ 12144

Last change on this file since 12144 was 12144, checked in by mkommend, 9 years ago

#2321: Removed trailing white space in DominateOnEqualQualities parameter name.

File size: 8.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Optimization.Operators {
33  /// <summary>
34  /// FastNonDominatedSort as described in: Deb, Pratap, Agrawal and Meyarivan, "A Fast and Elitist Multiobjective
35  /// Genetic Algorithm: NSGA-II", IEEE Transactions On Evolutionary Computation, Vol. 6, No. 2, April 2002
36  /// </summary>
37  [Item("FastNonDominatedSort", @"FastNonDominatedSort as described in: Deb, Pratap, Agrawal and Meyarivan, ""A Fast and Elitist Multiobjective
38Genetic Algorithm: NSGA-II"", IEEE Transactions On Evolutionary Computation, Vol. 6, No. 2, April 2002")]
39  [StorableClass]
40  public class FastNonDominatedSort : SingleSuccessorOperator, IMultiObjectiveOperator {
41    private enum DominationResult { Dominates, IsDominated, IsNonDominated };
42
43    #region Parameter properties
44    public IValueLookupParameter<BoolArray> MaximizationParameter {
45      get { return (IValueLookupParameter<BoolArray>)Parameters["Maximization"]; }
46    }
47    public IValueLookupParameter<BoolValue> DominateOnEqualQualitiesParameter {
48      get { return (ValueLookupParameter<BoolValue>)Parameters["DominateOnEqualQualities"]; }
49    }
50    public IScopeTreeLookupParameter<DoubleArray> QualitiesParameter {
51      get { return (IScopeTreeLookupParameter<DoubleArray>)Parameters["Qualities"]; }
52    }
53    public IScopeTreeLookupParameter<IntValue> RankParameter {
54      get { return (IScopeTreeLookupParameter<IntValue>)Parameters["Rank"]; }
55    }
56    #endregion
57
58    [StorableConstructor]
59    protected FastNonDominatedSort(bool deserializing) : base(deserializing) { }
60    protected FastNonDominatedSort(FastNonDominatedSort original, Cloner cloner) : base(original, cloner) { }
61    public FastNonDominatedSort() {
62      Parameters.Add(new ValueLookupParameter<BoolArray>("Maximization", "Whether each objective is maximization or minimization."));
63      Parameters.Add(new ValueLookupParameter<BoolValue>("DominateOnEqualQualities", "Flag which determines wether solutions with equal quality values should be treated as dominated."));
64      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("Qualities", "The qualities of a solution.", 1));
65      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("Rank", "The rank of a solution.", 1));
66    }
67
68    public override IOperation Apply() {
69      bool dominateOnEqualQualities = DominateOnEqualQualitiesParameter.ActualValue.Value;
70      BoolArray maximization = MaximizationParameter.ActualValue;
71      ItemArray<DoubleArray> qualities = QualitiesParameter.ActualValue;
72      if (qualities == null) throw new InvalidOperationException(Name + ": No qualities found.");
73
74      IScope scope = ExecutionContext.Scope;
75      int populationSize = scope.SubScopes.Count;
76
77      List<ScopeList> fronts = new List<ScopeList>();
78      Dictionary<IScope, List<int>> dominatedScopes = new Dictionary<IScope, List<int>>();
79      int[] dominationCounter = new int[populationSize];
80      ItemArray<IntValue> rank = new ItemArray<IntValue>(populationSize);
81
82      for (int pI = 0; pI < populationSize - 1; pI++) {
83        IScope p = scope.SubScopes[pI];
84        if (!dominatedScopes.ContainsKey(p))
85          dominatedScopes[p] = new List<int>();
86        for (int qI = pI + 1; qI < populationSize; qI++) {
87          DominationResult test = Dominates(qualities[pI], qualities[qI], maximization, dominateOnEqualQualities);
88          if (test == DominationResult.Dominates) {
89            dominatedScopes[p].Add(qI);
90            dominationCounter[qI] += 1;
91          } else if (test == DominationResult.IsDominated) {
92            dominationCounter[pI] += 1;
93            if (!dominatedScopes.ContainsKey(scope.SubScopes[qI]))
94              dominatedScopes.Add(scope.SubScopes[qI], new List<int>());
95            dominatedScopes[scope.SubScopes[qI]].Add(pI);
96          }
97          if (pI == populationSize - 2
98            && qI == populationSize - 1
99            && dominationCounter[qI] == 0) {
100            rank[qI] = new IntValue(0);
101            AddToFront(scope.SubScopes[qI], fronts, 0);
102          }
103        }
104        if (dominationCounter[pI] == 0) {
105          rank[pI] = new IntValue(0);
106          AddToFront(p, fronts, 0);
107        }
108      }
109      int i = 0;
110      while (i < fronts.Count && fronts[i].Count > 0) {
111        ScopeList nextFront = new ScopeList();
112        foreach (IScope p in fronts[i]) {
113          if (dominatedScopes.ContainsKey(p)) {
114            for (int k = 0; k < dominatedScopes[p].Count; k++) {
115              int dominatedScope = dominatedScopes[p][k];
116              dominationCounter[dominatedScope] -= 1;
117              if (dominationCounter[dominatedScope] == 0) {
118                rank[dominatedScope] = new IntValue(i + 1);
119                nextFront.Add(scope.SubScopes[dominatedScope]);
120              }
121            }
122          }
123        }
124        i += 1;
125        fronts.Add(nextFront);
126      }
127
128      RankParameter.ActualValue = rank;
129
130      scope.SubScopes.Clear();
131
132      for (i = 0; i < fronts.Count; i++) {
133        Scope frontScope = new Scope("Front " + i);
134        foreach (var p in fronts[i])
135          frontScope.SubScopes.Add(p);
136        if (frontScope.SubScopes.Count > 0)
137          scope.SubScopes.Add(frontScope);
138      }
139      return base.Apply();
140    }
141
142    private static DominationResult Dominates(DoubleArray left, DoubleArray right, BoolArray maximizations, bool dominateOnEqualQualities) {
143      if (dominateOnEqualQualities && left.SequenceEqual(right)) return DominationResult.Dominates;
144
145      bool leftIsBetter = false, rightIsBetter = false;
146      for (int i = 0; i < left.Length; i++) {
147        if (IsDominated(left[i], right[i], maximizations[i])) rightIsBetter = true;
148        else if (IsDominated(right[i], left[i], maximizations[i])) leftIsBetter = true;
149        if (leftIsBetter && rightIsBetter) break;
150      }
151
152      if (leftIsBetter && !rightIsBetter) return DominationResult.Dominates;
153      if (!leftIsBetter && rightIsBetter) return DominationResult.IsDominated;
154      return DominationResult.IsNonDominated;
155    }
156
157    private static bool IsDominated(double left, double right, bool maximization) {
158      return maximization && left < right
159        || !maximization && left > right;
160    }
161
162    private static void AddToFront(IScope p, List<ScopeList> fronts, int i) {
163      if (i == fronts.Count) fronts.Add(new ScopeList());
164      fronts[i].Add(p);
165    }
166
167    public override IDeepCloneable Clone(Cloner cloner) {
168      return new FastNonDominatedSort(this, cloner);
169    }
170
171    [StorableHook(HookType.AfterDeserialization)]
172    private void AfterDeserialization() {
173      // BackwardsCompatibility3.3
174      #region Backwards compatible code, remove with 3.4
175      if (!Parameters.ContainsKey("DominateOnEqualQualities"))
176        Parameters.Add(new ValueLookupParameter<BoolValue>("DominateOnEqualQualities", "Flag which determines wether solutions with equal quality values should be treated as dominated."));
177      #endregion
178    }
179  }
180}
Note: See TracBrowser for help on using the repository browser.