Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17189 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HEAL.Attic;
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")]
38  [StorableType("0D1919E6-FCDE-4216-831A-E6427C3D986E")]
39  public class FastNonDominatedSort : SingleSuccessorOperator, IMultiObjectiveOperator {
40
41    #region Parameter properties
42    public IValueLookupParameter<BoolArray> MaximizationParameter {
43      get { return (IValueLookupParameter<BoolArray>)Parameters["Maximization"]; }
44    }
45    public IValueLookupParameter<BoolValue> DominateOnEqualQualitiesParameter {
46      get { return (ValueLookupParameter<BoolValue>)Parameters["DominateOnEqualQualities"]; }
47    }
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    }
54    #endregion
55
56    [StorableConstructor]
57    protected FastNonDominatedSort(StorableConstructorFlag _) : base(_) { }
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."));
61      Parameters.Add(new ValueLookupParameter<BoolValue>("DominateOnEqualQualities", "Flag which determines wether solutions with equal quality values should be treated as dominated."));
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() {
67      bool dominateOnEqualQualities = DominateOnEqualQualitiesParameter.ActualValue.Value;
68      bool[] maximization = MaximizationParameter.ActualValue.ToArray();
69      double[][] qualities = QualitiesParameter.ActualValue.Select(x => x.ToArray()).ToArray();
70      if (qualities == null) throw new InvalidOperationException(Name + ": No qualities found.");
71
72      IScope scope = ExecutionContext.Scope;
73      int populationSize = scope.SubScopes.Count;
74
75      int[] rank;
76      var fronts = DominationCalculator<IScope>.CalculateAllParetoFronts(scope.SubScopes.ToArray(), qualities, maximization, out rank, dominateOnEqualQualities);
77
78      RankParameter.ActualValue = new ItemArray<IntValue>(rank.Select(x => new IntValue(x)));
79
80      scope.SubScopes.Clear();
81
82      for (var i = 0; i < fronts.Count; i++) {
83        Scope frontScope = new Scope("Front " + i);
84        foreach (var p in fronts[i])
85          frontScope.SubScopes.Add(p.Item1);
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    }
95
96    [StorableHook(HookType.AfterDeserialization)]
97    private void AfterDeserialization() {
98      // BackwardsCompatibility3.3
99      #region Backwards compatible code, remove with 3.4
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."));
102      #endregion
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.