Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RAPGA/HeuristicLab.Algorithms.RAPGA/3.3/RAPGAMainLoop.cs @ 8330

Last change on this file since 8330 was 8330, checked in by jkarder, 12 years ago

#1247: initial version

File size: 17.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
29using HeuristicLab.Selection;
30
31namespace HeuristicLab.Algorithms.RAPGA {
32  /// <summary>
33  /// An operator which represents the main loop of a relevant alleles preserving genetic algorithm.
34  /// </summary>
35  [Item("RAPGAMainLoop", "An operator which represents the main loop of a relevant alleles preserving genetic algorithm.")]
36  [StorableClass]
37  public sealed class RAPGAMainLoop : 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<IntValue> EvaluatedSolutionsParameter {
76      get { return (ValueLookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
77    }
78    public ValueLookupParameter<IntValue> PopulationSizeParameter {
79      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
80    }
81    public IValueLookupParameter<IntValue> MinimumPopulationSizeParameter {
82      get { return (IValueLookupParameter<IntValue>)Parameters["MinimumPopulationSize"]; }
83    }
84    public IValueLookupParameter<IntValue> MaximumPopulationSizeParameter {
85      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumPopulationSize"]; }
86    }
87    public IValueLookupParameter<DoubleValue> ComparisonFactorParameter {
88      get { return (IValueLookupParameter<DoubleValue>)Parameters["ComparisonFactor"]; }
89    }
90    public IValueLookupParameter<IntValue> EffortParameter {
91      get { return (IValueLookupParameter<IntValue>)Parameters["Effort"]; }
92    }
93    private ScopeParameter CurrentScopeParameter {
94      get { return (ScopeParameter)Parameters["CurrentScope"]; }
95    }
96
97    public IScope CurrentScope {
98      get { return CurrentScopeParameter.ActualValue; }
99    }
100    #endregion
101
102    [StorableConstructor]
103    private RAPGAMainLoop(bool deserializing) : base(deserializing) { }
104    private RAPGAMainLoop(RAPGAMainLoop original, Cloner cloner) : base(original, cloner) { }
105    public RAPGAMainLoop()
106      : base() {
107      Initialize();
108    }
109    public override IDeepCloneable Clone(Cloner cloner) {
110      return new RAPGAMainLoop(this, cloner);
111    }
112
113    private void Initialize() {
114      #region Create parameters
115      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
116      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
117      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
118      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
119      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
120      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
121      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
122      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."));
123      Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
124      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
125      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
126      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
127      Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
128      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population."));
129      Parameters.Add(new ValueLookupParameter<IntValue>("MinimumPopulationSize", "The minimum size of the population of solutions."));
130      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumPopulationSize", "The maximum size of the population of solutions."));
131      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactor", "The comparison factor."));
132      Parameters.Add(new ValueLookupParameter<IntValue>("Effort", "The maximum number of offspring created in each generation."));
133      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied."));
134      #endregion
135
136      #region Create operators
137      VariableCreator variableCreator = new VariableCreator();
138      Assigner assigner1 = new Assigner();
139      ResultsCollector resultsCollector1 = new ResultsCollector();
140      Placeholder analyzer1 = new Placeholder();
141      Placeholder selector = new Placeholder();
142      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
143      ChildrenCreator childrenCreator = new ChildrenCreator();
144      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
145      Placeholder crossover = new Placeholder();
146      StochasticBranch stochasticBranch = new StochasticBranch();
147      Placeholder mutator = new Placeholder();
148      UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
149      Placeholder evaluator = new Placeholder();
150      WeightedParentsQualityComparator comparator1 = new WeightedParentsQualityComparator();
151      ConditionalSelector conditionalSelector = new ConditionalSelector();
152      RightReducer rightReducer1 = new RightReducer();
153      SubScopesCounter subScopesCounter1 = new SubScopesCounter();
154      IntCounter intCounter1 = new IntCounter();
155      UniformSubScopesProcessor uniformSubScopesProcessor3 = new UniformSubScopesProcessor();
156      SubScopesRemover subScopesRemover = new SubScopesRemover();
157      Comparator comparator2 = new Comparator();
158      ConditionalBranch conditionalBranch1 = new ConditionalBranch();
159      BestSelector bestSelector1 = new BestSelector();
160      BestSelector bestSelector2 = new BestSelector();
161      RightReducer rightReducer2 = new RightReducer();
162      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
163      BestSelector bestSelector3 = new BestSelector();
164      RightReducer rightReducer3 = new RightReducer();
165      MergingReducer mergingReducer = new MergingReducer();
166      IntCounter intCounter2 = new IntCounter();
167      Comparator comparator3 = new Comparator();
168      Placeholder analyzer2 = new Placeholder();
169      ConditionalBranch conditionalBranch2 = new ConditionalBranch();
170      Assigner assigner2 = new Assigner();
171      SubScopesCounter subScopesCounter2 = new SubScopesCounter();
172      ResultsCollector resultsCollector2 = new ResultsCollector();
173      Comparator comparator4 = new Comparator();
174      ConditionalBranch conditionalBranch3 = new ConditionalBranch();
175
176      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class GeneticAlgorithm expects this to be called Generations
177      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("CurrentPopulationSize", new IntValue(0)));
178
179      assigner1.Name = "Initialize CurrentPopulationSize";
180      assigner1.LeftSideParameter.ActualName = "CurrentPopulationSize";
181      assigner1.RightSideParameter.ActualName = EvaluatedSolutionsParameter.ActualName;
182
183      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
184      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("CurrentPopulationSize"));
185      resultsCollector1.ResultsParameter.ActualName = "Results";
186
187      analyzer1.Name = "Analyzer";
188      analyzer1.OperatorParameter.ActualName = "Analyzer";
189
190      selector.Name = "Selector";
191      selector.OperatorParameter.ActualName = "Selector";
192
193      childrenCreator.ParentsPerChild = new IntValue(2);
194
195      crossover.Name = "Crossover";
196      crossover.OperatorParameter.ActualName = "Crossover";
197
198      stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
199      stochasticBranch.RandomParameter.ActualName = "Random";
200
201      mutator.Name = "Mutator";
202      mutator.OperatorParameter.ActualName = "Mutator";
203
204      uniformSubScopesProcessor2.Parallel.Value = true;
205
206      evaluator.Name = "Evaluator";
207      evaluator.OperatorParameter.ActualName = "Evaluator";
208
209      comparator1.ComparisonFactorParameter.ActualName = ComparisonFactorParameter.Name;
210      comparator1.LeftSideParameter.ActualName = QualityParameter.Name;
211      comparator1.MaximizationParameter.ActualName = MaximizationParameter.Name;
212      comparator1.RightSideParameter.ActualName = QualityParameter.Name;
213      comparator1.ResultParameter.ActualName = "SuccessfulOffspring";
214
215      conditionalSelector.ConditionParameter.ActualName = "SuccessfulOffspring";
216      conditionalSelector.ConditionParameter.Depth = 1;
217      conditionalSelector.CopySelected.Value = false;
218
219      subScopesCounter1.Name = "Count Successful Offspring";
220      subScopesCounter1.ValueParameter.ActualName = "NumberOfSuccessfulOffspring";
221
222      intCounter1.IncrementParameter.ActualName = "NumberOfSuccessfulOffspring";
223      intCounter1.IncrementParameter.Value = null;
224      intCounter1.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
225
226      uniformSubScopesProcessor3.Parallel.Value = true;
227
228      subScopesRemover.RemoveAllSubScopes = true;
229
230      comparator2.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
231      comparator2.LeftSideParameter.ActualName = "NumberOfSuccessfulOffspring";
232      comparator2.RightSideParameter.ActualName = MaximumPopulationSizeParameter.Name;
233      comparator2.ResultParameter.ActualName = "SelectMaximum";
234
235      conditionalBranch1.ConditionParameter.ActualName = "SelectMaximum";
236
237      bestSelector1.CopySelected = new BoolValue(true);
238      bestSelector1.MaximizationParameter.ActualName = MaximizationParameter.Name;
239      bestSelector1.NumberOfSelectedSubScopesParameter.ActualName = MaximumPopulationSizeParameter.Name;
240      bestSelector1.QualityParameter.ActualName = QualityParameter.Name;
241
242      bestSelector2.CopySelected = new BoolValue(true);
243      bestSelector2.MaximizationParameter.ActualName = MaximizationParameter.Name;
244      bestSelector2.NumberOfSelectedSubScopesParameter.ActualName = "NumberOfSuccessfulOffspring";
245      bestSelector2.QualityParameter.ActualName = QualityParameter.Name;
246
247      bestSelector3.CopySelected = new BoolValue(false);
248      bestSelector3.MaximizationParameter.ActualName = MaximizationParameter.Name;
249      bestSelector3.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
250      bestSelector3.QualityParameter.ActualName = QualityParameter.Name;
251
252      intCounter2.Increment = new IntValue(1);
253      intCounter2.ValueParameter.ActualName = "Generations";
254
255      comparator3.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
256      comparator3.LeftSideParameter.ActualName = "Generations";
257      comparator3.ResultParameter.ActualName = "Terminate";
258      comparator3.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
259
260      analyzer2.Name = "Analyzer";
261      analyzer2.OperatorParameter.ActualName = "Analyzer";
262
263      conditionalBranch2.ConditionParameter.ActualName = "Terminate";
264
265      assigner2.Name = "Reset CurrentPopulationSize";
266      assigner2.LeftSideParameter.ActualName = "CurrentPopulationSize";
267      assigner2.RightSideParameter.Value = new IntValue(0);
268
269      subScopesCounter2.Name = "Increment EvaluatedSolutions";
270      subScopesCounter2.ValueParameter.ActualName = "CurrentPopulationSize";
271
272      resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("CurrentPopulationSize"));
273      resultsCollector2.ResultsParameter.ActualName = "Results";
274
275      comparator4.Comparison = new Comparison(ComparisonType.Less);
276      comparator4.LeftSideParameter.ActualName = "CurrentPopulationSize";
277      comparator4.RightSideParameter.ActualName = MinimumPopulationSizeParameter.Name;
278      comparator4.ResultParameter.ActualName = "Terminate";
279
280      conditionalBranch3.ConditionParameter.ActualName = "Terminate";
281      #endregion
282
283      #region Create operator graph
284      OperatorGraph.InitialOperator = variableCreator;
285      variableCreator.Successor = assigner1;
286      assigner1.Successor = resultsCollector1;
287      resultsCollector1.Successor = analyzer1;
288      analyzer1.Successor = selector;
289      selector.Successor = subScopesProcessor1;
290      subScopesProcessor1.Operators.Add(new EmptyOperator());
291      subScopesProcessor1.Operators.Add(childrenCreator);
292      subScopesProcessor1.Successor = subScopesProcessor2;
293      childrenCreator.Successor = uniformSubScopesProcessor1;
294      uniformSubScopesProcessor1.Operator = crossover;
295      uniformSubScopesProcessor1.Successor = uniformSubScopesProcessor2;
296      crossover.Successor = stochasticBranch;
297      stochasticBranch.FirstBranch = mutator;
298      stochasticBranch.SecondBranch = null;
299      stochasticBranch.Successor = null;
300      mutator.Successor = null;
301      subScopesRemover.Successor = null;
302      uniformSubScopesProcessor2.Operator = evaluator;
303      uniformSubScopesProcessor2.Successor = conditionalSelector;
304      evaluator.Successor = comparator1;
305      conditionalSelector.Successor = rightReducer1;
306      rightReducer1.Successor = subScopesCounter1;
307      subScopesCounter1.Successor = intCounter1;
308      intCounter1.Successor = uniformSubScopesProcessor3;
309      uniformSubScopesProcessor3.Operator = subScopesRemover;
310      uniformSubScopesProcessor3.Successor = comparator2;
311      comparator2.Successor = conditionalBranch1;
312      conditionalBranch1.TrueBranch = bestSelector1;
313      conditionalBranch1.FalseBranch = bestSelector2;
314      bestSelector1.Successor = rightReducer2;
315      bestSelector2.Successor = rightReducer2;
316      subScopesProcessor2.Operators.Add(bestSelector3);
317      subScopesProcessor2.Operators.Add(new EmptyOperator());
318      subScopesProcessor2.Successor = mergingReducer;
319      bestSelector3.Successor = rightReducer3;
320      rightReducer3.Successor = null;
321      mergingReducer.Successor = intCounter2;
322      intCounter2.Successor = comparator3;
323      comparator3.Successor = analyzer2;
324      analyzer2.Successor = conditionalBranch2;
325      conditionalBranch2.FalseBranch = assigner2;
326      conditionalBranch2.TrueBranch = null;
327      conditionalBranch2.Successor = null;
328      assigner2.Successor = subScopesCounter2;
329      subScopesCounter2.Successor = resultsCollector2;
330      resultsCollector2.Successor = comparator4;
331      comparator4.Successor = conditionalBranch3;
332      conditionalBranch3.FalseBranch = selector;
333      conditionalBranch3.TrueBranch = null;
334      conditionalBranch3.Successor = null;
335      #endregion
336    }
337
338    public override IOperation Apply() {
339      if (CrossoverParameter.ActualName == null)
340        return null;
341      return base.Apply();
342    }
343  }
344}
Note: See TracBrowser for help on using the repository browser.