Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.TestFunctions.MultiObjective/3.3/Calculators/Crowding.cs @ 18242

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

#2875: Removed years in copyrights

File size: 4.1 KB
RevLine 
[13672]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[13672]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;
[13562]23using System.Collections.Generic;
[13620]24using System.Linq;
[13562]25
[14111]26namespace HeuristicLab.Problems.TestFunctions.MultiObjective {
[13562]27
[13988]28  /// <summary>
29  /// Crowding distance d(x,A) is usually defined between a point x and a set of points A
30  /// d(x,A) is then a weighted sum over all dimensions where for each dimension the next larger and the next smaller Point to x are subtracted
31  /// I extended the concept and defined the Crowding distance of a front A as the mean of the crowding distances of every point x in A
[14030]32  /// C(A) = mean(d(x,A)) where x in A  and d(x,A) is not infinite
33  /// Beware that Crowding is not normalized for the number of dimensions. A higher number of dimensions normlly indicated higher expected Crowding values
[13988]34  /// </summary>
[14018]35  public static class Crowding {
[13620]36
[13988]37    public static double Calculate(IEnumerable<double[]> front, double[,] bounds) {
[14030]38      return GetForFront(front, bounds).Where(d => !double.IsPositiveInfinity(d)).DefaultIfEmpty(double.PositiveInfinity).Average();
39    }
[14018]40
[14030]41    public static IEnumerable<double> GetForFront(IEnumerable<double[]> front, double[,] bounds) {
42      if (front == null) throw new ArgumentException("Fronts must not be null");
43      if (!front.Any()) throw new ArgumentException("Fronts must not be empty");
44      if (bounds == null) throw new ArgumentException("Bounds must not be null");
[13988]45      double[] pointsums = new double[front.Count()];
[13620]46
[13988]47      for (int dim = 0; dim < front.First().Length; dim++) {
[14018]48
[13988]49        double[] arr = front.Select(x => x[dim]).ToArray();
50        Array.Sort(arr);
[14018]51
[13988]52        double fmax = bounds[dim % bounds.GetLength(0), 1];
53        double fmin = bounds[dim % bounds.GetLength(0), 0];
[14018]54
[13988]55        int pointIdx = 0;
56        foreach (double[] point in front) {
57          double d = 0;
58          int pos = Array.BinarySearch(arr, point[dim]);
59          if (pos != 0 && pos != arr.Count() - 1) {
60            d = (arr[pos + 1] - arr[pos - 1]) / (fmax - fmin);
61            pointsums[pointIdx++] += d;
[14030]62          } else {
63            pointsums[pointIdx++] = Double.PositiveInfinity;
[13988]64          }
65        }
66      }
[14030]67      return pointsums;
68    }
[13936]69
[14030]70    public static double GetForSinglePoints(IEnumerable<double[]> front, double[,] bounds, int pointIndex) {
71      if (front == null) throw new ArgumentException("Fronts must not be null");
72      if (!front.Any()) throw new ArgumentException("Fronts must not be empty");
73      if (bounds == null) throw new ArgumentException("Bounds must not be null");
74      if (pointIndex < 0 || front.Count() <= pointIndex) throw new ArgumentException("PointIndex is not valid ");
75      double pointsum = 0;
76      double[] point = front.ElementAt(pointIndex);
77      for (int dim = 0; dim < front.First().Length; dim++) {
78
79        double[] arr = front.Select(x => x[dim]).ToArray();
80        Array.Sort(arr);
81
82        double fmax = bounds[dim % bounds.GetLength(0), 1];
83        double fmin = bounds[dim % bounds.GetLength(0), 0];
84
85        int pointIdx = pointIndex;
86
87        int pos = Array.BinarySearch(arr, point[dim]);
88        if (pos != 0 && pos != arr.Count() - 1) {
89          double d = (arr[pos + 1] - arr[pos - 1]) / (fmax - fmin);
90          pointsum += d;
[13672]91        }
[14030]92
[13988]93      }
[14030]94      return pointsum;
[13988]95    }
[13562]96
[13988]97  }
[13562]98}
Note: See TracBrowser for help on using the repository browser.