Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/MultiObjective/CrowdingCalculator.cs @ 17690

Last change on this file since 17690 was 17225, checked in by mkommend, 5 years ago

#2521: Integrated changes of #2943 into problem refactoring branch.

File size: 3.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26
27namespace HeuristicLab.Optimization {
28  /// <summary>
29  /// CrowdingCalculator distance d(x,A) is usually defined between a point x and a set of points A
30  /// d(x,A) is the sum over all dimensions where for each dimension the next larger and the next smaller Point to x are subtracted
31  /// see in more detail: "A fast elitist non-dominated sorting genetic algorithm for multi-objective optimization: NSGA-II" by K Deb, S Agrawal, A Pratap, T Meyarivan
32  /// CrowdingCalculator as a quality of the complete qualities is defined here as the mean of the crowding distances of every point x in A
33  /// C(A) = mean(d(x,A)) where x in A  and d(x,A) is not infinite
34  /// Beware that CrowdingCalculator is not normalized for the number of dimensions. A higher number of dimensions normally causes higher CrowdingCalculator values
35  /// </summary>
36  public static class CrowdingCalculator {
37    public static double CalculateCrowding<TP>(IEnumerable<TP> qualities) where TP : IReadOnlyList<double> {
38      return CalculateCrowdingDistances(qualities.ToArray()).Where(d => !double.IsPositiveInfinity(d)).DefaultIfEmpty(double.PositiveInfinity).Average();
39    }
40
41    public static IList<double> CalculateCrowdingDistances<TP>(TP[] qualities) where TP : IReadOnlyList<double> {
42      if (qualities == null) throw new ArgumentException("qualities must not be null");
43      if (!qualities.Any()) throw new ArgumentException("qualities must not be empty");
44
45      var lastIndex = qualities.Length - 1;
46      int objectiveCount = qualities[0].Count;
47
48      var pointsums = qualities.ToDictionary(x => x, x => 0.0);
49      for (var dim = 0; dim < objectiveCount; dim++) {
50        var arr = qualities.OrderBy(x => x[dim]).ToArray();
51
52        pointsums[arr[0]] = double.PositiveInfinity;
53        pointsums[arr[lastIndex]] = double.PositiveInfinity;
54
55        var d = arr[lastIndex][dim] - arr[0][dim];
56        if (d.IsAlmost(0.0)) d = 1.0;
57        for (var i = 1; i < lastIndex; i++)
58          pointsums[arr[i]] += (arr[i + 1][dim] - arr[i - 1][dim]) / d;
59      }
60      return qualities.Select(x => pointsums[x]).ToList();
61    }
62  }
63}
Note: See TracBrowser for help on using the repository browser.