Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1040

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