Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/02/20 11:39:51 (4 years ago)
Author:
dleko
Message:

#2825 Minor refactoring.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/Utility.cs

    r17679 r17707  
    3232        {
    3333            if (n < r) throw new InvalidOperationException($"Constraint was not met: n >= r (n = {n}, r = {r})");
    34             if (r < 1) throw new InvalidOperationException($"Constraint was not met: r >= 1 (r = {r})");
     34            if (r < 0) throw new InvalidOperationException($"Constraint was not met: r >= 0 (r = {r})");
    3535            if (n == r) return 1;
    36             return (int)(Factorial(n) / (Factorial(r) * Factorial(n - r)));
    37         }
    38 
    39         public static long Factorial(int n)
     36
     37            r = Math.Max(r, n - r);
     38
     39            long range1 = RangeMultiplication(r + 1, n - r);
     40            return (int)(range1 / Factorial(n - r));
     41        }
     42
     43        private static long RangeMultiplication(int start, int count)
     44        {
     45            long s = 1;
     46            return LeftFold((long a, int b) => checked(a * b), new List<int>(Enumerable.Range(start, count)), s);
     47        }
     48
     49        private static T2 LeftFold<T1, T2>(Func<T2, T1, T2> func, List<T1> elements, T2 start)
     50        {
     51            var item = start;
     52            foreach (var element in elements)
     53                item = func(item, element);
     54
     55            return item;
     56        }
     57
     58        public static long Factorial(long n)
    4059        {
    4160            if (n < 0 || n > 30) throw new InvalidOperationException($"Constraint for n was not met: 0 <= n <= 30 (n = {n})");
    4261            long product = 1;
    43             for (int i = 2; i <= n; i++)
     62            for (long i = 2; i <= n; i++)
    4463                product *= i;
    4564
    4665            return product;
    4766        }
     67
    4868        public static double[][] ToJaggedArray(this DoubleMatrix m)
    4969        {
     
    94114        {
    95115            if (referencePoints == null || !referencePoints.Any()) throw new ArgumentException($"{nameof(referencePoints)} is null or empty");
    96             int nDim = referencePoints.First().NumberOfDimensions;
     116            int obj = referencePoints.First().Objectives;
    97117            int pointCount = referencePoints.Count;
    98             double[,] array = new double[nDim, pointCount];
     118            double[,] array = new double[obj, pointCount];
    99119
    100120            for (int p = 0; p < pointCount; p++)
    101                 for (int d = 0; d < nDim; d++)
     121                for (int d = 0; d < obj; d++)
    102122                    array[d, p] = referencePoints[p].Values[d];
    103123
Note: See TracChangeset for help on using the changeset viewer.