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 | internal static List<T> Concat<T>(List<T> list1, List<T> list2)
|
---|
11 | {
|
---|
12 | List<T> resultList = new List<T>(list1.Count + list2.Count);
|
---|
13 |
|
---|
14 | resultList.AddRange(list1);
|
---|
15 | resultList.AddRange(list2);
|
---|
16 |
|
---|
17 | return resultList;
|
---|
18 | }
|
---|
19 |
|
---|
20 | /// <summary>
|
---|
21 | /// Returns the number of possible combinations of size <paramref name="r" /> from a set of
|
---|
22 | /// size <paramref name="n" />.
|
---|
23 | /// </summary>
|
---|
24 | /// <param name="n"></param>
|
---|
25 | /// <param name="r"></param>
|
---|
26 | /// <returns></returns>
|
---|
27 | /// <exception cref="ArgumentException">
|
---|
28 | /// Thrown, when <paramref name="r" /> > <paramref name="n" /> or when <paramref name="r"
|
---|
29 | /// /> < 1.
|
---|
30 | /// </exception>
|
---|
31 | internal static int NCR(int n, int r)
|
---|
32 | {
|
---|
33 | if (n < r) throw new ArgumentException($"Constraint was not met: n >= r (n = {n}, r = {r})");
|
---|
34 | if (r < 1) throw new ArgumentException($"Constraint was not met: r >= 1 (r = {r})");
|
---|
35 | if (n == r) return n;
|
---|
36 | return Factorial(n) / (Factorial(r) * Factorial(n - r));
|
---|
37 | }
|
---|
38 |
|
---|
39 | internal static int Factorial(int n)
|
---|
40 | {
|
---|
41 | if (n <= 0) throw new ArgumentException($"Constraint for n was not met: 0 < n <= 30 (n = {n})");
|
---|
42 | int product = 1;
|
---|
43 | for (int i = 2; i <= n; i++)
|
---|
44 | product *= i;
|
---|
45 |
|
---|
46 | return product;
|
---|
47 | }
|
---|
48 |
|
---|
49 | internal static DoubleMatrix ConvertToDoubleMatrix(List<ReferencePoint> referencePoints)
|
---|
50 | {
|
---|
51 | return new DoubleMatrix(ToArray(referencePoints));
|
---|
52 | }
|
---|
53 |
|
---|
54 | internal static double[][] ToFitnessMatrix(this List<Solution> solutions)
|
---|
55 | {
|
---|
56 | double[][] data = new double[solutions.Count][];
|
---|
57 | for (int i = 0; i < solutions.Count; i++)
|
---|
58 | {
|
---|
59 | Solution solution = solutions[i];
|
---|
60 | data[i] = new double[solution.Fitness.Length];
|
---|
61 | for (int j = 0; j < solution.Fitness.Length; j++)
|
---|
62 | {
|
---|
63 | data[i][j] = solution.Fitness[j];
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | return data;
|
---|
68 | }
|
---|
69 |
|
---|
70 | internal static DoubleMatrix ToMatrix(this IEnumerable<IReadOnlyList<double>> data)
|
---|
71 | {
|
---|
72 | var d2 = data.ToArray();
|
---|
73 | var mat = new DoubleMatrix(d2.Length, d2[0].Count);
|
---|
74 | for (var i = 0; i < mat.Rows; i++)
|
---|
75 | for (var j = 0; j < mat.Columns; j++)
|
---|
76 | mat[i, j] = d2[i][j];
|
---|
77 | return mat;
|
---|
78 | }
|
---|
79 |
|
---|
80 | private static double[,] ToArray(List<ReferencePoint> referencePoints)
|
---|
81 | {
|
---|
82 | if (referencePoints == null || !referencePoints.Any()) throw new ArgumentException($"{nameof(referencePoints)} is null or empty");
|
---|
83 | int nDim = referencePoints.First().NumberOfDimensions;
|
---|
84 | int pointCount = referencePoints.Count;
|
---|
85 | double[,] array = new double[nDim, pointCount];
|
---|
86 |
|
---|
87 | for (int p = 0; p < pointCount; p++)
|
---|
88 | for (int d = 0; d < nDim; d++)
|
---|
89 | array[d, p] = referencePoints[p].Values[d];
|
---|
90 |
|
---|
91 | return array;
|
---|
92 | }
|
---|
93 |
|
---|
94 | internal static TOut Min<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
|
---|
95 | {
|
---|
96 | //var it = inputs.GetEnumerator();
|
---|
97 | //it.MoveNext();
|
---|
98 | //var minValue = func(it.Current);
|
---|
99 | //while (it.MoveNext())
|
---|
100 | //{
|
---|
101 | // var currentValue = func(it.Current);
|
---|
102 | // if (minValue.CompareTo(currentValue) > 0) minValue = currentValue;
|
---|
103 | //}
|
---|
104 |
|
---|
105 | //return minValue;
|
---|
106 | return MinArgMin(func, inputs).Item2;
|
---|
107 | }
|
---|
108 |
|
---|
109 | internal static TIn ArgMin<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
|
---|
110 | {
|
---|
111 | return MinArgMin(func, inputs).Item1;
|
---|
112 | }
|
---|
113 |
|
---|
114 | internal static Tuple<TIn, TOut> MinArgMin<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
|
---|
115 | {
|
---|
116 | var it = inputs.GetEnumerator();
|
---|
117 | it.MoveNext();
|
---|
118 | var minArg = it.Current;
|
---|
119 | var minValue = func(it.Current);
|
---|
120 | while (it.MoveNext())
|
---|
121 | {
|
---|
122 | var currentArg = it.Current;
|
---|
123 | var currentValue = func(it.Current);
|
---|
124 | if (minValue.CompareTo(currentValue) > 0)
|
---|
125 | {
|
---|
126 | minArg = currentArg;
|
---|
127 | minValue = currentValue;
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | return Tuple.Create(minArg, minValue);
|
---|
132 | }
|
---|
133 |
|
---|
134 | internal static TOut Max<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
|
---|
135 | {
|
---|
136 | //var it = inputs.GetEnumerator();
|
---|
137 | //it.MoveNext();
|
---|
138 | //var maxValue = func(it.Current);
|
---|
139 | //while (it.MoveNext())
|
---|
140 | //{
|
---|
141 | // var currentValue = func(it.Current);
|
---|
142 | // if (maxValue.CompareTo(currentValue) < 0) maxValue = currentValue;
|
---|
143 | //}
|
---|
144 |
|
---|
145 | //return maxValue;
|
---|
146 | return MaxArgMax(func, inputs).Item2;
|
---|
147 | }
|
---|
148 |
|
---|
149 | internal static TIn ArgMax<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
|
---|
150 | {
|
---|
151 | return MaxArgMax(func, inputs).Item1;
|
---|
152 | }
|
---|
153 |
|
---|
154 | internal static Tuple<TIn, TOut> MaxArgMax<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
|
---|
155 | {
|
---|
156 | var it = inputs.GetEnumerator();
|
---|
157 | it.MoveNext();
|
---|
158 | var maxArg = it.Current;
|
---|
159 | var maxValue = func(it.Current);
|
---|
160 | while (it.MoveNext())
|
---|
161 | {
|
---|
162 | var currentArg = it.Current;
|
---|
163 | var currentValue = func(it.Current);
|
---|
164 | if (maxValue.CompareTo(currentValue) < 0)
|
---|
165 | {
|
---|
166 | maxArg = currentArg;
|
---|
167 | maxValue = currentValue;
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | return Tuple.Create(maxArg, maxValue);
|
---|
172 | }
|
---|
173 | }
|
---|
174 | } |
---|