Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17707


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

#2825 Minor refactoring.

Location:
branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3
Files:
3 edited

Legend:

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

    r17703 r17707  
    4141        }
    4242
    43         public int NumberOfObjectives
     43        public int Objectives
    4444        {
    4545            get
     
    259259        {
    260260            // Set population size
    261             int numberOfGeneratedReferencePoints = ReferencePoint.GetNumberOfGeneratedReferencePoints(NumberOfObjectives);
     261            int numberOfGeneratedReferencePoints = ReferencePoint.GetNumberOfGeneratedReferencePoints(Objectives);
     262            if (numberOfGeneratedReferencePoints == -1) throw new NotSupportedException("The number of objectives is not supported.");
    262263            PopulationSize.Value = ReferencePoint.GetPopulationSizeForReferencePoints(numberOfGeneratedReferencePoints);
    263264
     
    294295
    295296            solutions = GetInitialPopulation();
    296             referencePoints = ReferencePoint.GenerateReferencePoints(random, NumberOfObjectives);
     297            referencePoints = ReferencePoint.GenerateReferencePoints(random, Objectives);
    297298            ResultsGeneratedReferencePoints = Utility.ConvertToDoubleMatrix(referencePoints);
    298299        }
     
    382383            ResultsHypervolume = new DoubleValue(Hypervolume.Calculate(front, problem.ReferencePoint.CloneAsArray(), problem.Maximization));
    383384            ResultsDifferenceToBestKnownHypervolume = new DoubleValue(ResultsBestKnownHypervolume.Value - ResultsHypervolume.Value);
    384 
    385             Problem.Analyze(
    386                 solutions.Select(s => (Individual)new SingleEncodingIndividual(Problem.Encoding, new Scope { Variables = { new Variable(Problem.Encoding.Name, s.Chromosome) } })).ToArray(),
    387                 solutions.Select(s => s.Fitness).ToArray(),
    388                 Results,
    389                 random
    390                 );
    391385        }
    392386
  • branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/ReferencePoint.cs

    r17700 r17707  
    1717
    1818        public double[] Values { get; }
    19         public int NumberOfDimensions => Values.Length;
     19        public int Objectives => Values.Length;
    2020        public int NumberOfAssociatedSolutions { get; set; } = 0;
    2121
     
    2424        #region Constructors
    2525
    26         public ReferencePoint(IRandom random, int numberOfDimensions)
     26        public ReferencePoint(IRandom random, int obj)
    2727        {
    2828            this.random = random;
    29             Values = new double[numberOfDimensions];
     29            Values = new double[obj];
    3030        }
    3131
     
    3333        {
    3434            random = cloner.Clone(other.random);
    35             Values = new double[other.NumberOfDimensions];
     35            Values = new double[other.Objectives];
    3636            other.Values.CopyTo(Values, 0);
    3737        }
     
    5050
    5151        #region Static methods
     52
     53        public static Tuple<int, int> GetDivisions(int obj)
     54        {
     55            switch (obj)
     56            {
     57                case 3:
     58                    return Tuple.Create(12, 0);
     59
     60                case 5:
     61                    return Tuple.Create(6, 0);
     62
     63                case 8:
     64                    return Tuple.Create(3, 2);
     65
     66                case 10:
     67                    return Tuple.Create(3, 2);
     68
     69                case 15:
     70                    return Tuple.Create(2, 1);
     71
     72                default:
     73                    return null;
     74            }
     75        }
    5276
    5377        /// <summary>
     
    5579        /// cref="GenerateReferencePoints(IRandom, int)" />.
    5680        /// </summary>
    57         /// <param name="nDim"></param>
     81        /// <param name="obj"></param>
    5882        /// <returns></returns>
    59         public static int GetNumberOfGeneratedReferencePoints(int nDim)
    60         {
    61             int outerDiv;
    62             int innerDiv;
    63             switch (nDim)
    64             {
    65                 case 3:
    66                     outerDiv = 12;
    67                     innerDiv = 0;
    68                     break;
    69 
    70                 case 5:
    71                     outerDiv = 6;
    72                     innerDiv = 0;
    73                     break;
    74 
    75                 case 8:
    76                     outerDiv = 3;
    77                     innerDiv = 2;
    78                     break;
    79 
    80                 case 10:
    81                     outerDiv = 3;
    82                     innerDiv = 2;
    83                     break;
    84 
    85                 case 15:
    86                     outerDiv = 2;
    87                     innerDiv = 1;
    88                     break;
    89 
    90                 default:
    91                     return -1;
    92             }
    93 
    94             return GetNumberOfGeneratedReferencePoints(nDim, outerDiv, innerDiv);
    95         }
    96 
    97         public static int GetNumberOfGeneratedReferencePoints(int nDim, int outerDiv, int innerDiv = 0)
    98         {
    99             int outerPoints = Utility.NCR(nDim + outerDiv - 1, outerDiv);
    100             int innerPoints = innerDiv == 0 ? 0 : Utility.NCR(nDim + innerDiv - 1, innerDiv);
     83        public static int GetNumberOfGeneratedReferencePoints(int obj)
     84        {
     85            Tuple<int, int> division = GetDivisions(obj);
     86            if (division == null) return -1;
     87
     88            return GetNumberOfGeneratedReferencePoints(obj, division.Item1, division.Item2);
     89        }
     90
     91        public static int GetNumberOfGeneratedReferencePoints(int obj, int outerDiv, int innerDiv = 0)
     92        {
     93            int outerPoints = Utility.NCR(obj + outerDiv - 1, outerDiv);
     94            int innerPoints = innerDiv == 0 ? 0 : Utility.NCR(obj + innerDiv - 1, innerDiv);
    10195            return outerPoints + innerPoints;
    10296        }
     
    109103        /// <summary>
    110104        /// Generate reference points that are evenly distributed over a hyperplane with dimensions
    111         /// <paramref name="nDim" /> - 1 with the sum of the values in all dimensions being equal to
    112         /// 1 for each reference point. <paramref name="nDim" /> may only be one of {3, 5, 8, 10, 15}
     105        /// <paramref name="obj" /> - 1 with the sum of the values in all dimensions being equal to
     106        /// 1 for each reference point. <paramref name="obj" /> may only be one of {3, 5, 8, 10, 15}
    113107        /// -&gt; The number of inner divisions and outer divisions are determined based on the
    114108        /// values provided in the NSGA-III paper, chapter V: Results. If different dimensions are
     
    118112        /// The <see cref="IRandom" /> to give as a parameter to the ReferencePoint constructors.
    119113        /// </param>
    120         /// <param name="nDim">The dimensionality of the Reference Points</param>
     114        /// <param name="obj">The dimensionality of the Reference Points</param>
    121115        /// <returns></returns>
    122         public static List<ReferencePoint> GenerateReferencePoints(IRandom random, int nDim)
    123         {
    124             List<ReferencePoint> referencePoints;
    125             switch (nDim)
    126             {
    127                 case 3:
    128                     referencePoints = GenerateReferencePoints(random, nDim, 12, 0);
    129                     break;
    130 
    131                 case 5:
    132                     referencePoints = GenerateReferencePoints(random, nDim, 6, 0);
    133                     break;
    134 
    135                 case 8:
    136                     referencePoints = GenerateReferencePoints(random, nDim, 3, 2);
    137                     break;
    138 
    139                 case 10:
    140                     referencePoints = GenerateReferencePoints(random, nDim, 3, 2);
    141                     break;
    142 
    143                 case 15:
    144                     referencePoints = GenerateReferencePoints(random, nDim, 2, 1);
    145                     break;
    146 
    147                 default:
    148                     throw new NotSupportedException("Only the reference points for 3, 5, 8, 10 or 15 dimensions can be created.");
    149             }
    150 
    151             return referencePoints;
     116        public static List<ReferencePoint> GenerateReferencePoints(IRandom random, int obj)
     117        {
     118            Tuple<int, int> divisions = GetDivisions(obj);
     119
     120            if (divisions == null) return null;
     121            return GenerateReferencePoints(random, obj, divisions.Item1, divisions.Item2);
    152122        }
    153123
     
    159129        /// <summary>
    160130        /// Generate reference points that are evenly distributed over a hyperplane with dimensions
    161         /// <paramref name="nDim" /> - 1 with the sum of the values in all dimensions being equal to
     131        /// <paramref name="obj" /> - 1 with the sum of the values in all dimensions being equal to
    162132        /// 1 for each reference point.
    163133        /// </summary>
    164         /// <param name="nDim">The number of dimensions for the reference points.</param>
     134        /// <param name="obj">The number of dimensions for the reference points.</param>
    165135        /// <param name="outerDiv">The number of divisions for the outer reference points</param>
    166136        /// <returns>The generated reference points (Check Fig. 4 in NSGA-III paper).</returns>
     
    170140        /// not have inner reference points
    171141        /// </returns>
    172         public static List<ReferencePoint> GenerateReferencePoints(IRandom random, int nDim, int outerDiv, int innerDiv = 0)
    173         {
    174             if (nDim <= 0) throw new ArgumentException("nDim must be greater than 0");
     142        public static List<ReferencePoint> GenerateReferencePoints(IRandom random, int obj, int outerDiv, int innerDiv = 0)
     143        {
     144            if (obj <= 0) throw new ArgumentException("obj must be greater than 0");
    175145            if (outerDiv <= 1) throw new ArgumentException("outerDiv must be greater than 1");
    176146
    177147            List<ReferencePoint> referencePoints = new List<ReferencePoint>();
    178             ReferencePoint refPoint = new ReferencePoint(random, nDim);
    179             GenerateRecursive(referencePoints, refPoint, nDim, outerDiv, outerDiv, 0);
     148            ReferencePoint refPoint = new ReferencePoint(random, obj);
     149            GenerateRecursive(referencePoints, refPoint, obj, outerDiv, outerDiv, 0);
    180150
    181151            if (innerDiv > 0)
    182152            {
    183153                List<ReferencePoint> insideReferencePoints = new List<ReferencePoint>();
    184                 GenerateRecursive(insideReferencePoints, refPoint, nDim, innerDiv, innerDiv, 0);
    185 
    186                 double center = 1.0 / nDim;
     154                GenerateRecursive(insideReferencePoints, refPoint, obj, innerDiv, innerDiv, 0);
     155
     156                double center = 1.0 / obj;
    187157
    188158                for (int i = 0; i < insideReferencePoints.Count; i++)
  • 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.