Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3616 was 3616, checked in by swagner, 15 years ago

Worked on refactoring of algorithm analysis and tracing (#999)

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