1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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.Algorithms.GeneticAlgorithm;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Optimization.Operators;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.Selection;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Algorithms.ALPS {
|
---|
35 |
|
---|
36 | [Item("AlpsGeneticAlgorithmMainLoop", "An ALPS genetic algorithm main loop operator.")]
|
---|
37 | [StorableClass]
|
---|
38 | public sealed class AlpsGeneticAlgorithmMainLoop : AlgorithmOperator {
|
---|
39 | #region Parameter Properties
|
---|
40 | public ILookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
41 | get { return (ILookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
42 | }
|
---|
43 | public ILookupParameter<IOperator> AnalyzerParameter {
|
---|
44 | get { return (ILookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
45 | }
|
---|
46 | public ILookupParameter<IOperator> LayerAnalyzerParameter {
|
---|
47 | get { return (ILookupParameter<IOperator>)Parameters["LayerAnalyzer"]; }
|
---|
48 | }
|
---|
49 | #endregion
|
---|
50 |
|
---|
51 | public GeneticAlgorithmMainLoop MainOperator {
|
---|
52 | get { return OperatorGraph.Iterate().OfType<GeneticAlgorithmMainLoop>().First(); }
|
---|
53 | }
|
---|
54 | public EldersEmigrator EldersEmigrator {
|
---|
55 | get { return OperatorGraph.Iterate().OfType<EldersEmigrator>().First(); }
|
---|
56 | }
|
---|
57 | public LayerUpdator LayerUpdator {
|
---|
58 | get { return OperatorGraph.Iterate().OfType<LayerUpdator>().First(); }
|
---|
59 | }
|
---|
60 |
|
---|
61 | [StorableConstructor]
|
---|
62 | private AlpsGeneticAlgorithmMainLoop(bool deserializing)
|
---|
63 | : base(deserializing) { }
|
---|
64 | private AlpsGeneticAlgorithmMainLoop(AlpsGeneticAlgorithmMainLoop original, Cloner cloner)
|
---|
65 | : base(original, cloner) { }
|
---|
66 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
67 | return new AlpsGeneticAlgorithmMainLoop(this, cloner);
|
---|
68 | }
|
---|
69 | public AlpsGeneticAlgorithmMainLoop()
|
---|
70 | : base() {
|
---|
71 | Parameters.Add(new LookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations that the algorithm should process."));
|
---|
72 | Parameters.Add(new LookupParameter<IOperator>("Analyzer", "The operator used to the analyze all individuals."));
|
---|
73 | Parameters.Add(new LookupParameter<IOperator>("LayerAnalyzer", "The operator used to analyze each layer."));
|
---|
74 |
|
---|
75 | var variableCreator = new VariableCreator() { Name = "Initialize" };
|
---|
76 | var resultsCollector = new ResultsCollector();
|
---|
77 | var initAnalyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
|
---|
78 | var initLayerAnalyzerProcessor = new SubScopesProcessor();
|
---|
79 | var layerVariableCreator = new VariableCreator() { Name = "Initialize Layer" };
|
---|
80 | var initLayerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
|
---|
81 | var matingPoolCreator = new MatingPoolCreator() { Name = "Create Mating Pools" };
|
---|
82 | var matingPoolProcessor = new UniformSubScopesProcessor();
|
---|
83 | var initializeLayer = new Assigner() { Name = "Reset LayerEvaluatedSolutions" };
|
---|
84 | var mainOperator = CreatePreparedGeneticAlgorithmMainLoop();
|
---|
85 | var layerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
|
---|
86 | var generationsIcrementor = new IntCounter() { Name = "Increment Generations" };
|
---|
87 | var evaluatedSolutionsReducer = new DataReducer() { Name = "Increment EvaluatedSolutions" };
|
---|
88 | var eldersEmigrator = new EldersEmigrator() { Name = "Emigrate Elders" };
|
---|
89 | var layerUpdator = new LayerUpdator(mainOperator) { Name = "Update Layers" };
|
---|
90 | var analyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
|
---|
91 | var generationsComparator = new Comparator() { Name = "Generations >= MaximumGenerations" };
|
---|
92 | var terminateBranch = new ConditionalBranch() { Name = "Terminate?" };
|
---|
93 |
|
---|
94 | OperatorGraph.InitialOperator = variableCreator;
|
---|
95 |
|
---|
96 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
|
---|
97 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("GenerationsSinceLastRefresh", new IntValue(0)));
|
---|
98 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("OpenLayers", new IntValue(1)));
|
---|
99 | variableCreator.Successor = resultsCollector;
|
---|
100 |
|
---|
101 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
102 | resultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("LayerResults", "Result set for each layer", "Results"));
|
---|
103 | resultsCollector.Successor = initAnalyzerPlaceholder;
|
---|
104 |
|
---|
105 | initAnalyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
106 | initAnalyzerPlaceholder.Successor = initLayerAnalyzerProcessor;
|
---|
107 |
|
---|
108 | initLayerAnalyzerProcessor.Operators.Add(layerVariableCreator);
|
---|
109 | initLayerAnalyzerProcessor.Successor = matingPoolCreator;
|
---|
110 |
|
---|
111 | layerVariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("LayerEvaluatedSolutions"));
|
---|
112 | layerVariableCreator.CollectedValues.Add(new ValueParameter<ResultCollection>("Results"));
|
---|
113 | layerVariableCreator.Successor = initLayerAnalyzerPlaceholder;
|
---|
114 |
|
---|
115 | initLayerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
|
---|
116 | initLayerAnalyzerPlaceholder.Successor = null;
|
---|
117 |
|
---|
118 | matingPoolCreator.Successor = matingPoolProcessor;
|
---|
119 |
|
---|
120 | matingPoolProcessor.Parallel.Value = true;
|
---|
121 | matingPoolProcessor.Operator = initializeLayer;
|
---|
122 | matingPoolProcessor.Successor = generationsIcrementor;
|
---|
123 |
|
---|
124 | initializeLayer.LeftSideParameter.ActualName = "LayerEvaluatedSolutions";
|
---|
125 | initializeLayer.RightSideParameter.Value = new IntValue(0);
|
---|
126 | initializeLayer.Successor = mainOperator;
|
---|
127 |
|
---|
128 | generationsIcrementor.ValueParameter.ActualName = "Generations";
|
---|
129 | generationsIcrementor.Increment = new IntValue(1);
|
---|
130 | generationsIcrementor.Successor = evaluatedSolutionsReducer;
|
---|
131 |
|
---|
132 | evaluatedSolutionsReducer.ParameterToReduce.ActualName = "EvaluatedSolutions";
|
---|
133 | evaluatedSolutionsReducer.TargetParameter.ActualName = "EvaluatedSolutions";
|
---|
134 | evaluatedSolutionsReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
|
---|
135 | evaluatedSolutionsReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Sum);
|
---|
136 | evaluatedSolutionsReducer.Successor = eldersEmigrator;
|
---|
137 |
|
---|
138 | mainOperator.Successor = layerAnalyzerPlaceholder;
|
---|
139 |
|
---|
140 | layerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
|
---|
141 | layerAnalyzerPlaceholder.Successor = null;
|
---|
142 |
|
---|
143 | eldersEmigrator.Successor = layerUpdator;
|
---|
144 |
|
---|
145 | layerUpdator.Successor = analyzerPlaceholder;
|
---|
146 |
|
---|
147 | analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
148 | analyzerPlaceholder.Successor = generationsComparator;
|
---|
149 |
|
---|
150 | generationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
151 | generationsComparator.LeftSideParameter.ActualName = "Generations";
|
---|
152 | generationsComparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
153 | generationsComparator.ResultParameter.ActualName = "TerminateGenerations";
|
---|
154 | generationsComparator.Successor = terminateBranch;
|
---|
155 |
|
---|
156 | terminateBranch.ConditionParameter.ActualName = "TerminateGenerations";
|
---|
157 | terminateBranch.FalseBranch = matingPoolCreator;
|
---|
158 | }
|
---|
159 |
|
---|
160 | private GeneticAlgorithmMainLoop CreatePreparedGeneticAlgorithmMainLoop() {
|
---|
161 | var mainLoop = new GeneticAlgorithmMainLoop();
|
---|
162 | var selector = mainLoop.OperatorGraph.Iterate().OfType<Placeholder>().First(o => o.OperatorParameter.ActualName == "Selector");
|
---|
163 | var crossover = mainLoop.OperatorGraph.Iterate().OfType<Placeholder>().First(o => o.OperatorParameter.ActualName == "Crossover");
|
---|
164 | var subScopesCounter = mainLoop.OperatorGraph.Iterate().OfType<SubScopesCounter>().First();
|
---|
165 | var elitesMerger = mainLoop.OperatorGraph.Iterate().OfType<MergingReducer>().First();
|
---|
166 |
|
---|
167 | // Operator starts with selector
|
---|
168 | mainLoop.OperatorGraph.InitialOperator = selector;
|
---|
169 |
|
---|
170 | // Insert AgeCalculator between crossover and its successor
|
---|
171 | var crossoverSuccessor = crossover.Successor;
|
---|
172 | var ageCalculator = new DataReducer() { Name = "Calculate Age" };
|
---|
173 | ageCalculator.ParameterToReduce.ActualName = "Age";
|
---|
174 | ageCalculator.TargetParameter.ActualName = "Age";
|
---|
175 | ageCalculator.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Max);
|
---|
176 | ageCalculator.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign);
|
---|
177 | crossover.Successor = ageCalculator;
|
---|
178 | ageCalculator.Successor = crossoverSuccessor;
|
---|
179 |
|
---|
180 | // When counting the evaluated solutions, write in LayerEvaluatedSolutions
|
---|
181 | subScopesCounter.ValueParameter.ActualName = "LayerEvaluatedSolutions";
|
---|
182 | subScopesCounter.AccumulateParameter.Value = new BoolValue(false);
|
---|
183 |
|
---|
184 | // Instead of generational loop after merging of elites, increment ages of all individuals
|
---|
185 | var processor = new UniformSubScopesProcessor();
|
---|
186 | var incrementor = new IntCounter() { Name = "Increment Age" };
|
---|
187 | processor.Operator = incrementor;
|
---|
188 | processor.Successor = null;
|
---|
189 | incrementor.ValueParameter.ActualName = "Age";
|
---|
190 | incrementor.Increment = new IntValue(1);
|
---|
191 | incrementor.Successor = null;
|
---|
192 | elitesMerger.Successor = processor;
|
---|
193 |
|
---|
194 | // Parameterize
|
---|
195 | foreach (var stochasticOperator in mainLoop.OperatorGraph.Iterate().OfType<IStochasticOperator>())
|
---|
196 | stochasticOperator.RandomParameter.ActualName = "LocalRandom";
|
---|
197 | foreach (var stochasticBranch in mainLoop.OperatorGraph.Iterate().OfType<StochasticBranch>())
|
---|
198 | stochasticBranch.RandomParameter.ActualName = "LocalRandom";
|
---|
199 |
|
---|
200 | return mainLoop;
|
---|
201 | }
|
---|
202 | }
|
---|
203 | } |
---|