source: branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/LayerReseeder.cs @ 13096

Last change on this file since 13096 was 13096, checked in by pfleck, 7 years ago

#2269

  • Simplified operator graph in MainOperator.
  • Added item descriptions.
  • Removed unnecessary code.
File size: 7.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Drawing;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Selection;
31
32namespace HeuristicLab.Algorithms.ALPS {
33  [Item("LayerReseeder", "An operator that encapsulates the reseeding of the lowest layer in ALPS.")]
34  [StorableClass]
35  public sealed class LayerReseeder : SingleSuccessorOperator, IOperatorGraphOperator {
36    public static new Image StaticItemImage {
37      get { return HeuristicLab.Common.Resources.VSImageLibrary.Module; }
38    }
39    public override Image ItemImage {
40      get {
41        if (Breakpoint) return HeuristicLab.Common.Resources.VSImageLibrary.BreakpointActive;
42        else return base.ItemImage;
43      }
44    }
45
46    private ILookupParameter<BoolValue> ContinuousReseedingParameter {
47      get { return (ILookupParameter<BoolValue>)Parameters["ContinuousReseeding"]; }
48    }
49    private ILookupParameter<IntValue> GenerationsParameter {
50      get { return (ILookupParameter<IntValue>)Parameters["Generations"]; }
51    }
52    private ILookupParameter<IntValue> AgeGapParameter {
53      get { return (ILookupParameter<IntValue>)Parameters["AgeGap"]; }
54    }
55
56    [Storable]
57    private OperatorGraph operatorGraph;
58    public OperatorGraph OperatorGraph {
59      get { return operatorGraph; }
60    }
61
62    [StorableConstructor]
63    private LayerReseeder(bool deserializing)
64      : base(deserializing) { }
65
66    private LayerReseeder(LayerReseeder original, Cloner cloner)
67      : base(original, cloner) {
68      operatorGraph = cloner.Clone(original.operatorGraph);
69    }
70    public override IDeepCloneable Clone(Cloner cloner) {
71      return new LayerReseeder(this, cloner);
72    }
73
74    public LayerReseeder()
75      : base() {
76      Parameters.Add(new LookupParameter<BoolValue>("ContinuousReseeding"));
77      Parameters.Add(new LookupParameter<IntValue>("Generations"));
78      Parameters.Add(new LookupParameter<IntValue>("AgeGap"));
79
80      operatorGraph = new OperatorGraph();
81
82      var subScopesCounter = new SubScopesCounter();
83      var numberOfReplacedCalculator = new ExpressionCalculator() { Name = "NumberOfReplaced = if ContinuousReseeding then max(LayerPopulationSize - (PopulationSize - PopulationSize / AgeGap), 0) else PopulationSize" };
84      var numberOfNewIndividualsCalculator = new ExpressionCalculator() { Name = "NumberOfNewIndividuals = PopulationSize - (LayerPopulatioSize - NumberOfReplaced)" };
85      var oldestSelector = new BestSelector();
86      var processOldest = new SubScopesProcessor();
87      var removeIndividuals = new SubScopesRemover();
88      var createIndividuals = new SolutionsCreator();
89      var mergeIndididuals = new MergingReducer();
90      var initializeAgeProsessor = new UniformSubScopesProcessor();
91      var initializeAge = new VariableCreator() { Name = "Initialize Age" };
92      var incrEvaluatedSolutionsAfterReseeding = new SubScopesCounter() { Name = "Update EvaluatedSolutions" };
93      var subScopesCounter2 = new SubScopesCounter();
94
95      OperatorGraph.InitialOperator = subScopesCounter;
96
97      subScopesCounter.ValueParameter.ActualName = "LayerPopulationSize";
98      subScopesCounter.AccumulateParameter.Value = new BoolValue(false);
99      subScopesCounter.Successor = numberOfReplacedCalculator;
100
101      numberOfReplacedCalculator.CollectedValues.Add(new LookupParameter<BoolValue>("ContinuousReseeding"));
102      numberOfReplacedCalculator.CollectedValues.Add(new LookupParameter<IntValue>("LayerPopulationSize"));
103      numberOfReplacedCalculator.CollectedValues.Add(new LookupParameter<IntValue>("PopulationSize"));
104      numberOfReplacedCalculator.CollectedValues.Add(new LookupParameter<IntValue>("AgeGap"));
105      numberOfReplacedCalculator.ExpressionResultParameter.ActualName = "NumberOfReplaced";
106      const string numSelected = "LayerPopulationSize PopulationSize PopulationSize AgeGap / - -";
107      numberOfReplacedCalculator.ExpressionParameter.Value = new StringValue(numSelected + " 0 0 " + numSelected + " < if PopulationSize ContinuousReseeding if toint");
108      numberOfReplacedCalculator.Successor = numberOfNewIndividualsCalculator;
109
110      numberOfNewIndividualsCalculator.CollectedValues.Add(new LookupParameter<IntValue>("PopulationSize"));
111      numberOfNewIndividualsCalculator.CollectedValues.Add(new LookupParameter<IntValue>("NumberOfReplaced"));
112      numberOfNewIndividualsCalculator.CollectedValues.Add(new LookupParameter<IntValue>("LayerPopulationSize"));
113      numberOfNewIndividualsCalculator.ExpressionResultParameter.ActualName = "NumberOfNewIndividuals";
114      numberOfNewIndividualsCalculator.ExpressionParameter.Value = new StringValue("PopulationSize LayerPopulationSize NumberOfReplaced - - toint");
115      numberOfNewIndividualsCalculator.Successor = oldestSelector;
116
117      oldestSelector.QualityParameter.ActualName = "Age";
118      oldestSelector.NumberOfSelectedSubScopesParameter.ActualName = "NumberOfReplaced";
119      oldestSelector.MaximizationParameter.Value = new BoolValue(true);
120      oldestSelector.CopySelected = new BoolValue(false);
121      oldestSelector.Successor = processOldest;
122
123      processOldest.Operators.Add(new EmptyOperator());
124      processOldest.Operators.Add(removeIndividuals);
125      processOldest.Successor = mergeIndididuals;
126
127      removeIndividuals.Successor = createIndividuals;
128
129      createIndividuals.NumberOfSolutionsParameter.ActualName = "NumberOfNewIndividuals";
130      createIndividuals.Successor = initializeAgeProsessor;
131
132      initializeAgeProsessor.Operator = initializeAge;
133      initializeAgeProsessor.Successor = incrEvaluatedSolutionsAfterReseeding;
134
135      initializeAge.CollectedValues.Add(new ValueParameter<DoubleValue>("Age", new DoubleValue(0)));
136      initializeAge.Successor = null;
137
138      incrEvaluatedSolutionsAfterReseeding.ValueParameter.ActualName = "EvaluatedSolutions";
139      incrEvaluatedSolutionsAfterReseeding.AccumulateParameter.Value = new BoolValue(true);
140      incrEvaluatedSolutionsAfterReseeding.Successor = null;
141
142      mergeIndididuals.Successor = subScopesCounter2;
143
144      subScopesCounter2.ValueParameter.ActualName = "LayerPopulationSize";
145      subScopesCounter2.AccumulateParameter.Value = new BoolValue(false);
146    }
147
148    public override IOperation Apply() {
149      bool continuousReseeding = ContinuousReseedingParameter.ActualValue.Value;
150      int generations = GenerationsParameter.ActualValue.Value;
151      int ageGap = AgeGapParameter.ActualValue.Value;
152
153      var next = new OperationCollection(base.Apply());
154      if (continuousReseeding || generations % ageGap == 0) {
155        var layerZeroScope = ExecutionContext.Scope.SubScopes[0];
156        if (operatorGraph.InitialOperator != null)
157          next.Insert(0, ExecutionContext.CreateChildOperation(operatorGraph.InitialOperator, layerZeroScope));
158      }
159      return next;
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.