Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

  • worked on algorithms and parameters
File size: 10.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      SubScopesSorter subScopesSorter1 = new SubScopesSorter();
99      Placeholder selector = new Placeholder();
100      SequentialSubScopesProcessor sequentialSubScopesProcessor1 = new SequentialSubScopesProcessor();
101      ChildrenCreator childrenCreator = new ChildrenCreator();
102      UniformSequentialSubScopesProcessor uniformSequentialSubScopesProcessor = new UniformSequentialSubScopesProcessor();
103      Placeholder crossover = new Placeholder();
104      StochasticBranch stochasticBranch = new StochasticBranch();
105      Placeholder mutator = new Placeholder();
106      Placeholder evaluator = new Placeholder();
107      SubScopesRemover subScopesRemover = new SubScopesRemover();
108      SubScopesSorter subScopesSorter2 = new SubScopesSorter();
109      SequentialSubScopesProcessor sequentialSubScopesProcessor2 = new SequentialSubScopesProcessor();
110      LeftSelector leftSelector = new LeftSelector();
111      RightReducer rightReducer = new RightReducer();
112      MergingReducer mergingReducer = new MergingReducer();
113      IntCounter intCounter = new IntCounter();
114      Comparator comparator = new Comparator();
115      BestAverageWorstQualityCalculator bestAverageWorstQualityCalculator = new BestAverageWorstQualityCalculator();
116      ResultsCollector resultsCollector = new ResultsCollector();
117      ConditionalBranch conditionalBranch = new ConditionalBranch();
118
119      subScopesSorter1.DescendingParameter.ActualName = "Maximization";
120      subScopesSorter1.ValueParameter.ActualName = "Quality";
121      OperatorGraph.InitialOperator = subScopesSorter1;
122      subScopesSorter1.Successor = selector;
123
124      selector.Name = "Selector";
125      selector.OperatorParameter.ActualName = "Selector";
126      selector.Successor = sequentialSubScopesProcessor1;
127
128      sequentialSubScopesProcessor1.Operators.Add(new EmptyOperator());
129      sequentialSubScopesProcessor1.Operators.Add(childrenCreator);
130      sequentialSubScopesProcessor1.Successor = sequentialSubScopesProcessor2;
131
132      childrenCreator.ParentsPerChild = new IntData(2);
133      childrenCreator.Successor = uniformSequentialSubScopesProcessor;
134
135      uniformSequentialSubScopesProcessor.Operator = crossover;
136      uniformSequentialSubScopesProcessor.Successor = subScopesSorter2;
137
138      crossover.Name = "Crossover";
139      crossover.OperatorParameter.ActualName = "Crossover";
140      crossover.Successor = stochasticBranch;
141
142      stochasticBranch.FirstBranch = mutator;
143      stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
144      stochasticBranch.RandomParameter.ActualName = "Random";
145      stochasticBranch.SecondBranch = null;
146      stochasticBranch.Successor = evaluator;
147
148      mutator.Name = "Mutator";
149      mutator.OperatorParameter.ActualName = "Mutator";
150      mutator.Successor = null;
151
152      evaluator.Name = "Evaluator";
153      evaluator.OperatorParameter.ActualName = "Evaluator";
154      evaluator.Successor = subScopesRemover;
155
156      subScopesRemover.RemoveAllSubScopes = true;
157      subScopesRemover.Successor = null;
158
159      subScopesSorter2.DescendingParameter.ActualName = "Maximization";
160      subScopesSorter2.ValueParameter.ActualName = "Quality";
161      subScopesSorter2.Successor = null;
162
163      sequentialSubScopesProcessor2.Operators.Add(leftSelector);
164      sequentialSubScopesProcessor2.Operators.Add(new EmptyOperator());
165      sequentialSubScopesProcessor2.Successor = mergingReducer;
166
167      leftSelector.CopySelected = new BoolData(false);
168      leftSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
169      leftSelector.Successor = rightReducer;
170
171      rightReducer.Successor = null;
172
173      mergingReducer.Successor = intCounter;
174
175      intCounter.Increment = new IntData(1);
176      intCounter.ValueParameter.ActualName = "Generations";
177      intCounter.Successor = comparator;
178
179      comparator.Comparison = new ComparisonData(Comparison.GreaterOrEqual);
180      comparator.LeftSideParameter.ActualName = "Generations";
181      comparator.ResultParameter.ActualName = "Terminate";
182      comparator.RightSideParameter.ActualName = "MaximumGenerations";
183      comparator.Successor = bestAverageWorstQualityCalculator;
184
185      bestAverageWorstQualityCalculator.AverageQualityParameter.ActualName = "AverageQuality";
186      bestAverageWorstQualityCalculator.BestQualityParameter.ActualName = "BestQuality";
187      bestAverageWorstQualityCalculator.MaximizationParameter.ActualName = "Maximization";
188      bestAverageWorstQualityCalculator.QualityParameter.ActualName = "Quality";
189      bestAverageWorstQualityCalculator.WorstQualityParameter.ActualName = "WorstQuality";
190      bestAverageWorstQualityCalculator.Successor = resultsCollector;
191
192      LookupParameter<DoubleData> bestQuality = new LookupParameter<DoubleData>("BestQuality");
193      bestQuality.ActualName = "BestQuality";
194      resultsCollector.CollectedValues.Add(bestQuality);
195      LookupParameter<DoubleData> averageQuality = new LookupParameter<DoubleData>("AverageQuality");
196      averageQuality.ActualName = "AverageQuality";
197      resultsCollector.CollectedValues.Add(averageQuality);
198      LookupParameter<DoubleData> worstQuality = new LookupParameter<DoubleData>("WorstQuality");
199      worstQuality.ActualName = "WorstQuality";
200      resultsCollector.CollectedValues.Add(worstQuality);
201      resultsCollector.ResultsParameter.ActualName = "Results";
202      resultsCollector.Successor = conditionalBranch;
203
204      conditionalBranch.ConditionParameter.ActualName = "Terminate";
205      conditionalBranch.FalseBranch = subScopesSorter1;
206      conditionalBranch.TrueBranch = null;
207      conditionalBranch.Successor = null;
208      #endregion
209    }
210  }
211}
Note: See TracBrowser for help on using the repository browser.