1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Optimization.Operators;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.PluginInfrastructure;
|
---|
33 | using HeuristicLab.Random;
|
---|
34 | using HeuristicLab.Selection;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Algorithms.ALPS.SteadyState {
|
---|
37 | [Item("ALPS Steady-State Genetic Algorithm", "A genetic algorithmn with a steady-state age-layered population structure")]
|
---|
38 | [Creatable("Algorithms")]
|
---|
39 | [StorableClass]
|
---|
40 | public class AlpsSsGeneticAlgorithm : Alps {
|
---|
41 | #region Parameter Properties
|
---|
42 | private IValueParameter<IntValue> LayerSizeParameter {
|
---|
43 | get { return (IValueParameter<IntValue>)Parameters["LayerSize"]; }
|
---|
44 | }
|
---|
45 | private IValueParameter<IntValue> MaximumIterationsParameter {
|
---|
46 | get { return (IValueParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
47 | }
|
---|
48 | public IConstrainedValueParameter<ISelector> SelectorParameter {
|
---|
49 | get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
|
---|
50 | }
|
---|
51 | public IConstrainedValueParameter<ICrossover> CrossoverParameter {
|
---|
52 | get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
|
---|
53 | }
|
---|
54 | private IValueParameter<PercentValue> MutationProbabilityParameter {
|
---|
55 | get { return (IValueParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
56 | }
|
---|
57 | public IConstrainedValueParameter<IManipulator> MutatorParameter {
|
---|
58 | get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
|
---|
59 | }
|
---|
60 | private IValueParameter<IntValue> ElitesParameter {
|
---|
61 | get { return (IValueParameter<IntValue>)Parameters["Elites"]; }
|
---|
62 | }
|
---|
63 | private IFixedValueParameter<BoolValue> ReevaluateElitesParameter {
|
---|
64 | get { return (IFixedValueParameter<BoolValue>)Parameters["ReevaluateElites"]; }
|
---|
65 | }
|
---|
66 | private IValueParameter<IntValue> BatchSizeParameter {
|
---|
67 | get { return (IValueParameter<IntValue>)Parameters["BatchSize"]; }
|
---|
68 | }
|
---|
69 | #endregion
|
---|
70 |
|
---|
71 | #region Properties
|
---|
72 | public IntValue LayerSize {
|
---|
73 | get { return LayerSizeParameter.Value; }
|
---|
74 | set { LayerSizeParameter.Value = value; }
|
---|
75 | }
|
---|
76 | public IntValue MaximumIterations {
|
---|
77 | get { return MaximumIterationsParameter.Value; }
|
---|
78 | set { MaximumIterationsParameter.Value = value; }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public ISelector Selector {
|
---|
82 | get { return SelectorParameter.Value; }
|
---|
83 | set { SelectorParameter.Value = value; }
|
---|
84 | }
|
---|
85 | public ICrossover Crossover {
|
---|
86 | get { return CrossoverParameter.Value; }
|
---|
87 | set { CrossoverParameter.Value = value; }
|
---|
88 | }
|
---|
89 | public PercentValue MutationProbability {
|
---|
90 | get { return MutationProbabilityParameter.Value; }
|
---|
91 | set { MutationProbabilityParameter.Value = value; }
|
---|
92 | }
|
---|
93 | public IManipulator Mutator {
|
---|
94 | get { return MutatorParameter.Value; }
|
---|
95 | set { MutatorParameter.Value = value; }
|
---|
96 | }
|
---|
97 | public IntValue Elites {
|
---|
98 | get { return ElitesParameter.Value; }
|
---|
99 | set { ElitesParameter.Value = value; }
|
---|
100 | }
|
---|
101 | public bool ReevaluteElites {
|
---|
102 | get { return ReevaluateElitesParameter.Value.Value; }
|
---|
103 | set { ReevaluateElitesParameter.Value.Value = value; }
|
---|
104 | }
|
---|
105 | #endregion
|
---|
106 |
|
---|
107 | private AlpsSsGeneticAlgorithmMainLoop MainLoop {
|
---|
108 | get { return OperatorGraph.Iterate().OfType<AlpsSsGeneticAlgorithmMainLoop>().First(); }
|
---|
109 | }
|
---|
110 |
|
---|
111 | [StorableConstructor]
|
---|
112 | private AlpsSsGeneticAlgorithm(bool deserializing)
|
---|
113 | : base(deserializing) { }
|
---|
114 | private AlpsSsGeneticAlgorithm(AlpsSsGeneticAlgorithm original, Cloner cloner)
|
---|
115 | : base(original, cloner) {
|
---|
116 | Initialize();
|
---|
117 | }
|
---|
118 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
119 | return new AlpsSsGeneticAlgorithm(this, cloner);
|
---|
120 | }
|
---|
121 |
|
---|
122 | public AlpsSsGeneticAlgorithm()
|
---|
123 | : base() {
|
---|
124 | Parameters.Add(new ValueParameter<IntValue>("LayerSize", "The size of the population of solutions each layer.", new IntValue(100)));
|
---|
125 | Parameters.Add(new ValueParameter<IntValue>("MaximumIterations", "The maximum number of iterations that should be processed.", new IntValue(1000)));
|
---|
126 | Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
|
---|
127 | Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
|
---|
128 | Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
|
---|
129 | Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
130 | Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
|
---|
131 | Parameters.Add(new FixedValueParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)", new BoolValue(false)) { Hidden = true });
|
---|
132 | Parameters.Add(new ValueParameter<IntValue>("BatchSize", "Number of inner iterations before updates and analyzers are fired.", new IntValue(100)) { Hidden = true });
|
---|
133 |
|
---|
134 | var randomCreator = new RandomCreator();
|
---|
135 | var workingScopeCreator = new NamedSubScopesCreator() { Name = "Create Working- and Layers-Scope" };
|
---|
136 | var layersProcessor = new NamedSubScopeProcessor() { Name = "Process Layers-Scope" };
|
---|
137 | var layerCreator = new SubScopesCreator() { Name = "Create Layers" };
|
---|
138 | var layerProcessor = new UniformSubScopesProcessor();
|
---|
139 | var layerVariableCreator = new VariableCreator() { Name = "Initialize Layer" };
|
---|
140 | var layerNumberCreator = new ScopeIndexAssigner() { Name = "Create Layer Number" };
|
---|
141 | var layerSolutionsCreator = new SolutionsCreator();
|
---|
142 | var initializeAgeProcessor = new UniformSubScopesProcessor();
|
---|
143 | var initializeAge = new VariableCreator() { Name = "Initialize Age" };
|
---|
144 | var initializeLocalEvaluatedSolutions = new ExpressionCalculator() { Name = "Initialize local EvaluatedSolutions" };
|
---|
145 | var initializeEvaluatedSolutions = new Assigner() { Name = "Initialize EvaluatedSolutions" };
|
---|
146 | var initializePopulationSize = new Assigner() { Name = "Initialize PopulationSize" };
|
---|
147 | var resultsCollector = new ResultsCollector();
|
---|
148 | var mainLoop = new AlpsSsGeneticAlgorithmMainLoop();
|
---|
149 |
|
---|
150 | OperatorGraph.InitialOperator = randomCreator;
|
---|
151 |
|
---|
152 | randomCreator.SeedParameter.Value = null;
|
---|
153 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
154 | randomCreator.Successor = workingScopeCreator;
|
---|
155 |
|
---|
156 | workingScopeCreator.NamesParameter.Value = new StringArray(new[] { "WorkingScope", "LayersScope", });
|
---|
157 | workingScopeCreator.Successor = layersProcessor;
|
---|
158 |
|
---|
159 | layersProcessor.TargetScopeParameter.ActualName = "LayersScope";
|
---|
160 | layersProcessor.Operator = layerCreator;
|
---|
161 | layersProcessor.Successor = initializeLocalEvaluatedSolutions;
|
---|
162 |
|
---|
163 | layerCreator.NumberOfSubScopesParameter.ActualName = "NumberOfLayers";
|
---|
164 | layerCreator.NumberOfSubScopesParameter.Value = null;
|
---|
165 | layerCreator.Successor = layerProcessor;
|
---|
166 |
|
---|
167 | layerProcessor.Operator = layerVariableCreator;
|
---|
168 |
|
---|
169 | layerVariableCreator.CollectedValues.Add(new ValueParameter<ResultCollection>("LayerResults"));
|
---|
170 | layerVariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Layer"));
|
---|
171 | layerVariableCreator.Successor = layerNumberCreator;
|
---|
172 |
|
---|
173 | layerNumberCreator.ValueParameter.ActualName = "Layer";
|
---|
174 | layerNumberCreator.Successor = layerSolutionsCreator;
|
---|
175 |
|
---|
176 | layerSolutionsCreator.NumberOfSolutionsParameter.ActualName = LayerSizeParameter.Name;
|
---|
177 | layerSolutionsCreator.Successor = initializeAgeProcessor;
|
---|
178 |
|
---|
179 | initializeAgeProcessor.Operator = initializeAge;
|
---|
180 |
|
---|
181 | initializeAge.CollectedValues.Add(new ValueParameter<IntValue>("EvalsCreated", new IntValue(1)));
|
---|
182 | initializeAge.CollectedValues.Add(new ValueParameter<IntValue>("LastMove", new IntValue(1)));
|
---|
183 |
|
---|
184 | initializeLocalEvaluatedSolutions.ExpressionResultParameter.ActualName = "LocalEvaluatedSolutions";
|
---|
185 | initializeLocalEvaluatedSolutions.ExpressionParameter.Value = new StringValue("LayerSize NumberOfLayers * toint");
|
---|
186 | initializeLocalEvaluatedSolutions.CollectedValues.Add(new LookupParameter<IntValue>("LayerSize"));
|
---|
187 | initializeLocalEvaluatedSolutions.CollectedValues.Add(new LookupParameter<IntValue>("NumberOfLayers"));
|
---|
188 | initializeLocalEvaluatedSolutions.Successor = initializeEvaluatedSolutions;
|
---|
189 |
|
---|
190 | initializeEvaluatedSolutions.LeftSideParameter.ActualName = "EvaluatedSolutions";
|
---|
191 | initializeEvaluatedSolutions.RightSideParameter.ActualName = "LocalEvaluatedSolutions";
|
---|
192 | initializeEvaluatedSolutions.Successor = initializePopulationSize;
|
---|
193 |
|
---|
194 | initializePopulationSize.LeftSideParameter.ActualName = "PopulationSize";
|
---|
195 | initializePopulationSize.RightSideParameter.ActualName = "EvaluatedSolutions";
|
---|
196 | initializePopulationSize.Successor = resultsCollector;
|
---|
197 |
|
---|
198 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("EvaluatedSolutions"));
|
---|
199 | resultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("LayerResults", "Result set for each layer", "LayerResults", 2));
|
---|
200 | resultsCollector.Successor = mainLoop;
|
---|
201 |
|
---|
202 | foreach (var selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(s => !(s is IMultiObjectiveSelector)).OrderBy(s => Name))
|
---|
203 | SelectorParameter.ValidValues.Add(selector);
|
---|
204 | var tournamentSelector = SelectorParameter.ValidValues.OfType<TournamentSelector>().FirstOrDefault();
|
---|
205 | if (tournamentSelector != null) {
|
---|
206 | tournamentSelector.GroupSizeParameter.Value = new IntValue(4);
|
---|
207 | SelectorParameter.Value = tournamentSelector;
|
---|
208 | }
|
---|
209 |
|
---|
210 | ParameterizeSelectors();
|
---|
211 | Initialize();
|
---|
212 | }
|
---|
213 |
|
---|
214 | #region Events
|
---|
215 | protected override void OnProblemChanged() {
|
---|
216 | base.OnProblemChanged();
|
---|
217 | ParameterizeSolutionsCreator();
|
---|
218 | ParameterizeMainLoop();
|
---|
219 | ParameterizeSelectors();
|
---|
220 | ParameterizeIterationBasedOperators();
|
---|
221 | UpdateCrossovers();
|
---|
222 | UpdateMutators();
|
---|
223 | }
|
---|
224 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
225 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
226 | ParameterizeSolutionsCreator();
|
---|
227 | }
|
---|
228 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
229 | base.Problem_EvaluatorChanged(sender, e);
|
---|
230 | ParameterizeSolutionsCreator();
|
---|
231 | ParameterizeMainLoop();
|
---|
232 | ParameterizeSelectors();
|
---|
233 | }
|
---|
234 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
235 | base.Problem_OperatorsChanged(sender, e);
|
---|
236 | ParameterizeIterationBasedOperators();
|
---|
237 | UpdateCrossovers();
|
---|
238 | UpdateMutators();
|
---|
239 | }
|
---|
240 | protected override void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
241 | base.Evaluator_QualityParameter_ActualNameChanged(sender, e);
|
---|
242 | ParameterizeMainLoop();
|
---|
243 | ParameterizeSelectors();
|
---|
244 | }
|
---|
245 | #endregion
|
---|
246 |
|
---|
247 | #region Parameterization
|
---|
248 | private void Initialize() {
|
---|
249 | }
|
---|
250 | private void ParameterizeSolutionsCreator() {
|
---|
251 | }
|
---|
252 | private void ParameterizeMainLoop() {
|
---|
253 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
254 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
255 | }
|
---|
256 | private void ParameterizeSelectors() {
|
---|
257 | foreach (var selector in SelectorParameter.ValidValues) {
|
---|
258 | selector.CopySelected = new BoolValue(true);
|
---|
259 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2);
|
---|
260 | selector.NumberOfSelectedSubScopesParameter.Hidden = true;
|
---|
261 | ParameterizeStochasticOperatorForLayer(selector);
|
---|
262 | }
|
---|
263 | if (Problem != null) {
|
---|
264 | foreach (var selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
265 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
266 | selector.MaximizationParameter.Hidden = true;
|
---|
267 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
268 | selector.QualityParameter.Hidden = true;
|
---|
269 | }
|
---|
270 | }
|
---|
271 | }
|
---|
272 | private void ParameterizeIterationBasedOperators() {
|
---|
273 | if (Problem != null) {
|
---|
274 | foreach (var @operator in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
275 | @operator.IterationsParameter.ActualName = "Iteration";
|
---|
276 | @operator.IterationsParameter.Hidden = true;
|
---|
277 | @operator.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
278 | @operator.MaximumIterationsParameter.Hidden = true;
|
---|
279 | }
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | protected override ReductionOperations GetAgeInheritanceReduction(AgeInheritance ageInheritance) {
|
---|
284 | switch (ageInheritance) {
|
---|
285 | case ALPS.AgeInheritance.Older: return ReductionOperations.Min;
|
---|
286 | case ALPS.AgeInheritance.Agerage: return ReductionOperations.Avg;
|
---|
287 | case ALPS.AgeInheritance.Younger: return ReductionOperations.Max;
|
---|
288 | default: throw new NotSupportedException("AgeInheritance " + ageInheritance + " is not supported.");
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | protected override void ParameterizeStochasticOperator(IOperator @operator) {
|
---|
293 | var stochasticOperator = @operator as IStochasticOperator;
|
---|
294 | if (stochasticOperator != null) {
|
---|
295 | stochasticOperator.RandomParameter.ActualName = "Random";
|
---|
296 | stochasticOperator.RandomParameter.Hidden = true;
|
---|
297 | }
|
---|
298 | }
|
---|
299 | protected override void ParameterizeStochasticOperatorForLayer(IOperator @operator) {
|
---|
300 | var stochasticOperator = @operator as IStochasticOperator;
|
---|
301 | if (stochasticOperator != null) {
|
---|
302 | stochasticOperator.RandomParameter.ActualName = "Random";
|
---|
303 | stochasticOperator.RandomParameter.Hidden = true;
|
---|
304 | }
|
---|
305 | }
|
---|
306 | #endregion
|
---|
307 |
|
---|
308 | #region Updates
|
---|
309 | private void UpdateCrossovers() {
|
---|
310 | var oldCrossover = CrossoverParameter.Value;
|
---|
311 | var defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
|
---|
312 | CrossoverParameter.ValidValues.Clear();
|
---|
313 | foreach (var crossover in Problem.Operators.OfType<ICrossover>().OrderBy(c => c.Name)) {
|
---|
314 | ParameterizeStochasticOperatorForLayer(crossover);
|
---|
315 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
316 | }
|
---|
317 | if (oldCrossover != null) {
|
---|
318 | var crossover = CrossoverParameter.ValidValues.FirstOrDefault(c => c.GetType() == oldCrossover.GetType());
|
---|
319 | if (crossover != null)
|
---|
320 | CrossoverParameter.Value = crossover;
|
---|
321 | else
|
---|
322 | oldCrossover = null;
|
---|
323 | }
|
---|
324 | if (oldCrossover == null && defaultCrossover != null)
|
---|
325 | CrossoverParameter.Value = defaultCrossover;
|
---|
326 | }
|
---|
327 | private void UpdateMutators() {
|
---|
328 | var oldMutator = MutatorParameter.Value;
|
---|
329 | MutatorParameter.ValidValues.Clear();
|
---|
330 | foreach (var mutator in Problem.Operators.OfType<IManipulator>().OrderBy(m => m.Name)) {
|
---|
331 | ParameterizeStochasticOperatorForLayer(mutator);
|
---|
332 | MutatorParameter.ValidValues.Add(mutator);
|
---|
333 | }
|
---|
334 | if (oldMutator != null) {
|
---|
335 | var mutator = MutatorParameter.ValidValues.FirstOrDefault(m => m.GetType() == oldMutator.GetType());
|
---|
336 | if (mutator != null)
|
---|
337 | MutatorParameter.Value = mutator;
|
---|
338 | }
|
---|
339 | }
|
---|
340 | #endregion
|
---|
341 | }
|
---|
342 | } |
---|