Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.MultiObjectiveTestFunctions/HeuristicLab.Problems.MultiObjectiveTestFunctions/3.3/NonDominatedSelect.cs @ 13672

Last change on this file since 13672 was 13672, checked in by bwerth, 8 years ago

#1087 added Analyzers, reworked PFStore, added licence information, cleaned code

File size: 3.4 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Core;
25
26namespace HeuristicLab.Problems.MultiObjectiveTestFunctions {
27
28  public class NonDominatedSelect {
29    private enum DominationResult { Dominates, IsDominated, IsNonDominated };
30
31    public static IEnumerable<double[]> selectNonDominatedVectors(IEnumerable<double[]> qualities, bool[] maximization, bool dominateOnEqualQualities) {
32      int populationSize = qualities.Count();
33
34      List<double[]> front = new List<double[]>();
35      foreach (double[] row in qualities) {
36        bool insert = true;
37        for (int i = 0; i < front.Count; i++) {
38          DominationResult res = Dominates(front[i], row, maximization, dominateOnEqualQualities);
39          if (res == DominationResult.Dominates) { insert = false; break; }           //Vector domiates Row
40          else if (res == DominationResult.IsDominated) {   //Row dominates Vector
41            front.RemoveRange(i, 1);
42          }
43        }
44        if (insert) {
45          front.Add(row);
46        }
47      }
48
49      return front;
50    }
51
52    private static DominationResult Dominates(double[] left, double[] right, bool[] maximizations, bool dominateOnEqualQualities) {
53      //mkommend Caution: do not use LINQ.SequenceEqual for comparing the two quality arrays (left and right) due to performance reasons
54      if (dominateOnEqualQualities) {
55        var equal = true;
56        for (int i = 0; i < left.Length; i++) {
57          if (left[i] != right[i]) {
58            equal = false;
59            break;
60          }
61        }
62        if (equal) return DominationResult.Dominates;
63      }
64
65      bool leftIsBetter = false, rightIsBetter = false;
66      for (int i = 0; i < left.Length; i++) {
67        if (IsDominated(left[i], right[i], maximizations[i])) rightIsBetter = true;
68        else if (IsDominated(right[i], left[i], maximizations[i])) leftIsBetter = true;
69        if (leftIsBetter && rightIsBetter) break;
70      }
71
72      if (leftIsBetter && !rightIsBetter) return DominationResult.Dominates;
73      if (!leftIsBetter && rightIsBetter) return DominationResult.IsDominated;
74      return DominationResult.IsNonDominated;
75    }
76
77    private static bool IsDominated(double left, double right, bool maximization) {
78      return maximization && left < right
79        || !maximization && left > right;
80    }
81
82    private static void AddToFront(IScope p, List<ScopeList> fronts, int i) {
83      if (i == fronts.Count) fronts.Add(new ScopeList());
84      fronts[i].Add(p);
85    }
86
87  }
88}
Note: See TracBrowser for help on using the repository browser.