Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1087 regorganized testfunctions, added view for qualities

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