Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1247:

  • added operator that evaluates expressions
  • added selection pressure analyzer
  • fixed bug in counting of evaluated solutions
File size: 21.8 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;
27using HeuristicLab.Optimization.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Selection;
31
32namespace HeuristicLab.Algorithms.RAPGA {
33  /// <summary>
34  /// An operator which represents the main loop of a relevant alleles preserving genetic algorithm.
35  /// </summary>
36  [Item("RAPGAMainLoop", "An operator which represents the main loop of a relevant alleles preserving genetic algorithm.")]
37  [StorableClass]
38  public sealed class RAPGAMainLoop : AlgorithmOperator {
39    #region Parameter properties
40    public ValueLookupParameter<IRandom> RandomParameter {
41      get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
42    }
43    public ValueLookupParameter<BoolValue> MaximizationParameter {
44      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
45    }
46    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
47      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
48    }
49    public ValueLookupParameter<IOperator> SelectorParameter {
50      get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
51    }
52    public ValueLookupParameter<IOperator> CrossoverParameter {
53      get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
54    }
55    public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
56      get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
57    }
58    public ValueLookupParameter<IOperator> MutatorParameter {
59      get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
60    }
61    public ValueLookupParameter<IOperator> EvaluatorParameter {
62      get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
63    }
64    public ValueLookupParameter<IntValue> ElitesParameter {
65      get { return (ValueLookupParameter<IntValue>)Parameters["Elites"]; }
66    }
67    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
68      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
69    }
70    public ValueLookupParameter<VariableCollection> ResultsParameter {
71      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
72    }
73    public ValueLookupParameter<IOperator> AnalyzerParameter {
74      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
75    }
76    public ValueLookupParameter<IntValue> EvaluatedSolutionsParameter {
77      get { return (ValueLookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
78    }
79    public ValueLookupParameter<IntValue> PopulationSizeParameter {
80      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
81    }
82    public IValueLookupParameter<IntValue> MinimumPopulationSizeParameter {
83      get { return (IValueLookupParameter<IntValue>)Parameters["MinimumPopulationSize"]; }
84    }
85    public IValueLookupParameter<IntValue> MaximumPopulationSizeParameter {
86      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumPopulationSize"]; }
87    }
88    public IValueLookupParameter<DoubleValue> ComparisonFactorParameter {
89      get { return (IValueLookupParameter<DoubleValue>)Parameters["ComparisonFactor"]; }
90    }
91    public IValueLookupParameter<IntValue> EffortParameter {
92      get { return (IValueLookupParameter<IntValue>)Parameters["Effort"]; }
93    }
94    public IValueLookupParameter<IntValue> BatchSizeParameter {
95      get { return (IValueLookupParameter<IntValue>)Parameters["BatchSize"]; }
96    }
97    public IValueLookupParameter<ISolutionSimilarityCalculator> SimilarityCalculatorParameter {
98      get { return (IValueLookupParameter<ISolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
99    }
100    private ScopeParameter CurrentScopeParameter {
101      get { return (ScopeParameter)Parameters["CurrentScope"]; }
102    }
103
104    public IScope CurrentScope {
105      get { return CurrentScopeParameter.ActualValue; }
106    }
107    #endregion
108
109    [StorableConstructor]
110    private RAPGAMainLoop(bool deserializing) : base(deserializing) { }
111    private RAPGAMainLoop(RAPGAMainLoop original, Cloner cloner) : base(original, cloner) { }
112    public RAPGAMainLoop()
113      : base() {
114      Initialize();
115    }
116    public override IDeepCloneable Clone(Cloner cloner) {
117      return new RAPGAMainLoop(this, cloner);
118    }
119
120    private void Initialize() {
121      #region Create parameters
122      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
123      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
124      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
125      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
126      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
127      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
128      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
129      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."));
130      Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
131      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
132      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
133      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
134      Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
135      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population."));
136      Parameters.Add(new ValueLookupParameter<IntValue>("MinimumPopulationSize", "The minimum size of the population of solutions."));
137      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumPopulationSize", "The maximum size of the population of solutions."));
138      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactor", "The comparison factor."));
139      Parameters.Add(new ValueLookupParameter<IntValue>("Effort", "The maximum number of offspring created in each generation."));
140      Parameters.Add(new ValueLookupParameter<IntValue>("BatchSize", "The number of children that should be created during one iteration of the offspring creation process.", new IntValue(10)));
141      Parameters.Add(new ValueLookupParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The operator used to calculate the similarity between two solutions."));
142      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied."));
143      #endregion
144
145      #region Create operators
146      VariableCreator variableCreator = new VariableCreator();
147      Assigner assigner1 = new Assigner();
148      ResultsCollector resultsCollector = new ResultsCollector();
149      Placeholder analyzer1 = new Placeholder();
150      Placeholder selector = new Placeholder();
151      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
152      ChildrenCreator childrenCreator = new ChildrenCreator();
153      UniformSubScopesProcessor uniformSubScopesProcessor = new UniformSubScopesProcessor();
154      Placeholder crossover = new Placeholder();
155      StochasticBranch stochasticBranch = new StochasticBranch();
156      Placeholder mutator = new Placeholder();
157      Placeholder evaluator = new Placeholder();
158      WeightedParentsQualityComparator weightedParentsQualityComparator = new WeightedParentsQualityComparator();
159      SubScopesRemover subScopesRemover = new SubScopesRemover();
160      IntCounter intCounter1 = new IntCounter();
161      IntCounter intCounter2 = new IntCounter();
162      ConditionalSelector conditionalSelector = new ConditionalSelector();
163      RightReducer rightReducer1 = new RightReducer();
164      DuplicatesSelector duplicateSelector = new DuplicatesSelector();
165      LeftReducer leftReducer1 = new LeftReducer();
166      ProgressiveOffspringPreserver progressiveOffspringSelector = new ProgressiveOffspringPreserver();
167      SubScopesCounter subScopesCounter2 = new SubScopesCounter();
168      Comparator comparator1 = new Comparator();
169      ConditionalBranch conditionalBranch1 = new ConditionalBranch();
170      Comparator comparator2 = new Comparator();
171      ConditionalBranch conditionalBranch2 = new ConditionalBranch();
172      LeftReducer leftReducer2 = new LeftReducer();
173      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
174      BestSelector bestSelector = new BestSelector();
175      RightReducer rightReducer2 = new RightReducer();
176      ScopeCleaner scopeCleaner = new ScopeCleaner();
177      OffspringRestorer offspringRestorer = new OffspringRestorer();
178      MergingReducer mergingReducer = new MergingReducer();
179      Assigner assigner2 = new Assigner();
180      IntCounter intCounter3 = new IntCounter();
181      SubScopesCounter subScopesCounter3 = new SubScopesCounter();
182      ExpressionEvaluator expressionEvaluator = new ExpressionEvaluator();
183      Placeholder analyzer2 = new Placeholder();
184      Comparator comparator3 = new Comparator();
185      ConditionalBranch conditionalBranch3 = new ConditionalBranch();
186      Comparator comparator4 = new Comparator();
187      ConditionalBranch conditionalBranch4 = new ConditionalBranch();
188      Assigner assigner3 = new Assigner();
189      Assigner assigner4 = new Assigner();
190      Assigner assigner5 = new Assigner();
191
192      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class RAPGA expects this to be called Generations
193      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("CurrentPopulationSize", new IntValue(0)));
194      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("LastPopulationSize", new IntValue(0)));
195      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("NumberOfCreatedOffspring", new IntValue(0)));
196      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("NumberOfSuccessfulOffspring", new IntValue(0)));
197      variableCreator.CollectedValues.Add(new ValueParameter<ScopeList>("OffspringList", new ScopeList()));
198
199      assigner1.Name = "Initialize CurrentPopulationSize";
200      assigner1.LeftSideParameter.ActualName = "CurrentPopulationSize";
201      assigner1.RightSideParameter.ActualName = PopulationSizeParameter.Name;
202
203      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
204      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("CurrentPopulationSize"));
205      resultsCollector.ResultsParameter.ActualName = "Results";
206
207      analyzer1.Name = "Analyzer";
208      analyzer1.OperatorParameter.ActualName = "Analyzer";
209
210      selector.Name = "Selector";
211      selector.OperatorParameter.ActualName = "Selector";
212
213      childrenCreator.ParentsPerChild = new IntValue(2);
214
215      uniformSubScopesProcessor.Parallel.Value = true;
216
217      crossover.Name = "Crossover";
218      crossover.OperatorParameter.ActualName = "Crossover";
219
220      stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
221      stochasticBranch.RandomParameter.ActualName = "Random";
222
223      mutator.Name = "Mutator";
224      mutator.OperatorParameter.ActualName = "Mutator";
225
226      evaluator.Name = "Evaluator";
227      evaluator.OperatorParameter.ActualName = "Evaluator";
228
229      weightedParentsQualityComparator.ComparisonFactorParameter.ActualName = ComparisonFactorParameter.Name;
230      weightedParentsQualityComparator.LeftSideParameter.ActualName = QualityParameter.Name;
231      weightedParentsQualityComparator.MaximizationParameter.ActualName = MaximizationParameter.Name;
232      weightedParentsQualityComparator.RightSideParameter.ActualName = QualityParameter.Name;
233      weightedParentsQualityComparator.ResultParameter.ActualName = "SuccessfulOffspring";
234
235      subScopesRemover.RemoveAllSubScopes = true;
236
237      intCounter1.Name = "Increment NumberOfCreatedOffspring";
238      intCounter1.ValueParameter.ActualName = "NumberOfCreatedOffspring";
239      intCounter1.Increment = null;
240      intCounter1.IncrementParameter.ActualName = BatchSizeParameter.Name;
241
242      intCounter2.Name = "Increment EvaluatedSolutions";
243      intCounter2.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
244      intCounter2.Increment = null;
245      intCounter2.IncrementParameter.ActualName = BatchSizeParameter.Name;
246
247      conditionalSelector.ConditionParameter.ActualName = "SuccessfulOffspring";
248      conditionalSelector.ConditionParameter.Depth = 1;
249      conditionalSelector.CopySelected.Value = false;
250
251      duplicateSelector.CopySelected.Value = false;
252
253      progressiveOffspringSelector.OffspringListParameter.ActualName = "OffspringList";
254      progressiveOffspringSelector.ElitesParameter.ActualName = ElitesParameter.Name;
255      progressiveOffspringSelector.MaximumPopulationSizeParameter.ActualName = MaximumPopulationSizeParameter.Name;
256
257      subScopesCounter2.Name = "Count Successful Offspring";
258      subScopesCounter2.ValueParameter.ActualName = "NumberOfSuccessfulOffspring";
259
260      comparator1.Name = "NumberOfSuccessfulOffspring >= MaximumPopulationSize";
261      comparator1.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
262      comparator1.LeftSideParameter.ActualName = "NumberOfSuccessfulOffspring";
263      comparator1.RightSideParameter.ActualName = MaximumPopulationSizeParameter.Name;
264      comparator1.ResultParameter.ActualName = "Break";
265
266      conditionalBranch1.Name = "Break?";
267      conditionalBranch1.ConditionParameter.ActualName = "Break";
268
269      comparator2.Name = "NumberOfCreatedOffspring >= Effort";
270      comparator2.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
271      comparator2.LeftSideParameter.ActualName = "NumberOfCreatedOffspring";
272      comparator2.RightSideParameter.ActualName = EffortParameter.Name;
273      comparator2.ResultParameter.ActualName = "Break";
274
275      conditionalBranch2.Name = "Break?";
276      conditionalBranch2.ConditionParameter.ActualName = "Break";
277
278      bestSelector.CopySelected = new BoolValue(false);
279      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
280      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
281      bestSelector.QualityParameter.ActualName = QualityParameter.Name;
282
283      assigner2.Name = "Update LastPopulationSize";
284      assigner2.LeftSideParameter.ActualName = "LastPopulationSize";
285      assigner2.RightSideParameter.ActualName = "CurrentPopulationSize";
286
287      intCounter3.Name = "Increment Generations";
288      intCounter3.Increment = new IntValue(1);
289      intCounter3.ValueParameter.ActualName = "Generations";
290
291      subScopesCounter3.Name = "Update CurrentPopulationSize";
292      subScopesCounter3.ValueParameter.ActualName = "CurrentPopulationSize";
293      subScopesCounter3.AccumulateParameter.Value = new BoolValue(false);
294
295      expressionEvaluator.Name = "Evaluate ActualSelectionPressure";
296      expressionEvaluator.CollectedValues.Add(new ValueLookupParameter<IntValue>("CurrentPopulationSize"));
297      expressionEvaluator.CollectedValues.Add(new ValueLookupParameter<IntValue>("LastPopulationSize"));
298      expressionEvaluator.CollectedValues.Add(new ValueLookupParameter<IntValue>("NumberOfCreatedOffspring"));
299      expressionEvaluator.CollectedValues.Add(new ValueLookupParameter<IntValue>("NumberOfSuccessfulOffspring"));
300      expressionEvaluator.ExpressionParameter.Value = new StringValue("CurrentPopulationSize NumberOfCreatedOffspring NumberOfSuccessfulOffspring - + LastPopulationSize /");
301      expressionEvaluator.ExpressionResultParameter.ActualName = "ActualSelectionPressure";
302
303      analyzer2.Name = "Analyzer";
304      analyzer2.OperatorParameter.ActualName = "Analyzer";
305
306      comparator3.Name = "Generations >= MaximumGenerations";
307      comparator3.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
308      comparator3.LeftSideParameter.ActualName = "Generations";
309      comparator3.ResultParameter.ActualName = "Terminate";
310      comparator3.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
311
312      conditionalBranch3.Name = "Terminate?";
313      conditionalBranch3.ConditionParameter.ActualName = "Terminate";
314
315      comparator4.Name = "CurrentPopulationSize < MinimumPopulationSize";
316      comparator4.Comparison = new Comparison(ComparisonType.Less);
317      comparator4.LeftSideParameter.ActualName = "CurrentPopulationSize";
318      comparator4.RightSideParameter.ActualName = MinimumPopulationSizeParameter.Name;
319      comparator4.ResultParameter.ActualName = "Terminate";
320
321      conditionalBranch4.Name = "Terminate?";
322      conditionalBranch4.ConditionParameter.ActualName = "Terminate";
323
324      assigner3.Name = "Reset NumberOfCreatedOffspring";
325      assigner3.LeftSideParameter.ActualName = "NumberOfCreatedOffspring";
326      assigner3.RightSideParameter.Value = new IntValue(0);
327
328      assigner4.Name = "Reset NumberOfSuccessfulOffspring";
329      assigner4.LeftSideParameter.ActualName = "NumberOfSuccessfulOffspring";
330      assigner4.RightSideParameter.Value = new IntValue(0);
331
332      assigner5.Name = "Reset OffspringList";
333      assigner5.LeftSideParameter.ActualName = "OffspringList";
334      assigner5.RightSideParameter.Value = new ScopeList();
335      #endregion
336
337      #region Create operator graph
338      OperatorGraph.InitialOperator = variableCreator;
339      variableCreator.Successor = assigner1;
340      assigner1.Successor = resultsCollector;
341      resultsCollector.Successor = analyzer1;
342      analyzer1.Successor = selector;
343      selector.Successor = subScopesProcessor1;
344      subScopesProcessor1.Operators.Add(new EmptyOperator());
345      subScopesProcessor1.Operators.Add(childrenCreator);
346      subScopesProcessor1.Successor = comparator1;
347      childrenCreator.Successor = uniformSubScopesProcessor;
348      uniformSubScopesProcessor.Operator = crossover;
349      uniformSubScopesProcessor.Successor = intCounter1;
350      crossover.Successor = stochasticBranch;
351      stochasticBranch.FirstBranch = mutator;
352      stochasticBranch.SecondBranch = null;
353      mutator.Successor = null;
354      stochasticBranch.Successor = evaluator;
355      evaluator.Successor = weightedParentsQualityComparator;
356      weightedParentsQualityComparator.Successor = subScopesRemover;
357      intCounter1.Successor = intCounter2;
358      intCounter2.Successor = conditionalSelector;
359      conditionalSelector.Successor = rightReducer1;
360      rightReducer1.Successor = duplicateSelector;
361      duplicateSelector.Successor = leftReducer1;
362      leftReducer1.Successor = progressiveOffspringSelector;
363      progressiveOffspringSelector.Successor = subScopesCounter2;
364      comparator1.Successor = conditionalBranch1;
365      conditionalBranch1.FalseBranch = comparator2;
366      conditionalBranch1.TrueBranch = subScopesProcessor2;
367      comparator2.Successor = conditionalBranch2;
368      conditionalBranch2.FalseBranch = leftReducer2;
369      conditionalBranch2.TrueBranch = subScopesProcessor2;
370      leftReducer2.Successor = selector;
371      subScopesProcessor2.Operators.Add(bestSelector);
372      subScopesProcessor2.Operators.Add(scopeCleaner);
373      subScopesProcessor2.Successor = mergingReducer;
374      bestSelector.Successor = rightReducer2;
375      rightReducer2.Successor = null;
376      scopeCleaner.Successor = offspringRestorer;
377      mergingReducer.Successor = assigner2;
378      assigner2.Successor = intCounter3;
379      intCounter3.Successor = subScopesCounter3;
380      subScopesCounter3.Successor = expressionEvaluator;
381      expressionEvaluator.Successor = analyzer2;
382      analyzer2.Successor = comparator3;
383      comparator3.Successor = conditionalBranch3;
384      conditionalBranch3.FalseBranch = comparator4;
385      conditionalBranch3.TrueBranch = null;
386      conditionalBranch3.Successor = null;
387      comparator4.Successor = conditionalBranch4;
388      conditionalBranch4.FalseBranch = assigner3;
389      conditionalBranch4.TrueBranch = null;
390      conditionalBranch4.Successor = null;
391      assigner3.Successor = assigner4;
392      assigner4.Successor = assigner5;
393      assigner5.Successor = selector;
394
395      #endregion
396    }
397
398    public override IOperation Apply() {
399      if (CrossoverParameter.ActualName == null)
400        return null;
401      return base.Apply();
402    }
403  }
404}
Note: See TracBrowser for help on using the repository browser.