Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17667


Ignore:
Timestamp:
07/13/20 14:20:24 (4 years ago)
Author:
dleko
Message:

#2825 Bugfix: The correct number of reference points are returned on ReferencePoint.GetNumberOfGeneratedReferencePoints.

Location:
branches/2825-NSGA3
Files:
5 edited

Legend:

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

    r17666 r17667  
    1313        [DataRow(3, 6)]
    1414        [DataRow(4, 24)]
    15         public void TestFactorial(int input, int result)
     15        [DataRow(10, 3628800)]
     16        [DataRow(12, 479001600)]
     17        [DataRow(13, 6227020800)]
     18        [DataRow(14, 87178291200)]
     19        public void TestFactorial(int input, long result)
    1620        {
    1721            Assert.AreEqual(result, Utility.Factorial(input));
     
    2125        [DataRow(-1)]
    2226        [DataRow(31)]
    23         public void TestFactorial2(int input)
     27        public void TestFactorialConstraint(int input)
    2428        {
    2529            Assert.ThrowsException<InvalidOperationException>(() => { Utility.Factorial(input); });
    2630        }
     31
     32        [DataTestMethod]
     33        [DataRow(14, 12, 91)]
     34        public void TestNCR(int n, int r, int result)
     35        {
     36            Assert.AreEqual(result, Utility.NCR(n, r));
     37        }
    2738    }
    2839}
  • branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/HeuristicLab.Algorithms.NSGA3-3.3.csproj

    r17665 r17667  
    118118    <Compile Include="Utility.cs" />
    119119  </ItemGroup>
     120  <ItemGroup>
     121    <ProjectReference Include="..\..\..\..\trunk\HeuristicLab.Problems.TestFunctions.MultiObjective\3.3\HeuristicLab.Problems.TestFunctions.MultiObjective-3.3.csproj">
     122      <Project>{D53E8E48-CFAA-4F57-AC35-63BEF4476159}</Project>
     123      <Name>HeuristicLab.Problems.TestFunctions.MultiObjective-3.3</Name>
     124    </ProjectReference>
     125  </ItemGroup>
    120126  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    121127  <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
  • branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/NSGA3.cs

    r17665 r17667  
    1111using HeuristicLab.Optimization;
    1212using HeuristicLab.Parameters;
     13using HeuristicLab.Problems.TestFunctions.MultiObjective;
    1314using HeuristicLab.Random;
    1415
     
    4142        }
    4243
    43         public int NumberOfObjectives => Problem.Maximization.Length;
     44        public int NumberOfObjectives
     45        {
     46            get
     47            {
     48                if (!(Problem is MultiObjectiveTestFunctionProblem testFunctionProblem)) throw new NotSupportedException("Only test multi objective test function problems are supported");
     49                return testFunctionProblem.Objectives;
     50            }
     51        }
    4452
    4553        #endregion ProblemProperties
     
    211219            base.Initialize(cancellationToken);
    212220
    213             PopulationSize.Value = ReferencePoint.GetNumberOfGeneratedReferencePoints(Problem.Maximization.Length);
     221            int pop = ReferencePoint.GetNumberOfGeneratedReferencePoints(NumberOfObjectives);
     222            PopulationSize.Value = pop;
    214223            InitResults();
    215224            InitReferencePoints();
     
    264273                // create copies of generated reference points (to preserve the original ones for
    265274                // the next generation) maybe todo: use cloner?
    266                 //ToNextGeneration(CreateCopyOfReferencePoints());
    267275
    268276                List<Solution> qt = Mutate(Recombine(solutions));
  • branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/ReferencePoint.cs

    r17663 r17667  
    4545        /// <param name="nDim"></param>
    4646        /// <returns></returns>
    47         internal static int GetNumberOfGeneratedReferencePoints(int nDim)
     47        public static int GetNumberOfGeneratedReferencePoints(int nDim)
    4848        {
    4949            int outerDiv;
     
    8080            }
    8181
    82             return GetNumberOfGeneratedReferencePoints(nDim, outerDiv, innerDiv);
    83         }
    84 
    85         internal static int GetNumberOfGeneratedReferencePoints(int nDim, int outerDiv, int innerDiv = 0)
    86         {
    87             return Utility.NCR(nDim + outerDiv - 1, outerDiv) + (innerDiv == 0 ? 0 : Utility.NCR(nDim + innerDiv - 1, innerDiv));
     82            int nRefPoints = GetNumberOfGeneratedReferencePoints(nDim, outerDiv, innerDiv);
     83            return nRefPoints;
     84            //return GetNumberOfGeneratedReferencePoints(nDim, outerDiv, innerDiv);
     85        }
     86
     87        public static int GetNumberOfGeneratedReferencePoints(int nDim, int outerDiv, int innerDiv = 0)
     88        {
     89            int outerPoints = Utility.NCR(nDim + outerDiv - 1, outerDiv);
     90            int innerPoints = innerDiv == 0 ? 0 : Utility.NCR(nDim + innerDiv - 1, innerDiv);
     91            int nRefPoints = outerPoints + innerPoints;
     92            return nRefPoints;
    8893        }
    8994
     
    101106        /// <param name="nDim">The dimensionality of the Reference Points</param>
    102107        /// <returns></returns>
    103         internal static List<ReferencePoint> GenerateReferencePoints(IRandom random, int nDim)
     108        public static List<ReferencePoint> GenerateReferencePoints(IRandom random, int nDim)
    104109        {
    105110            List<ReferencePoint> referencePoints;
     
    151156        /// not have inner reference points
    152157        /// </returns>
    153         internal static List<ReferencePoint> GenerateReferencePoints(IRandom random, int nDim, int outerDiv, int innerDiv = 0)
     158        public static List<ReferencePoint> GenerateReferencePoints(IRandom random, int nDim, int outerDiv, int innerDiv = 0)
    154159        {
    155160            if (nDim <= 0) throw new ArgumentException("nDim must be greater than 0");
  • branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/Utility.cs

    r17664 r17667  
    66namespace HeuristicLab.Algorithms.NSGA3
    77{
    8     internal static class Utility
     8    public static class Utility
    99    {
    10         internal static List<T> Concat<T>(List<T> list1, List<T> list2)
     10        public static List<T> Concat<T>(List<T> list1, List<T> list2)
    1111        {
    1212            List<T> resultList = new List<T>(list1.Count + list2.Count);
     
    2525        /// <param name="r"></param>
    2626        /// <returns></returns>
    27         /// <exception cref="ArgumentException">
     27        /// <exception cref="InvalidOperationException">
    2828        /// Thrown, when <paramref name="r" /> &gt; <paramref name="n" /> or when <paramref name="r"
    2929        /// /> &lt; 1.
    3030        /// </exception>
    31         internal static int NCR(int n, int r)
     31        public static int NCR(int n, int r)
    3232        {
    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));
     33            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})");
     35            if (n == r) return 1;
     36            return (int)(Factorial(n) / (Factorial(r) * Factorial(n - r)));
    3737        }
    3838
    39         internal static int Factorial(int n)
     39        public static long Factorial(int n)
    4040        {
    41             if (n <= 0) throw new ArgumentException($"Constraint for n was not met: 0 < n <= 30 (n = {n})");
    42             int product = 1;
     41            if (n < 0 || n > 30) throw new InvalidOperationException($"Constraint for n was not met: 0 <= n <= 30 (n = {n})");
     42            long product = 1;
    4343            for (int i = 2; i <= n; i++)
    4444                product *= i;
     
    4747        }
    4848
    49         internal static DoubleMatrix ConvertToDoubleMatrix(List<ReferencePoint> referencePoints)
     49        public static DoubleMatrix ConvertToDoubleMatrix(List<ReferencePoint> referencePoints)
    5050        {
    5151            return new DoubleMatrix(ToArray(referencePoints));
    5252        }
    5353
    54         internal static double[][] ToFitnessMatrix(this List<Solution> solutions)
     54        public static double[][] ToFitnessMatrix(this List<Solution> solutions)
    5555        {
    5656            double[][] data = new double[solutions.Count][];
     
    6868        }
    6969
    70         internal static DoubleMatrix ToMatrix(this IEnumerable<IReadOnlyList<double>> data)
     70        public static DoubleMatrix ToMatrix(this IEnumerable<IReadOnlyList<double>> data)
    7171        {
    7272            var d2 = data.ToArray();
     
    9292        }
    9393
    94         internal static TOut Min<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
     94        public static TOut Min<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
    9595        {
    9696            return MinArgMin(func, inputs).Item2;
    9797        }
    9898
    99         internal static TIn ArgMin<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
     99        public static TIn ArgMin<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
    100100        {
    101101            return MinArgMin(func, inputs).Item1;
     
    114114        /// </param>
    115115        /// <returns></returns>
    116         internal static Tuple<TIn, TOut> MinArgMin<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
     116        public static Tuple<TIn, TOut> MinArgMin<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
    117117        {
    118118            if (func == null) throw new ArgumentNullException(nameof(func));
     
    139139        }
    140140
    141         internal static TOut Max<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
     141        public static TOut Max<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
    142142        {
    143143            return MaxArgMax(func, inputs).Item2;
    144144        }
    145145
    146         internal static TIn ArgMax<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
     146        public static TIn ArgMax<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
    147147        {
    148148            return MaxArgMax(func, inputs).Item1;
     
    161161        /// </param>
    162162        /// <returns></returns>
    163         internal static Tuple<TIn, TOut> MaxArgMax<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
     163        public static Tuple<TIn, TOut> MaxArgMax<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
    164164        {
    165165            if (func == null) throw new ArgumentNullException(nameof(func));
Note: See TracChangeset for help on using the changeset viewer.