Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

  • worked on algorithms
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.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Selection;
29
30namespace HeuristicLab.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      ResultsCollector resultsCollector = new ResultsCollector();
116      ConditionalBranch conditionalBranch = new ConditionalBranch();
117
118      subScopesSorter1.DescendingParameter.ActualName = "Maximization";
119      subScopesSorter1.ValueParameter.ActualName = "Quality";
120      OperatorGraph.InitialOperator = subScopesSorter1;
121      subScopesSorter1.Successor = selector;
122
123      selector.Name = "Selector";
124      selector.OperatorParameter.ActualName = "Selector";
125      selector.Successor = sequentialSubScopesProcessor1;
126
127      sequentialSubScopesProcessor1.Operators.Add(new EmptyOperator());
128      sequentialSubScopesProcessor1.Operators.Add(childrenCreator);
129      sequentialSubScopesProcessor1.Successor = sequentialSubScopesProcessor2;
130
131      childrenCreator.ParentsPerChild = new IntData(2);
132      childrenCreator.Successor = uniformSequentialSubScopesProcessor;
133
134      uniformSequentialSubScopesProcessor.Operator = crossover;
135      uniformSequentialSubScopesProcessor.Successor = subScopesSorter2;
136
137      crossover.Name = "Crossover";
138      crossover.OperatorParameter.ActualName = "Crossover";
139      crossover.Successor = stochasticBranch;
140
141      stochasticBranch.FirstBranch = mutator;
142      stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
143      stochasticBranch.RandomParameter.ActualName = "Random";
144      stochasticBranch.SecondBranch = null;
145      stochasticBranch.Successor = evaluator;
146
147      mutator.Name = "Mutator";
148      mutator.OperatorParameter.ActualName = "Mutator";
149      mutator.Successor = null;
150
151      evaluator.Name = "Evaluator";
152      evaluator.OperatorParameter.ActualName = "Evaluator";
153      evaluator.Successor = subScopesRemover;
154
155      subScopesRemover.RemoveAllSubScopes = true;
156      subScopesRemover.Successor = null;
157
158      subScopesSorter2.DescendingParameter.ActualName = "Maximization";
159      subScopesSorter2.ValueParameter.ActualName = "Quality";
160      subScopesSorter2.Successor = null;
161
162      sequentialSubScopesProcessor2.Operators.Add(leftSelector);
163      sequentialSubScopesProcessor2.Operators.Add(new EmptyOperator());
164      sequentialSubScopesProcessor2.Successor = mergingReducer;
165
166      leftSelector.CopySelected = new BoolData(false);
167      leftSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
168      leftSelector.Successor = rightReducer;
169
170      rightReducer.Successor = null;
171
172      mergingReducer.Successor = intCounter;
173
174      intCounter.Increment = new IntData(1);
175      intCounter.ValueParameter.ActualName = "Generations";
176      intCounter.Successor = comparator;
177
178      comparator.Comparison = new ComparisonData(Comparison.GreaterOrEqual);
179      comparator.LeftSideParameter.ActualName = "Generations";
180      comparator.ResultParameter.ActualName = "Terminate";
181      comparator.RightSideParameter.ActualName = "MaximumGenerations";
182      comparator.Successor = resultsCollector;
183
184      SubScopesLookupParameter<DoubleData> quality = new SubScopesLookupParameter<DoubleData>("Qualities");
185      quality.ActualName = "Quality";
186      resultsCollector.CollectedValues.Add(quality);
187      resultsCollector.ResultsParameter.ActualName = "Results";
188      resultsCollector.Successor = conditionalBranch;
189
190      conditionalBranch.ConditionParameter.ActualName = "Terminate";
191      conditionalBranch.FalseBranch = subScopesSorter1;
192      conditionalBranch.TrueBranch = null;
193      conditionalBranch.Successor = null;
194      #endregion
195    }
196  }
197}
Note: See TracBrowser for help on using the repository browser.