Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14018 was 14018, checked in by mkommend, 8 years ago

#1087: Rewrote and adapted the multi-objective calculators.

File size: 3.6 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    public 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    public static IEnumerable<double[]> removeNonReferenceDominatingVectors(IEnumerable<double[]> qualities, double[] reference, bool[] maximization, bool dominateOnEqualQualities) {
53      List<double[]> front = new List<double[]>();
54      foreach (double[] vec in qualities) {
55        if (Dominates(vec, reference, maximization, dominateOnEqualQualities) == DominationResult.Dominates) {
56          front.Add(vec);
57        }
58      }
59      return front;
60    }
61
62    public static DominationResult Dominates(double[] left, double[] right, bool[] maximizations, bool dominateOnEqualQualities) {
63      //mkommend Caution: do not use LINQ.SequenceEqual for comparing the two quality arrays (left and right) due to performance reasons
64      if (dominateOnEqualQualities) {
65        var equal = true;
66        for (int i = 0; i < left.Length; i++) {
67          if (left[i] != right[i]) {
68            equal = false;
69            break;
70          }
71        }
72        if (equal) return DominationResult.Dominates;
73      }
74
75      bool leftIsBetter = false, rightIsBetter = false;
76      for (int i = 0; i < left.Length; i++) {
77        if (IsDominated(left[i], right[i], maximizations[i])) rightIsBetter = true;
78        else if (IsDominated(right[i], left[i], maximizations[i])) leftIsBetter = true;
79        if (leftIsBetter && rightIsBetter) break;
80      }
81
82      if (leftIsBetter && !rightIsBetter) return DominationResult.Dominates;
83      if (!leftIsBetter && rightIsBetter) return DominationResult.IsDominated;
84      return DominationResult.IsNonDominated;
85    }
86
87    private static bool IsDominated(double left, double right, bool maximization) {
88      return maximization && left < right
89        || !maximization && left > right;
90    }
91
92  }
93}
Note: See TracBrowser for help on using the repository browser.