Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2875: Removed years in copyrights

File size: 3.6 KB
RevLine 
[13562]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[13562]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;
[16565]23using HEAL.Attic;
[13562]24
[14111]25namespace HeuristicLab.Problems.TestFunctions.MultiObjective {
[13562]26
[14085]27  public static class NonDominatedSelect {
[16565]28    [StorableType("5EDA6CC0-30C6-4B71-8713-F84BB61B1180")]
[14018]29    public enum DominationResult { Dominates, IsDominated, IsNonDominated };
[13562]30
[14081]31    public static IEnumerable<double[]> SelectNonDominatedVectors(IEnumerable<double[]> qualities, bool[] maximization, bool dominateOnEqualQualities) {
[13562]32
[13988]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
[14081]40            front.RemoveAt(i);
[13988]41          }
[13562]42        }
[13988]43        if (insert) {
44          front.Add(row);
[13562]45        }
[13988]46      }
[13562]47
[13988]48      return front;
49    }
[13562]50
[14081]51    public static IEnumerable<double[]> GetDominatingVectors(IEnumerable<double[]> qualities, double[] reference, bool[] maximization, bool dominateOnEqualQualities) {
[13988]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);
[13562]56        }
[13988]57      }
58      return front;
59    }
[13562]60
[14018]61    public static DominationResult Dominates(double[] left, double[] right, bool[] maximizations, bool dominateOnEqualQualities) {
[13988]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          }
[13936]70        }
[13988]71        if (equal) return DominationResult.Dominates;
72      }
[13562]73
[13988]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      }
[13562]80
[13988]81      if (leftIsBetter && !rightIsBetter) return DominationResult.Dominates;
82      if (!leftIsBetter && rightIsBetter) return DominationResult.IsDominated;
83      return DominationResult.IsNonDominated;
[13562]84    }
[13988]85
86    private static bool IsDominated(double left, double right, bool maximization) {
87      return maximization && left < right
88        || !maximization && left > right;
89    }
90
91  }
[13562]92}
Note: See TracBrowser for help on using the repository browser.