[13672] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2016 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 |
|
---|
| 22 | using System;
|
---|
[13562] | 23 | using System.Collections.Generic;
|
---|
[13620] | 24 | using System.Linq;
|
---|
[13562] | 25 |
|
---|
| 26 | namespace HeuristicLab.Problems.MultiObjectiveTestFunctions {
|
---|
| 27 |
|
---|
| 28 | /// <summary>
|
---|
[13620] | 29 | /// Crowding distance d(x,A) is usually defined between a point x and a set of points A
|
---|
[13562] | 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
|
---|
[13620] | 32 | /// C(A) = mean(d(x,A)) where x in A and d(x,A) is not infinite
|
---|
[13562] | 33 | /// </summary>
|
---|
[13672] | 34 | public class Crowding {
|
---|
[13620] | 35 |
|
---|
[13672] | 36 | public static double Calculate(IEnumerable<double[]> front, double[,] bounds) {
|
---|
| 37 | double sum = 0;
|
---|
| 38 | int c = 0;
|
---|
| 39 | double[] pointsums = new double[front.Count()];
|
---|
[13620] | 40 |
|
---|
[13672] | 41 | for (int dim = 0; dim < bounds.GetLength(0); dim++) {
|
---|
| 42 | double[] arr = front.Select(x => x[dim]).ToArray();
|
---|
| 43 | Array.Sort(arr);
|
---|
| 44 | double fmax = bounds[dim % bounds.GetLength(0), 1];
|
---|
| 45 | double fmin = bounds[dim % bounds.GetLength(0), 0];
|
---|
| 46 | int pointIdx = 0;
|
---|
| 47 | foreach (double[] point in front) {
|
---|
| 48 | double d = 0;
|
---|
| 49 | int pos = Array.BinarySearch(arr, point[dim]);
|
---|
| 50 | if (pos != 0 && pos != arr.Count() - 1) {
|
---|
| 51 | d = (arr[pos + 1] - arr[pos - 1]) / (fmax - fmin);
|
---|
| 52 | pointsums[pointIdx++] += d;
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
[13562] | 56 |
|
---|
[13672] | 57 | foreach (double d in pointsums) {
|
---|
[13620] | 58 | if (!Double.IsInfinity(d)) {
|
---|
| 59 | sum += d;
|
---|
| 60 | c++;
|
---|
| 61 | }
|
---|
[13562] | 62 | }
|
---|
[13672] | 63 | return c == 0 ? Double.PositiveInfinity : sum / c;
|
---|
[13562] | 64 | }
|
---|
| 65 |
|
---|
| 66 | }
|
---|
| 67 | }
|
---|