Changeset 17720 for branches/2825-NSGA3
- Timestamp:
- 08/10/20 16:05:20 (4 years ago)
- 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 76 76 private const string SetSeedRandomlyName = "Set Seed Randomly"; 77 77 private const string SeedName = "Seed"; 78 private const string AnalyzeEveryGenerationName = "Analyze Every Generation"; 78 79 79 80 // Results Names … … 128 129 } 129 130 131 private IFixedValueParameter<BoolValue> AnalyzeEveryGenerationParameter 132 { 133 get { return (IFixedValueParameter<BoolValue>)Parameters[AnalyzeEveryGenerationName]; } 134 } 135 130 136 #endregion ParameterProperties 131 137 … … 143 149 144 150 public BoolValue SetSeedRandomly => SetSeedRandomlyParameter.Value; 151 145 152 public IntValue Seed => SeedParameter.Value; 146 153 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; 149 155 150 156 #endregion Properties … … 219 225 Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true))); 220 226 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))); 221 228 } 222 229 … … 249 256 base.Initialize(cancellationToken); 250 257 251 SetParameters();252 258 InitResults(); 253 259 InitFields(); 254 260 Analyze(); 255 }256 257 // todo: trigger this when the problem parameters change258 private void SetParameters()259 {260 // Set population size261 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 probability266 MutationProbability.Value = 1.0 / Problem.Encoding.Length;267 268 // todo: Set MaximumGenerations in Problem definition269 261 } 270 262 … … 330 322 { 331 323 // todo: make parameter out of this 332 List<Solution> qt = Mutate(Recombine(solutions) , 30);324 List<Solution> qt = Mutate(Recombine(solutions)); 333 325 foreach (var solution in qt) 334 326 solution.Fitness = Evaluate(solution.Chromosome); … … 336 328 List<Solution> rt = Utility.Concat(solutions, qt); 337 329 338 // todo: remove this check339 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 342 330 solutions = NSGA3Selection.SelectSolutionsForNextGeneration(rt, GetCopyOfReferencePoints(), Problem.Maximization, PopulationSize.Value, random); 331 332 if (AnalyzeEveryGeneration.Value) 333 Analyze(); 343 334 344 335 ResultsCurrentGeneration.Value++; … … 399 390 } 400 391 392 // todo: try both recombination methods 401 393 private List<Solution> Recombine(List<Solution> solutions) 402 394 { 403 395 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); 420 406 } 421 407 422 408 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) 426 433 { 427 434 foreach (var solution in solutions) -
branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/NSGA3Selection.cs
r17692 r17720 28 28 // Do non-dominated sort 29 29 var qualities = Utility.ToFitnessMatrix(rt); 30 // compute the pareto fronts using the DominationCalculator and discard the qualities 31 // part in the inner tuples30 31 // compute the pareto fronts using the DominationCalculator 32 32 var fronts = DominationCalculator<Solution>.CalculateAllParetoFronts(rt.ToArray(), qualities, maximization, out int[] rank, true) 33 33 .Select(list => new List<Solution>(list.Select(pair => pair.Item1))).ToList(); … … 109 109 { 110 110 solution.ConvertedFitness[i] = solution.ConvertedFitness[i] / (intercepts[i] - idealPoint[i]); 111 // todo: try this 112 //solution.ConvertedFitness[i] = solution.ConvertedFitness[i] / intercepts[i]; 111 113 } 112 114 else -
branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/Utility.cs
r17707 r17720 116 116 int obj = referencePoints.First().Objectives; 117 117 int pointCount = referencePoints.Count; 118 double[,] array = new double[ obj, pointCount];118 double[,] array = new double[pointCount, obj]; 119 119 120 120 for (int p = 0; p < pointCount; p++) 121 121 for (int d = 0; d < obj; d++) 122 array[ d, p] = referencePoints[p].Values[d];122 array[p, d] = referencePoints[p].Values[d]; 123 123 124 124 return array;
Note: See TracChangeset
for help on using the changeset viewer.