Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.NSGA2/3.3/FastNonDominatedSort.cs @ 4039

Last change on this file since 4039 was 4039, checked in by abeham, 14 years ago

#1040

  • Ported the other two operators
File size: 6.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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
21using System;
22using System.Collections.Generic;
23using System.Linq;
24using System.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Common;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Parameters;
31
32namespace HeuristicLab.Algorithms.NSGA2 {
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 {
41    private enum DominationResult { Dominates, IsDominated, IsNonDominated };
42
43    public IValueLookupParameter<BoolArray> MaximizationsParameter {
44      get { return (IValueLookupParameter<BoolArray>)Parameters["Maximizations"]; }
45    }
46
47    public IScopeTreeLookupParameter<DoubleArray> QualitiesParameter {
48      get { return (IScopeTreeLookupParameter<DoubleArray>)Parameters["Qualities"]; }
49    }
50
51    public IScopeTreeLookupParameter<IntValue> RankParameter {
52      get { return (IScopeTreeLookupParameter<IntValue>)Parameters["Rank"]; }
53    }
54
55    public FastNonDominatedSort() {
56      Parameters.Add(new ValueLookupParameter<BoolArray>("Maximizations", "Whether each objective is maximization or minimization."));
57      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("Qualities", "The qualities of a solution.", 1));
58      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("Rank", "The rank of a solution.", 1));
59    }
60
61    public override IOperation Apply() {
62      BoolArray maximizations = MaximizationsParameter.ActualValue;
63      ItemArray<DoubleArray> qualities = QualitiesParameter.ActualValue;
64      if (qualities == null) throw new InvalidOperationException(Name + ": No qualities found.");
65
66      IScope scope = ExecutionContext.Scope;
67      int populationSize = scope.SubScopes.Count;
68
69      List<ScopeList> fronts = new List<ScopeList>();
70      Dictionary<IScope, List<int>> dominatedScopes = new Dictionary<IScope, List<int>>();
71      int[] dominationCounter = new int[populationSize];
72      ItemArray<IntValue> rank = new ItemArray<IntValue>(populationSize);
73
74      for (int pI = 0; pI < populationSize - 1; pI++) {
75        IScope p = scope.SubScopes[pI];
76        dominatedScopes[p] = new List<int>();
77        for (int qI = pI + 1; qI < populationSize; qI++) {
78          DominationResult test = Dominates(qualities[pI], qualities[qI], maximizations);
79          if (test == DominationResult.Dominates) {
80            dominatedScopes[p].Add(qI);
81            dominationCounter[qI] += 1;
82          } else if (test == DominationResult.IsDominated) {
83            dominationCounter[pI] += 1;
84            dominatedScopes[scope.SubScopes[qI]].Add(pI);
85          }
86          if (pI == populationSize - 2
87            && qI == populationSize - 1
88            && dominationCounter[qI] == 0) {
89            rank[qI] = new IntValue(0);
90            AddToFront(scope.SubScopes[qI], fronts, 0);
91          }
92        }
93        if (dominationCounter[pI] == 0) {
94          rank[pI] = new IntValue(0);
95          AddToFront(p, fronts, 0);
96        }
97      }
98      int i = 0;
99      while (i < fronts.Count && fronts[i].Count > 0) {
100        ScopeList nextFront = new ScopeList();
101        foreach (IScope p in fronts[i]) {
102          for (int k = 0; k < dominatedScopes[p].Count; k++) {
103            int dominatedScope = dominatedScopes[p][k];
104            dominationCounter[k] -= 1;
105            if (dominationCounter[k] == 0) {
106              rank[k] = new IntValue(i + 1);
107              nextFront.Add(scope.SubScopes[k]);
108            }
109          }
110        }
111        i += 1;
112        fronts.Add(nextFront);
113      }
114
115      RankParameter.ActualValue = rank;
116     
117      scope.SubScopes.Clear();
118     
119      for (i = 0; i < fronts.Count; i++) {
120        Scope frontScope = new Scope("Front " + i);
121        foreach (var p in fronts[i])
122          frontScope.SubScopes.Add(p);
123        if (frontScope.SubScopes.Count > 0)
124          scope.SubScopes.Add(frontScope);
125      }
126      return base.Apply();
127    }
128
129    private DominationResult Dominates(DoubleArray left, DoubleArray right, BoolArray maximizations) {
130      bool leftIsBetter = false, rightIsBetter = false;
131      for (int i = 0; i < left.Length; i++) {
132        if (IsDominated(left[i], right[i], maximizations[i])) rightIsBetter = true;
133        else if (IsDominated(right[i], left[i], maximizations[i])) leftIsBetter = true;
134        if (leftIsBetter && rightIsBetter) break;
135      }
136      if (leftIsBetter && !rightIsBetter) return DominationResult.Dominates;
137      if (!leftIsBetter && rightIsBetter) return DominationResult.IsDominated;
138      return DominationResult.IsNonDominated;
139    }
140
141    /*private bool Dominates(DoubleArray left, DoubleArray right, BoolArray maximizations) {
142      for (int i = 0; i < left.Length; i++) {
143        if (IsDominated(left[i], right[i], maximizations[i]))
144          return false;
145      }
146      return true;
147    }*/
148
149    private bool IsDominated(double left, double right, bool maximization) {
150      return maximization && left < right
151        || !maximization && left > right;
152    }
153
154    private void AddToFront(IScope p, List<ScopeList> fronts, int i) {
155      if (i == fronts.Count) fronts.Add(new ScopeList());
156      fronts[i].Add(p);
157    }
158  }
159}
Note: See TracBrowser for help on using the repository browser.