Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3017 was 3017, checked in by epitzer, 14 years ago

Merge StorableClassType.Empty into StorableClassType.MarkedOnly and make it the default if not specified (#548)

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