Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.SGA/3.3/SGAOperator.cs @ 2975

Last change on this file since 2975 was 2924, checked in by swagner, 14 years ago

Operator architecture refactoring (#95)

  • implemented reviewers' comments on version r2917.
File size: 11.6 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.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Evolutionary;
26using HeuristicLab.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Selection;
29
30namespace HeuristicLab.Algorithms.SGA {
31  /// <summary>
32  /// An operator which represents a Standard Genetic Algorithm.
33  /// </summary>
34  [Item("SGAOperator", "An operator which represents a Standard Genetic Algorithm.")]
35  [Creatable("Test")]
36  public class SGAOperator : AlgorithmOperator {
37    #region Parameter properties
38    public ValueLookupParameter<IRandom> RandomParameter {
39      get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
40    }
41    public ValueLookupParameter<BoolData> MaximizationParameter {
42      get { return (ValueLookupParameter<BoolData>)Parameters["Maximization"]; }
43    }
44    public SubScopesLookupParameter<DoubleData> QualityParameter {
45      get { return (SubScopesLookupParameter<DoubleData>)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<DoubleData> MutationProbabilityParameter {
54      get { return (ValueLookupParameter<DoubleData>)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<IntData> ElitesParameter {
63      get { return (ValueLookupParameter<IntData>)Parameters["Elites"]; }
64    }
65    public ValueLookupParameter<IntData> MaximumGenerationsParameter {
66      get { return (ValueLookupParameter<IntData>)Parameters["MaximumGenerations"]; }
67    }
68    public ValueLookupParameter<VariableCollection> ResultsParameter {
69      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
70    }
71    private ScopeParameter CurrentScopeParameter {
72      get { return (ScopeParameter)Parameters["CurrentScope"]; }
73    }
74
75    public IScope CurrentScope {
76      get { return CurrentScopeParameter.ActualValue; }
77    }
78    #endregion
79
80    public SGAOperator()
81      : base() {
82      #region Create parameters
83      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
84      Parameters.Add(new ValueLookupParameter<BoolData>("Maximization", "True if the problem is a maximization problem, otherwise false."));
85      Parameters.Add(new SubScopesLookupParameter<DoubleData>("Quality", "The value which represents the quality of a solution."));
86      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
87      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
88      Parameters.Add(new ValueLookupParameter<DoubleData>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
89      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
90      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
91      Parameters.Add(new ValueLookupParameter<IntData>("Elites", "The numer of elite solutions which are kept in each generation."));
92      Parameters.Add(new ValueLookupParameter<IntData>("MaximumGenerations", "The maximum number of generations which should be processed."));
93      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
94      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the SGA should be applied."));
95      #endregion
96
97      #region Create operator graph
98      VariableCreator variableCreator = new VariableCreator();
99      ResultsCollector resultsCollector = new ResultsCollector();
100      SubScopesSorter subScopesSorter1 = new SubScopesSorter();
101      Placeholder selector = new Placeholder();
102      SequentialSubScopesProcessor sequentialSubScopesProcessor1 = new SequentialSubScopesProcessor();
103      ChildrenCreator childrenCreator = new ChildrenCreator();
104      UniformSequentialSubScopesProcessor uniformSequentialSubScopesProcessor = new UniformSequentialSubScopesProcessor();
105      Placeholder crossover = new Placeholder();
106      StochasticBranch stochasticBranch = new StochasticBranch();
107      Placeholder mutator = new Placeholder();
108      Placeholder evaluator = new Placeholder();
109      SubScopesRemover subScopesRemover = new SubScopesRemover();
110      SubScopesSorter subScopesSorter2 = new SubScopesSorter();
111      SequentialSubScopesProcessor sequentialSubScopesProcessor2 = new SequentialSubScopesProcessor();
112      LeftSelector leftSelector = new LeftSelector();
113      RightReducer rightReducer = new RightReducer();
114      MergingReducer mergingReducer = new MergingReducer();
115      IntCounter intCounter = new IntCounter();
116      Comparator comparator = new Comparator();
117      BestAverageWorstQualityCalculator bestAverageWorstQualityCalculator = new BestAverageWorstQualityCalculator();
118      DataTableValuesCollector dataTableValuesCollector = new DataTableValuesCollector();
119      ConditionalBranch conditionalBranch = new ConditionalBranch();
120
121      OperatorGraph.InitialOperator = variableCreator;
122
123      variableCreator.CollectedValues.Add(new ValueParameter<IntData>("Generations", new IntData(0)));
124      variableCreator.CollectedValues.Add(new ValueParameter<DoubleData>("Best Quality", new DoubleData(0)));
125      variableCreator.CollectedValues.Add(new ValueParameter<DoubleData>("Average Quality", new DoubleData(0)));
126      variableCreator.CollectedValues.Add(new ValueParameter<DoubleData>("Worst Quality", new DoubleData(0)));
127      variableCreator.CollectedValues.Add(new ValueParameter<DataTable>("Qualities", new DataTable("Qualities")));
128      variableCreator.Successor = resultsCollector;
129
130      resultsCollector.CollectedValues.Add(new LookupParameter<IntData>("Generations"));
131      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleData>("Best Quality"));
132      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleData>("Average Quality"));
133      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleData>("Worst Quality"));
134      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Qualities"));
135      resultsCollector.ResultsParameter.ActualName = "Results";
136      resultsCollector.Successor = subScopesSorter1;
137
138      subScopesSorter1.DescendingParameter.ActualName = "Maximization";
139      subScopesSorter1.ValueParameter.ActualName = "Quality";
140      subScopesSorter1.Successor = selector;
141
142      selector.Name = "Selector";
143      selector.OperatorParameter.ActualName = "Selector";
144      selector.Successor = sequentialSubScopesProcessor1;
145
146      sequentialSubScopesProcessor1.Operators.Add(new EmptyOperator());
147      sequentialSubScopesProcessor1.Operators.Add(childrenCreator);
148      sequentialSubScopesProcessor1.Successor = sequentialSubScopesProcessor2;
149
150      childrenCreator.ParentsPerChild = new IntData(2);
151      childrenCreator.Successor = uniformSequentialSubScopesProcessor;
152
153      uniformSequentialSubScopesProcessor.Operator = crossover;
154      uniformSequentialSubScopesProcessor.Successor = subScopesSorter2;
155
156      crossover.Name = "Crossover";
157      crossover.OperatorParameter.ActualName = "Crossover";
158      crossover.Successor = stochasticBranch;
159
160      stochasticBranch.FirstBranch = mutator;
161      stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
162      stochasticBranch.RandomParameter.ActualName = "Random";
163      stochasticBranch.SecondBranch = null;
164      stochasticBranch.Successor = evaluator;
165
166      mutator.Name = "Mutator";
167      mutator.OperatorParameter.ActualName = "Mutator";
168      mutator.Successor = null;
169
170      evaluator.Name = "Evaluator";
171      evaluator.OperatorParameter.ActualName = "Evaluator";
172      evaluator.Successor = subScopesRemover;
173
174      subScopesRemover.RemoveAllSubScopes = true;
175      subScopesRemover.Successor = null;
176
177      subScopesSorter2.DescendingParameter.ActualName = "Maximization";
178      subScopesSorter2.ValueParameter.ActualName = "Quality";
179      subScopesSorter2.Successor = null;
180
181      sequentialSubScopesProcessor2.Operators.Add(leftSelector);
182      sequentialSubScopesProcessor2.Operators.Add(new EmptyOperator());
183      sequentialSubScopesProcessor2.Successor = mergingReducer;
184
185      leftSelector.CopySelected = new BoolData(false);
186      leftSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
187      leftSelector.Successor = rightReducer;
188
189      rightReducer.Successor = null;
190
191      mergingReducer.Successor = intCounter;
192
193      intCounter.Increment = new IntData(1);
194      intCounter.ValueParameter.ActualName = "Generations";
195      intCounter.Successor = comparator;
196
197      comparator.Comparison = new ComparisonData(Comparison.GreaterOrEqual);
198      comparator.LeftSideParameter.ActualName = "Generations";
199      comparator.ResultParameter.ActualName = "Terminate";
200      comparator.RightSideParameter.ActualName = "MaximumGenerations";
201      comparator.Successor = bestAverageWorstQualityCalculator;
202
203      bestAverageWorstQualityCalculator.AverageQualityParameter.ActualName = "Average Quality";
204      bestAverageWorstQualityCalculator.BestQualityParameter.ActualName = "Best Quality";
205      bestAverageWorstQualityCalculator.MaximizationParameter.ActualName = "Maximization";
206      bestAverageWorstQualityCalculator.QualityParameter.ActualName = "Quality";
207      bestAverageWorstQualityCalculator.WorstQualityParameter.ActualName = "Worst Quality";
208      bestAverageWorstQualityCalculator.Successor = dataTableValuesCollector;
209
210      dataTableValuesCollector.CollectedValues.Add(new LookupParameter<DoubleData>("Best Quality"));
211      dataTableValuesCollector.CollectedValues.Add(new LookupParameter<DoubleData>("Average Quality"));
212      dataTableValuesCollector.CollectedValues.Add(new LookupParameter<DoubleData>("Worst Quality"));
213      dataTableValuesCollector.DataTableParameter.ActualName = "Qualities";
214      dataTableValuesCollector.Successor = conditionalBranch;
215
216      conditionalBranch.ConditionParameter.ActualName = "Terminate";
217      conditionalBranch.FalseBranch = subScopesSorter1;
218      conditionalBranch.TrueBranch = null;
219      conditionalBranch.Successor = null;
220      #endregion
221    }
222  }
223}
Note: See TracBrowser for help on using the repository browser.