Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/Utility.cs @ 17558

Last change on this file since 17558 was 17558, checked in by dleko, 4 years ago

#2825: Generate structured reference points and show them in Results tab.

File size: 2.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Data;
5
6namespace HeuristicLab.Algorithms.NSGA3
7{
8    internal static class Utility
9    {
10        /// <summary>
11        /// Returns the number of possible combinations of size <paramref name="r" /> from a set of
12        /// size <paramref name="n" />.
13        /// </summary>
14        /// <param name="n"></param>
15        /// <param name="r"></param>
16        /// <returns></returns>
17        /// <exception cref="ArgumentException">
18        /// Thrown, when <paramref name="r" /> &gt; <paramref name="n" /> or when <paramref name="r"
19        /// /> &lt; 1.
20        /// </exception>
21        internal static int NCR(int n, int r)
22        {
23            if (n < r) throw new ArgumentException($"Constraint was not met: n >= r (n = {n}, r = {r})");
24            if (r < 1) throw new ArgumentException($"Constraint was not met: r >= 1 (r = {r})");
25            if (n == r) return n;
26            return Factorial(n) / (Factorial(r) * Factorial(n - r));
27        }
28
29        internal static int Factorial(int n)
30        {
31            if (n <= 0) throw new ArgumentException($"Constraint for n was not met: 0 < n <= 30 (n = {n})");
32            int product = 1;
33            for (int i = 2; i <= n; i++)
34                product *= i;
35
36            return product;
37        }
38
39        internal static DoubleMatrix ConvertToDoubleMatrix(List<ReferencePoint> referencePoints)
40        {
41            return new DoubleMatrix(ToArray(referencePoints));
42        }
43
44        internal static double[,] ToArray(List<ReferencePoint> referencePoints)
45        {
46            if (referencePoints == null || !referencePoints.Any()) throw new ArgumentException($"{nameof(referencePoints)} is null or empty");
47            int nDim = referencePoints.First().NumberOfDimensions;
48            int pointCount = referencePoints.Count;
49            double[,] array = new double[nDim, pointCount];
50
51            for (int p = 0; p < pointCount; p++)
52                for (int d = 0; d < nDim; d++)
53                    array[d, p] = referencePoints[p].Values[d];
54
55            return array;
56        }
57    }
58}
Note: See TracBrowser for help on using the repository browser.