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 |
|
---|
22 | using System.Linq;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Optimization.Operators;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Selection;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Algorithms.ALPS {
|
---|
34 | [Item("LayerUpdator", "Operator which manages the reseeding and creation of layers.")]
|
---|
35 | [StorableClass]
|
---|
36 | public 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 | // TODO: if expression calculator supports array indexing, use expression calculator
|
---|
56 | //var openNewLayerCalculator = new ExpressionCalculator();
|
---|
57 | var openNewLayerCalculator = new OpenNewLayerCalculator();
|
---|
58 | var openNewLayerBranch = new ConditionalBranch() { Name = "OpenNewLayer?" };
|
---|
59 | //var layerCloner = new LastSubScopeCloner() { Name = "Copy Lower Layer" };
|
---|
60 | var layerCreator = new LayerCreator() { Name = "Create new Layer" };
|
---|
61 | var incrOpenLayers = new IntCounter() { Name = "Incr. OpenLayers" };
|
---|
62 | var newLayerResultsCollector = new ResultsCollector() { Name = "Collect new Layer Results" };
|
---|
63 | var newLayerSelector = new RightSelector();
|
---|
64 | var newLayerProsessor = new SubScopesProcessor();
|
---|
65 | var newLayerLayerProcessor = new LayerUniformSubScopesProcessor();
|
---|
66 | var newLayerReducer = new MergingReducer();
|
---|
67 | var layerInitializer = new Assigner() { Name = "Init Layer" };
|
---|
68 | var correctAgeProcessor = new UniformSubScopesProcessor() { Name = "Hack: Decrement Age" };
|
---|
69 | var correctAgeDecrementor = new IntCounter() { Name = "Age = Age - 1" };
|
---|
70 | var incrEvaluatedSolutionsForNewLayer = new SubScopesCounter() { Name = "Update EvaluatedSolutions" };
|
---|
71 | var newResultCollectionAssigner = new Assigner() { Name = "Empty Results" };
|
---|
72 | var regenerateLayerZeroSelector = new LeftSelector();
|
---|
73 | var regenerateLayerZeroProsessor = new SubScopesProcessor();
|
---|
74 | var regenerateLayerZeroLayerProsessor = new LayerUniformSubScopesProcessor();
|
---|
75 | var regenerateLayerZeroReducer = new MergingReducer();
|
---|
76 | var layerSorter = new LayerSorter();
|
---|
77 | var subScopesRemover = new SubScopesRemover();
|
---|
78 | var solutionsCreator = new SolutionsCreator();
|
---|
79 | var initializeAgeProsessor = new UniformSubScopesProcessor();
|
---|
80 | var initializeAge = new VariableCreator() { Name = "Initialize Age" };
|
---|
81 | var incrEvaluatedSolutionsAfterReseeding = new SubScopesCounter() { Name = "Update EvaluatedSolutions" };
|
---|
82 | var resetGenSlr = new Assigner() { Name = "Reset GenerationsSinceLastRefresh" };
|
---|
83 |
|
---|
84 | OperatorGraph.InitialOperator = incGenSlr;
|
---|
85 |
|
---|
86 | incGenSlr.ValueParameter.ActualName = "GenerationsSinceLastRefresh";
|
---|
87 | incGenSlr.Increment = new IntValue(1);
|
---|
88 | incGenSlr.Successor = genSlrEqAgeGap;
|
---|
89 |
|
---|
90 | genSlrEqAgeGap.LeftSideParameter.ActualName = "GenerationsSinceLastRefresh";
|
---|
91 | genSlrEqAgeGap.RightSideParameter.ActualName = "AgeGap";
|
---|
92 | genSlrEqAgeGap.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
93 | genSlrEqAgeGap.ResultParameter.ActualName = "UpdateLayers";
|
---|
94 | genSlrEqAgeGap.Successor = updateLayersBranch;
|
---|
95 |
|
---|
96 | updateLayersBranch.ConditionParameter.ActualName = "UpdateLayers";
|
---|
97 | updateLayersBranch.TrueBranch = openNewLayerCalculator;
|
---|
98 |
|
---|
99 | //openNewLayerCalculator.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
100 | //openNewLayerCalculator.CollectedValues.Add(new LookupParameter<IntValue>("AgeGap"));
|
---|
101 | //openNewLayerCalculator.CollectedValues.Add(new LookupParameter<IntValue>("OpenLayers"));
|
---|
102 | //openNewLayerCalculator.CollectedValues.Add(new LookupParameter<IntValue>("NumberOfLayers"));
|
---|
103 | //openNewLayerCalculator.ExpressionParameter.Value = new StringValue("Generations AgeLimits [] OpenedLayers 1 - 1 + = false OpenLayers NumberOfLayers < if");
|
---|
104 | //openNewLayerCalculator.ExpressionResultParameter.ActualName = "OpenNewLayer";
|
---|
105 | openNewLayerCalculator.Successor = openNewLayerBranch;
|
---|
106 |
|
---|
107 | openNewLayerBranch.ConditionParameter.ActualName = "OpenNewLayer";
|
---|
108 | openNewLayerBranch.TrueBranch = layerCreator;
|
---|
109 | openNewLayerBranch.Successor = regenerateLayerZeroSelector;
|
---|
110 |
|
---|
111 | layerCreator.Successor = newLayerSelector;
|
---|
112 |
|
---|
113 | newLayerSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
|
---|
114 | newLayerSelector.CopySelected = new BoolValue(false);
|
---|
115 | newLayerSelector.Successor = newLayerProsessor;
|
---|
116 |
|
---|
117 | newLayerProsessor.Operators.Add(new EmptyOperator());
|
---|
118 | newLayerProsessor.Operators.Add(newLayerLayerProcessor);
|
---|
119 | newLayerProsessor.Successor = newLayerReducer;
|
---|
120 |
|
---|
121 | newLayerLayerProcessor.Operator = incrEvaluatedSolutionsForNewLayer;
|
---|
122 |
|
---|
123 | newLayerReducer.Successor = incrOpenLayers;
|
---|
124 |
|
---|
125 | layerInitializer.LeftSideParameter.ActualName = "Layer";
|
---|
126 | layerInitializer.RightSideParameter.ActualName = "OpenLayers";
|
---|
127 | layerInitializer.Successor = correctAgeProcessor;
|
---|
128 |
|
---|
129 | correctAgeProcessor.Operator = correctAgeDecrementor;
|
---|
130 | correctAgeProcessor.Successor = incrEvaluatedSolutionsForNewLayer;
|
---|
131 |
|
---|
132 | correctAgeDecrementor.Increment = new IntValue(-1);
|
---|
133 | correctAgeDecrementor.ValueParameter.ActualName = "Age";
|
---|
134 |
|
---|
135 | incrEvaluatedSolutionsForNewLayer.ValueParameter.ActualName = "EvaluatedSolutions";
|
---|
136 | incrEvaluatedSolutionsForNewLayer.AccumulateParameter.Value = new BoolValue(true);
|
---|
137 | //incrEvaluatedSolutionsForNewLayer.Successor = newResultCollectionAssigner;
|
---|
138 | incrEvaluatedSolutionsForNewLayer.Successor = mainOperator;
|
---|
139 |
|
---|
140 | newResultCollectionAssigner.LeftSideParameter.ActualName = "LayerResults";
|
---|
141 | newResultCollectionAssigner.RightSideParameter.Value = new ResultCollection();
|
---|
142 | newResultCollectionAssigner.Successor = mainOperator;
|
---|
143 | // TODO mainOperator increment age
|
---|
144 |
|
---|
145 | incrOpenLayers.ValueParameter.ActualName = "OpenLayers";
|
---|
146 | incrOpenLayers.Increment = new IntValue(1);
|
---|
147 | incrOpenLayers.Successor = newLayerResultsCollector;
|
---|
148 |
|
---|
149 | newLayerResultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("LayerResults", "Result set for each layer", "LayerResults"));
|
---|
150 | newLayerResultsCollector.CopyValue = new BoolValue(false);
|
---|
151 | newLayerResultsCollector.Successor = null;
|
---|
152 |
|
---|
153 | regenerateLayerZeroSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
|
---|
154 | regenerateLayerZeroSelector.CopySelected = new BoolValue(false);
|
---|
155 | regenerateLayerZeroSelector.Successor = regenerateLayerZeroProsessor;
|
---|
156 |
|
---|
157 | regenerateLayerZeroProsessor.Operators.Add(new EmptyOperator());
|
---|
158 | regenerateLayerZeroProsessor.Operators.Add(regenerateLayerZeroLayerProsessor);
|
---|
159 | regenerateLayerZeroProsessor.Successor = regenerateLayerZeroReducer;
|
---|
160 |
|
---|
161 | regenerateLayerZeroLayerProsessor.Operator = subScopesRemover;
|
---|
162 |
|
---|
163 | regenerateLayerZeroReducer.Successor = layerSorter;
|
---|
164 |
|
---|
165 | layerSorter.Successor = resetGenSlr;
|
---|
166 |
|
---|
167 | subScopesRemover.Successor = solutionsCreator;
|
---|
168 |
|
---|
169 | solutionsCreator.Successor = initializeAgeProsessor;
|
---|
170 |
|
---|
171 | initializeAgeProsessor.Operator = initializeAge;
|
---|
172 | initializeAgeProsessor.Successor = incrEvaluatedSolutionsAfterReseeding;
|
---|
173 |
|
---|
174 | initializeAge.CollectedValues.Add(new ValueParameter<IntValue>("Age", new IntValue(0)));
|
---|
175 | initializeAge.Successor = null;
|
---|
176 |
|
---|
177 | incrEvaluatedSolutionsAfterReseeding.ValueParameter.ActualName = "EvaluatedSolutions";
|
---|
178 | incrEvaluatedSolutionsAfterReseeding.AccumulateParameter.Value = new BoolValue(true);
|
---|
179 | incrEvaluatedSolutionsAfterReseeding.Successor = null;
|
---|
180 |
|
---|
181 | resetGenSlr.LeftSideParameter.ActualName = "GenerationsSinceLastRefresh";
|
---|
182 | resetGenSlr.RightSideParameter.Value = new IntValue(0);
|
---|
183 | resetGenSlr.Successor = null;
|
---|
184 | }
|
---|
185 | }
|
---|
186 | } |
---|