Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/GeneticAlgorithmMainLoop.cs @ 4656

Last change on this file since 4656 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 10.5 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.Core;
23using HeuristicLab.Data;
24using HeuristicLab.Operators;
25using HeuristicLab.Optimization.Operators;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Selection;
29
30namespace HeuristicLab.Algorithms.GeneticAlgorithm {
31  /// <summary>
32  /// An operator which represents the main loop of a genetic algorithm.
33  /// </summary>
34  [Item("GeneticAlgorithmMainLoop", "An operator which represents the main loop of a genetic algorithm.")]
35  [StorableClass]
36  public sealed class GeneticAlgorithmMainLoop : AlgorithmOperator {
37    #region Parameter properties
38    public ValueLookupParameter<IRandom> RandomParameter {
39      get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
40    }
41    public ValueLookupParameter<BoolValue> MaximizationParameter {
42      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
43    }
44    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
45      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
46    }
47    public ValueLookupParameter<IOperator> SelectorParameter {
48      get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
49    }
50    public ValueLookupParameter<IOperator> CrossoverParameter {
51      get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
52    }
53    public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
54      get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
55    }
56    public ValueLookupParameter<IOperator> MutatorParameter {
57      get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
58    }
59    public ValueLookupParameter<IOperator> EvaluatorParameter {
60      get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
61    }
62    public ValueLookupParameter<IntValue> ElitesParameter {
63      get { return (ValueLookupParameter<IntValue>)Parameters["Elites"]; }
64    }
65    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
66      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
67    }
68    public ValueLookupParameter<VariableCollection> ResultsParameter {
69      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
70    }
71    public ValueLookupParameter<IOperator> AnalyzerParameter {
72      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
73    }
74    private ScopeParameter CurrentScopeParameter {
75      get { return (ScopeParameter)Parameters["CurrentScope"]; }
76    }
77
78    public IScope CurrentScope {
79      get { return CurrentScopeParameter.ActualValue; }
80    }
81    #endregion
82
83    [StorableConstructor]
84    private GeneticAlgorithmMainLoop(bool deserializing) : base() { }
85    public GeneticAlgorithmMainLoop()
86      : base() {
87      Initialize();
88    }
89
90    private void Initialize() {
91      #region Create parameters
92      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
93      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
94      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
95      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
96      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
97      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
98      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
99      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
100      Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
101      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
102      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
103      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
104      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied."));
105      #endregion
106
107      #region Create operators
108      VariableCreator variableCreator = new VariableCreator();
109      ResultsCollector resultsCollector1 = new ResultsCollector();
110      Placeholder analyzer1 = new Placeholder();
111      Placeholder selector = new Placeholder();
112      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
113      ChildrenCreator childrenCreator = new ChildrenCreator();
114      UniformSubScopesProcessor uniformSubScopesProcessor = new UniformSubScopesProcessor();
115      Placeholder crossover = new Placeholder();
116      StochasticBranch stochasticBranch = new StochasticBranch();
117      Placeholder mutator = new Placeholder();
118      Placeholder evaluator = new Placeholder();
119      SubScopesRemover subScopesRemover = new SubScopesRemover();
120      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
121      BestSelector bestSelector = new BestSelector();
122      RightReducer rightReducer = new RightReducer();
123      MergingReducer mergingReducer = new MergingReducer();
124      IntCounter intCounter = new IntCounter();
125      Comparator comparator = new Comparator();
126      ResultsCollector resultsCollector2 = new ResultsCollector();
127      Placeholder analyzer2 = new Placeholder();
128      ConditionalBranch conditionalBranch = new ConditionalBranch();
129
130      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class GeneticAlgorithm expects this to be called Generations
131
132      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
133      resultsCollector1.ResultsParameter.ActualName = "Results";
134
135      analyzer1.Name = "Analyzer";
136      analyzer1.OperatorParameter.ActualName = "Analyzer";
137
138      selector.Name = "Selector";
139      selector.OperatorParameter.ActualName = "Selector";
140
141      childrenCreator.ParentsPerChild = new IntValue(2);
142
143      crossover.Name = "Crossover";
144      crossover.OperatorParameter.ActualName = "Crossover";
145
146      stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
147      stochasticBranch.RandomParameter.ActualName = "Random";
148
149      mutator.Name = "Mutator";
150      mutator.OperatorParameter.ActualName = "Mutator";
151
152      evaluator.Name = "Evaluator";
153      evaluator.OperatorParameter.ActualName = "Evaluator";
154
155      subScopesRemover.RemoveAllSubScopes = true;
156
157      bestSelector.CopySelected = new BoolValue(false);
158      bestSelector.MaximizationParameter.ActualName = "Maximization";
159      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
160      bestSelector.QualityParameter.ActualName = "Quality";
161
162      intCounter.Increment = new IntValue(1);
163      intCounter.ValueParameter.ActualName = "Generations";
164
165      comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
166      comparator.LeftSideParameter.ActualName = "Generations";
167      comparator.ResultParameter.ActualName = "Terminate";
168      comparator.RightSideParameter.ActualName = "MaximumGenerations";
169
170      resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
171      resultsCollector2.ResultsParameter.ActualName = "Results";
172
173      analyzer2.Name = "Analyzer";
174      analyzer2.OperatorParameter.ActualName = "Analyzer";
175
176      conditionalBranch.ConditionParameter.ActualName = "Terminate";
177      #endregion
178
179      #region Create operator graph
180      OperatorGraph.InitialOperator = variableCreator;
181      variableCreator.Successor = resultsCollector1;
182      resultsCollector1.Successor = analyzer1;
183      analyzer1.Successor = selector;
184      selector.Successor = subScopesProcessor1;
185      subScopesProcessor1.Operators.Add(new EmptyOperator());
186      subScopesProcessor1.Operators.Add(childrenCreator);
187      subScopesProcessor1.Successor = subScopesProcessor2;
188      childrenCreator.Successor = uniformSubScopesProcessor;
189      uniformSubScopesProcessor.Operator = crossover;
190      uniformSubScopesProcessor.Successor = null;
191      crossover.Successor = stochasticBranch;
192      stochasticBranch.FirstBranch = mutator;
193      stochasticBranch.SecondBranch = null;
194      stochasticBranch.Successor = evaluator;
195      mutator.Successor = null;
196      evaluator.Successor = subScopesRemover;
197      subScopesRemover.Successor = null;
198      subScopesProcessor2.Operators.Add(bestSelector);
199      subScopesProcessor2.Operators.Add(new EmptyOperator());
200      subScopesProcessor2.Successor = mergingReducer;
201      bestSelector.Successor = rightReducer;
202      rightReducer.Successor = null;
203      mergingReducer.Successor = intCounter;
204      intCounter.Successor = comparator;
205      comparator.Successor = resultsCollector2;
206      resultsCollector2.Successor = analyzer2;
207      analyzer2.Successor = conditionalBranch;
208      conditionalBranch.FalseBranch = selector;
209      conditionalBranch.TrueBranch = null;
210      conditionalBranch.Successor = null;
211      #endregion
212    }
213
214    public override IOperation Apply() {
215      if (CrossoverParameter.ActualValue == null)
216        return null;
217      return base.Apply();
218    }
219  }
220}
Note: See TracBrowser for help on using the repository browser.