Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.TestFunctions.MultiObjective/3.3/NonDominatedSelect.cs @ 16725

Last change on this file since 16725 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

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