1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Operators;
|
---|
26 | using HeuristicLab.Optimization.Operators;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Selection;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Algorithms.GeneticAlgorithm {
|
---|
32 | /// <summary>
|
---|
33 | /// An operator which represents the main loop of a genetic algorithm.
|
---|
34 | /// </summary>
|
---|
35 | [Item("GeneticAlgorithmMainLoop", "An operator which represents the main loop of a genetic algorithm.")]
|
---|
36 | [StorableClass]
|
---|
37 | public sealed class GeneticAlgorithmMainLoop : AlgorithmOperator {
|
---|
38 | #region Parameter properties
|
---|
39 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
40 | get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
41 | }
|
---|
42 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
43 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
44 | }
|
---|
45 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
46 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
47 | }
|
---|
48 | public ValueLookupParameter<IOperator> SelectorParameter {
|
---|
49 | get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
|
---|
50 | }
|
---|
51 | public ValueLookupParameter<IOperator> CrossoverParameter {
|
---|
52 | get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
|
---|
53 | }
|
---|
54 | public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
|
---|
55 | get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
56 | }
|
---|
57 | public ValueLookupParameter<IOperator> MutatorParameter {
|
---|
58 | get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
|
---|
59 | }
|
---|
60 | public ValueLookupParameter<IOperator> EvaluatorParameter {
|
---|
61 | get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
|
---|
62 | }
|
---|
63 | public ValueLookupParameter<IntValue> ElitesParameter {
|
---|
64 | get { return (ValueLookupParameter<IntValue>)Parameters["Elites"]; }
|
---|
65 | }
|
---|
66 | public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
67 | get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
68 | }
|
---|
69 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
70 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
71 | }
|
---|
72 | public ValueLookupParameter<IOperator> AnalyzerParameter {
|
---|
73 | get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
74 | }
|
---|
75 | public ValueLookupParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
76 | get { return (ValueLookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
|
---|
77 | }
|
---|
78 | public ValueLookupParameter<IntValue> PopulationSizeParameter {
|
---|
79 | get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
80 | }
|
---|
81 | private ScopeParameter CurrentScopeParameter {
|
---|
82 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
83 | }
|
---|
84 |
|
---|
85 | public IScope CurrentScope {
|
---|
86 | get { return CurrentScopeParameter.ActualValue; }
|
---|
87 | }
|
---|
88 | #endregion
|
---|
89 |
|
---|
90 | [StorableConstructor]
|
---|
91 | private GeneticAlgorithmMainLoop(bool deserializing) : base(deserializing) { }
|
---|
92 | private GeneticAlgorithmMainLoop(GeneticAlgorithmMainLoop original, Cloner cloner)
|
---|
93 | : base(original, cloner) {
|
---|
94 | }
|
---|
95 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
96 | return new GeneticAlgorithmMainLoop(this, cloner);
|
---|
97 | }
|
---|
98 | public GeneticAlgorithmMainLoop()
|
---|
99 | : base() {
|
---|
100 | Initialize();
|
---|
101 | }
|
---|
102 |
|
---|
103 | private void Initialize() {
|
---|
104 | #region Create parameters
|
---|
105 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
106 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
107 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
108 | Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
|
---|
109 | Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
|
---|
110 | Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
|
---|
111 | Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
|
---|
112 | Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions. This operator is executed in parallel, if an engine is used which supports parallelization."));
|
---|
113 | Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
|
---|
114 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
|
---|
115 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
116 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
|
---|
117 | Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
|
---|
118 | Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population."));
|
---|
119 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied."));
|
---|
120 | #endregion
|
---|
121 |
|
---|
122 | #region Create operators
|
---|
123 | VariableCreator variableCreator = new VariableCreator();
|
---|
124 | ResultsCollector resultsCollector1 = new ResultsCollector();
|
---|
125 | Placeholder analyzer1 = new Placeholder();
|
---|
126 | Placeholder selector = new Placeholder();
|
---|
127 | SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
|
---|
128 | ChildrenCreator childrenCreator = new ChildrenCreator();
|
---|
129 | UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
|
---|
130 | Placeholder crossover = new Placeholder();
|
---|
131 | StochasticBranch stochasticBranch = new StochasticBranch();
|
---|
132 | Placeholder mutator = new Placeholder();
|
---|
133 | SubScopesRemover subScopesRemover = new SubScopesRemover();
|
---|
134 | UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
|
---|
135 | Placeholder evaluator = new Placeholder();
|
---|
136 | SubScopesCounter subScopesCounter = new SubScopesCounter();
|
---|
137 | SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
|
---|
138 | BestSelector bestSelector = new BestSelector();
|
---|
139 | RightReducer rightReducer = new RightReducer();
|
---|
140 | MergingReducer mergingReducer = new MergingReducer();
|
---|
141 | IntCounter intCounter = new IntCounter();
|
---|
142 | Comparator comparator = new Comparator();
|
---|
143 | ResultsCollector resultsCollector2 = new ResultsCollector();
|
---|
144 | Placeholder analyzer2 = new Placeholder();
|
---|
145 | ConditionalBranch conditionalBranch = new ConditionalBranch();
|
---|
146 |
|
---|
147 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class GeneticAlgorithm expects this to be called Generations
|
---|
148 |
|
---|
149 | resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
150 | resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>(EvaluatedSolutionsParameter.Name));
|
---|
151 | resultsCollector1.ResultsParameter.ActualName = "Results";
|
---|
152 |
|
---|
153 | analyzer1.Name = "Analyzer";
|
---|
154 | analyzer1.OperatorParameter.ActualName = "Analyzer";
|
---|
155 |
|
---|
156 | selector.Name = "Selector";
|
---|
157 | selector.OperatorParameter.ActualName = "Selector";
|
---|
158 |
|
---|
159 | childrenCreator.ParentsPerChild = new IntValue(2);
|
---|
160 |
|
---|
161 | crossover.Name = "Crossover";
|
---|
162 | crossover.OperatorParameter.ActualName = "Crossover";
|
---|
163 |
|
---|
164 | stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
|
---|
165 | stochasticBranch.RandomParameter.ActualName = "Random";
|
---|
166 |
|
---|
167 | mutator.Name = "Mutator";
|
---|
168 | mutator.OperatorParameter.ActualName = "Mutator";
|
---|
169 |
|
---|
170 | subScopesRemover.RemoveAllSubScopes = true;
|
---|
171 |
|
---|
172 | uniformSubScopesProcessor2.Parallel.Value = true;
|
---|
173 |
|
---|
174 | evaluator.Name = "Evaluator";
|
---|
175 | evaluator.OperatorParameter.ActualName = "Evaluator";
|
---|
176 |
|
---|
177 | subScopesCounter.Name = "Increment EvaluatedSolutions";
|
---|
178 | subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
|
---|
179 |
|
---|
180 | bestSelector.CopySelected = new BoolValue(false);
|
---|
181 | bestSelector.MaximizationParameter.ActualName = "Maximization";
|
---|
182 | bestSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
|
---|
183 | bestSelector.QualityParameter.ActualName = "Quality";
|
---|
184 |
|
---|
185 | intCounter.Increment = new IntValue(1);
|
---|
186 | intCounter.ValueParameter.ActualName = "Generations";
|
---|
187 |
|
---|
188 | comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
189 | comparator.LeftSideParameter.ActualName = "Generations";
|
---|
190 | comparator.ResultParameter.ActualName = "Terminate";
|
---|
191 | comparator.RightSideParameter.ActualName = "MaximumGenerations";
|
---|
192 |
|
---|
193 | resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
194 | resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>(EvaluatedSolutionsParameter.Name));
|
---|
195 | resultsCollector2.ResultsParameter.ActualName = "Results";
|
---|
196 |
|
---|
197 | analyzer2.Name = "Analyzer";
|
---|
198 | analyzer2.OperatorParameter.ActualName = "Analyzer";
|
---|
199 |
|
---|
200 | conditionalBranch.ConditionParameter.ActualName = "Terminate";
|
---|
201 | #endregion
|
---|
202 |
|
---|
203 | #region Create operator graph
|
---|
204 | OperatorGraph.InitialOperator = variableCreator;
|
---|
205 | variableCreator.Successor = resultsCollector1;
|
---|
206 | resultsCollector1.Successor = analyzer1;
|
---|
207 | analyzer1.Successor = selector;
|
---|
208 | selector.Successor = subScopesProcessor1;
|
---|
209 | subScopesProcessor1.Operators.Add(new EmptyOperator());
|
---|
210 | subScopesProcessor1.Operators.Add(childrenCreator);
|
---|
211 | subScopesProcessor1.Successor = subScopesProcessor2;
|
---|
212 | childrenCreator.Successor = uniformSubScopesProcessor1;
|
---|
213 | uniformSubScopesProcessor1.Operator = crossover;
|
---|
214 | uniformSubScopesProcessor1.Successor = uniformSubScopesProcessor2;
|
---|
215 | crossover.Successor = stochasticBranch;
|
---|
216 | stochasticBranch.FirstBranch = mutator;
|
---|
217 | stochasticBranch.SecondBranch = null;
|
---|
218 | stochasticBranch.Successor = subScopesRemover;
|
---|
219 | mutator.Successor = null;
|
---|
220 | subScopesRemover.Successor = null;
|
---|
221 | uniformSubScopesProcessor2.Operator = evaluator;
|
---|
222 | uniformSubScopesProcessor2.Successor = subScopesCounter;
|
---|
223 | evaluator.Successor = null;
|
---|
224 | subScopesCounter.Successor = null;
|
---|
225 | subScopesProcessor2.Operators.Add(bestSelector);
|
---|
226 | subScopesProcessor2.Operators.Add(new EmptyOperator());
|
---|
227 | subScopesProcessor2.Successor = mergingReducer;
|
---|
228 | bestSelector.Successor = rightReducer;
|
---|
229 | rightReducer.Successor = null;
|
---|
230 | mergingReducer.Successor = intCounter;
|
---|
231 | intCounter.Successor = comparator;
|
---|
232 | comparator.Successor = resultsCollector2;
|
---|
233 | resultsCollector2.Successor = analyzer2;
|
---|
234 | analyzer2.Successor = conditionalBranch;
|
---|
235 | conditionalBranch.FalseBranch = selector;
|
---|
236 | conditionalBranch.TrueBranch = null;
|
---|
237 | conditionalBranch.Successor = null;
|
---|
238 | #endregion
|
---|
239 | }
|
---|
240 |
|
---|
241 | public override IOperation Apply() {
|
---|
242 | if (CrossoverParameter.ActualValue == null)
|
---|
243 | return null;
|
---|
244 | return base.Apply();
|
---|
245 | }
|
---|
246 | }
|
---|
247 | }
|
---|