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.Core;
|
---|
23 | using HeuristicLab.Data;
|
---|
24 | using HeuristicLab.Operators;
|
---|
25 | using HeuristicLab.Optimization.Operators;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm {
|
---|
30 | /// <summary>
|
---|
31 | /// An operator which represents the main loop of an offspring selection genetic algorithm.
|
---|
32 | /// </summary>
|
---|
33 | [Item("OffspringSelectionGeneticAlgorithmMainLoop", "An operator which represents the main loop of an offspring selection genetic algorithm.")]
|
---|
34 | [StorableClass]
|
---|
35 | public sealed class OffspringSelectionGeneticAlgorithmMainLoop : AlgorithmOperator {
|
---|
36 | #region Parameter properties
|
---|
37 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
38 | get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
39 | }
|
---|
40 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
41 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
42 | }
|
---|
43 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
44 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
45 | }
|
---|
46 | public ValueLookupParameter<IOperator> SelectorParameter {
|
---|
47 | get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
|
---|
48 | }
|
---|
49 | public ValueLookupParameter<IOperator> CrossoverParameter {
|
---|
50 | get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
|
---|
51 | }
|
---|
52 | public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
|
---|
53 | get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
54 | }
|
---|
55 | public ValueLookupParameter<IOperator> MutatorParameter {
|
---|
56 | get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
|
---|
57 | }
|
---|
58 | public ValueLookupParameter<IOperator> EvaluatorParameter {
|
---|
59 | get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
|
---|
60 | }
|
---|
61 | public ValueLookupParameter<IntValue> ElitesParameter {
|
---|
62 | get { return (ValueLookupParameter<IntValue>)Parameters["Elites"]; }
|
---|
63 | }
|
---|
64 | public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
65 | get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
66 | }
|
---|
67 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
68 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
69 | }
|
---|
70 | public ValueLookupParameter<IOperator> AnalyzerParameter {
|
---|
71 | get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
72 | }
|
---|
73 | public ValueLookupParameter<DoubleValue> SuccessRatioParameter {
|
---|
74 | get { return (ValueLookupParameter<DoubleValue>)Parameters["SuccessRatio"]; }
|
---|
75 | }
|
---|
76 | public LookupParameter<DoubleValue> ComparisonFactorParameter {
|
---|
77 | get { return (LookupParameter<DoubleValue>)Parameters["ComparisonFactor"]; }
|
---|
78 | }
|
---|
79 | public ValueLookupParameter<DoubleValue> ComparisonFactorStartParameter {
|
---|
80 | get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorStart"]; }
|
---|
81 | }
|
---|
82 | public ValueLookupParameter<IOperator> ComparisonFactorModifierParameter {
|
---|
83 | get { return (ValueLookupParameter<IOperator>)Parameters["ComparisonFactorModifier"]; }
|
---|
84 | }
|
---|
85 | public ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
|
---|
86 | get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
|
---|
87 | }
|
---|
88 | public ValueLookupParameter<BoolValue> OffspringSelectionBeforeMutationParameter {
|
---|
89 | get { return (ValueLookupParameter<BoolValue>)Parameters["OffspringSelectionBeforeMutation"]; }
|
---|
90 | }
|
---|
91 | #endregion
|
---|
92 |
|
---|
93 | [StorableConstructor]
|
---|
94 | private OffspringSelectionGeneticAlgorithmMainLoop(bool deserializing) : base() { }
|
---|
95 | public OffspringSelectionGeneticAlgorithmMainLoop()
|
---|
96 | : base() {
|
---|
97 | Initialize();
|
---|
98 | }
|
---|
99 |
|
---|
100 | private void Initialize() {
|
---|
101 | #region Create parameters
|
---|
102 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
103 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
104 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
105 | Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
|
---|
106 | Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
|
---|
107 | Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
|
---|
108 | Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
|
---|
109 | Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
|
---|
110 | Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
|
---|
111 | Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
|
---|
112 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
|
---|
113 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
114 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
|
---|
115 | Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved."));
|
---|
116 | Parameters.Add(new LookupParameter<DoubleValue>("ComparisonFactor", "The comparison factor is used to determine whether the offspring should be compared to the better parent, the worse parent or a quality value linearly interpolated between them. It is in the range [0;1]."));
|
---|
117 | Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorStart", "The initial value for the comparison factor."));
|
---|
118 | Parameters.Add(new ValueLookupParameter<IOperator>("ComparisonFactorModifier", "The operator used to modify the comparison factor."));
|
---|
119 | Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm."));
|
---|
120 | Parameters.Add(new ValueLookupParameter<BoolValue>("OffspringSelectionBeforeMutation", "True if the offspring selection step should be applied before mutation, false if it should be applied after mutation."));
|
---|
121 | #endregion
|
---|
122 |
|
---|
123 | #region Create operators
|
---|
124 | VariableCreator variableCreator = new VariableCreator();
|
---|
125 | Assigner comparisonFactorInitializer = new Assigner();
|
---|
126 | Placeholder analyzer1 = new Placeholder();
|
---|
127 | ResultsCollector resultsCollector1 = new ResultsCollector();
|
---|
128 | ResultsCollector resultsCollector2 = new ResultsCollector();
|
---|
129 | OffspringSelectionGeneticAlgorithmMainOperator mainOperator = new OffspringSelectionGeneticAlgorithmMainOperator();
|
---|
130 | IntCounter generationsCounter = new IntCounter();
|
---|
131 | Comparator maxGenerationsComparator = new Comparator();
|
---|
132 | Comparator maxSelectionPressureComparator = new Comparator();
|
---|
133 | Comparator maxEvaluatedSolutionsComparator = new Comparator();
|
---|
134 | Placeholder comparisonFactorModifier = new Placeholder();
|
---|
135 | Placeholder analyzer2 = new Placeholder();
|
---|
136 | ResultsCollector resultsCollector3 = new ResultsCollector();
|
---|
137 | ConditionalBranch conditionalBranch1 = new ConditionalBranch();
|
---|
138 | ConditionalBranch conditionalBranch2 = new ConditionalBranch();
|
---|
139 | ConditionalBranch conditionalBranch3 = new ConditionalBranch();
|
---|
140 |
|
---|
141 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class OffspringSelectionGeneticAlgorithm expects this to be called Generations
|
---|
142 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
|
---|
143 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("SelectionPressure", new DoubleValue(0)));
|
---|
144 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("CurrentSuccessRatio", new DoubleValue(0)));
|
---|
145 |
|
---|
146 | comparisonFactorInitializer.Name = "Initialize ComparisonFactor (placeholder)";
|
---|
147 | comparisonFactorInitializer.LeftSideParameter.ActualName = ComparisonFactorParameter.Name;
|
---|
148 | comparisonFactorInitializer.RightSideParameter.ActualName = ComparisonFactorStartParameter.Name;
|
---|
149 |
|
---|
150 | analyzer1.Name = "Analyzer (placeholder)";
|
---|
151 | analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
152 |
|
---|
153 | resultsCollector1.CopyValue = new BoolValue(false);
|
---|
154 | resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
155 | resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Curent Comparison Factor", null, ComparisonFactorParameter.Name));
|
---|
156 | resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Selection Pressure", null, "SelectionPressure"));
|
---|
157 | resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Success Ratio", null, "CurrentSuccessRatio"));
|
---|
158 | resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
159 |
|
---|
160 | resultsCollector2.CopyValue = new BoolValue(true);
|
---|
161 | resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
|
---|
162 | resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
163 |
|
---|
164 | mainOperator.ComparisonFactorParameter.ActualName = ComparisonFactorParameter.Name;
|
---|
165 | mainOperator.CrossoverParameter.ActualName = CrossoverParameter.Name;
|
---|
166 | mainOperator.CurrentSuccessRatioParameter.ActualName = "CurrentSuccessRatio";
|
---|
167 | mainOperator.ElitesParameter.ActualName = ElitesParameter.Name;
|
---|
168 | mainOperator.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
|
---|
169 | mainOperator.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
|
---|
170 | mainOperator.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
171 | mainOperator.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
|
---|
172 | mainOperator.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
|
---|
173 | mainOperator.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
174 | mainOperator.OffspringSelectionBeforeMutationParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
|
---|
175 | mainOperator.QualityParameter.ActualName = QualityParameter.Name;
|
---|
176 | mainOperator.RandomParameter.ActualName = RandomParameter.Name;
|
---|
177 | mainOperator.SelectionPressureParameter.ActualName = "SelectionPressure";
|
---|
178 | mainOperator.SelectorParameter.ActualName = SelectorParameter.Name;
|
---|
179 | mainOperator.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
|
---|
180 |
|
---|
181 | generationsCounter.Increment = new IntValue(1);
|
---|
182 | generationsCounter.ValueParameter.ActualName = "Generations";
|
---|
183 |
|
---|
184 | maxGenerationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
185 | maxGenerationsComparator.LeftSideParameter.ActualName = "Generations";
|
---|
186 | maxGenerationsComparator.ResultParameter.ActualName = "TerminateMaximumGenerations";
|
---|
187 | maxGenerationsComparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
188 |
|
---|
189 | maxSelectionPressureComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
190 | maxSelectionPressureComparator.LeftSideParameter.ActualName = "SelectionPressure";
|
---|
191 | maxSelectionPressureComparator.ResultParameter.ActualName = "TerminateSelectionPressure";
|
---|
192 | maxSelectionPressureComparator.RightSideParameter.ActualName = MaximumSelectionPressureParameter.Name;
|
---|
193 |
|
---|
194 | maxEvaluatedSolutionsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
195 | maxEvaluatedSolutionsComparator.LeftSideParameter.ActualName = "EvaluatedSolutions";
|
---|
196 | maxEvaluatedSolutionsComparator.ResultParameter.ActualName = "TerminateEvaluatedSolutions";
|
---|
197 | maxEvaluatedSolutionsComparator.RightSideParameter.ActualName = "MaximumEvaluatedSolutions";
|
---|
198 |
|
---|
199 | comparisonFactorModifier.Name = "Update ComparisonFactor (placeholder)";
|
---|
200 | comparisonFactorModifier.OperatorParameter.ActualName = ComparisonFactorModifierParameter.Name;
|
---|
201 |
|
---|
202 | analyzer2.Name = "Analyzer (placeholder)";
|
---|
203 | analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
204 |
|
---|
205 | resultsCollector3.CopyValue = new BoolValue(true);
|
---|
206 | resultsCollector3.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
|
---|
207 | resultsCollector3.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
208 |
|
---|
209 | conditionalBranch1.Name = "MaximumSelectionPressure reached?";
|
---|
210 | conditionalBranch1.ConditionParameter.ActualName = "TerminateSelectionPressure";
|
---|
211 |
|
---|
212 | conditionalBranch2.Name = "MaximumGenerations reached?";
|
---|
213 | conditionalBranch2.ConditionParameter.ActualName = "TerminateMaximumGenerations";
|
---|
214 |
|
---|
215 | conditionalBranch3.Name = "MaximumEvaluatedSolutions reached?";
|
---|
216 | conditionalBranch3.ConditionParameter.ActualName = "TerminateEvaluatedSolutions";
|
---|
217 | #endregion
|
---|
218 |
|
---|
219 | #region Create operator graph
|
---|
220 | OperatorGraph.InitialOperator = variableCreator;
|
---|
221 | variableCreator.Successor = comparisonFactorInitializer;
|
---|
222 | comparisonFactorInitializer.Successor = analyzer1;
|
---|
223 | analyzer1.Successor = resultsCollector1;
|
---|
224 | resultsCollector1.Successor = resultsCollector2;
|
---|
225 | resultsCollector2.Successor = mainOperator;
|
---|
226 | mainOperator.Successor = generationsCounter;
|
---|
227 | generationsCounter.Successor = maxGenerationsComparator;
|
---|
228 | maxGenerationsComparator.Successor = maxSelectionPressureComparator;
|
---|
229 | maxSelectionPressureComparator.Successor = maxEvaluatedSolutionsComparator;
|
---|
230 | maxEvaluatedSolutionsComparator.Successor = comparisonFactorModifier;
|
---|
231 | comparisonFactorModifier.Successor = analyzer2;
|
---|
232 | analyzer2.Successor = resultsCollector3;
|
---|
233 | resultsCollector3.Successor = conditionalBranch1;
|
---|
234 | conditionalBranch1.FalseBranch = conditionalBranch2;
|
---|
235 | conditionalBranch1.TrueBranch = null;
|
---|
236 | conditionalBranch1.Successor = null;
|
---|
237 | conditionalBranch2.FalseBranch = conditionalBranch3;
|
---|
238 | conditionalBranch2.TrueBranch = null;
|
---|
239 | conditionalBranch2.Successor = null;
|
---|
240 | conditionalBranch3.FalseBranch = mainOperator;
|
---|
241 | conditionalBranch3.TrueBranch = null;
|
---|
242 | conditionalBranch3.Successor = null;
|
---|
243 | #endregion
|
---|
244 | }
|
---|
245 | }
|
---|
246 | }
|
---|