Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.MultiObjectiveTestFunctions/HeuristicLab.Problems.MultiObjectiveTestFunctions/3.3/Calculators/Utilities.cs @ 14018

Last change on this file since 14018 was 14018, checked in by mkommend, 6 years ago

#1087: Rewrote and adapted the multi-objective calculators.

File size: 2.3 KB
Line 
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
21using System;
22using System.Collections.Generic;
23using System.Linq;
24
25namespace HeuristicLab.Problems.MultiObjectiveTestFunctions {
26  internal static class Utilities {
27    internal static double MinimumDistance(double[] point, IEnumerable<double[]> points) {
28      if (point == null) throw new ArgumentNullException("Point must not be null.");
29      if (points == null) throw new ArgumentNullException("Points must not be null.");
30      if (!points.Any()) throw new ArgumentException("Points must not be empty.");
31
32      double minDistance = double.MaxValue;
33      foreach (double[] r in points) {
34        if (r.Length != point.Length) throw new ArgumentException("Dimensions of Points and Point do not match.");
35
36        double squaredDistance = 0;
37        for (int i = 0; i < r.Length; i++) {
38          squaredDistance += (point[i] - r[i]) * (point[i] - r[i]);
39        }
40        minDistance = Math.Min(squaredDistance, minDistance);
41      }
42
43      return Math.Sqrt(minDistance);
44    }
45
46    internal class DimensionComparer : IComparer<double[]> {
47      private readonly int dim;
48      private readonly int descending;
49
50      public DimensionComparer(int dimension, bool descending) {
51        this.dim = dimension;
52        this.descending = descending ? -1 : 1;
53      }
54
55      public int Compare(double[] x, double[] y) {
56        if (x[dim] < y[dim]) return -descending;
57        else if (x[dim] > y[dim]) return descending;
58        else return 0;
59      }
60    }
61  }
62}
Note: See TracBrowser for help on using the repository browser.