Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/OffspringSelectionGeneticAlgorithmMainLoop.cs @ 5304

Last change on this file since 5304 was 5208, checked in by swagner, 14 years ago

Adapted EAs to enable parallel solution evaluation (#1333)

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