Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/OffspringSelectionGeneticAlgorithmMainLoop.cs @ 10449

Last change on this file since 10449 was 9673, checked in by mkommend, 11 years ago

#2038: Merged r9553:9555, r9569, r9591:r9592, r9618 into the stable branch.

File size: 16.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 IValueLookupParameter<BoolValue> ReevaluateElitesParameter {
66      get { return (IValueLookupParameter<BoolValue>)Parameters["ReevaluateElites"]; }
67    }
68    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
69      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
70    }
71    public ValueLookupParameter<VariableCollection> ResultsParameter {
72      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
73    }
74    public ValueLookupParameter<IOperator> AnalyzerParameter {
75      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
76    }
77    public ValueLookupParameter<DoubleValue> SuccessRatioParameter {
78      get { return (ValueLookupParameter<DoubleValue>)Parameters["SuccessRatio"]; }
79    }
80    public LookupParameter<DoubleValue> ComparisonFactorParameter {
81      get { return (LookupParameter<DoubleValue>)Parameters["ComparisonFactor"]; }
82    }
83    public ValueLookupParameter<DoubleValue> ComparisonFactorStartParameter {
84      get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorStart"]; }
85    }
86    public ValueLookupParameter<IOperator> ComparisonFactorModifierParameter {
87      get { return (ValueLookupParameter<IOperator>)Parameters["ComparisonFactorModifier"]; }
88    }
89    public ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
90      get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
91    }
92    public ValueLookupParameter<BoolValue> OffspringSelectionBeforeMutationParameter {
93      get { return (ValueLookupParameter<BoolValue>)Parameters["OffspringSelectionBeforeMutation"]; }
94    }
95    public LookupParameter<IntValue> EvaluatedSolutionsParameter {
96      get { return (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
97    }
98    #endregion
99
100    [StorableConstructor]
101    private OffspringSelectionGeneticAlgorithmMainLoop(bool deserializing) : base(deserializing) { }
102    private OffspringSelectionGeneticAlgorithmMainLoop(OffspringSelectionGeneticAlgorithmMainLoop original, Cloner cloner)
103      : base(original, cloner) {
104    }
105    public override IDeepCloneable Clone(Cloner cloner) {
106      return new OffspringSelectionGeneticAlgorithmMainLoop(this, cloner);
107    }
108    public OffspringSelectionGeneticAlgorithmMainLoop()
109      : base() {
110      Initialize();
111    }
112
113    [StorableHook(HookType.AfterDeserialization)]
114    private void AfterDeserialization() {
115      // BackwardsCompatibility3.3
116      #region Backwards compatible code, remove with 3.4
117      if (!Parameters.ContainsKey("ReevaluateElites")) {
118        Parameters.Add(new ValueLookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
119      }
120      #endregion
121    }
122
123    private void Initialize() {
124      #region Create parameters
125      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
126      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
127      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
128      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
129      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
130      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
131      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
132      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
133      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."));
134      Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
135      Parameters.Add(new ValueLookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
136      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
137      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
138      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
139      Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved."));
140      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]."));
141      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorStart", "The initial value for the comparison factor."));
142      Parameters.Add(new ValueLookupParameter<IOperator>("ComparisonFactorModifier", "The operator used to modify the comparison factor."));
143      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm."));
144      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."));
145      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
146      #endregion
147
148      #region Create operators
149      VariableCreator variableCreator = new VariableCreator();
150      Assigner comparisonFactorInitializer = new Assigner();
151      Placeholder analyzer1 = new Placeholder();
152      ResultsCollector resultsCollector1 = new ResultsCollector();
153      OffspringSelectionGeneticAlgorithmMainOperator mainOperator = new OffspringSelectionGeneticAlgorithmMainOperator();
154      IntCounter generationsCounter = new IntCounter();
155      Comparator maxGenerationsComparator = new Comparator();
156      Comparator maxSelectionPressureComparator = new Comparator();
157      Comparator maxEvaluatedSolutionsComparator = new Comparator();
158      Placeholder comparisonFactorModifier = new Placeholder();
159      Placeholder analyzer2 = new Placeholder();
160      ConditionalBranch conditionalBranch1 = new ConditionalBranch();
161      ConditionalBranch conditionalBranch2 = new ConditionalBranch();
162      ConditionalBranch conditionalBranch3 = new ConditionalBranch();
163
164      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class OffspringSelectionGeneticAlgorithm expects this to be called Generations
165      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("SelectionPressure", new DoubleValue(0)));
166      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("CurrentSuccessRatio", new DoubleValue(0)));
167
168      comparisonFactorInitializer.Name = "Initialize ComparisonFactor (placeholder)";
169      comparisonFactorInitializer.LeftSideParameter.ActualName = ComparisonFactorParameter.Name;
170      comparisonFactorInitializer.RightSideParameter.ActualName = ComparisonFactorStartParameter.Name;
171
172      analyzer1.Name = "Analyzer (placeholder)";
173      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
174
175      resultsCollector1.CopyValue = new BoolValue(false);
176      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
177      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Comparison Factor", null, ComparisonFactorParameter.Name));
178      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Selection Pressure", "Displays the rising selection pressure during a generation.", "SelectionPressure"));
179      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Success Ratio", "Indicates how many successful children were already found during a generation (relative to the population size).", "CurrentSuccessRatio"));
180      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
181
182      mainOperator.ComparisonFactorParameter.ActualName = ComparisonFactorParameter.Name;
183      mainOperator.CrossoverParameter.ActualName = CrossoverParameter.Name;
184      mainOperator.CurrentSuccessRatioParameter.ActualName = "CurrentSuccessRatio";
185      mainOperator.ElitesParameter.ActualName = ElitesParameter.Name;
186      mainOperator.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
187      mainOperator.EvaluatedSolutionsParameter.ActualName = EvaluatedSolutionsParameter.Name;
188      mainOperator.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
189      mainOperator.MaximizationParameter.ActualName = MaximizationParameter.Name;
190      mainOperator.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
191      mainOperator.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
192      mainOperator.MutatorParameter.ActualName = MutatorParameter.Name;
193      mainOperator.OffspringSelectionBeforeMutationParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
194      mainOperator.QualityParameter.ActualName = QualityParameter.Name;
195      mainOperator.RandomParameter.ActualName = RandomParameter.Name;
196      mainOperator.SelectionPressureParameter.ActualName = "SelectionPressure";
197      mainOperator.SelectorParameter.ActualName = SelectorParameter.Name;
198      mainOperator.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
199
200      generationsCounter.Increment = new IntValue(1);
201      generationsCounter.ValueParameter.ActualName = "Generations";
202
203      maxGenerationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
204      maxGenerationsComparator.LeftSideParameter.ActualName = "Generations";
205      maxGenerationsComparator.ResultParameter.ActualName = "TerminateMaximumGenerations";
206      maxGenerationsComparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
207
208      maxSelectionPressureComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
209      maxSelectionPressureComparator.LeftSideParameter.ActualName = "SelectionPressure";
210      maxSelectionPressureComparator.ResultParameter.ActualName = "TerminateSelectionPressure";
211      maxSelectionPressureComparator.RightSideParameter.ActualName = MaximumSelectionPressureParameter.Name;
212
213      maxEvaluatedSolutionsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
214      maxEvaluatedSolutionsComparator.LeftSideParameter.ActualName = EvaluatedSolutionsParameter.Name;
215      maxEvaluatedSolutionsComparator.ResultParameter.ActualName = "TerminateEvaluatedSolutions";
216      maxEvaluatedSolutionsComparator.RightSideParameter.ActualName = "MaximumEvaluatedSolutions";
217
218      comparisonFactorModifier.Name = "Update ComparisonFactor (placeholder)";
219      comparisonFactorModifier.OperatorParameter.ActualName = ComparisonFactorModifierParameter.Name;
220
221      analyzer2.Name = "Analyzer (placeholder)";
222      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
223
224      conditionalBranch1.Name = "MaximumSelectionPressure reached?";
225      conditionalBranch1.ConditionParameter.ActualName = "TerminateSelectionPressure";
226
227      conditionalBranch2.Name = "MaximumGenerations reached?";
228      conditionalBranch2.ConditionParameter.ActualName = "TerminateMaximumGenerations";
229
230      conditionalBranch3.Name = "MaximumEvaluatedSolutions reached?";
231      conditionalBranch3.ConditionParameter.ActualName = "TerminateEvaluatedSolutions";
232      #endregion
233
234      #region Create operator graph
235      OperatorGraph.InitialOperator = variableCreator;
236      variableCreator.Successor = comparisonFactorInitializer;
237      comparisonFactorInitializer.Successor = analyzer1;
238      analyzer1.Successor = resultsCollector1;
239      resultsCollector1.Successor = mainOperator;
240      mainOperator.Successor = generationsCounter;
241      generationsCounter.Successor = maxGenerationsComparator;
242      maxGenerationsComparator.Successor = maxSelectionPressureComparator;
243      maxSelectionPressureComparator.Successor = maxEvaluatedSolutionsComparator;
244      maxEvaluatedSolutionsComparator.Successor = comparisonFactorModifier;
245      comparisonFactorModifier.Successor = analyzer2;
246      analyzer2.Successor = conditionalBranch1;
247      conditionalBranch1.FalseBranch = conditionalBranch2;
248      conditionalBranch1.TrueBranch = null;
249      conditionalBranch1.Successor = null;
250      conditionalBranch2.FalseBranch = conditionalBranch3;
251      conditionalBranch2.TrueBranch = null;
252      conditionalBranch2.Successor = null;
253      conditionalBranch3.FalseBranch = mainOperator;
254      conditionalBranch3.TrueBranch = null;
255      conditionalBranch3.Successor = null;
256      #endregion
257    }
258  }
259}
Note: See TracBrowser for help on using the repository browser.