Changeset 16583
 Timestamp:
 02/01/19 11:20:07 (5 months ago)
 Location:
 branches/2987_MOEAD_Algorithm/HeuristicLab.Algorithms.MOEAD/3.4
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

branches/2987_MOEAD_Algorithm/HeuristicLab.Algorithms.MOEAD/3.4/MOEADAlgorithm.cs
r16560 r16583 24 24 25 25 protected override void Run(CancellationToken cancellationToken) { 26 if (previousExecutionState != ExecutionState.Paused) { 27 InitializeAlgorithm(cancellationToken); 28 } 29 26 30 var populationSize = PopulationSize.Value; 27 31 bool[] maximization = ((BoolArray)Problem.MaximizationParameter.ActualValue).CloneAsArray(); 
branches/2987_MOEAD_Algorithm/HeuristicLab.Algorithms.MOEAD/3.4/MOEADAlgorithmBase.cs
r16561 r16583 3 3 using HeuristicLab.Core; 4 4 using HeuristicLab.Data; 5 using HeuristicLab.ExpressionGenerator; 5 6 using HeuristicLab.Optimization; 6 7 using HeuristicLab.Parameters; … … 58 59 [Storable] 59 60 protected IScope globalScope; 61 62 [Storable] 63 protected ExecutionState previousExecutionState; 60 64 #endregion 61 65 … … 211 215 functionType = original.functionType; 212 216 evaluatedSolutions = original.evaluatedSolutions; 217 previousExecutionState = original.previousExecutionState; 213 218 214 219 if (original.IdealPoint != null) { … … 291 296 } 292 297 293 protected override void Initialize(CancellationToken cancellationToken) {298 protected void InitializeAlgorithm(CancellationToken cancellationToken) { 294 299 globalScope = new Scope("Global Scope"); 295 300 executionContext = new ExecutionContext(null, this, globalScope); … … 335 340 336 341 evaluatedSolutions = populationSize; 342 } 343 344 protected override void Initialize(CancellationToken cancellationToken) { 345 globalScope = new Scope("Global Scope"); 346 executionContext = new ExecutionContext(null, this, globalScope); 347 348 // set the execution context for parameters to allow lookup 349 foreach (var parameter in Problem.Parameters.OfType<IValueParameter>()) { 350 // we need all of these in order for the wiring of the operators to work 351 globalScope.Variables.Add(new Variable(parameter.Name, parameter.Value)); 352 } 353 globalScope.Variables.Add(new Variable("Results", Results)); // make results available as a parameter for analyzers etc. 354 355 var rand = RandomParameter.Value; 356 if (SetSeedRandomly) Seed = RandomSeedGenerator.GetSeed(); 357 rand.Reset(Seed); 358 359 bool[] maximization = ((BoolArray)Problem.MaximizationParameter.ActualValue).CloneAsArray(); 360 var dimensions = maximization.Length; 361 362 var populationSize = PopulationSize.Value; 363 364 InitializePopulation(executionContext, cancellationToken, rand, maximization); 365 InitializeUniformWeights(rand, populationSize, dimensions); 366 InitializeNeighbourHood(lambda, populationSize, NeighbourSize); 367 368 IdealPoint = new double[dimensions]; 369 IdealPoint.UpdateIdeal(population); 370 371 NadirPoint = new double[dimensions]; 372 NadirPoint.UpdateNadir(population); 373 374 var functionTypeString = FunctionTypeParameter.Value.Value; 375 switch (functionTypeString) { 376 case "Chebyshev": 377 functionType = FunctionType.TCHE; 378 break; 379 case "PBI": 380 functionType = FunctionType.PBI; 381 break; 382 case "Weighted Sum": 383 functionType = FunctionType.AGG; 384 break; 385 } 386 387 evaluatedSolutions = populationSize; 337 388 338 389 base.Initialize(cancellationToken); … … 342 393 343 394 protected void InitializeUniformWeights(IRandom random, int populationSize, int dimensions) { 344 if (dimensions > 2) { 345 throw new ArgumentException("The current implementation doesn't support more than 2 dimensions."); 346 } 347 348 lambda = new double[populationSize][]; 349 var values = SequenceGenerator.GenerateSteps(0m, 1m, 1m / populationSize).ToArray(); 350 351 for (int i = 0; i < populationSize; ++i) { 352 var w = (double)values[i]; 353 lambda[i] = new[] { w, 1  w }; 354 } 355 lambda.ShuffleInPlace(random); 395 lambda = Enumerable.Range(0, populationSize).Select(_ => GenerateSample(random, dimensions)).ToArray(); 396 } 397 398 // implements random number generation from https://en.wikipedia.org/wiki/Dirichlet_distribution#Random_number_generation 399 private double[] GenerateSample(IRandom random, int dim) { 400 var sum = 0d; 401 var sample = new double[dim]; 402 for (int i = 0; i < dim; ++i) { 403 sample[i] = GammaDistributedRandom.NextDouble(random, 1, 1); 404 sum += sample[i]; 405 } 406 for (int i = 0; i < dim; ++i) { 407 sample[i] /= sum; 408 } 409 return sample; 356 410 } 357 411 … … 446 500 switch (functionType) { 447 501 case FunctionType.TCHE: { 448 double maxFun = 1.0e+30;502 double maxFun = double.MinValue; 449 503 450 504 for (int n = 0; n < dim; n++) { 451 505 double diff = Math.Abs(qualities[n]  IdealPoint[n]); 452 506 453 double feval = lambda[n] == 0 ? 0.0001 * diff : diff * lambda[n]; 507 var l = lambda[n].IsAlmost(0) ? 0.0001 : lambda[n]; 508 //var feval = l * diff; 509 // introduce objective scaling 510 var feval = l * (qualities[n]  IdealPoint[n]) / (NadirPoint[n]  IdealPoint[n]); 454 511 if (feval > maxFun) { 455 512 maxFun = feval; … … 608 665 } 609 666 667 protected override void OnExecutionStateChanged() { 668 previousExecutionState = ExecutionState; 669 base.OnExecutionStateChanged(); 670 } 671 610 672 protected override void OnStopped() { 611 673 if (solutions != null) { … … 621 683 jointPopulation.Clear(); 622 684 } 685 executionContext.Scope.SubScopes.Clear(); 623 686 base.OnStopped(); 624 687 }
Note: See TracChangeset
for help on using the changeset viewer.