Free cookie consent management tool by TermsFeed Policy Generator

source: branches/EnhancedProgress/HeuristicLab.Optimization.Operators/3.3/MultiObjective/FastNonDominatedSort.cs @ 15529

Last change on this file since 15529 was 15080, checked in by abeham, 7 years ago

#2783: Moved pareto front calculation from MultiObjectiveBasicProblem to a new class DominationCalculator<T>. Changed FastNonDominatedSort to use the new class.

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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
42    #region Parameter properties
43    public IValueLookupParameter<BoolArray> MaximizationParameter {
44      get { return (IValueLookupParameter<BoolArray>)Parameters["Maximization"]; }
45    }
46    public IValueLookupParameter<BoolValue> DominateOnEqualQualitiesParameter {
47      get { return (ValueLookupParameter<BoolValue>)Parameters["DominateOnEqualQualities"]; }
48    }
49    public IScopeTreeLookupParameter<DoubleArray> QualitiesParameter {
50      get { return (IScopeTreeLookupParameter<DoubleArray>)Parameters["Qualities"]; }
51    }
52    public IScopeTreeLookupParameter<IntValue> RankParameter {
53      get { return (IScopeTreeLookupParameter<IntValue>)Parameters["Rank"]; }
54    }
55    #endregion
56
57    [StorableConstructor]
58    protected FastNonDominatedSort(bool deserializing) : base(deserializing) { }
59    protected FastNonDominatedSort(FastNonDominatedSort original, Cloner cloner) : base(original, cloner) { }
60    public FastNonDominatedSort() {
61      Parameters.Add(new ValueLookupParameter<BoolArray>("Maximization", "Whether each objective is maximization or minimization."));
62      Parameters.Add(new ValueLookupParameter<BoolValue>("DominateOnEqualQualities", "Flag which determines wether solutions with equal quality values should be treated as dominated."));
63      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("Qualities", "The qualities of a solution.", 1));
64      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("Rank", "The rank of a solution.", 1));
65    }
66
67    public override IOperation Apply() {
68      bool dominateOnEqualQualities = DominateOnEqualQualitiesParameter.ActualValue.Value;
69      bool[] maximization = MaximizationParameter.ActualValue.ToArray();
70      double[][] qualities = QualitiesParameter.ActualValue.Select(x => x.ToArray()).ToArray();
71      if (qualities == null) throw new InvalidOperationException(Name + ": No qualities found.");
72
73      IScope scope = ExecutionContext.Scope;
74      int populationSize = scope.SubScopes.Count;
75
76      int[] rank;
77      var fronts = DominationCalculator<IScope>.CalculateAllParetoFronts(scope.SubScopes.ToArray(), qualities, maximization, out rank, dominateOnEqualQualities);
78
79      RankParameter.ActualValue = new ItemArray<IntValue>(rank.Select(x => new IntValue(x)));
80
81      scope.SubScopes.Clear();
82
83      for (var i = 0; i < fronts.Count; i++) {
84        Scope frontScope = new Scope("Front " + i);
85        foreach (var p in fronts[i])
86          frontScope.SubScopes.Add(p.Item1);
87        if (frontScope.SubScopes.Count > 0)
88          scope.SubScopes.Add(frontScope);
89      }
90      return base.Apply();
91    }
92
93    public override IDeepCloneable Clone(Cloner cloner) {
94      return new FastNonDominatedSort(this, cloner);
95    }
96
97    [StorableHook(HookType.AfterDeserialization)]
98    private void AfterDeserialization() {
99      // BackwardsCompatibility3.3
100      #region Backwards compatible code, remove with 3.4
101      if (!Parameters.ContainsKey("DominateOnEqualQualities"))
102        Parameters.Add(new ValueLookupParameter<BoolValue>("DominateOnEqualQualities", "Flag which determines wether solutions with equal quality values should be treated as dominated."));
103      #endregion
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.