Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/AlpsGeneticAlgorithmMainLoop.cs @ 11583

Last change on this file since 11583 was 11583, checked in by pfleck, 9 years ago

#2269

  • Implemented MatingPoolCreator.
  • Added a modified GeneticAlgorithMainLoop as main operator in ALPS-GA-MainLoop.
File size: 12.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Linq;
23using HeuristicLab.Algorithms.GeneticAlgorithm;
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.Selection;
33
34namespace HeuristicLab.Algorithms.ALPS {
35
36  [Item("AlpsGeneticAlgorithmMainLoop", "An ALPS genetic algorithm main loop operator.")]
37  public sealed class AlpsGeneticAlgorithmMainLoop : AlgorithmOperator {
38    #region Parameter Properties
39    public ILookupParameter<IRandom> RandomParameter {
40      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
41    }
42    /*public ILookupParameter<BoolValue> MaximizationParameter {
43      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
44    }
45    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
46      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
47    }
48    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
49      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
50    }
51    public ILookupParameter<IOperator> EvaluatorParameter {
52      get { return (ILookupParameter<IOperator>)Parameters["Evaluator"]; }
53    }*/
54    public ILookupParameter<IntValue> PopulationSizeParameter {
55      get { return (ILookupParameter<IntValue>)Parameters["PopulationSize"]; }
56    }
57    public ILookupParameter<IntValue> MaximumGenerationsParameter {
58      get { return (ILookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
59    }
60    public ILookupParameter<IOperator> SelectorParameter {
61      get { return (ILookupParameter<IOperator>)Parameters["Selector"]; }
62    }
63    public ILookupParameter<IOperator> CrossoverParameter {
64      get { return (ILookupParameter<IOperator>)Parameters["Crossover"]; }
65    }
66    public ILookupParameter<PercentValue> MutationProbabilityParameter {
67      get { return (ILookupParameter<PercentValue>)Parameters["MutationProbability"]; }
68    }
69    public ILookupParameter<IOperator> MutatorParameter {
70      get { return (ILookupParameter<IOperator>)Parameters["Mutator"]; }
71    }
72    public ILookupParameter<IntValue> ElitesParameter {
73      get { return (ILookupParameter<IntValue>)Parameters["Elites"]; }
74    }
75    public IValueLookupParameter<BoolValue> ReevaluateElitesParameter {
76      get { return (IValueLookupParameter<BoolValue>)Parameters["ReevaluateElites"]; }
77    }
78    public ILookupParameter<ResultCollection> ResultsParameter {
79      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
80    }
81    public ILookupParameter<IOperator> AnalyzerParameter {
82      get { return (ILookupParameter<IOperator>)Parameters["Analyzer"]; }
83    }
84    public ILookupParameter<IOperator> LayerAnalyzerParameter {
85      get { return (ILookupParameter<IOperator>)Parameters["LayerAnalyzer"]; }
86    }
87    #endregion
88
89    public GeneticAlgorithmMainLoop MainOperator {
90      get { return OperatorGraph.Iterate().OfType<GeneticAlgorithmMainLoop>().First(); }
91    }
92
93    [StorableConstructor]
94    private AlpsGeneticAlgorithmMainLoop(bool deserializing)
95      : base(deserializing) { }
96    private AlpsGeneticAlgorithmMainLoop(AlpsGeneticAlgorithmMainLoop original, Cloner cloner)
97      : base(original, cloner) { }
98    public override IDeepCloneable Clone(Cloner cloner) {
99      return new AlpsGeneticAlgorithmMainLoop(this, cloner);
100    }
101    public AlpsGeneticAlgorithmMainLoop()
102      : base() {
103      Parameters.Add(new LookupParameter<IRandom>("Random", "A pseudo random number generator."));
104      /*Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
105      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
106      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
107      Parameters.Add(new LookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));*/
108      Parameters.Add(new LookupParameter<IntValue>("PopulationSize", "The size of the population of solutions."));
109      Parameters.Add(new LookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations that the algorithm should process."));
110      Parameters.Add(new LookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
111      Parameters.Add(new LookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
112      Parameters.Add(new LookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
113      Parameters.Add(new LookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
114      Parameters.Add(new LookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
115      Parameters.Add(new LookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
116      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The results collection to store the results."));
117      Parameters.Add(new LookupParameter<IOperator>("Analyzer", "The operator used to the analyze all individuals."));
118      Parameters.Add(new LookupParameter<IOperator>("LayerAnalyzer", "The operator used to analyze each layer."));
119
120      var variableCreator = new VariableCreator() { Name = "Initialize" };
121      var resultsCollector = new ResultsCollector();
122      var initAnalyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
123      var initLayerAnalyzerProcessor = new SubScopesProcessor();
124      var initLayerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
125      var matingPoolCreator = new MatingPoolCreator() { Name = "Create Mating Pools" };
126      var matingPoolProcessor = new UniformSubScopesProcessor();
127      var mainOperator = PrepareGeneticAlgorithmMainLoop();
128      var layerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
129      var generationsIcrementor = new IntCounter() { Name = "Increment Generations" };
130      var evaluatedSolutionsReducer = new DataReducer() { Name = "Increment EvaluatedSolutions" };
131      var eldersEmigrator = new CombinedOperator() { Name = "Emigrate Elders" };
132      var layerUpdator = new CombinedOperator() { Name = "Update Layers" };
133      var analyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
134      var generationsComparator = new Comparator() { Name = "Generations >= MaximumGenerations" };
135      var terminateBranch = new ConditionalBranch() { Name = "Terminate?" };
136
137      OperatorGraph.InitialOperator = variableCreator;
138
139      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
140      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("GenerationsSinceLastRefresh", new IntValue(0)));
141      variableCreator.Successor = resultsCollector;
142
143      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
144      resultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("LayerResults", "Result set for each layer", "Results"));
145      resultsCollector.Successor = initAnalyzerPlaceholder;
146
147      initAnalyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
148      initAnalyzerPlaceholder.Successor = initLayerAnalyzerProcessor;
149
150      initLayerAnalyzerProcessor.Operators.Add(initLayerAnalyzerPlaceholder);
151      initLayerAnalyzerProcessor.Successor = matingPoolCreator;
152
153      initLayerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
154      initLayerAnalyzerPlaceholder.Successor = null;
155
156      matingPoolCreator.Successor = matingPoolProcessor;
157
158      matingPoolProcessor.Parallel.Value = true;
159      matingPoolProcessor.Operator = mainOperator;
160      matingPoolProcessor.Successor = generationsIcrementor;
161
162      generationsIcrementor.ValueParameter.ActualName = "Generations";
163      generationsIcrementor.Increment = new IntValue(1);
164      generationsIcrementor.Successor = evaluatedSolutionsReducer;
165
166      evaluatedSolutionsReducer.ParameterToReduce.ActualName = "EvaluatedSolutions";
167      evaluatedSolutionsReducer.TargetParameter.ActualName = "EvaluatedSolutions";
168      evaluatedSolutionsReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
169      evaluatedSolutionsReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Sum);
170      evaluatedSolutionsReducer.Successor = eldersEmigrator;
171
172      mainOperator.Successor = layerAnalyzerPlaceholder;
173
174      layerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
175      layerAnalyzerPlaceholder.Successor = null;
176
177      eldersEmigrator.Successor = layerUpdator;
178
179      layerUpdator.Successor = analyzerPlaceholder;
180
181      analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
182      analyzerPlaceholder.Successor = generationsComparator;
183
184      generationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
185      generationsComparator.LeftSideParameter.ActualName = "Generations";
186      generationsComparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
187      generationsComparator.ResultParameter.ActualName = "TerminateGenerations";
188      generationsComparator.Successor = terminateBranch;
189
190      terminateBranch.ConditionParameter.ActualName = "TerminateGenerations";
191      terminateBranch.FalseBranch = matingPoolCreator;
192    }
193
194    private GeneticAlgorithmMainLoop PrepareGeneticAlgorithmMainLoop() {
195      var mainLoop = new GeneticAlgorithmMainLoop();
196      var selector = mainLoop.OperatorGraph.Iterate().OfType<Placeholder>().First(o => o.OperatorParameter.ActualName == "Selector");
197      var crossover = mainLoop.OperatorGraph.Iterate().OfType<Placeholder>().First(o => o.OperatorParameter.ActualName == "Crossover");
198      var elitesMerger = mainLoop.OperatorGraph.Iterate().OfType<MergingReducer>().First();
199
200      // Operator starts with selector
201      mainLoop.OperatorGraph.InitialOperator = selector;
202
203      // Insert AgeCalculator between crossover and its successor
204      var crossoverSuccessor = crossover.Successor;
205      var ageCalculator = new DataReducer() { Name = "Calculate Age" };
206      ageCalculator.ParameterToReduce.ActualName = "Age";
207      ageCalculator.TargetParameter.ActualName = "Age";
208      ageCalculator.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Max);
209      ageCalculator.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign);
210      crossover.Successor = ageCalculator;
211      ageCalculator.Successor = crossoverSuccessor;
212
213      // Instead of generational loop after merging of elites, increment ages of all individuals
214      var processor = new UniformSubScopesProcessor();
215      var incrementor = new IntCounter() { Name = "Increment Age" };
216      processor.Operator = incrementor;
217      processor.Successor = null;
218      incrementor.ValueParameter.ActualName = "Age";
219      incrementor.Increment = new IntValue(1);
220      incrementor.Successor = null;
221      elitesMerger.Successor = processor;
222
223      // Parameterize
224      foreach (var stochasticOperator in mainLoop.OperatorGraph.Iterate().OfType<IStochasticOperator>())
225        stochasticOperator.RandomParameter.ActualName = "LocalRandom";
226      foreach (var stochasticBranch in mainLoop.OperatorGraph.Iterate().OfType<StochasticBranch>())
227        stochasticBranch.RandomParameter.ActualName = "LocalRandom";
228
229      // Remove unnessesary subtrees
230      //foreach (var @operator in mainLoop.OperatorGraph.Iterate().OfType<SingleSuccessorOperator>().Where(o => o.Successor == selector))
231      //  @operator.Successor = null;
232
233      return mainLoop;
234    }
235  }
236}
Note: See TracBrowser for help on using the repository browser.