Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

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