Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2943_MOBasicProblem_MOCMAES/HeuristicLab.Optimization/3.3/MultiObjective/CrowdingCalculator.cs @ 16310

Last change on this file since 16310 was 16310, checked in by bwerth, 5 years ago

#2943 worked on MOBasicProblem and MOAnalyzers

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.Data;
26
27namespace HeuristicLab.Optimization {
28
29  /// <summary>
30  /// CrowdingCalculator distance d(x,A) is usually defined between a point x and a set of points A
31  /// 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
32  /// 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
33  /// CrowdingCalculator as a quality of the complete qualities is defined here as the mean of the crowding distances of every point x in A
34  /// C(A) = mean(d(x,A)) where x in A  and d(x,A) is not infinite
35  /// Beware that CrowdingCalculator is not normalized for the number of dimensions. A higher number of dimensions normally causes higher CrowdingCalculator values
36  /// </summary>
37  public static class CrowdingCalculator {
38
39    public static double CalculateCrowding<TP>(IEnumerable<TP> qualities) where TP : IReadOnlyList<double> {
40      return CalculateCrowdingDistances(qualities.ToArray()).Where(d => !double.IsPositiveInfinity(d)).DefaultIfEmpty(double.PositiveInfinity).Average();
41    }
42
43    public static IList<double> CalculateCrowdingDistances<TP>(TP[] qualities) where TP : IReadOnlyList<double> {
44      if (qualities == null) throw new ArgumentException("qualities must not be null");
45      if (!qualities.Any()) throw new ArgumentException("qualities must not be empty");
46
47      var lastIndex = qualities.Length-1;
48      int objectiveCount = qualities[0].Count;
49
50      var pointsums = qualities.ToDictionary(x => x, x => 0.0);
51      for (var dim = 0; dim < objectiveCount; dim++) {
52        var arr = qualities.OrderBy(x => x[dim]).ToArray();
53
54        pointsums[arr[0]] = double.PositiveInfinity;
55        pointsums[arr[lastIndex]] = double.PositiveInfinity;
56
57        var d = arr[lastIndex][dim]-arr[0][dim];
58
59        for (var i = 1; i < lastIndex; i++)
60          pointsums[arr[i]] += (arr[i + 1][dim] - arr[i - 1][dim])/d;
61      }
62      return qualities.Select(x=> pointsums[x]).ToList();
63    }
64
65  }
66}
Note: See TracBrowser for help on using the repository browser.