Ignore:
Timestamp:
02/28/19 14:23:36 (2 years ago)
Author:
bburlacu
Message:

#2987: Migrate to new persistence. Add support for objective scaling.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2987_MOEAD_Algorithm/HeuristicLab.Algorithms.MOEAD/3.4/MOEADAlgorithmBase.cs

    r16583 r16630  
    1 using HeuristicLab.Analysis;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using HEAL.Attic;
     23using HeuristicLab.Analysis;
    224using HeuristicLab.Common;
    325using HeuristicLab.Core;
     
    628using HeuristicLab.Optimization;
    729using HeuristicLab.Parameters;
    8 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     30using HeuristicLab.Problems.DataAnalysis;
    931using HeuristicLab.Random;
    1032using System;
    1133using System.Collections.Generic;
    1234using System.Linq;
    13 
    1435using CancellationToken = System.Threading.CancellationToken;
    1536
    1637namespace HeuristicLab.Algorithms.MOEAD {
    1738  [Item("MOEADAlgorithmBase", "Base class for all MOEA/D algorithm variants.")]
    18   [StorableClass]
     39  [StorableType("E00BAC79-C6F9-42B6-8468-DEEC7FFCE804")]
    1940  public abstract class MOEADAlgorithmBase : BasicAlgorithm {
    2041    #region data members
     
    6283    [Storable]
    6384    protected ExecutionState previousExecutionState;
     85
     86    [Storable]
     87    protected ExecutionState executionState;
    6488    #endregion
    6589
     
    81105    private const string MaximumNumberOfReplacedSolutionsParameterName = "MaximumNumberOfReplacedSolutions";
    82106    private const string FunctionTypeParameterName = "FunctionType";
     107    private const string NormalizeObjectivesParameterName = "NormalizeObjectives";
    83108
    84109    public IValueParameter<MultiAnalyzer> AnalyzerParameter {
     
    91116    public IFixedValueParameter<IntValue> NeighbourSizeParameter {
    92117      get { return (IFixedValueParameter<IntValue>)Parameters[NeighbourSizeParameterName]; }
     118    }
     119    public IFixedValueParameter<BoolValue> NormalizeObjectivesParameter {
     120      get { return (IFixedValueParameter<BoolValue>)Parameters[NormalizeObjectivesParameterName]; }
    93121    }
    94122    public IFixedValueParameter<IntValue> MaximumNumberOfReplacedSolutionsParameter {
     
    141169      get { return SetSeedRandomlyParameter.Value.Value; }
    142170      set { SetSeedRandomlyParameter.Value.Value = value; }
     171    }
     172    public bool NormalizeObjectives {
     173      get { return NormalizeObjectivesParameter.Value.Value; }
     174      set { NormalizeObjectivesParameter.Value.Value = value; }
    143175    }
    144176    public IntValue PopulationSize {
     
    204236      Parameters.Add(new FixedValueParameter<IntValue>(MaximumNumberOfReplacedSolutionsParameterName, new IntValue(2)));
    205237      Parameters.Add(new FixedValueParameter<DoubleValue>(NeighbourhoodSelectionProbabilityParameterName, new DoubleValue(0.1)));
     238      Parameters.Add(new FixedValueParameter<BoolValue>(NormalizeObjectivesParameterName, new BoolValue(true)));
    206239
    207240      var functionTypeParameter = new ConstrainedValueParameter<StringValue>(FunctionTypeParameterName);
     
    258291    }
    259292
     293
     294    [StorableHook(HookType.AfterDeserialization)]
     295    private void AfterDeserialization() {
     296      if (!Parameters.ContainsKey(NormalizeObjectivesParameterName)) {
     297        Parameters.Add(new FixedValueParameter<BoolValue>(NormalizeObjectivesParameterName, new BoolValue(true)));
     298      }
     299    }
     300
    260301    [StorableConstructor]
    261     protected MOEADAlgorithmBase(bool deserializing) : base(deserializing) { }
     302    protected MOEADAlgorithmBase(StorableConstructorFlag _) : base(_) { }
    262303    #endregion
    263304
     
    292333    }
    293334
    294     public override void Prepare() {
    295       base.Prepare();
    296     }
    297 
    298335    protected void InitializeAlgorithm(CancellationToken cancellationToken) {
    299       globalScope = new Scope("Global Scope");
    300       executionContext = new ExecutionContext(null, this, globalScope);
    301 
    302       // set the execution context for parameters to allow lookup
    303       foreach (var parameter in Problem.Parameters.OfType<IValueParameter>()) {
    304         // we need all of these in order for the wiring of the operators to work
    305         globalScope.Variables.Add(new Variable(parameter.Name, parameter.Value));
    306       }
    307       globalScope.Variables.Add(new Variable("Results", Results)); // make results available as a parameter for analyzers etc.
    308 
    309336      var rand = RandomParameter.Value;
    310337      if (SetSeedRandomly) Seed = RandomSeedGenerator.GetSeed();
     
    320347      InitializeNeighbourHood(lambda, populationSize, NeighbourSize);
    321348
     349      //IdealPoint = Enumerable.Repeat(double.MaxValue, dimensions).ToArray();
    322350      IdealPoint = new double[dimensions];
    323351      IdealPoint.UpdateIdeal(population);
    324352
     353      //NadirPoint = Enumerable.Repeat(double.MinValue, dimensions).ToArray();
    325354      NadirPoint = new double[dimensions];
    326355      NadirPoint.UpdateNadir(population);
     
    353382      globalScope.Variables.Add(new Variable("Results", Results)); // make results available as a parameter for analyzers etc.
    354383
    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;
    388 
    389384      base.Initialize(cancellationToken);
    390385    }
     
    396391    }
    397392
    398     // implements random number generation from https://en.wikipedia.org/wiki/Dirichlet_distribution#Random_number_generation
     393    // implements random number generation from https://en.wikipedia.org/wiki/Dirichlet_distribution#Random_number_generation
    399394    private double[] GenerateSample(IRandom random, int dim) {
    400395      var sum = 0d;
     
    503498
    504499            for (int n = 0; n < dim; n++) {
    505               double diff = Math.Abs(qualities[n] - IdealPoint[n]);
    506 
    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]);
     500              var diff = qualities[n] - IdealPoint[n];
     501              if (NormalizeObjectives) {
     502                diff /= NadirPoint[n] - IdealPoint[n];
     503              }
     504              var l = lambda[n].IsAlmost(0) ? 1e-4 : lambda[n];
     505              var feval = l * Math.Abs(diff);
     506
    511507              if (feval > maxFun) {
    512508                maxFun = feval;
     
    666662
    667663    protected override void OnExecutionStateChanged() {
    668       previousExecutionState = ExecutionState;
     664      previousExecutionState = executionState;
     665      executionState = ExecutionState;
    669666      base.OnExecutionStateChanged();
    670667    }
    671668
    672     protected override void OnStopped() {
     669    public void ClearState() {
    673670      if (solutions != null) {
    674671        solutions.Clear();
     
    683680        jointPopulation.Clear();
    684681      }
    685       executionContext.Scope.SubScopes.Clear();
     682      lambda = null;
     683      neighbourhood = null;
     684      if (executionContext != null && executionContext.Scope != null) {
     685        executionContext.Scope.SubScopes.Clear();
     686      }
     687    }
     688
     689    protected override void OnStopped() {
     690      ClearState();
    686691      base.OnStopped();
    687692    }
Note: See TracChangeset for help on using the changeset viewer.