Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3730 was 3698, checked in by abeham, 15 years ago

#893

  • fixed review comments regarding comparison factor results / modification
  • corrected the description of a parameter
  • removed unnecessary parameters
File size: 13.6 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.Analysis;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Selection;
30
31namespace HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm {
32  /// <summary>
33  /// An operator which represents the main loop of an offspring selection genetic algorithm.
34  /// </summary>
35  [Item("OffspringSelectionGeneticAlgorithmMainLoop", "An operator which represents the main loop of an offspring selection genetic algorithm.")]
36  [StorableClass]
37  public sealed class OffspringSelectionGeneticAlgorithmMainLoop : 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<DoubleValue> SuccessRatioParameter {
76      get { return (ValueLookupParameter<DoubleValue>)Parameters["SuccessRatio"]; }
77    }
78    public ValueLookupParameter<IOperator> ComparisonFactorModifierParameter {
79      get { return (ValueLookupParameter<IOperator>)Parameters["ComparisonFactorModifier"]; }
80    }
81    public ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
82      get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
83    }
84    public ValueLookupParameter<BoolValue> OffspringSelectionBeforeMutationParameter {
85      get { return (ValueLookupParameter<BoolValue>)Parameters["OffspringSelectionBeforeMutation"]; }
86    }
87    #endregion
88
89    [StorableConstructor]
90    private OffspringSelectionGeneticAlgorithmMainLoop(bool deserializing) : base() { }
91    public OffspringSelectionGeneticAlgorithmMainLoop()
92      : base() {
93      Initialize();
94    }
95
96    private void Initialize() {
97      #region Create parameters
98      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
99      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
100      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
101      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
102      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
103      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
104      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
105      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
106      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
107      Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
108      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
109      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
110      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
111      Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved."));
112      Parameters.Add(new ValueLookupParameter<IOperator>("ComparisonFactorModifier", "The operator used to modify the comparison factor."));
113      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm."));
114      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."));
115      #endregion
116
117      #region Create operators
118      VariableCreator variableCreator = new VariableCreator();
119      Placeholder comparisonFactorModifier1 = new Placeholder();
120      Placeholder analyzer1 = new Placeholder();
121      ResultsCollector resultsCollector1 = new ResultsCollector();
122      ResultsCollector resultsCollector2 = new ResultsCollector();
123      OffspringSelectionGeneticAlgorithmMainOperator mainOperator = new OffspringSelectionGeneticAlgorithmMainOperator();
124      IntCounter generationsCounter = new IntCounter();
125      Comparator maxGenerationsComparator = new Comparator();
126      Comparator maxSelectionPressureComparator = new Comparator();
127      Placeholder comparisonFactorModifier2 = new Placeholder();
128      Placeholder analyzer2 = new Placeholder();
129      ResultsCollector resultsCollector3 = new ResultsCollector();
130      ConditionalBranch conditionalBranch1 = new ConditionalBranch();
131      ConditionalBranch conditionalBranch2 = new ConditionalBranch();
132
133      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
134      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
135      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("SelectionPressure", new DoubleValue(0)));
136      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("CurrentSuccessRatio", new DoubleValue(0)));
137      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("ComparisonFactor", new DoubleValue(0)));
138
139      comparisonFactorModifier1.Name = "Initialize ComparisonFactor (placeholder)";
140      comparisonFactorModifier1.OperatorParameter.ActualName = ComparisonFactorModifierParameter.Name;
141
142      analyzer1.Name = "Analyzer (placeholder)";
143      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
144
145      resultsCollector1.CopyValue = new BoolValue(false);
146      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
147      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Curent Comparison Factor", null, "ComparisonFactor"));
148      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Selection Pressure", null, "SelectionPressure"));
149      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Success Ratio", null, "CurrentSuccessRatio"));
150      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
151
152      resultsCollector2.CopyValue = new BoolValue(true);
153      resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
154      resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
155
156      mainOperator.ComparisonFactorParameter.ActualName = "ComparisonFactor";
157      mainOperator.CrossoverParameter.ActualName = CrossoverParameter.Name;
158      mainOperator.CurrentSuccessRatioParameter.ActualName = "CurrentSuccessRatio";
159      mainOperator.ElitesParameter.ActualName = ElitesParameter.Name;
160      mainOperator.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
161      mainOperator.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
162      mainOperator.MaximizationParameter.ActualName = MaximizationParameter.Name;
163      mainOperator.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
164      mainOperator.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
165      mainOperator.MutatorParameter.ActualName = MutatorParameter.Name;
166      mainOperator.OffspringSelectionBeforeMutationParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
167      mainOperator.QualityParameter.ActualName = QualityParameter.Name;
168      mainOperator.RandomParameter.ActualName = RandomParameter.Name;
169      mainOperator.SelectionPressureParameter.ActualName = "SelectionPressure";
170      mainOperator.SelectorParameter.ActualName = SelectorParameter.Name;
171      mainOperator.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
172
173      generationsCounter.Increment = new IntValue(1);
174      generationsCounter.ValueParameter.ActualName = "Generations";
175
176      maxGenerationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
177      maxGenerationsComparator.LeftSideParameter.ActualName = "Generations";
178      maxGenerationsComparator.ResultParameter.ActualName = "TerminateMaximumGenerations";
179      maxGenerationsComparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
180
181      maxSelectionPressureComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
182      maxSelectionPressureComparator.LeftSideParameter.ActualName = "SelectionPressure";
183      maxSelectionPressureComparator.ResultParameter.ActualName = "TerminateSelectionPressure";
184      maxSelectionPressureComparator.RightSideParameter.ActualName = MaximumSelectionPressureParameter.Name;
185
186      comparisonFactorModifier2.Name = "Update ComparisonFactor (placeholder)";
187      comparisonFactorModifier2.OperatorParameter.ActualName = ComparisonFactorModifierParameter.Name;
188
189      analyzer2.Name = "Analyzer (placeholder)";
190      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
191
192      resultsCollector3.CopyValue = new BoolValue(true);
193      resultsCollector3.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
194      resultsCollector3.ResultsParameter.ActualName = ResultsParameter.Name;
195
196      conditionalBranch1.Name = "MaximumSelectionPressure reached?";
197      conditionalBranch1.ConditionParameter.ActualName = "TerminateSelectionPressure";
198
199      conditionalBranch2.Name = "MaximumGenerations reached?";
200      conditionalBranch2.ConditionParameter.ActualName = "TerminateMaximumGenerations";
201      #endregion
202
203      #region Create operator graph
204      OperatorGraph.InitialOperator = variableCreator;
205      variableCreator.Successor = comparisonFactorModifier1;
206      comparisonFactorModifier1.Successor = analyzer1;
207      analyzer1.Successor = resultsCollector1;
208      resultsCollector1.Successor = resultsCollector2;
209      resultsCollector2.Successor = mainOperator;
210      mainOperator.Successor = generationsCounter;
211      generationsCounter.Successor = maxGenerationsComparator;
212      maxGenerationsComparator.Successor = maxSelectionPressureComparator;
213      maxSelectionPressureComparator.Successor = comparisonFactorModifier2;
214      comparisonFactorModifier2.Successor = analyzer2;
215      analyzer2.Successor = resultsCollector3;
216      resultsCollector3.Successor = conditionalBranch1;
217      conditionalBranch1.FalseBranch = conditionalBranch2;
218      conditionalBranch1.TrueBranch = null;
219      conditionalBranch1.Successor = null;
220      conditionalBranch2.FalseBranch = mainOperator;
221      conditionalBranch2.TrueBranch = null;
222      conditionalBranch2.Successor = null;
223      #endregion
224    }
225  }
226}
Note: See TracBrowser for help on using the repository browser.