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<IRandom> RandomParameter {
|
---|
41 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
42 | }
|
---|
43 | /*public ILookupParameter<BoolValue> MaximizationParameter {
|
---|
44 | get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
45 | }
|
---|
46 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
47 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
48 | }
|
---|
49 | public ILookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
50 | get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
51 | }
|
---|
52 | public ILookupParameter<IOperator> EvaluatorParameter {
|
---|
53 | get { return (ILookupParameter<IOperator>)Parameters["Evaluator"]; }
|
---|
54 | }*/
|
---|
55 | public ILookupParameter<IntValue> PopulationSizeParameter {
|
---|
56 | get { return (ILookupParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
57 | }
|
---|
58 | public ILookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
59 | get { return (ILookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
60 | }
|
---|
61 | public ILookupParameter<IOperator> SelectorParameter {
|
---|
62 | get { return (ILookupParameter<IOperator>)Parameters["Selector"]; }
|
---|
63 | }
|
---|
64 | public ILookupParameter<IOperator> CrossoverParameter {
|
---|
65 | get { return (ILookupParameter<IOperator>)Parameters["Crossover"]; }
|
---|
66 | }
|
---|
67 | public ILookupParameter<PercentValue> MutationProbabilityParameter {
|
---|
68 | get { return (ILookupParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
69 | }
|
---|
70 | public ILookupParameter<IOperator> MutatorParameter {
|
---|
71 | get { return (ILookupParameter<IOperator>)Parameters["Mutator"]; }
|
---|
72 | }
|
---|
73 | public ILookupParameter<IntValue> ElitesParameter {
|
---|
74 | get { return (ILookupParameter<IntValue>)Parameters["Elites"]; }
|
---|
75 | }
|
---|
76 | public IValueLookupParameter<BoolValue> ReevaluateElitesParameter {
|
---|
77 | get { return (IValueLookupParameter<BoolValue>)Parameters["ReevaluateElites"]; }
|
---|
78 | }
|
---|
79 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
80 | get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
81 | }
|
---|
82 | public ILookupParameter<IOperator> AnalyzerParameter {
|
---|
83 | get { return (ILookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
84 | }
|
---|
85 | public ILookupParameter<IOperator> LayerAnalyzerParameter {
|
---|
86 | get { return (ILookupParameter<IOperator>)Parameters["LayerAnalyzer"]; }
|
---|
87 | }
|
---|
88 | #endregion
|
---|
89 |
|
---|
90 | public GeneticAlgorithmMainLoop MainOperator {
|
---|
91 | get { return OperatorGraph.Iterate().OfType<GeneticAlgorithmMainLoop>().First(); }
|
---|
92 | }
|
---|
93 |
|
---|
94 | [StorableConstructor]
|
---|
95 | private AlpsGeneticAlgorithmMainLoop(bool deserializing)
|
---|
96 | : base(deserializing) { }
|
---|
97 | private AlpsGeneticAlgorithmMainLoop(AlpsGeneticAlgorithmMainLoop original, Cloner cloner)
|
---|
98 | : base(original, cloner) { }
|
---|
99 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
100 | return new AlpsGeneticAlgorithmMainLoop(this, cloner);
|
---|
101 | }
|
---|
102 | public AlpsGeneticAlgorithmMainLoop()
|
---|
103 | : base() {
|
---|
104 | Parameters.Add(new LookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
105 | /*Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
106 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
107 | Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
|
---|
108 | Parameters.Add(new LookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));*/
|
---|
109 | Parameters.Add(new LookupParameter<IntValue>("PopulationSize", "The size of the population of solutions."));
|
---|
110 | Parameters.Add(new LookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations that the algorithm should process."));
|
---|
111 | Parameters.Add(new LookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
|
---|
112 | Parameters.Add(new LookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
|
---|
113 | Parameters.Add(new LookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
|
---|
114 | Parameters.Add(new LookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
|
---|
115 | Parameters.Add(new LookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
|
---|
116 | Parameters.Add(new LookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
|
---|
117 | Parameters.Add(new LookupParameter<ResultCollection>("Results", "The results collection to store the results."));
|
---|
118 | Parameters.Add(new LookupParameter<IOperator>("Analyzer", "The operator used to the analyze all individuals."));
|
---|
119 | Parameters.Add(new LookupParameter<IOperator>("LayerAnalyzer", "The operator used to analyze each layer."));
|
---|
120 |
|
---|
121 | var variableCreator = new VariableCreator() { Name = "Initialize" };
|
---|
122 | var resultsCollector = new ResultsCollector();
|
---|
123 | var initAnalyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
|
---|
124 | var initLayerAnalyzerProcessor = new SubScopesProcessor();
|
---|
125 | var initLayerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
|
---|
126 | var matingPoolCreator = new MatingPoolCreator() { Name = "Create Mating Pools" };
|
---|
127 | var matingPoolProcessor = new UniformSubScopesProcessor();
|
---|
128 | var mainOperator = PrepareGeneticAlgorithmMainLoop();
|
---|
129 | var layerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
|
---|
130 | var generationsIcrementor = new IntCounter() { Name = "Increment Generations" };
|
---|
131 | var evaluatedSolutionsReducer = new DataReducer() { Name = "Increment EvaluatedSolutions" };
|
---|
132 | var eldersEmigrator = new EldersEmigrator() { Name = "Emigrate Elders" };
|
---|
133 | var layerUpdator = new CombinedOperator() { Name = "Update Layers" };
|
---|
134 | var analyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
|
---|
135 | var generationsComparator = new Comparator() { Name = "Generations >= MaximumGenerations" };
|
---|
136 | var terminateBranch = new ConditionalBranch() { Name = "Terminate?" };
|
---|
137 |
|
---|
138 | OperatorGraph.InitialOperator = variableCreator;
|
---|
139 |
|
---|
140 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
|
---|
141 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("GenerationsSinceLastRefresh", new IntValue(0)));
|
---|
142 | variableCreator.Successor = resultsCollector;
|
---|
143 |
|
---|
144 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
145 | resultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("LayerResults", "Result set for each layer", "Results"));
|
---|
146 | resultsCollector.Successor = initAnalyzerPlaceholder;
|
---|
147 |
|
---|
148 | initAnalyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
149 | initAnalyzerPlaceholder.Successor = initLayerAnalyzerProcessor;
|
---|
150 |
|
---|
151 | initLayerAnalyzerProcessor.Operators.Add(initLayerAnalyzerPlaceholder);
|
---|
152 | initLayerAnalyzerProcessor.Successor = matingPoolCreator;
|
---|
153 |
|
---|
154 | initLayerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
|
---|
155 | initLayerAnalyzerPlaceholder.Successor = null;
|
---|
156 |
|
---|
157 | matingPoolCreator.Successor = matingPoolProcessor;
|
---|
158 |
|
---|
159 | matingPoolProcessor.Parallel.Value = true;
|
---|
160 | matingPoolProcessor.Operator = mainOperator;
|
---|
161 | matingPoolProcessor.Successor = generationsIcrementor;
|
---|
162 |
|
---|
163 | generationsIcrementor.ValueParameter.ActualName = "Generations";
|
---|
164 | generationsIcrementor.Increment = new IntValue(1);
|
---|
165 | generationsIcrementor.Successor = evaluatedSolutionsReducer;
|
---|
166 |
|
---|
167 | evaluatedSolutionsReducer.ParameterToReduce.ActualName = "EvaluatedSolutions";
|
---|
168 | evaluatedSolutionsReducer.TargetParameter.ActualName = "EvaluatedSolutions";
|
---|
169 | evaluatedSolutionsReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
|
---|
170 | evaluatedSolutionsReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Sum);
|
---|
171 | evaluatedSolutionsReducer.Successor = eldersEmigrator;
|
---|
172 |
|
---|
173 | mainOperator.Successor = layerAnalyzerPlaceholder;
|
---|
174 |
|
---|
175 | layerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
|
---|
176 | layerAnalyzerPlaceholder.Successor = null;
|
---|
177 |
|
---|
178 | eldersEmigrator.Successor = layerUpdator;
|
---|
179 |
|
---|
180 | layerUpdator.Successor = analyzerPlaceholder;
|
---|
181 |
|
---|
182 | analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
183 | analyzerPlaceholder.Successor = generationsComparator;
|
---|
184 |
|
---|
185 | generationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
186 | generationsComparator.LeftSideParameter.ActualName = "Generations";
|
---|
187 | generationsComparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
188 | generationsComparator.ResultParameter.ActualName = "TerminateGenerations";
|
---|
189 | generationsComparator.Successor = terminateBranch;
|
---|
190 |
|
---|
191 | terminateBranch.ConditionParameter.ActualName = "TerminateGenerations";
|
---|
192 | terminateBranch.FalseBranch = matingPoolCreator;
|
---|
193 | }
|
---|
194 |
|
---|
195 | private GeneticAlgorithmMainLoop PrepareGeneticAlgorithmMainLoop() {
|
---|
196 | var mainLoop = new GeneticAlgorithmMainLoop();
|
---|
197 | var selector = mainLoop.OperatorGraph.Iterate().OfType<Placeholder>().First(o => o.OperatorParameter.ActualName == "Selector");
|
---|
198 | var crossover = mainLoop.OperatorGraph.Iterate().OfType<Placeholder>().First(o => o.OperatorParameter.ActualName == "Crossover");
|
---|
199 | var elitesMerger = mainLoop.OperatorGraph.Iterate().OfType<MergingReducer>().First();
|
---|
200 |
|
---|
201 | // Operator starts with selector
|
---|
202 | mainLoop.OperatorGraph.InitialOperator = selector;
|
---|
203 |
|
---|
204 | // Insert AgeCalculator between crossover and its successor
|
---|
205 | var crossoverSuccessor = crossover.Successor;
|
---|
206 | var ageCalculator = new DataReducer() { Name = "Calculate Age" };
|
---|
207 | ageCalculator.ParameterToReduce.ActualName = "Age";
|
---|
208 | ageCalculator.TargetParameter.ActualName = "Age";
|
---|
209 | ageCalculator.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Max);
|
---|
210 | ageCalculator.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign);
|
---|
211 | crossover.Successor = ageCalculator;
|
---|
212 | ageCalculator.Successor = crossoverSuccessor;
|
---|
213 |
|
---|
214 | // Instead of generational loop after merging of elites, increment ages of all individuals
|
---|
215 | var processor = new UniformSubScopesProcessor();
|
---|
216 | var incrementor = new IntCounter() { Name = "Increment Age" };
|
---|
217 | processor.Operator = incrementor;
|
---|
218 | processor.Successor = null;
|
---|
219 | incrementor.ValueParameter.ActualName = "Age";
|
---|
220 | incrementor.Increment = new IntValue(1);
|
---|
221 | incrementor.Successor = null;
|
---|
222 | elitesMerger.Successor = processor;
|
---|
223 |
|
---|
224 | // Parameterize
|
---|
225 | foreach (var stochasticOperator in mainLoop.OperatorGraph.Iterate().OfType<IStochasticOperator>())
|
---|
226 | stochasticOperator.RandomParameter.ActualName = "LocalRandom";
|
---|
227 | foreach (var stochasticBranch in mainLoop.OperatorGraph.Iterate().OfType<StochasticBranch>())
|
---|
228 | stochasticBranch.RandomParameter.ActualName = "LocalRandom";
|
---|
229 |
|
---|
230 | // Remove unnessesary subtrees
|
---|
231 | //foreach (var @operator in mainLoop.OperatorGraph.Operators.OfType<SingleSuccessorOperator>().Where(o => o.Successor == selector))
|
---|
232 | // @operator.Successor = null;
|
---|
233 |
|
---|
234 | return mainLoop;
|
---|
235 | }
|
---|
236 | }
|
---|
237 | } |
---|