Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2350

  • Worked on AlpsSsGeneticAlgorithmMainLoop.
  • Added SteadyStateMatingPoolCreator, RandomLayerProcessor and RandomIntAssigner.
File size: 15.3 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    [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>("MaximumIterations", "The maximum number of iterations 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 initializeLayerPopulationSize = new SubScopesCounter() { Name = "Init LayerPopulationCounter" };
135      var initializeLocalEvaluatedSolutions = new Assigner() { Name = "Initialize LayerEvaluatedSolutions" };
136      var initializeGlobalEvaluatedSolutions = new DataReducer() { Name = "Initialize EvaluatedSolutions" };
137      var initializeCurrentPopulationSize = new Assigner() { Name = "CurrentPopulationSize = EvaluatedSolutions" };
138      var resultsCollector = new ResultsCollector();
139      var mainLoop = new AlpsSsGeneticAlgorithmMainLoop();
140
141      OperatorGraph.InitialOperator = randomCreator;
142
143      randomCreator.SeedParameter.Value = null;
144      randomCreator.SetSeedRandomlyParameter.Value = null;
145      randomCreator.Successor = layer0Creator;
146
147      layer0Creator.NumberOfSubScopesParameter.Value = new IntValue(1);
148      layer0Creator.Successor = layer0Processor;
149
150      layer0Processor.Operator = layer0VariableCreator;
151      layer0Processor.Successor = initializeGlobalEvaluatedSolutions;
152
153      layer0VariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Layer", new IntValue(0)));
154      layer0VariableCreator.Successor = layer0SolutionsCreator;
155
156      layer0SolutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
157      layer0SolutionsCreator.Successor = initializeAgeProcessor;
158
159      initializeAgeProcessor.Operator = initializeAge;
160      initializeAgeProcessor.Successor = initializeLayerPopulationSize;
161
162      initializeAge.CollectedValues.Add(new ValueParameter<IntValue>("Age", new IntValue(1)));
163
164      initializeLayerPopulationSize.ValueParameter.ActualName = "LayerPopulationSize";
165      initializeLayerPopulationSize.Successor = initializeLocalEvaluatedSolutions;
166
167      initializeLocalEvaluatedSolutions.LeftSideParameter.ActualName = "LayerEvaluatedSolutions";
168      initializeLocalEvaluatedSolutions.RightSideParameter.ActualName = "LayerPopulationSize";
169      initializeLocalEvaluatedSolutions.Successor = null;
170
171      initializeGlobalEvaluatedSolutions.ReductionOperation.Value.Value = ReductionOperations.Sum;
172      initializeGlobalEvaluatedSolutions.TargetOperation.Value.Value = ReductionOperations.Assign;
173      initializeGlobalEvaluatedSolutions.ParameterToReduce.ActualName = "LayerEvaluatedSolutions";
174      initializeGlobalEvaluatedSolutions.TargetParameter.ActualName = "EvaluatedSolutions";
175      initializeGlobalEvaluatedSolutions.Successor = initializeCurrentPopulationSize;
176
177      initializeCurrentPopulationSize.LeftSideParameter.ActualName = "CurrentPoulationSize";
178      initializeCurrentPopulationSize.RightSideParameter.ActualName = "EvaluatedSolutions";
179      initializeCurrentPopulationSize.Successor = resultsCollector;
180
181      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
182      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Current PopulationSize", null, "CurrentPopulationSize"));
183      resultsCollector.Successor = mainLoop;
184
185      foreach (var selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(s => !(s is IMultiObjectiveSelector)).OrderBy(s => Name))
186        SelectorParameter.ValidValues.Add(selector);
187      var tournamentSelector = SelectorParameter.ValidValues.OfType<TournamentSelector>().FirstOrDefault();
188      if (tournamentSelector != null) {
189        tournamentSelector.GroupSizeParameter.Value = new IntValue(5);
190        SelectorParameter.Value = tournamentSelector;
191      }
192
193      ParameterizeSelectors();
194      Initialize();
195    }
196
197    #region Events
198    protected override void OnProblemChanged() {
199      base.OnProblemChanged();
200      ParameterizeSolutionsCreator();
201      ParameterizeMainLoop();
202      ParameterizeSelectors();
203      ParameterizeIterationBasedOperators();
204      UpdateCrossovers();
205      UpdateMutators();
206    }
207    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
208      base.Problem_SolutionCreatorChanged(sender, e);
209      ParameterizeSolutionsCreator();
210    }
211    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
212      base.Problem_EvaluatorChanged(sender, e);
213      ParameterizeSolutionsCreator();
214      ParameterizeMainLoop();
215      ParameterizeSelectors();
216    }
217    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
218      base.Problem_OperatorsChanged(sender, e);
219      ParameterizeIterationBasedOperators();
220      UpdateCrossovers();
221      UpdateMutators();
222    }
223    protected override void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
224      base.Evaluator_QualityParameter_ActualNameChanged(sender, e);
225      ParameterizeMainLoop();
226      ParameterizeSelectors();
227    }
228    #endregion
229
230    #region Parameterization
231    private void Initialize() {
232      //
233    }
234    private void ParameterizeSolutionsCreator() {
235      //MainLoop.LayerUpdator.SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
236      //MainLoop.LayerUpdator.SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
237    }
238    private void ParameterizeMainLoop() {
239      //MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
240      //MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
241      //MainLoop.MainOperator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
242      //MainLoop.MainOperator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
243      //MainLoop.MainOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
244      //MainLoop.LayerUpdator.SolutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
245    }
246    private void ParameterizeSelectors() {
247      foreach (var selector in SelectorParameter.ValidValues) {
248        selector.CopySelected = new BoolValue(true);
249        // Explicit setting of NumberOfSelectedSubScopesParameter is not required anymore because the NumberOfSelectedSubScopesCalculator calculates it itself
250        //selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize - Elites.Value));
251        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
252        ParameterizeStochasticOperatorForLayer(selector);
253      }
254      if (Problem != null) {
255        foreach (var selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
256          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
257          selector.MaximizationParameter.Hidden = true;
258          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
259          selector.QualityParameter.Hidden = true;
260        }
261      }
262    }
263    private void ParameterizeIterationBasedOperators() {
264      if (Problem != null) {
265        foreach (var @operator in Problem.Operators.OfType<IIterationBasedOperator>()) {
266          @operator.IterationsParameter.ActualName = "Iteration";
267          @operator.IterationsParameter.Hidden = true;
268          @operator.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
269          @operator.MaximumIterationsParameter.Hidden = true;
270        }
271      }
272    }
273
274    protected override void ParameterizeStochasticOperatorForLayer(IOperator @operator) {
275      var stochasticOperator = @operator as IStochasticOperator;
276      if (stochasticOperator != null) {
277        stochasticOperator.RandomParameter.ActualName = GlobalRandomCreator.Name;
278        stochasticOperator.RandomParameter.Hidden = true;
279      }
280    }
281
282    #endregion
283
284    #region Updates
285    private void UpdateCrossovers() {
286      var oldCrossover = CrossoverParameter.Value;
287      var defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
288      CrossoverParameter.ValidValues.Clear();
289      foreach (var crossover in Problem.Operators.OfType<ICrossover>().OrderBy(c => c.Name)) {
290        ParameterizeStochasticOperatorForLayer(crossover);
291        CrossoverParameter.ValidValues.Add(crossover);
292      }
293      if (oldCrossover != null) {
294        var crossover = CrossoverParameter.ValidValues.FirstOrDefault(c => c.GetType() == oldCrossover.GetType());
295        if (crossover != null)
296          CrossoverParameter.Value = crossover;
297        else
298          oldCrossover = null;
299      }
300      if (oldCrossover == null && defaultCrossover != null)
301        CrossoverParameter.Value = defaultCrossover;
302    }
303    private void UpdateMutators() {
304      var oldMutator = MutatorParameter.Value;
305      MutatorParameter.ValidValues.Clear();
306      foreach (var mutator in Problem.Operators.OfType<IManipulator>().OrderBy(m => m.Name)) {
307        ParameterizeStochasticOperatorForLayer(mutator);
308        MutatorParameter.ValidValues.Add(mutator);
309      }
310      if (oldMutator != null) {
311        var mutator = MutatorParameter.ValidValues.FirstOrDefault(m => m.GetType() == oldMutator.GetType());
312        if (mutator != null)
313          MutatorParameter.Value = mutator;
314      }
315    }
316    #endregion
317  }
318}
Note: See TracBrowser for help on using the repository browser.