Changeset 14614


Ignore:
Timestamp:
01/27/17 09:58:48 (4 years ago)
Author:
pfleck
Message:

#2592

  • Fixed scale invariant initialization by changing the initial sigma to a DoubleArray for initializing the covariance matrix (similar to the CMA-ES).
  • Made the MO-CMA-ES pausable (although the algorithm state is not yet storable when paused).
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/MOCMAEvolutionStrategy/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/MOCMASEvolutionStrategy.cs

    r14612 r14614  
    4242  [StorableClass]
    4343  public class MOCMASEvolutionStrategy : BasicAlgorithm {
    44     public override Type ProblemType
    45     {
     44    public override Type ProblemType {
    4645      get { return typeof(MultiObjectiveBasicProblem<RealVectorEncoding>); }
    4746    }
    48     public new MultiObjectiveBasicProblem<RealVectorEncoding> Problem
    49     {
     47    public new MultiObjectiveBasicProblem<RealVectorEncoding> Problem {
    5048      get { return (MultiObjectiveBasicProblem<RealVectorEncoding>)base.Problem; }
    5149      set { base.Problem = value; }
    5250    }
    53     public override bool SupportsPause
    54     {
    55       get { return false; }
     51    public override bool SupportsPause {
     52      get { return true; }
    5653    }
    5754
     
    9087
    9188    #region ParameterProperties
    92     public IFixedValueParameter<IntValue> MaximumRuntimeParameter
    93     {
     89    public IFixedValueParameter<IntValue> MaximumRuntimeParameter {
    9490      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumRuntimeName]; }
    9591    }
    96     public IFixedValueParameter<IntValue> SeedParameter
    97     {
     92    public IFixedValueParameter<IntValue> SeedParameter {
    9893      get { return (IFixedValueParameter<IntValue>)Parameters[SeedName]; }
    9994    }
    100     public FixedValueParameter<BoolValue> SetSeedRandomlyParameter
    101     {
     95    public FixedValueParameter<BoolValue> SetSeedRandomlyParameter {
    10296      get { return (FixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyName]; }
    10397    }
    104     public IFixedValueParameter<IntValue> PopulationSizeParameter
    105     {
     98    public IFixedValueParameter<IntValue> PopulationSizeParameter {
    10699      get { return (IFixedValueParameter<IntValue>)Parameters[PopulationSizeName]; }
    107100    }
    108     public IFixedValueParameter<IntValue> MaximumGenerationsParameter
    109     {
     101    public IFixedValueParameter<IntValue> MaximumGenerationsParameter {
    110102      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumGenerationsName]; }
    111103    }
    112     public IFixedValueParameter<IntValue> MaximumEvaluatedSolutionsParameter
    113     {
     104    public IFixedValueParameter<IntValue> MaximumEvaluatedSolutionsParameter {
    114105      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumEvaluatedSolutionsName]; }
    115106    }
    116     public IFixedValueParameter<DoubleValue> InitialSigmaParameter
    117     {
    118       get { return (IFixedValueParameter<DoubleValue>)Parameters[InitialSigmaName]; }
    119     }
    120     public IConstrainedValueParameter<IIndicator> IndicatorParameter
    121     {
     107    public IValueParameter<DoubleArray> InitialSigmaParameter {
     108      get { return (IValueParameter<DoubleArray>)Parameters[InitialSigmaName]; }
     109    }
     110    public IConstrainedValueParameter<IIndicator> IndicatorParameter {
    122111      get { return (IConstrainedValueParameter<IIndicator>)Parameters[IndicatorName]; }
    123112    }
     
    125114
    126115    #region Properties
    127     public int MaximumRuntime
    128     {
     116    public int MaximumRuntime {
    129117      get { return MaximumRuntimeParameter.Value.Value; }
    130118      set { MaximumRuntimeParameter.Value.Value = value; }
    131119    }
    132     public int Seed
    133     {
     120    public int Seed {
    134121      get { return SeedParameter.Value.Value; }
    135122      set { SeedParameter.Value.Value = value; }
    136123    }
    137     public bool SetSeedRandomly
    138     {
     124    public bool SetSeedRandomly {
    139125      get { return SetSeedRandomlyParameter.Value.Value; }
    140126      set { SetSeedRandomlyParameter.Value.Value = value; }
    141127    }
    142     public int PopulationSize
    143     {
     128    public int PopulationSize {
    144129      get { return PopulationSizeParameter.Value.Value; }
    145130      set { PopulationSizeParameter.Value.Value = value; }
    146131    }
    147     public int MaximumGenerations
    148     {
     132    public int MaximumGenerations {
    149133      get { return MaximumGenerationsParameter.Value.Value; }
    150134      set { MaximumGenerationsParameter.Value.Value = value; }
    151135    }
    152     public int MaximumEvaluatedSolutions
    153     {
     136    public int MaximumEvaluatedSolutions {
    154137      get { return MaximumEvaluatedSolutionsParameter.Value.Value; }
    155138      set { MaximumEvaluatedSolutionsParameter.Value.Value = value; }
    156139    }
    157     public double InitialSigma
    158     {
    159       get { return InitialSigmaParameter.Value.Value; }
    160       set { InitialSigmaParameter.Value.Value = value; }
    161     }
    162     public IIndicator Indicator
    163     {
     140    public DoubleArray InitialSigma {
     141      get { return InitialSigmaParameter.Value; }
     142      set { InitialSigmaParameter.Value = value; }
     143    }
     144    public IIndicator Indicator {
    164145      get { return IndicatorParameter.Value; }
    165146      set { IndicatorParameter.Value = value; }
     
    170151
    171152    #region ResultsProperties
    172     private int ResultsEvaluations
    173     {
     153    private int ResultsEvaluations {
    174154      get { return ((IntValue)Results[EvaluationsResultName].Value).Value; }
    175155      set { ((IntValue)Results[EvaluationsResultName].Value).Value = value; }
    176156    }
    177     private int ResultsIterations
    178     {
     157    private int ResultsIterations {
    179158      get { return ((IntValue)Results[IterationsResultName].Value).Value; }
    180159      set { ((IntValue)Results[IterationsResultName].Value).Value = value; }
    181160    }
    182161    #region Datatable
    183     private DataTable ResultsQualities
    184     {
     162    private DataTable ResultsQualities {
    185163      get { return (DataTable)Results[TimetableResultName].Value; }
    186164    }
    187     private DataRow ResultsBestHypervolumeDataLine
    188     {
     165    private DataRow ResultsBestHypervolumeDataLine {
    189166      get { return ResultsQualities.Rows[BestHypervolumeResultName]; }
    190167    }
    191     private DataRow ResultsHypervolumeDataLine
    192     {
     168    private DataRow ResultsHypervolumeDataLine {
    193169      get { return ResultsQualities.Rows[HypervolumeResultName]; }
    194170    }
    195     private DataRow ResultsGenerationalDistanceDataLine
    196     {
     171    private DataRow ResultsGenerationalDistanceDataLine {
    197172      get { return ResultsQualities.Rows[GenerationalDistanceResultName]; }
    198173    }
    199     private DataRow ResultsInvertedGenerationalDistanceDataLine
    200     {
     174    private DataRow ResultsInvertedGenerationalDistanceDataLine {
    201175      get { return ResultsQualities.Rows[InvertedGenerationalDistanceResultName]; }
    202176    }
    203     private DataRow ResultsCrowdingDataLine
    204     {
     177    private DataRow ResultsCrowdingDataLine {
    205178      get { return ResultsQualities.Rows[CrowdingResultName]; }
    206179    }
    207     private DataRow ResultsSpacingDataLine
    208     {
     180    private DataRow ResultsSpacingDataLine {
    209181      get { return ResultsQualities.Rows[SpacingResultName]; }
    210182    }
    211     private DataRow ResultsHypervolumeDifferenceDataLine
    212     {
     183    private DataRow ResultsHypervolumeDifferenceDataLine {
    213184      get { return ResultsQualities.Rows[DifferenceToBestKnownHypervolumeResultName]; }
    214185    }
    215186    #endregion
    216187    //QualityIndicators
    217     private double ResultsHypervolume
    218     {
     188    private double ResultsHypervolume {
    219189      get { return ((DoubleValue)Results[HypervolumeResultName].Value).Value; }
    220190      set { ((DoubleValue)Results[HypervolumeResultName].Value).Value = value; }
    221191    }
    222     private double ResultsGenerationalDistance
    223     {
     192    private double ResultsGenerationalDistance {
    224193      get { return ((DoubleValue)Results[GenerationalDistanceResultName].Value).Value; }
    225194      set { ((DoubleValue)Results[GenerationalDistanceResultName].Value).Value = value; }
    226195    }
    227     private double ResultsInvertedGenerationalDistance
    228     {
     196    private double ResultsInvertedGenerationalDistance {
    229197      get { return ((DoubleValue)Results[InvertedGenerationalDistanceResultName].Value).Value; }
    230198      set { ((DoubleValue)Results[InvertedGenerationalDistanceResultName].Value).Value = value; }
    231199    }
    232     private double ResultsCrowding
    233     {
     200    private double ResultsCrowding {
    234201      get { return ((DoubleValue)Results[CrowdingResultName].Value).Value; }
    235202      set { ((DoubleValue)Results[CrowdingResultName].Value).Value = value; }
    236203    }
    237     private double ResultsSpacing
    238     {
     204    private double ResultsSpacing {
    239205      get { return ((DoubleValue)Results[SpacingResultName].Value).Value; }
    240206      set { ((DoubleValue)Results[SpacingResultName].Value).Value = value; }
    241207    }
    242     private double ResultsBestHypervolume
    243     {
     208    private double ResultsBestHypervolume {
    244209      get { return ((DoubleValue)Results[BestHypervolumeResultName].Value).Value; }
    245210      set { ((DoubleValue)Results[BestHypervolumeResultName].Value).Value = value; }
    246211    }
    247     private double ResultsBestKnownHypervolume
    248     {
     212    private double ResultsBestKnownHypervolume {
    249213      get { return ((DoubleValue)Results[BestKnownHypervolumeResultName].Value).Value; }
    250214      set { ((DoubleValue)Results[BestKnownHypervolumeResultName].Value).Value = value; }
    251215    }
    252     private double ResultsDifferenceBestKnownHypervolume
    253     {
     216    private double ResultsDifferenceBestKnownHypervolume {
    254217      get { return ((DoubleValue)Results[DifferenceToBestKnownHypervolumeResultName].Value).Value; }
    255218      set { ((DoubleValue)Results[DifferenceToBestKnownHypervolumeResultName].Value).Value = value; }
     
    257220    }
    258221    //Solutions
    259     private DoubleMatrix ResultsSolutions
    260     {
     222    private DoubleMatrix ResultsSolutions {
    261223      get { return ((DoubleMatrix)Results[CurrentFrontResultName].Value); }
    262224      set { Results[CurrentFrontResultName].Value = value; }
    263225    }
    264     private ScatterPlotContent ResultsScatterPlot
    265     {
     226    private ScatterPlotContent ResultsScatterPlot {
    266227      get { return ((ScatterPlotContent)Results[ScatterPlotResultName].Value); }
    267228      set { Results[ScatterPlotResultName].Value = value; }
     
    275236      Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
    276237      Parameters.Add(new FixedValueParameter<IntValue>(PopulationSizeName, "λ (lambda) - the size of the offspring population.", new IntValue(20)));
    277       Parameters.Add(new FixedValueParameter<DoubleValue>(InitialSigmaName, "The initial sigma is a single value > 0.", new DoubleValue(0.5)));
     238      Parameters.Add(new ValueParameter<DoubleArray>(InitialSigmaName, "The initial sigma can be a single value or a value for each dimension. All values need to be > 0.", new DoubleArray(new[] { 0.5 })));
    278239      Parameters.Add(new FixedValueParameter<IntValue>(MaximumGenerationsName, "The maximum number of generations which should be processed.", new IntValue(1000)));
    279240      Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluatedSolutionsName, "The maximum number of evaluated solutions that should be computed.", new IntValue(int.MaxValue)));
     
    289250
    290251    #region Mainloop
    291     protected override void Run(CancellationToken cancellationToken) {
    292       // Set up the algorithm
     252
     253    protected override void Initialize(CancellationToken cancellationToken) {
    293254      if (SetSeedRandomly) Seed = new System.Random().Next();
    294255      random.Reset(Seed);
     
    300261      AnalyzeSolutions();
    301262
     263      ResultsIterations = 1;
     264    }
     265
     266    protected override void Run(CancellationToken cancellationToken) {
    302267      // Loop until iteration limit reached or canceled.
    303       for (ResultsIterations = 1; ResultsIterations < MaximumGenerations; ResultsIterations++) {
     268      while (ResultsIterations < MaximumGenerations) {
    304269        try {
    305270          Iterate();
     271          ResultsIterations++;
    306272          cancellationToken.ThrowIfCancellationRequested();
    307         }
    308         finally {
     273        } finally {
    309274          AnalyzeSolutions();
    310275        }
     
    328293    #region Initialization
    329294    private MOCMAESIndividual InitializeIndividual(RealVector x) {
    330       var zeros = new RealVector(Enumerable.Range(0, Problem.Encoding.Length).Select(i => (Problem.Encoding.Bounds[i, 1] + Problem.Encoding.Bounds[i, 0]) / 2).ToArray());
    331       var identity = new double[x.Length, x.Length];
    332       for (var i = 0; i < x.Length; i++) identity[i, i] = (Problem.Encoding.Bounds[i % Problem.Encoding.Bounds.Rows, 1] - Problem.Encoding.Bounds[i % Problem.Encoding.Bounds.Rows, 0]);
    333       return new MOCMAESIndividual(x, internals.TargetSuccessProbability, InitialSigma, zeros, identity, internals);
     295      var zeros = new RealVector(x.Length);
     296      var c = new double[x.Length, x.Length];
     297      double sigma = InitialSigma.Max();
     298      for (var i = 0; i < x.Length; i++) {
     299        double d = InitialSigma[i % InitialSigma.Length] / sigma;
     300        c[i, i] = d * d;
     301      }
     302      return new MOCMAESIndividual(x, internals.TargetSuccessProbability, sigma, zeros, c, internals);
    334303    }
    335304    private void InitSolutions() {
Note: See TracChangeset for help on using the changeset viewer.