Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/LayerUpdator.cs @ 12858

Last change on this file since 12858 was 12570, checked in by pfleck, 9 years ago

#2269

  • Added missing Hook.
  • Sealed some classes.
File size: 9.7 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Optimization.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Selection;
32
33namespace HeuristicLab.Algorithms.ALPS {
34  [Item("LayerUpdator", "Operator which manages the reseeding and creation of layers.")]
35  [StorableClass]
36  public sealed class LayerUpdator : AlgorithmOperator {
37
38    public SolutionsCreator SolutionsCreator {
39      get { return OperatorGraph.Iterate().OfType<SolutionsCreator>().First(); }
40    }
41
42    [StorableConstructor]
43    private LayerUpdator(bool deserializing)
44      : base(deserializing) { }
45    private LayerUpdator(LayerUpdator original, Cloner cloner)
46      : base(original, cloner) { }
47    public override IDeepCloneable Clone(Cloner cloner) {
48      return new LayerUpdator(this, cloner);
49    }
50    public LayerUpdator(IOperator mainOperator)
51      : base() {
52      var incGenSlr = new IntCounter() { Name = "Incr. GenerationsSinceLastRefresh" };
53      var genSlrEqAgeGap = new Comparator() { Name = "GenerationsSinceLastRefresh >= AgeGap" };
54      var updateLayersBranch = new ConditionalBranch() { Name = "UpdateLayers?" };
55      var maxLayerReached = new Comparator() { Name = "MaxLayersReached = OpenLayers >= NumberOfLayers" };
56      var maxLayerReachedBranch = new ConditionalBranch() { Name = "MaxLayersReached?" };
57      var openNewLayerCalculator = new ExpressionCalculator() { Name = "OpenNewLayer = Generations >= AgeLimits[OpenLayers - 1]" };
58      var openNewLayerBranch = new ConditionalBranch() { Name = "OpenNewLayer?" };
59      var layerCreator = new LayerCreator() { Name = "Create new Layer" };
60      var incrOpenLayers = new IntCounter() { Name = "Incr. OpenLayers" };
61      var newLayerResultsCollector = new ResultsCollector() { Name = "Collect new Layer Results" };
62      var newLayerSelector = new RightSelector();
63      var newLayerProsessor = new SubScopesProcessor() { Name = "NewLayerProcessor" };
64      var newLayerLayerProcessor = new LayerUniformSubScopesProcessor();
65      var newLayerReducer = new MergingReducer();
66      var layerInitializer = new Assigner() { Name = "Init Layer" };
67      var correctAgeProcessor = new UniformSubScopesProcessor() { Name = "Hack: Decrement Age" };
68      var correctAgeDecrementor = new IntCounter() { Name = "Age = Age - 1" };
69      var incrEvaluatedSolutionsForNewLayer = new SubScopesCounter() { Name = "Update EvaluatedSolutions" };
70      var newResultCollectionAssigner = new Assigner() { Name = "Empty Results" };
71      var regenerateLayerZeroSelector = new LeftSelector();
72      var regenerateLayerZeroProsessor = new SubScopesProcessor() { Name = "LayerZeroProcessor" };
73      var regenerateLayerZeroLayerProsessor = new LayerUniformSubScopesProcessor();
74      var regenerateLayerZeroReducer = new MergingReducer();
75      var layerSorter = new LayerSorter();
76      var subScopesRemover = new SubScopesRemover();
77      var solutionsCreator = new SolutionsCreator();
78      var initializeAgeProsessor = new UniformSubScopesProcessor();
79      var initializeAge = new VariableCreator() { Name = "Initialize Age" };
80      var incrEvaluatedSolutionsAfterReseeding = new SubScopesCounter() { Name = "Update EvaluatedSolutions" };
81      var resetGenSlr = new Assigner() { Name = "Reset GenerationsSinceLastRefresh" };
82
83      OperatorGraph.InitialOperator = incGenSlr;
84
85      incGenSlr.ValueParameter.ActualName = "GenerationsSinceLastRefresh";
86      incGenSlr.Increment = new IntValue(1);
87      incGenSlr.Successor = genSlrEqAgeGap;
88
89      genSlrEqAgeGap.LeftSideParameter.ActualName = "GenerationsSinceLastRefresh";
90      genSlrEqAgeGap.RightSideParameter.ActualName = "AgeGap";
91      genSlrEqAgeGap.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
92      genSlrEqAgeGap.ResultParameter.ActualName = "UpdateLayers";
93      genSlrEqAgeGap.Successor = updateLayersBranch;
94
95      updateLayersBranch.ConditionParameter.ActualName = "UpdateLayers";
96      updateLayersBranch.TrueBranch = maxLayerReached;
97
98      maxLayerReached.LeftSideParameter.ActualName = "OpenLayers";
99      maxLayerReached.RightSideParameter.ActualName = "NumberOfLayers";
100      maxLayerReached.ResultParameter.ActualName = "MaxLayerReached";
101      maxLayerReached.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
102      maxLayerReached.Successor = maxLayerReachedBranch;
103
104      maxLayerReachedBranch.ConditionParameter.ActualName = "MaxLayerReached";
105      maxLayerReachedBranch.FalseBranch = openNewLayerCalculator;
106      maxLayerReachedBranch.Successor = regenerateLayerZeroSelector;
107
108      openNewLayerCalculator.CollectedValues.Add(new LookupParameter<IntArray>("AgeLimits"));
109      openNewLayerCalculator.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
110      openNewLayerCalculator.CollectedValues.Add(new LookupParameter<IntValue>("NumberOfLayers"));
111      openNewLayerCalculator.CollectedValues.Add(new LookupParameter<IntValue>("OpenLayers"));
112      openNewLayerCalculator.ExpressionResultParameter.ActualName = "OpenNewLayer";
113      openNewLayerCalculator.ExpressionParameter.Value = new StringValue("Generations 1 + AgeLimits OpenLayers 1 - [] >");
114      openNewLayerCalculator.Successor = openNewLayerBranch;
115
116      openNewLayerBranch.ConditionParameter.ActualName = "OpenNewLayer";
117      openNewLayerBranch.TrueBranch = layerCreator;
118
119      layerCreator.Successor = newLayerSelector;
120
121      newLayerSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
122      newLayerSelector.CopySelected = new BoolValue(false);
123      newLayerSelector.Successor = newLayerProsessor;
124
125      newLayerProsessor.Operators.Add(new EmptyOperator());
126      newLayerProsessor.Operators.Add(newLayerLayerProcessor);
127      newLayerProsessor.Successor = newLayerReducer;
128
129      newLayerLayerProcessor.Operator = incrEvaluatedSolutionsForNewLayer;
130
131      newLayerReducer.Successor = incrOpenLayers;
132
133      layerInitializer.LeftSideParameter.ActualName = "Layer";
134      layerInitializer.RightSideParameter.ActualName = "OpenLayers";
135      layerInitializer.Successor = correctAgeProcessor;
136
137      correctAgeProcessor.Operator = correctAgeDecrementor;
138      correctAgeProcessor.Successor = incrEvaluatedSolutionsForNewLayer;
139
140      correctAgeDecrementor.Increment = new IntValue(-1);
141      correctAgeDecrementor.ValueParameter.ActualName = "Age";
142
143      incrEvaluatedSolutionsForNewLayer.ValueParameter.ActualName = "EvaluatedSolutions";
144      incrEvaluatedSolutionsForNewLayer.AccumulateParameter.Value = new BoolValue(true);
145      incrEvaluatedSolutionsForNewLayer.Successor = mainOperator;
146
147      newResultCollectionAssigner.LeftSideParameter.ActualName = "LayerResults";
148      newResultCollectionAssigner.RightSideParameter.Value = new ResultCollection();
149      newResultCollectionAssigner.Successor = mainOperator;
150      // TODO mainOperator increment age
151
152      incrOpenLayers.ValueParameter.ActualName = "OpenLayers";
153      incrOpenLayers.Increment = new IntValue(1);
154      incrOpenLayers.Successor = newLayerResultsCollector;
155
156      newLayerResultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("LayerResults", "Result set for each layer", "LayerResults"));
157      newLayerResultsCollector.CopyValue = new BoolValue(false);
158      newLayerResultsCollector.Successor = null;
159
160      regenerateLayerZeroSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
161      regenerateLayerZeroSelector.CopySelected = new BoolValue(false);
162      regenerateLayerZeroSelector.Successor = regenerateLayerZeroProsessor;
163
164      regenerateLayerZeroProsessor.Operators.Add(new EmptyOperator());
165      regenerateLayerZeroProsessor.Operators.Add(regenerateLayerZeroLayerProsessor);
166      regenerateLayerZeroProsessor.Successor = regenerateLayerZeroReducer;
167
168      regenerateLayerZeroLayerProsessor.Operator = subScopesRemover;
169
170      regenerateLayerZeroReducer.Successor = layerSorter;
171
172      layerSorter.Successor = resetGenSlr;
173
174      subScopesRemover.Successor = solutionsCreator;
175
176      solutionsCreator.Successor = initializeAgeProsessor;
177
178      initializeAgeProsessor.Operator = initializeAge;
179      initializeAgeProsessor.Successor = incrEvaluatedSolutionsAfterReseeding;
180
181      initializeAge.CollectedValues.Add(new ValueParameter<IntValue>("Age", new IntValue(0)));
182      initializeAge.Successor = null;
183
184      incrEvaluatedSolutionsAfterReseeding.ValueParameter.ActualName = "EvaluatedSolutions";
185      incrEvaluatedSolutionsAfterReseeding.AccumulateParameter.Value = new BoolValue(true);
186      incrEvaluatedSolutionsAfterReseeding.Successor = null;
187
188      resetGenSlr.LeftSideParameter.ActualName = "GenerationsSinceLastRefresh";
189      resetGenSlr.RightSideParameter.Value = new IntValue(0);
190      resetGenSlr.Successor = null;
191    }
192  }
193}
Note: See TracBrowser for help on using the repository browser.