Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/GeneticAlgorithmMainLoop.cs @ 9553

Last change on this file since 9553 was 9553, checked in by mkommend, 12 years ago

#2038: Added possibility to reevaluate elites in the GA.

File size: 13.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.GeneticAlgorithm {
32  /// <summary>
33  /// An operator which represents the main loop of a genetic algorithm.
34  /// </summary>
35  [Item("GeneticAlgorithmMainLoop", "An operator which represents the main loop of a genetic algorithm.")]
36  [StorableClass]
37  public sealed class GeneticAlgorithmMainLoop : 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 IValueLookupParameter<BoolValue> ReevaluateElitesParameter {
67      get { return (IValueLookupParameter<BoolValue>)Parameters["ReevaluateElites"]; }
68    }
69    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
70      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
71    }
72    public ValueLookupParameter<VariableCollection> ResultsParameter {
73      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
74    }
75    public ValueLookupParameter<IOperator> AnalyzerParameter {
76      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
77    }
78    public ValueLookupParameter<IntValue> EvaluatedSolutionsParameter {
79      get { return (ValueLookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
80    }
81    public ValueLookupParameter<IntValue> PopulationSizeParameter {
82      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
83    }
84    private ScopeParameter CurrentScopeParameter {
85      get { return (ScopeParameter)Parameters["CurrentScope"]; }
86    }
87
88    public IScope CurrentScope {
89      get { return CurrentScopeParameter.ActualValue; }
90    }
91    #endregion
92
93    [StorableConstructor]
94    private GeneticAlgorithmMainLoop(bool deserializing) : base(deserializing) { }
95    private GeneticAlgorithmMainLoop(GeneticAlgorithmMainLoop original, Cloner cloner)
96      : base(original, cloner) {
97    }
98    public override IDeepCloneable Clone(Cloner cloner) {
99      return new GeneticAlgorithmMainLoop(this, cloner);
100    }
101    public GeneticAlgorithmMainLoop()
102      : base() {
103      Initialize();
104    }
105
106    private void Initialize() {
107      #region Create parameters
108      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
109      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
110      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
111      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
112      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
113      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
114      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
115      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."));
116      Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
117      Parameters.Add(new ValueLookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
118      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
119      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
120      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
121      Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
122      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population."));
123      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied."));
124      #endregion
125
126      #region Create operators
127      VariableCreator variableCreator = new VariableCreator();
128      ResultsCollector resultsCollector1 = new ResultsCollector();
129      Placeholder analyzer1 = new Placeholder();
130      Placeholder selector = new Placeholder();
131      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
132      ChildrenCreator childrenCreator = new ChildrenCreator();
133      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
134      Placeholder crossover = new Placeholder();
135      StochasticBranch stochasticBranch = new StochasticBranch();
136      Placeholder mutator = new Placeholder();
137      SubScopesRemover subScopesRemover = new SubScopesRemover();
138      UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
139      Placeholder evaluator = new Placeholder();
140      SubScopesCounter subScopesCounter = new SubScopesCounter();
141      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
142      BestSelector bestSelector = new BestSelector();
143      RightReducer rightReducer = new RightReducer();
144      MergingReducer mergingReducer = new MergingReducer();
145      IntCounter intCounter = new IntCounter();
146      Comparator comparator = new Comparator();
147      Placeholder analyzer2 = new Placeholder();
148      ConditionalBranch conditionalBranch = new ConditionalBranch();
149      ConditionalBranch reevaluateElitesBranch = new ConditionalBranch();
150      UniformSubScopesProcessor uniformSubScopesProcessor3 = new UniformSubScopesProcessor();
151      Placeholder evaluator2 = new Placeholder();
152      SubScopesCounter subScopesCounter2 = new SubScopesCounter();
153
154      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class GeneticAlgorithm expects this to be called Generations
155
156      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
157      resultsCollector1.ResultsParameter.ActualName = "Results";
158
159      analyzer1.Name = "Analyzer";
160      analyzer1.OperatorParameter.ActualName = "Analyzer";
161
162      selector.Name = "Selector";
163      selector.OperatorParameter.ActualName = "Selector";
164
165      childrenCreator.ParentsPerChild = new IntValue(2);
166
167      crossover.Name = "Crossover";
168      crossover.OperatorParameter.ActualName = "Crossover";
169
170      stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
171      stochasticBranch.RandomParameter.ActualName = "Random";
172
173      mutator.Name = "Mutator";
174      mutator.OperatorParameter.ActualName = "Mutator";
175
176      subScopesRemover.RemoveAllSubScopes = true;
177
178      uniformSubScopesProcessor2.Parallel.Value = true;
179
180      evaluator.Name = "Evaluator";
181      evaluator.OperatorParameter.ActualName = "Evaluator";
182
183      subScopesCounter.Name = "Increment EvaluatedSolutions";
184      subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
185
186      bestSelector.CopySelected = new BoolValue(false);
187      bestSelector.MaximizationParameter.ActualName = "Maximization";
188      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
189      bestSelector.QualityParameter.ActualName = "Quality";
190
191      intCounter.Increment = new IntValue(1);
192      intCounter.ValueParameter.ActualName = "Generations";
193
194      comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
195      comparator.LeftSideParameter.ActualName = "Generations";
196      comparator.ResultParameter.ActualName = "Terminate";
197      comparator.RightSideParameter.ActualName = "MaximumGenerations";
198
199      analyzer2.Name = "Analyzer";
200      analyzer2.OperatorParameter.ActualName = "Analyzer";
201
202      conditionalBranch.ConditionParameter.ActualName = "Terminate";
203
204      reevaluateElitesBranch.ConditionParameter.ActualName = "ReevaluateElites";
205      reevaluateElitesBranch.Name = "Reevaluate elites ?";
206
207      uniformSubScopesProcessor3.Parallel.Value = true;
208
209      evaluator2.Name = "Evaluator";
210      evaluator2.OperatorParameter.ActualName = "Evaluator";
211
212      subScopesCounter2.Name = "Increment EvaluatedSolutions";
213      subScopesCounter2.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
214
215      #endregion
216
217      #region Create operator graph
218      OperatorGraph.InitialOperator = variableCreator;
219      variableCreator.Successor = resultsCollector1;
220      resultsCollector1.Successor = analyzer1;
221      analyzer1.Successor = selector;
222      selector.Successor = subScopesProcessor1;
223      subScopesProcessor1.Operators.Add(new EmptyOperator());
224      subScopesProcessor1.Operators.Add(childrenCreator);
225      subScopesProcessor1.Successor = subScopesProcessor2;
226      childrenCreator.Successor = uniformSubScopesProcessor1;
227      uniformSubScopesProcessor1.Operator = crossover;
228      uniformSubScopesProcessor1.Successor = uniformSubScopesProcessor2;
229      crossover.Successor = stochasticBranch;
230      stochasticBranch.FirstBranch = mutator;
231      stochasticBranch.SecondBranch = null;
232      stochasticBranch.Successor = subScopesRemover;
233      mutator.Successor = null;
234      subScopesRemover.Successor = null;
235      uniformSubScopesProcessor2.Operator = evaluator;
236      uniformSubScopesProcessor2.Successor = subScopesCounter;
237      evaluator.Successor = null;
238      subScopesCounter.Successor = null;
239      subScopesProcessor2.Operators.Add(bestSelector);
240      subScopesProcessor2.Operators.Add(new EmptyOperator());
241      subScopesProcessor2.Successor = mergingReducer;
242      bestSelector.Successor = rightReducer;
243      rightReducer.Successor = reevaluateElitesBranch;
244      reevaluateElitesBranch.TrueBranch = uniformSubScopesProcessor3;
245      uniformSubScopesProcessor3.Operator = evaluator;
246      uniformSubScopesProcessor3.Successor = subScopesCounter2;
247      reevaluateElitesBranch.FalseBranch = null;
248      reevaluateElitesBranch.Successor = null;
249      mergingReducer.Successor = intCounter;
250      intCounter.Successor = comparator;
251      comparator.Successor = analyzer2;
252      analyzer2.Successor = conditionalBranch;
253      conditionalBranch.FalseBranch = selector;
254      conditionalBranch.TrueBranch = null;
255      conditionalBranch.Successor = null;
256      #endregion
257    }
258
259    [StorableHook(HookType.AfterDeserialization)]
260    private void AfterDeserialization() {
261      if (!Parameters.ContainsKey("ReevaluateElites")) {
262        Parameters.Add(new ValueLookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
263      }
264    }
265
266    public override IOperation Apply() {
267      if (CrossoverParameter.ActualValue == null)
268        return null;
269      return base.Apply();
270    }
271  }
272}
Note: See TracBrowser for help on using the repository browser.