Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16171 was 16171, checked in by bwerth, 6 years ago

#2943 worked on MOBasicProblem - added Interfaces;reworked MOCalculators; several minor changes

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
40    public static double CalculateCrowding<TP>(IEnumerable<TP> qualities) where TP : IReadOnlyList<double> {
41      return CalculateCrowdingDistances(qualities.ToArray()).Where(d => !double.IsPositiveInfinity(d)).DefaultIfEmpty(double.PositiveInfinity).Average();
42    }
43
44    public static IList<double> CalculateCrowdingDistances<TP>(TP[] qualities) where TP : IReadOnlyList<double> {
45      if (qualities == null) throw new ArgumentException("qualities must not be null");
46      if (!qualities.Any()) throw new ArgumentException("qualities must not be empty");
47
48      var lastIndex = qualities.Length-1;
49      int objectiveCount = qualities[0].Count;
50
51      var pointsums = qualities.ToDictionary(x => x, x => 0.0);
52      for (var dim = 0; dim < objectiveCount; dim++) {
53        var arr = qualities.OrderBy(x => x[dim]).ToArray();
54
55        pointsums[arr[0]] = double.PositiveInfinity;
56        pointsums[arr[lastIndex]] = double.PositiveInfinity;
57
58        var d = arr[lastIndex][dim]-arr[0][dim];
59
60        for (var i = 1; i < lastIndex; i++)
61          pointsums[arr[i]] += (arr[i + 1][dim] - arr[i - 1][dim])/d;
62      }
63      return qualities.Select(x=> pointsums[x]).ToList();
64    }
65
66  }
67}
Note: See TracBrowser for help on using the repository browser.