Free cookie consent management tool by TermsFeed Policy Generator

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

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

#999

  • updated OSGA
File size: 14.0 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<DoubleValue> ComparisonFactorLowerBoundParameter {
79      get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorLowerBound"]; }
80    }
81    public ValueLookupParameter<DoubleValue> ComparisonFactorUpperBoundParameter {
82      get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorUpperBound"]; }
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 ValueLookupParameter<DoubleValue>("ComparisonFactorLowerBound", "The lower bound of the comparison factor (start)."));
119      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorUpperBound", "The upper bound of the comparison factor (end)."));
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      Placeholder comparisonFactorModifier = new Placeholder();
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 analyzer2 = new Placeholder();
136      ResultsCollector resultsCollector3 = new ResultsCollector();
137      ConditionalBranch conditionalBranch1 = new ConditionalBranch();
138      ConditionalBranch conditionalBranch2 = new ConditionalBranch();
139
140      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
141      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
142      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("SelectionPressure", new DoubleValue(0)));
143      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("CurrentSuccessRatio", new DoubleValue(0)));
144
145      comparisonFactorModifier.Name = "Modify ComparisonFactor (placeholder)";
146      comparisonFactorModifier.OperatorParameter.ActualName = ComparisonFactorModifierParameter.Name;
147
148      analyzer1.Name = "Analyzer (placeholder)";
149      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
150
151      resultsCollector1.CopyValue = new BoolValue(false);
152      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
153      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
154      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Curent Comparison Factor", null, "ComparisonFactor"));
155      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Selection Pressure", null, "SelectionPressure"));
156      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Success Ratio", null, "CurrentSuccessRatio"));
157      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
158
159      resultsCollector2.CopyValue = new BoolValue(true);
160      resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
161      resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
162
163      mainOperator.ComparisonFactorParameter.ActualName = "ComparisonFactor";
164      mainOperator.CrossoverParameter.ActualName = CrossoverParameter.Name;
165      mainOperator.CurrentSuccessRatioParameter.ActualName = "CurrentSuccessRatio";
166      mainOperator.ElitesParameter.ActualName = ElitesParameter.Name;
167      mainOperator.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
168      mainOperator.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
169      mainOperator.MaximizationParameter.ActualName = MaximizationParameter.Name;
170      mainOperator.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
171      mainOperator.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
172      mainOperator.MutatorParameter.ActualName = MutatorParameter.Name;
173      mainOperator.OffspringSelectionBeforeMutationParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
174      mainOperator.QualityParameter.ActualName = QualityParameter.Name;
175      mainOperator.RandomParameter.ActualName = RandomParameter.Name;
176      mainOperator.SelectionPressureParameter.ActualName = "SelectionPressure";
177      mainOperator.SelectorParameter.ActualName = SelectorParameter.Name;
178      mainOperator.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
179
180      generationsCounter.Increment = new IntValue(1);
181      generationsCounter.ValueParameter.ActualName = "Generations";
182
183      maxGenerationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
184      maxGenerationsComparator.LeftSideParameter.ActualName = "Generations";
185      maxGenerationsComparator.ResultParameter.ActualName = "TerminateMaximumGenerations";
186      maxGenerationsComparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
187
188      maxSelectionPressureComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
189      maxSelectionPressureComparator.LeftSideParameter.ActualName = "SelectionPressure";
190      maxSelectionPressureComparator.ResultParameter.ActualName = "TerminateSelectionPressure";
191      maxSelectionPressureComparator.RightSideParameter.ActualName = MaximumSelectionPressureParameter.Name;
192
193      analyzer2.Name = "Analyzer (placeholder)";
194      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
195
196      resultsCollector3.CopyValue = new BoolValue(true);
197      resultsCollector3.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
198      resultsCollector3.ResultsParameter.ActualName = ResultsParameter.Name;
199
200      conditionalBranch1.Name = "MaximumSelectionPressure reached?";
201      conditionalBranch1.ConditionParameter.ActualName = "TerminateSelectionPressure";
202
203      conditionalBranch2.Name = "MaximumGenerations reached?";
204      conditionalBranch2.ConditionParameter.ActualName = "TerminateMaximumGenerations";
205      #endregion
206
207      #region Create operator graph
208      OperatorGraph.InitialOperator = variableCreator;
209      variableCreator.Successor = comparisonFactorModifier;
210      comparisonFactorModifier.Successor = analyzer1;
211      analyzer1.Successor = resultsCollector1;
212      resultsCollector1.Successor = resultsCollector2;
213      resultsCollector2.Successor = mainOperator;
214      mainOperator.Successor = generationsCounter;
215      generationsCounter.Successor = maxGenerationsComparator;
216      maxGenerationsComparator.Successor = maxSelectionPressureComparator;
217      maxSelectionPressureComparator.Successor = analyzer2;
218      analyzer2.Successor = resultsCollector3;
219      resultsCollector3.Successor = conditionalBranch1;
220      conditionalBranch1.FalseBranch = conditionalBranch2;
221      conditionalBranch1.TrueBranch = null;
222      conditionalBranch1.Successor = null;
223      conditionalBranch2.FalseBranch = mainOperator;
224      conditionalBranch2.TrueBranch = null;
225      conditionalBranch2.Successor = null;
226      #endregion
227    }
228  }
229}
Note: See TracBrowser for help on using the repository browser.