Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.TestFunctions.MultiObjective/3.3/NonDominatedSelect.cs @ 15521

Last change on this file since 15521 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

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