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<IntArray> PopulationSizeParameter {
|
---|
43 | get { return (IValueParameter<IntArray>)Parameters["PopulationSize"]; }
|
---|
44 | }
|
---|
45 | private IValueParameter<IntValue> MaximumGenerationsParameter {
|
---|
46 | get { return (IValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
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 | #endregion
|
---|
67 |
|
---|
68 | #region Properties
|
---|
69 | public IntArray PopulationSize {
|
---|
70 | get { return PopulationSizeParameter.Value; }
|
---|
71 | set { PopulationSizeParameter.Value = value; }
|
---|
72 | }
|
---|
73 | public IntValue MaximumGenerations {
|
---|
74 | get { return MaximumGenerationsParameter.Value; }
|
---|
75 | set { MaximumGenerationsParameter.Value = value; }
|
---|
76 | }
|
---|
77 |
|
---|
78 | public ISelector Selector {
|
---|
79 | get { return SelectorParameter.Value; }
|
---|
80 | set { SelectorParameter.Value = value; }
|
---|
81 | }
|
---|
82 | public ICrossover Crossover {
|
---|
83 | get { return CrossoverParameter.Value; }
|
---|
84 | set { CrossoverParameter.Value = value; }
|
---|
85 | }
|
---|
86 | public PercentValue MutationProbability {
|
---|
87 | get { return MutationProbabilityParameter.Value; }
|
---|
88 | set { MutationProbabilityParameter.Value = value; }
|
---|
89 | }
|
---|
90 | public IManipulator Mutator {
|
---|
91 | get { return MutatorParameter.Value; }
|
---|
92 | set { MutatorParameter.Value = value; }
|
---|
93 | }
|
---|
94 | public IntValue Elites {
|
---|
95 | get { return ElitesParameter.Value; }
|
---|
96 | set { ElitesParameter.Value = value; }
|
---|
97 | }
|
---|
98 | public bool ReevaluteElites {
|
---|
99 | get { return ReevaluateElitesParameter.Value.Value; }
|
---|
100 | set { ReevaluateElitesParameter.Value.Value = value; }
|
---|
101 | }
|
---|
102 | #endregion
|
---|
103 |
|
---|
104 | [StorableConstructor]
|
---|
105 | private AlpsSsGeneticAlgorithm(bool deserializing)
|
---|
106 | : base(deserializing) { }
|
---|
107 | private AlpsSsGeneticAlgorithm(AlpsSsGeneticAlgorithm original, Cloner cloner)
|
---|
108 | : base(original, cloner) {
|
---|
109 | Initialize();
|
---|
110 | }
|
---|
111 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
112 | return new AlpsSsGeneticAlgorithm(this, cloner);
|
---|
113 | }
|
---|
114 |
|
---|
115 | public AlpsSsGeneticAlgorithm()
|
---|
116 | : base() {
|
---|
117 | Parameters.Add(new ValueParameter<IntArray>("PopulationSize", "The size of the population of solutions each layer.", new IntArray(new[] { 100 })));
|
---|
118 | Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(1000)));
|
---|
119 | Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
|
---|
120 | Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
|
---|
121 | Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
|
---|
122 | Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
123 | Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
|
---|
124 | 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 });
|
---|
125 | AgeInheritance = new ReductionOperation(ReductionOperations.Min);
|
---|
126 |
|
---|
127 | var randomCreator = new RandomCreator();
|
---|
128 | var layer0Creator = new SubScopesCreator() { Name = "Create Layer Zero" };
|
---|
129 | var layer0Processor = new LayerUniformSubScopesProcessor();
|
---|
130 | var layer0VariableCreator = new VariableCreator();
|
---|
131 | var layer0SolutionsCreator = new SolutionsCreator();
|
---|
132 | var initializeAgeProcessor = new UniformSubScopesProcessor();
|
---|
133 | var initializeAge = new VariableCreator() { Name = "Initialize Age" };
|
---|
134 | var initializeLocalEvaluatedSolutions = new SubScopesCounter() { Name = "Initialize LayerEvaluatedSolutions" };
|
---|
135 | var initializeGlobalEvaluatedSolutions = new DataReducer() { Name = "Initialize EvaluatedSolutions" };
|
---|
136 | var resultsCollector = new ResultsCollector();
|
---|
137 | var mainLoop = new AlpsSsGeneticAlgorithmMainLoop();
|
---|
138 |
|
---|
139 | OperatorGraph.InitialOperator = randomCreator;
|
---|
140 |
|
---|
141 | randomCreator.SeedParameter.Value = null;
|
---|
142 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
143 | randomCreator.Successor = layer0Creator;
|
---|
144 |
|
---|
145 | layer0Creator.NumberOfSubScopesParameter.Value = new IntValue(1);
|
---|
146 | layer0Creator.Successor = layer0Processor;
|
---|
147 |
|
---|
148 | layer0Processor.Operator = layer0VariableCreator;
|
---|
149 | layer0Processor.Successor = initializeGlobalEvaluatedSolutions;
|
---|
150 |
|
---|
151 | layer0VariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Layer", new IntValue(0)));
|
---|
152 | layer0VariableCreator.Successor = layer0SolutionsCreator;
|
---|
153 |
|
---|
154 | layer0SolutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
155 | layer0SolutionsCreator.Successor = initializeAgeProcessor;
|
---|
156 |
|
---|
157 | initializeAgeProcessor.Operator = initializeAge;
|
---|
158 | initializeAgeProcessor.Successor = initializeLocalEvaluatedSolutions;
|
---|
159 |
|
---|
160 | initializeAge.CollectedValues.Add(new ValueParameter<IntValue>("Age", new IntValue(0)));
|
---|
161 |
|
---|
162 | initializeLocalEvaluatedSolutions.ValueParameter.ActualName = "LayerEvaluatedSolutions";
|
---|
163 |
|
---|
164 | initializeGlobalEvaluatedSolutions.ReductionOperation.Value.Value = ReductionOperations.Sum;
|
---|
165 | initializeGlobalEvaluatedSolutions.TargetOperation.Value.Value = ReductionOperations.Assign;
|
---|
166 | initializeGlobalEvaluatedSolutions.ParameterToReduce.ActualName = "LayerEvaluatedSolutions";
|
---|
167 | initializeGlobalEvaluatedSolutions.TargetParameter.ActualName = "EvaluatedSolutions";
|
---|
168 | initializeGlobalEvaluatedSolutions.Successor = resultsCollector;
|
---|
169 |
|
---|
170 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
|
---|
171 | resultsCollector.Successor = mainLoop;
|
---|
172 |
|
---|
173 | foreach (var selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(s => !(s is IMultiObjectiveSelector)).OrderBy(s => Name))
|
---|
174 | SelectorParameter.ValidValues.Add(selector);
|
---|
175 | var tournamentSelector = SelectorParameter.ValidValues.OfType<TournamentSelector>().FirstOrDefault();
|
---|
176 | if (tournamentSelector != null) {
|
---|
177 | tournamentSelector.GroupSizeParameter.Value = new IntValue(5);
|
---|
178 | SelectorParameter.Value = tournamentSelector;
|
---|
179 | }
|
---|
180 |
|
---|
181 | ParameterizeSelectors();
|
---|
182 | Initialize();
|
---|
183 | }
|
---|
184 |
|
---|
185 | #region Events
|
---|
186 | protected override void OnProblemChanged() {
|
---|
187 | base.OnProblemChanged();
|
---|
188 | ParameterizeSolutionsCreator();
|
---|
189 | ParameterizeMainLoop();
|
---|
190 | ParameterizeSelectors();
|
---|
191 | ParameterizeIterationBasedOperators();
|
---|
192 | UpdateCrossovers();
|
---|
193 | UpdateMutators();
|
---|
194 | }
|
---|
195 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
196 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
197 | ParameterizeSolutionsCreator();
|
---|
198 | }
|
---|
199 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
200 | base.Problem_EvaluatorChanged(sender, e);
|
---|
201 | ParameterizeSolutionsCreator();
|
---|
202 | ParameterizeMainLoop();
|
---|
203 | ParameterizeSelectors();
|
---|
204 | }
|
---|
205 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
206 | base.Problem_OperatorsChanged(sender, e);
|
---|
207 | ParameterizeIterationBasedOperators();
|
---|
208 | UpdateCrossovers();
|
---|
209 | UpdateMutators();
|
---|
210 | }
|
---|
211 | protected override void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
212 | base.Evaluator_QualityParameter_ActualNameChanged(sender, e);
|
---|
213 | ParameterizeMainLoop();
|
---|
214 | ParameterizeSelectors();
|
---|
215 | }
|
---|
216 | #endregion
|
---|
217 |
|
---|
218 | #region Parameterization
|
---|
219 | private void Initialize() {
|
---|
220 | //
|
---|
221 | }
|
---|
222 | private void ParameterizeSolutionsCreator() {
|
---|
223 | //MainLoop.LayerUpdator.SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
224 | //MainLoop.LayerUpdator.SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
225 | }
|
---|
226 | private void ParameterizeMainLoop() {
|
---|
227 | //MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
228 | //MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
229 | //MainLoop.MainOperator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
230 | //MainLoop.MainOperator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
231 | //MainLoop.MainOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
232 | //MainLoop.LayerUpdator.SolutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
233 | }
|
---|
234 | private void ParameterizeSelectors() {
|
---|
235 | foreach (var selector in SelectorParameter.ValidValues) {
|
---|
236 | selector.CopySelected = new BoolValue(true);
|
---|
237 | // Explicit setting of NumberOfSelectedSubScopesParameter is not required anymore because the NumberOfSelectedSubScopesCalculator calculates it itself
|
---|
238 | //selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize - Elites.Value));
|
---|
239 | selector.NumberOfSelectedSubScopesParameter.Hidden = true;
|
---|
240 | ParameterizeStochasticOperatorForLayer(selector);
|
---|
241 | }
|
---|
242 | if (Problem != null) {
|
---|
243 | foreach (var selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
244 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
245 | selector.MaximizationParameter.Hidden = true;
|
---|
246 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
247 | selector.QualityParameter.Hidden = true;
|
---|
248 | }
|
---|
249 | }
|
---|
250 | }
|
---|
251 | private void ParameterizeIterationBasedOperators() {
|
---|
252 | if (Problem != null) {
|
---|
253 | foreach (var @operator in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
254 | @operator.IterationsParameter.ActualName = "Generations";
|
---|
255 | @operator.IterationsParameter.Hidden = true;
|
---|
256 | @operator.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
257 | @operator.MaximumIterationsParameter.Hidden = true;
|
---|
258 | }
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | protected override void ParameterizeStochasticOperatorForLayer(IOperator @operator) {
|
---|
263 | var stochasticOperator = @operator as IStochasticOperator;
|
---|
264 | if (stochasticOperator != null) {
|
---|
265 | stochasticOperator.RandomParameter.ActualName = GlobalRandomCreator.Name;
|
---|
266 | stochasticOperator.RandomParameter.Hidden = true;
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | #endregion
|
---|
271 |
|
---|
272 | #region Updates
|
---|
273 | private void UpdateCrossovers() {
|
---|
274 | var oldCrossover = CrossoverParameter.Value;
|
---|
275 | var defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
|
---|
276 | CrossoverParameter.ValidValues.Clear();
|
---|
277 | foreach (var crossover in Problem.Operators.OfType<ICrossover>().OrderBy(c => c.Name)) {
|
---|
278 | ParameterizeStochasticOperatorForLayer(crossover);
|
---|
279 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
280 | }
|
---|
281 | if (oldCrossover != null) {
|
---|
282 | var crossover = CrossoverParameter.ValidValues.FirstOrDefault(c => c.GetType() == oldCrossover.GetType());
|
---|
283 | if (crossover != null)
|
---|
284 | CrossoverParameter.Value = crossover;
|
---|
285 | else
|
---|
286 | oldCrossover = null;
|
---|
287 | }
|
---|
288 | if (oldCrossover == null && defaultCrossover != null)
|
---|
289 | CrossoverParameter.Value = defaultCrossover;
|
---|
290 | }
|
---|
291 | private void UpdateMutators() {
|
---|
292 | var oldMutator = MutatorParameter.Value;
|
---|
293 | MutatorParameter.ValidValues.Clear();
|
---|
294 | foreach (var mutator in Problem.Operators.OfType<IManipulator>().OrderBy(m => m.Name)) {
|
---|
295 | ParameterizeStochasticOperatorForLayer(mutator);
|
---|
296 | MutatorParameter.ValidValues.Add(mutator);
|
---|
297 | }
|
---|
298 | if (oldMutator != null) {
|
---|
299 | var mutator = MutatorParameter.ValidValues.FirstOrDefault(m => m.GetType() == oldMutator.GetType());
|
---|
300 | if (mutator != null)
|
---|
301 | MutatorParameter.Value = mutator;
|
---|
302 | }
|
---|
303 | }
|
---|
304 | #endregion
|
---|
305 | }
|
---|
306 | } |
---|