Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3107_LearningALPS/HeuristicLab.Algorithms.ALPS/3.3/ReseedingController.cs @ 17975

Last change on this file since 17975 was 17854, checked in by pfleck, 4 years ago

#3107 Added a new operator hook for ALPS for adapting the reinitialization strategy.

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 System;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HEAL.Attic;
30
31namespace HeuristicLab.Algorithms.ALPS {
32  [Item("ReseedingController", "An operator that controls if reseeding is needed.")]
33  [StorableType("67294ABE-C118-41F0-B736-0576F30F664F")]
34  public sealed class ReseedingController : SingleSuccessorOperator {
35    public ILookupParameter<IntValue> GenerationsParameter {
36      get { return (ILookupParameter<IntValue>)Parameters["Generations"]; }
37    }
38
39    public ILookupParameter<IntValue> AgeGapParameter {
40      get { return (ILookupParameter<IntValue>)Parameters["AgeGap"]; }
41    }
42
43    public OperatorParameter FirstLayerOperatorParameter {
44      get { return (OperatorParameter)Parameters["FirstLayerOperator"]; }
45    }
46
47    public OperatorParameter StrategyAdaptionOperatorParameter {
48      get { return (OperatorParameter)Parameters["ReinitializationStrategyOperator"]; }
49    }
50
51    public IOperator FirstLayerOperator {
52      get { return FirstLayerOperatorParameter.Value; }
53      set { FirstLayerOperatorParameter.Value = value; }
54    }
55
56    public IOperator ReinitializationStrategyOperator {
57      get { return StrategyAdaptionOperatorParameter.Value; }
58      set { StrategyAdaptionOperatorParameter.Value = value; }
59    }
60
61    [StorableConstructor]
62    private ReseedingController(StorableConstructorFlag _) : base(_) { }
63
64    [StorableHook(HookType.AfterDeserialization)]
65    private void AfterDeserialization() {
66      if (!Parameters.ContainsKey("ReinitializationStrategyOperator")) {
67        Parameters.Add(new OperatorParameter("ReinitializationStrategyOperator", "The optional operator to control the strategy parameter."));
68      }
69    }
70
71    private ReseedingController(ReseedingController original, Cloner cloner)
72      : base(original, cloner) { }
73
74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new ReseedingController(this, cloner);
76    }
77
78    public ReseedingController()
79      : base() {
80      Parameters.Add(new LookupParameter<IntValue>("Generations", "The current number of generations."));
81      Parameters.Add(new ValueLookupParameter<IntValue>("AgeGap", "The frequency of reseeding the lowest layer and scaling factor for the age-limits for the layers."));
82      Parameters.Add(new OperatorParameter("FirstLayerOperator", "The operator that is performed on the first layer if reseeding is required."));
83      Parameters.Add(new OperatorParameter("ReinitializationStrategyOperator", "The optional operator to control the strategy parameter."));
84    }
85
86    public override IOperation Apply() {
87      int generations = GenerationsParameter.ActualValue.Value;
88      int ageGap = AgeGapParameter.ActualValue.Value;
89
90      var next = new OperationCollection(base.Apply());
91      if (generations % ageGap == 0) {
92        if (FirstLayerOperator != null) {
93          var layerZeroScope = ExecutionContext.Scope.SubScopes.First();
94          next.Insert(0, ExecutionContext.CreateChildOperation(FirstLayerOperator, layerZeroScope));
95        }
96
97        // insert at index 0 => strategy adaption is scheduled before reinitialization
98        if (ReinitializationStrategyOperator != null) {
99          var lastLayerScope = ExecutionContext.Scope.SubScopes.Last();
100          next.Insert(0, ExecutionContext.CreateChildOperation(ReinitializationStrategyOperator, lastLayerScope));
101        }
102      }
103      return next;
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.