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 |
|
---|
22 | using HeuristicLab.Core;
|
---|
23 | using HeuristicLab.Data;
|
---|
24 | using HeuristicLab.Evolutionary;
|
---|
25 | using HeuristicLab.Operators;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Selection;
|
---|
29 |
|
---|
30 | namespace 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 | [Storable]
|
---|
38 | private ProportionalSelector proportionalSelector;
|
---|
39 |
|
---|
40 | #region Parameter properties
|
---|
41 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
42 | get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
43 | }
|
---|
44 | public ValueLookupParameter<BoolData> MaximizationParameter {
|
---|
45 | get { return (ValueLookupParameter<BoolData>)Parameters["Maximization"]; }
|
---|
46 | }
|
---|
47 | public SubScopesLookupParameter<DoubleData> QualityParameter {
|
---|
48 | get { return (SubScopesLookupParameter<DoubleData>)Parameters["Quality"]; }
|
---|
49 | }
|
---|
50 | public ValueLookupParameter<IOperator> CrossoverOperatorParameter {
|
---|
51 | get { return (ValueLookupParameter<IOperator>)Parameters["CrossoverOperator"]; }
|
---|
52 | }
|
---|
53 | public ValueLookupParameter<DoubleData> MutationProbabilityParameter {
|
---|
54 | get { return (ValueLookupParameter<DoubleData>)Parameters["MutationProbability"]; }
|
---|
55 | }
|
---|
56 | public ValueLookupParameter<IOperator> MutationOperatorParameter {
|
---|
57 | get { return (ValueLookupParameter<IOperator>)Parameters["MutationOperator"]; }
|
---|
58 | }
|
---|
59 | public ValueLookupParameter<IOperator> SolutionEvaluatorParameter {
|
---|
60 | get { return (ValueLookupParameter<IOperator>)Parameters["SolutionEvaluator"]; }
|
---|
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 | private ScopeParameter CurrentScopeParameter {
|
---|
69 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
70 | }
|
---|
71 |
|
---|
72 | public IScope CurrentScope {
|
---|
73 | get { return CurrentScopeParameter.ActualValue; }
|
---|
74 | }
|
---|
75 | #endregion
|
---|
76 |
|
---|
77 | public SGAOperator()
|
---|
78 | : base() {
|
---|
79 | #region Create parameters
|
---|
80 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
81 | Parameters.Add(new ValueLookupParameter<BoolData>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
82 | Parameters.Add(new SubScopesLookupParameter<DoubleData>("Quality", "The value which represents the quality of a solution."));
|
---|
83 | Parameters.Add(new ValueLookupParameter<IOperator>("CrossoverOperator", "The operator used to cross solutions."));
|
---|
84 | Parameters.Add(new ValueLookupParameter<DoubleData>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
|
---|
85 | Parameters.Add(new ValueLookupParameter<IOperator>("MutationOperator", "The operator used to mutate solutions."));
|
---|
86 | Parameters.Add(new ValueLookupParameter<IOperator>("SolutionEvaluator", "The operator used to evaluate solutions."));
|
---|
87 | Parameters.Add(new ValueLookupParameter<IntData>("Elites", "The numer of elite solutions which are kept in each generation."));
|
---|
88 | Parameters.Add(new ValueLookupParameter<IntData>("MaximumGenerations", "The maximum number of generations which should be processed."));
|
---|
89 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the SGA should be applied."));
|
---|
90 | #endregion
|
---|
91 |
|
---|
92 | #region Create operator graph
|
---|
93 | SubScopesSorter subScopesSorter1 = new SubScopesSorter();
|
---|
94 | proportionalSelector = new ProportionalSelector();
|
---|
95 | SequentialSubScopesProcessor sequentialSubScopesProcessor1 = new SequentialSubScopesProcessor();
|
---|
96 | ChildrenCreator childrenCreator = new ChildrenCreator();
|
---|
97 | UniformSequentialSubScopesProcessor uniformSequentialSubScopesProcessor = new UniformSequentialSubScopesProcessor();
|
---|
98 | Placeholder crossover = new Placeholder();
|
---|
99 | StochasticBranch stochasticBranch = new StochasticBranch();
|
---|
100 | Placeholder mutator = new Placeholder();
|
---|
101 | Placeholder evaluator = new Placeholder();
|
---|
102 | SubScopesRemover subScopesRemover = new SubScopesRemover();
|
---|
103 | SubScopesSorter subScopesSorter2 = new SubScopesSorter();
|
---|
104 | SequentialSubScopesProcessor sequentialSubScopesProcessor2 = new SequentialSubScopesProcessor();
|
---|
105 | LeftSelector leftSelector = new LeftSelector();
|
---|
106 | RightReducer rightReducer = new RightReducer();
|
---|
107 | MergingReducer mergingReducer = new MergingReducer();
|
---|
108 | IntCounter intCounter = new IntCounter();
|
---|
109 | Comparator comparator = new Comparator();
|
---|
110 | ConditionalBranch conditionalBranch = new ConditionalBranch();
|
---|
111 |
|
---|
112 | subScopesSorter1.DescendingParameter.ActualName = "Maximization";
|
---|
113 | subScopesSorter1.ValueParameter.ActualName = "Quality";
|
---|
114 | OperatorGraph.InitialOperator = subScopesSorter1;
|
---|
115 | subScopesSorter1.Successor = proportionalSelector;
|
---|
116 |
|
---|
117 | proportionalSelector.CopySelected = new BoolData(true);
|
---|
118 | proportionalSelector.MaximizationParameter.ActualName = "Maximization";
|
---|
119 | // NOTE: NumberOfSelectedSubScopes is set dynamically when the operator is executed
|
---|
120 | proportionalSelector.QualityParameter.ActualName = "Quality";
|
---|
121 | proportionalSelector.RandomParameter.ActualName = "Random";
|
---|
122 | proportionalSelector.Windowing = new BoolData(true);
|
---|
123 | proportionalSelector.Successor = sequentialSubScopesProcessor1;
|
---|
124 |
|
---|
125 | sequentialSubScopesProcessor1.Operators.Add(new EmptyOperator());
|
---|
126 | sequentialSubScopesProcessor1.Operators.Add(childrenCreator);
|
---|
127 | sequentialSubScopesProcessor1.Successor = sequentialSubScopesProcessor2;
|
---|
128 |
|
---|
129 | childrenCreator.ParentsPerChild = new IntData(2);
|
---|
130 | childrenCreator.Successor = uniformSequentialSubScopesProcessor;
|
---|
131 |
|
---|
132 | uniformSequentialSubScopesProcessor.Operator = crossover;
|
---|
133 | uniformSequentialSubScopesProcessor.Successor = subScopesSorter2;
|
---|
134 |
|
---|
135 | crossover.Name = "CrossoverOperator";
|
---|
136 | crossover.OperatorParameter.ActualName = "CrossoverOperator";
|
---|
137 | crossover.Successor = stochasticBranch;
|
---|
138 |
|
---|
139 | stochasticBranch.FirstBranch = mutator;
|
---|
140 | stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
|
---|
141 | stochasticBranch.RandomParameter.ActualName = "Random";
|
---|
142 | stochasticBranch.SecondBranch = null;
|
---|
143 | stochasticBranch.Successor = evaluator;
|
---|
144 |
|
---|
145 | mutator.Name = "MutationOperator";
|
---|
146 | mutator.OperatorParameter.ActualName = "MutationOperator";
|
---|
147 | mutator.Successor = null;
|
---|
148 |
|
---|
149 | evaluator.Name = "SolutionEvaluator";
|
---|
150 | evaluator.OperatorParameter.ActualName = "SolutionEvaluator";
|
---|
151 | evaluator.Successor = subScopesRemover;
|
---|
152 |
|
---|
153 | subScopesRemover.RemoveAllSubScopes = true;
|
---|
154 | subScopesRemover.Successor = null;
|
---|
155 |
|
---|
156 | subScopesSorter2.DescendingParameter.ActualName = "Maximization";
|
---|
157 | subScopesSorter2.ValueParameter.ActualName = "Quality";
|
---|
158 | subScopesSorter2.Successor = null;
|
---|
159 |
|
---|
160 | sequentialSubScopesProcessor2.Operators.Add(leftSelector);
|
---|
161 | sequentialSubScopesProcessor2.Operators.Add(new EmptyOperator());
|
---|
162 | sequentialSubScopesProcessor2.Successor = mergingReducer;
|
---|
163 |
|
---|
164 | leftSelector.CopySelected = new BoolData(false);
|
---|
165 | leftSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
|
---|
166 | leftSelector.Successor = rightReducer;
|
---|
167 |
|
---|
168 | rightReducer.Successor = null;
|
---|
169 |
|
---|
170 | mergingReducer.Successor = intCounter;
|
---|
171 |
|
---|
172 | intCounter.Increment = new IntData(1);
|
---|
173 | intCounter.ValueParameter.ActualName = "Generations";
|
---|
174 | intCounter.Successor = comparator;
|
---|
175 |
|
---|
176 | comparator.Comparison = new ComparisonData(Comparison.GreaterOrEqual);
|
---|
177 | comparator.LeftSideParameter.ActualName = "Generations";
|
---|
178 | comparator.ResultParameter.ActualName = "Terminate";
|
---|
179 | comparator.RightSideParameter.ActualName = "MaximumGenerations";
|
---|
180 | comparator.Successor = conditionalBranch;
|
---|
181 |
|
---|
182 | conditionalBranch.ConditionParameter.ActualName = "Terminate";
|
---|
183 | conditionalBranch.FalseBranch = subScopesSorter1;
|
---|
184 | conditionalBranch.TrueBranch = null;
|
---|
185 | conditionalBranch.Successor = null;
|
---|
186 | #endregion
|
---|
187 | }
|
---|
188 |
|
---|
189 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
190 | SGAOperator clone = (SGAOperator)base.Clone(cloner);
|
---|
191 | clone.proportionalSelector = (ProportionalSelector)cloner.Clone(proportionalSelector);
|
---|
192 | return clone;
|
---|
193 | }
|
---|
194 |
|
---|
195 | public override IOperation Apply() {
|
---|
196 | int populationSize = CurrentScope.SubScopes.Count;
|
---|
197 | // dynamically set the number of parents which are selected for reproduction
|
---|
198 | proportionalSelector.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (populationSize - ElitesParameter.ActualValue.Value));
|
---|
199 | return base.Apply();
|
---|
200 | }
|
---|
201 | }
|
---|
202 | }
|
---|