Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ALPS/HeuristicLab.Algorithms.ALPS.SteadyState/3.3/AlpsSsGeneticAlgorithm.cs @ 12138

Last change on this file since 12138 was 12138, checked in by pfleck, 8 years ago

#2350

  • Added the AlpsSsGeneticAlgorithmMainOperator.
  • Changed some minor things.
File size: 15.5 KB
Line 
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
22using System;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Optimization.Operators;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33using HeuristicLab.Random;
34using HeuristicLab.Selection;
35
36namespace 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> 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    #endregion
67
68    #region Properties
69    public IntArray PopulationSize {
70      get { return PopulationSizeParameter.Value; }
71      set { PopulationSizeParameter.Value = value; }
72    }
73    public IntValue MaximumIterations {
74      get { return MaximumIterationsParameter.Value; }
75      set { MaximumIterationsParameter.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    private AlpsSsGeneticAlgorithmMainLoop MainLoop {
105      get { return OperatorGraph.Iterate().OfType<AlpsSsGeneticAlgorithmMainLoop>().First(); }
106    }
107
108    [StorableConstructor]
109    private AlpsSsGeneticAlgorithm(bool deserializing)
110      : base(deserializing) { }
111    private AlpsSsGeneticAlgorithm(AlpsSsGeneticAlgorithm original, Cloner cloner)
112      : base(original, cloner) {
113      Initialize();
114    }
115    public override IDeepCloneable Clone(Cloner cloner) {
116      return new AlpsSsGeneticAlgorithm(this, cloner);
117    }
118
119    public AlpsSsGeneticAlgorithm()
120      : base() {
121      Parameters.Add(new ValueParameter<IntArray>("PopulationSize", "The size of the population of solutions each layer.", new IntArray(new[] { 100 })));
122      Parameters.Add(new ValueParameter<IntValue>("MaximumIterations", "The maximum number of iterations that should be processed.", new IntValue(1000)));
123      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
124      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
125      Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
126      Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
127      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
128      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 });
129      AgeInheritance = new ReductionOperation(ReductionOperations.Min);
130
131      var randomCreator = new RandomCreator();
132      var layer0Creator = new SubScopesCreator() { Name = "Create Layer Zero" };
133      var layer0Processor = new LayerUniformSubScopesProcessor();
134      var layer0VariableCreator = new VariableCreator();
135      var layer0SolutionsCreator = new SolutionsCreator();
136      var initializeAgeProcessor = new UniformSubScopesProcessor();
137      var initializeAge = new VariableCreator() { Name = "Initialize Age" };
138      var initializeLayerPopulationSize = new SubScopesCounter() { Name = "Init LayerPopulationCounter" };
139      var initializeLocalEvaluatedSolutions = new Assigner() { Name = "Initialize LayerEvaluatedSolutions" };
140      var initializeGlobalEvaluatedSolutions = new DataReducer() { Name = "Initialize EvaluatedSolutions" };
141      var initializeCurrentPopulationSize = new Assigner() { Name = "CurrentPopulationSize = EvaluatedSolutions" };
142      var resultsCollector = new ResultsCollector();
143      var mainLoop = new AlpsSsGeneticAlgorithmMainLoop();
144
145      OperatorGraph.InitialOperator = randomCreator;
146
147      randomCreator.SeedParameter.Value = null;
148      randomCreator.SetSeedRandomlyParameter.Value = null;
149      randomCreator.Successor = layer0Creator;
150
151      layer0Creator.NumberOfSubScopesParameter.Value = new IntValue(1);
152      layer0Creator.Successor = layer0Processor;
153
154      layer0Processor.Operator = layer0VariableCreator;
155      layer0Processor.Successor = initializeGlobalEvaluatedSolutions;
156
157      layer0VariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Layer", new IntValue(0)));
158      layer0VariableCreator.Successor = layer0SolutionsCreator;
159
160      layer0SolutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
161      layer0SolutionsCreator.Successor = initializeAgeProcessor;
162
163      initializeAgeProcessor.Operator = initializeAge;
164      initializeAgeProcessor.Successor = initializeLayerPopulationSize;
165
166      initializeAge.CollectedValues.Add(new ValueParameter<IntValue>("EvalsCreated", new IntValue(1)));
167
168      initializeLayerPopulationSize.ValueParameter.ActualName = "LayerPopulationSize";
169      initializeLayerPopulationSize.Successor = initializeLocalEvaluatedSolutions;
170
171      initializeLocalEvaluatedSolutions.LeftSideParameter.ActualName = "LayerEvaluatedSolutions";
172      initializeLocalEvaluatedSolutions.RightSideParameter.ActualName = "LayerPopulationSize";
173      initializeLocalEvaluatedSolutions.Successor = null;
174
175      initializeGlobalEvaluatedSolutions.ReductionOperation.Value.Value = ReductionOperations.Sum;
176      initializeGlobalEvaluatedSolutions.TargetOperation.Value.Value = ReductionOperations.Assign;
177      initializeGlobalEvaluatedSolutions.ParameterToReduce.ActualName = "LayerEvaluatedSolutions";
178      initializeGlobalEvaluatedSolutions.TargetParameter.ActualName = "EvaluatedSolutions";
179      initializeGlobalEvaluatedSolutions.Successor = initializeCurrentPopulationSize;
180
181      initializeCurrentPopulationSize.LeftSideParameter.ActualName = "CurrentPoulationSize";
182      initializeCurrentPopulationSize.RightSideParameter.ActualName = "EvaluatedSolutions";
183      initializeCurrentPopulationSize.Successor = resultsCollector;
184
185      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
186      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Current PopulationSize", null, "CurrentPopulationSize"));
187      resultsCollector.Successor = mainLoop;
188
189      foreach (var selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(s => !(s is IMultiObjectiveSelector)).OrderBy(s => Name))
190        SelectorParameter.ValidValues.Add(selector);
191      var tournamentSelector = SelectorParameter.ValidValues.OfType<TournamentSelector>().FirstOrDefault();
192      if (tournamentSelector != null) {
193        tournamentSelector.GroupSizeParameter.Value = new IntValue(5);
194        SelectorParameter.Value = tournamentSelector;
195      }
196
197      ParameterizeSelectors();
198      Initialize();
199    }
200
201    #region Events
202    protected override void OnProblemChanged() {
203      base.OnProblemChanged();
204      ParameterizeSolutionsCreator();
205      ParameterizeMainLoop();
206      ParameterizeSelectors();
207      ParameterizeIterationBasedOperators();
208      UpdateCrossovers();
209      UpdateMutators();
210    }
211    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
212      base.Problem_SolutionCreatorChanged(sender, e);
213      ParameterizeSolutionsCreator();
214    }
215    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
216      base.Problem_EvaluatorChanged(sender, e);
217      ParameterizeSolutionsCreator();
218      ParameterizeMainLoop();
219      ParameterizeSelectors();
220    }
221    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
222      base.Problem_OperatorsChanged(sender, e);
223      ParameterizeIterationBasedOperators();
224      UpdateCrossovers();
225      UpdateMutators();
226    }
227    protected override void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
228      base.Evaluator_QualityParameter_ActualNameChanged(sender, e);
229      ParameterizeMainLoop();
230      ParameterizeSelectors();
231    }
232    #endregion
233
234    #region Parameterization
235    private void Initialize() {
236      //
237    }
238    private void ParameterizeSolutionsCreator() {
239      //MainLoop.LayerUpdator.SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
240      //MainLoop.LayerUpdator.SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
241    }
242    private void ParameterizeMainLoop() {
243      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
244      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
245      //MainLoop.MainOperator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
246      //MainLoop.MainOperator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
247      //MainLoop.MainOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
248      //MainLoop.LayerUpdator.SolutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
249    }
250    private void ParameterizeSelectors() {
251      foreach (var selector in SelectorParameter.ValidValues) {
252        selector.CopySelected = new BoolValue(true);
253        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2);
254        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
255        ParameterizeStochasticOperatorForLayer(selector);
256      }
257      if (Problem != null) {
258        foreach (var selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
259          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
260          selector.MaximizationParameter.Hidden = true;
261          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
262          selector.QualityParameter.Hidden = true;
263        }
264      }
265    }
266    private void ParameterizeIterationBasedOperators() {
267      if (Problem != null) {
268        foreach (var @operator in Problem.Operators.OfType<IIterationBasedOperator>()) {
269          @operator.IterationsParameter.ActualName = "Iteration";
270          @operator.IterationsParameter.Hidden = true;
271          @operator.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
272          @operator.MaximumIterationsParameter.Hidden = true;
273        }
274      }
275    }
276
277    protected override void ParameterizeStochasticOperator(IOperator @operator) {
278      var stochasticOperator = @operator as IStochasticOperator;
279      if (stochasticOperator != null) {
280        stochasticOperator.RandomParameter.ActualName = "Random";
281        stochasticOperator.RandomParameter.Hidden = true;
282      }
283    }
284    protected override void ParameterizeStochasticOperatorForLayer(IOperator @operator) {
285      var stochasticOperator = @operator as IStochasticOperator;
286      if (stochasticOperator != null) {
287        stochasticOperator.RandomParameter.ActualName = "Random";
288        stochasticOperator.RandomParameter.Hidden = true;
289      }
290    }
291    #endregion
292
293    #region Updates
294    private void UpdateCrossovers() {
295      var oldCrossover = CrossoverParameter.Value;
296      var defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
297      CrossoverParameter.ValidValues.Clear();
298      foreach (var crossover in Problem.Operators.OfType<ICrossover>().OrderBy(c => c.Name)) {
299        ParameterizeStochasticOperatorForLayer(crossover);
300        CrossoverParameter.ValidValues.Add(crossover);
301      }
302      if (oldCrossover != null) {
303        var crossover = CrossoverParameter.ValidValues.FirstOrDefault(c => c.GetType() == oldCrossover.GetType());
304        if (crossover != null)
305          CrossoverParameter.Value = crossover;
306        else
307          oldCrossover = null;
308      }
309      if (oldCrossover == null && defaultCrossover != null)
310        CrossoverParameter.Value = defaultCrossover;
311    }
312    private void UpdateMutators() {
313      var oldMutator = MutatorParameter.Value;
314      MutatorParameter.ValidValues.Clear();
315      foreach (var mutator in Problem.Operators.OfType<IManipulator>().OrderBy(m => m.Name)) {
316        ParameterizeStochasticOperatorForLayer(mutator);
317        MutatorParameter.ValidValues.Add(mutator);
318      }
319      if (oldMutator != null) {
320        var mutator = MutatorParameter.ValidValues.FirstOrDefault(m => m.GetType() == oldMutator.GetType());
321        if (mutator != null)
322          MutatorParameter.Value = mutator;
323      }
324    }
325    #endregion
326  }
327}
Note: See TracBrowser for help on using the repository browser.