Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3744 was 3744, checked in by abeham, 14 years ago

#893

  • Changed initialization of comparison factor
  • fixed some bugs in OffspringSelector
File size: 14.4 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 LookupParameter<DoubleValue> ComparisonFactorParameter {
79      get { return (LookupParameter<DoubleValue>)Parameters["ComparisonFactor"]; }
80    }
81    public ValueLookupParameter<DoubleValue> ComparisonFactorStartParameter {
82      get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorStart"]; }
83    }
84    public ValueLookupParameter<IOperator> ComparisonFactorModifierParameter {
85      get { return (ValueLookupParameter<IOperator>)Parameters["ComparisonFactorModifier"]; }
86    }
87    public ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
88      get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
89    }
90    public ValueLookupParameter<BoolValue> OffspringSelectionBeforeMutationParameter {
91      get { return (ValueLookupParameter<BoolValue>)Parameters["OffspringSelectionBeforeMutation"]; }
92    }
93    #endregion
94
95    [StorableConstructor]
96    private OffspringSelectionGeneticAlgorithmMainLoop(bool deserializing) : base() { }
97    public OffspringSelectionGeneticAlgorithmMainLoop()
98      : base() {
99      Initialize();
100    }
101
102    private void Initialize() {
103      #region Create parameters
104      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
105      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
106      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
107      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
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."));
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<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved."));
118      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]."));
119      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorStart", "The initial value for the comparison factor."));
120      Parameters.Add(new ValueLookupParameter<IOperator>("ComparisonFactorModifier", "The operator used to modify the comparison factor."));
121      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm."));
122      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."));
123      #endregion
124
125      #region Create operators
126      VariableCreator variableCreator = new VariableCreator();
127      Assigner comparisonFactorInitializer = new Assigner();
128      Placeholder analyzer1 = new Placeholder();
129      ResultsCollector resultsCollector1 = new ResultsCollector();
130      ResultsCollector resultsCollector2 = new ResultsCollector();
131      OffspringSelectionGeneticAlgorithmMainOperator mainOperator = new OffspringSelectionGeneticAlgorithmMainOperator();
132      IntCounter generationsCounter = new IntCounter();
133      Comparator maxGenerationsComparator = new Comparator();
134      Comparator maxSelectionPressureComparator = new Comparator();
135      Placeholder comparisonFactorModifier = new Placeholder();
136      Placeholder analyzer2 = new Placeholder();
137      ResultsCollector resultsCollector3 = new ResultsCollector();
138      ConditionalBranch conditionalBranch1 = new ConditionalBranch();
139      ConditionalBranch conditionalBranch2 = new ConditionalBranch();
140
141      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
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      comparisonFactorModifier.Name = "Update ComparisonFactor (placeholder)";
195      comparisonFactorModifier.OperatorParameter.ActualName = ComparisonFactorModifierParameter.Name;
196
197      analyzer2.Name = "Analyzer (placeholder)";
198      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
199
200      resultsCollector3.CopyValue = new BoolValue(true);
201      resultsCollector3.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
202      resultsCollector3.ResultsParameter.ActualName = ResultsParameter.Name;
203
204      conditionalBranch1.Name = "MaximumSelectionPressure reached?";
205      conditionalBranch1.ConditionParameter.ActualName = "TerminateSelectionPressure";
206
207      conditionalBranch2.Name = "MaximumGenerations reached?";
208      conditionalBranch2.ConditionParameter.ActualName = "TerminateMaximumGenerations";
209      #endregion
210
211      #region Create operator graph
212      OperatorGraph.InitialOperator = variableCreator;
213      variableCreator.Successor = comparisonFactorInitializer;
214      comparisonFactorInitializer.Successor = analyzer1;
215      analyzer1.Successor = resultsCollector1;
216      resultsCollector1.Successor = resultsCollector2;
217      resultsCollector2.Successor = mainOperator;
218      mainOperator.Successor = generationsCounter;
219      generationsCounter.Successor = maxGenerationsComparator;
220      maxGenerationsComparator.Successor = maxSelectionPressureComparator;
221      maxSelectionPressureComparator.Successor = comparisonFactorModifier;
222      comparisonFactorModifier.Successor = analyzer2;
223      analyzer2.Successor = resultsCollector3;
224      resultsCollector3.Successor = conditionalBranch1;
225      conditionalBranch1.FalseBranch = conditionalBranch2;
226      conditionalBranch1.TrueBranch = null;
227      conditionalBranch1.Successor = null;
228      conditionalBranch2.FalseBranch = mainOperator;
229      conditionalBranch2.TrueBranch = null;
230      conditionalBranch2.Successor = null;
231      #endregion
232    }
233  }
234}
Note: See TracBrowser for help on using the repository browser.