Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17720


Ignore:
Timestamp:
08/10/20 16:05:20 (4 years ago)
Author:
dleko
Message:

#2825 Use HeuristicLab's SBX recombination.
Add AnalyzeEveryGeneration parameter (for longer tests).
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

    r17719 r17720  
    7676        private const string SetSeedRandomlyName = "Set Seed Randomly";
    7777        private const string SeedName = "Seed";
     78        private const string AnalyzeEveryGenerationName = "Analyze Every Generation";
    7879
    7980        // Results Names
     
    128129        }
    129130
     131        private IFixedValueParameter<BoolValue> AnalyzeEveryGenerationParameter
     132        {
     133            get { return (IFixedValueParameter<BoolValue>)Parameters[AnalyzeEveryGenerationName]; }
     134        }
     135
    130136        #endregion ParameterProperties
    131137
     
    143149
    144150        public BoolValue SetSeedRandomly => SetSeedRandomlyParameter.Value;
     151
    145152        public IntValue Seed => SeedParameter.Value;
    146153
    147         // todo: create one property for the Generated Reference Points and one for the current
    148         // generations reference points
     154        public BoolValue AnalyzeEveryGeneration => AnalyzeEveryGenerationParameter.Value;
    149155
    150156        #endregion Properties
     
    219225            Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
    220226            Parameters.Add(new FixedValueParameter<IntValue>(SeedName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
     227            Parameters.Add(new FixedValueParameter<BoolValue>(AnalyzeEveryGenerationName, "If set to false, will only calculate the indicators (IGD, etc.) on the last generation and when pausing or stopping the algorithm.", new BoolValue(true)));
    221228        }
    222229
     
    249256            base.Initialize(cancellationToken);
    250257
    251             SetParameters();
    252258            InitResults();
    253259            InitFields();
    254260            Analyze();
    255         }
    256 
    257         // todo: trigger this when the problem parameters change
    258         private void SetParameters()
    259         {
    260             // Set population size
    261             int numberOfGeneratedReferencePoints = ReferencePoint.GetNumberOfGeneratedReferencePoints(Objectives);
    262             if (numberOfGeneratedReferencePoints == -1) throw new NotSupportedException("The number of objectives is not supported.");
    263             PopulationSize.Value = ReferencePoint.GetPopulationSizeForReferencePoints(numberOfGeneratedReferencePoints);
    264 
    265             // Set mutation probability
    266             MutationProbability.Value = 1.0 / Problem.Encoding.Length;
    267 
    268             // todo: Set MaximumGenerations in Problem definition
    269261        }
    270262
     
    330322                {
    331323                    // todo: make parameter out of this
    332                     List<Solution> qt = Mutate(Recombine(solutions), 30);
     324                    List<Solution> qt = Mutate(Recombine(solutions));
    333325                    foreach (var solution in qt)
    334326                        solution.Fitness = Evaluate(solution.Chromosome);
     
    336328                    List<Solution> rt = Utility.Concat(solutions, qt);
    337329
    338                     // todo: remove this check
    339                     for (int i = 0; i < rt.Count / 2; i++)
    340                         if (!solutions.Contains(rt[i])) throw new Exception($"This should never happen: !solutions.Contains(rt[{i}])");
    341 
    342330                    solutions = NSGA3Selection.SelectSolutionsForNextGeneration(rt, GetCopyOfReferencePoints(), Problem.Maximization, PopulationSize.Value, random);
     331
     332                    if (AnalyzeEveryGeneration.Value)
     333                        Analyze();
    343334
    344335                    ResultsCurrentGeneration.Value++;
     
    399390        }
    400391
     392        // todo: try both recombination methods
    401393        private List<Solution> Recombine(List<Solution> solutions)
    402394        {
    403395            List<Solution> childSolutions = new List<Solution>();
    404 
    405             for (int i = 0; i < solutions.Count; i += 2)
    406             {
    407                 int parentIndex1 = random.Next(solutions.Count);
    408                 int parentIndex2 = random.Next(solutions.Count);
    409                 // ensure that the parents are not the same object
    410                 if (parentIndex1 == parentIndex2) parentIndex2 = (parentIndex2 + 1) % solutions.Count;
    411                 var parent1 = solutions[parentIndex1];
    412                 var parent2 = solutions[parentIndex2];
    413 
    414                 // Do crossover with crossoverProbabilty == 1 in order to guarantee that a crossover happens
    415                 var children = SimulatedBinaryCrossover.Apply(random,
    416                 Problem.Encoding.Bounds, parent1.Chromosome, parent2.Chromosome, CrossoverProbability.Value);
    417 
    418                 childSolutions.Add(new Solution(children.Item1));
    419                 childSolutions.Add(new Solution(children.Item2));
     396            for (int i = 0; i < solutions.Count; i++)
     397            {
     398                var parent1 = solutions[random.Next(solutions.Count)];
     399                var parent2 = solutions[random.Next(solutions.Count)];
     400                var childChromosome = HeuristicLab.Encodings.RealVectorEncoding.SimulatedBinaryCrossover.Apply(random, parent1.Chromosome, parent2.Chromosome, new DoubleValue(5));
     401
     402                BoundsChecker.Apply(childChromosome, Problem.Encoding.Bounds);
     403
     404                Solution child = new Solution(childChromosome);
     405                childSolutions.Add(child);
    420406            }
    421407
    422408            return childSolutions;
    423         }
    424 
    425         private List<Solution> Mutate(List<Solution> solutions, double eta)
     409
     410            //List<Solution> childSolutions = new List<Solution>();
     411
     412            //for (int i = 0; i < solutions.Count; i += 2)
     413            //{
     414            //    int parentIndex1 = random.Next(solutions.Count);
     415            //    int parentIndex2 = random.Next(solutions.Count);
     416            //    // ensure that the parents are not the same object
     417            //    if (parentIndex1 == parentIndex2) parentIndex2 = (parentIndex2 + 1) % solutions.Count;
     418            //    var parent1 = solutions[parentIndex1];
     419            //    var parent2 = solutions[parentIndex2];
     420
     421            // // Do crossover with crossoverProbabilty == 1 in order to guarantee that a crossover
     422            // happens var children = SimulatedBinaryCrossover.Apply(random,
     423            // Problem.Encoding.Bounds, parent1.Chromosome, parent2.Chromosome, CrossoverProbability.Value);
     424
     425            //    childSolutions.Add(new Solution(children.Item1));
     426            //    childSolutions.Add(new Solution(children.Item2));
     427            //}
     428
     429            //return childSolutions;
     430        }
     431
     432        private List<Solution> Mutate(List<Solution> solutions, double eta = 20)
    426433        {
    427434            foreach (var solution in solutions)
  • branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/NSGA3Selection.cs

    r17692 r17720  
    2828            // Do non-dominated sort
    2929            var qualities = Utility.ToFitnessMatrix(rt);
    30             // compute the pareto fronts using the DominationCalculator and discard the qualities
    31             // part in the inner tuples
     30
     31            // compute the pareto fronts using the DominationCalculator
    3232            var fronts = DominationCalculator<Solution>.CalculateAllParetoFronts(rt.ToArray(), qualities, maximization, out int[] rank, true)
    3333                .Select(list => new List<Solution>(list.Select(pair => pair.Item1))).ToList();
     
    109109                    {
    110110                        solution.ConvertedFitness[i] = solution.ConvertedFitness[i] / (intercepts[i] - idealPoint[i]);
     111                        // todo: try this
     112                        //solution.ConvertedFitness[i] = solution.ConvertedFitness[i] / intercepts[i];
    111113                    }
    112114                    else
  • branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/Utility.cs

    r17707 r17720  
    116116            int obj = referencePoints.First().Objectives;
    117117            int pointCount = referencePoints.Count;
    118             double[,] array = new double[obj, pointCount];
     118            double[,] array = new double[pointCount, obj];
    119119
    120120            for (int p = 0; p < pointCount; p++)
    121121                for (int d = 0; d < obj; d++)
    122                     array[d, p] = referencePoints[p].Values[d];
     122                    array[p, d] = referencePoints[p].Values[d];
    123123
    124124            return array;
Note: See TracChangeset for help on using the changeset viewer.