Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16583 for branches


Ignore:
Timestamp:
02/01/19 11:20:07 (5 years ago)
Author:
bburlacu
Message:

#2987: Implement better uniform weight generation, objective scaling, algorithm Pause support.

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  
    2424
    2525    protected override void Run(CancellationToken cancellationToken) {
     26      if (previousExecutionState != ExecutionState.Paused) {
     27        InitializeAlgorithm(cancellationToken);
     28      }
     29
    2630      var populationSize = PopulationSize.Value;
    2731      bool[] maximization = ((BoolArray)Problem.MaximizationParameter.ActualValue).CloneAsArray();
  • branches/2987_MOEAD_Algorithm/HeuristicLab.Algorithms.MOEAD/3.4/MOEADAlgorithmBase.cs

    r16561 r16583  
    33using HeuristicLab.Core;
    44using HeuristicLab.Data;
     5using HeuristicLab.ExpressionGenerator;
    56using HeuristicLab.Optimization;
    67using HeuristicLab.Parameters;
     
    5859    [Storable]
    5960    protected IScope globalScope;
     61
     62    [Storable]
     63    protected ExecutionState previousExecutionState;
    6064    #endregion
    6165
     
    211215      functionType = original.functionType;
    212216      evaluatedSolutions = original.evaluatedSolutions;
     217      previousExecutionState = original.previousExecutionState;
    213218
    214219      if (original.IdealPoint != null) {
     
    291296    }
    292297
    293     protected override void Initialize(CancellationToken cancellationToken) {
     298    protected void InitializeAlgorithm(CancellationToken cancellationToken) {
    294299      globalScope = new Scope("Global Scope");
    295300      executionContext = new ExecutionContext(null, this, globalScope);
     
    335340
    336341      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;
    337388
    338389      base.Initialize(cancellationToken);
     
    342393
    343394    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;
    356410    }
    357411
     
    446500      switch (functionType) {
    447501        case FunctionType.TCHE: {
    448             double maxFun = -1.0e+30;
     502            double maxFun = double.MinValue;
    449503
    450504            for (int n = 0; n < dim; n++) {
    451505              double diff = Math.Abs(qualities[n] - IdealPoint[n]);
    452506
    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]);
    454511              if (feval > maxFun) {
    455512                maxFun = feval;
     
    608665    }
    609666
     667    protected override void OnExecutionStateChanged() {
     668      previousExecutionState = ExecutionState;
     669      base.OnExecutionStateChanged();
     670    }
     671
    610672    protected override void OnStopped() {
    611673      if (solutions != null) {
     
    621683        jointPopulation.Clear();
    622684      }
     685      executionContext.Scope.SubScopes.Clear();
    623686      base.OnStopped();
    624687    }
Note: See TracChangeset for help on using the changeset viewer.