[17558] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using HeuristicLab.Data;
|
---|
| 5 |
|
---|
| 6 | namespace 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" /> > <paramref name="n" /> or when <paramref name="r"
|
---|
| 19 | /// /> < 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 | } |
---|