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

Last change on this file since 13095 was 13095, checked in by pfleck, 4 years ago

#2269

  • Added the possibility of continuous reseeding (percentage based reseeding of layer 0).
  • Restructured operator graph.
  • Deleted LayerUpdator (replaced by LayerOpener`)
  • Deleted LayerSorter.
  • Moved preparing of GeneticAlgorithmMainLoop to AlpsGeneticAlgorithmMainOperator.
File size: 8.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.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;
32
33namespace HeuristicLab.Algorithms.ALPS {
34
35  [Item("AlpsGeneticAlgorithmMainLoop", "An ALPS genetic algorithm main loop operator.")]
36  [StorableClass]
37  public sealed class AlpsGeneticAlgorithmMainLoop : AlgorithmOperator {
38    #region Parameter Properties
39    public ValueLookupParameter<BoolValue> MaximizationParameter {
40      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
41    }
42    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
43      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
44    }
45    public ILookupParameter<IntValue> MaximumGenerationsParameter {
46      get { return (ILookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
47    }
48    public ILookupParameter<IOperator> AnalyzerParameter {
49      get { return (ILookupParameter<IOperator>)Parameters["Analyzer"]; }
50    }
51    public ILookupParameter<IOperator> LayerAnalyzerParameter {
52      get { return (ILookupParameter<IOperator>)Parameters["LayerAnalyzer"]; }
53    }
54    #endregion
55
56    public GeneticAlgorithmMainLoop MainOperator {
57      get { return OperatorGraph.Iterate().OfType<GeneticAlgorithmMainLoop>().First(); }
58    }
59    public EldersEmigrator EldersEmigrator {
60      get { return OperatorGraph.Iterate().OfType<EldersEmigrator>().First(); }
61    }
62
63    [StorableConstructor]
64    private AlpsGeneticAlgorithmMainLoop(bool deserializing)
65      : base(deserializing) { }
66    private AlpsGeneticAlgorithmMainLoop(AlpsGeneticAlgorithmMainLoop original, Cloner cloner)
67      : base(original, cloner) { }
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new AlpsGeneticAlgorithmMainLoop(this, cloner);
70    }
71    public AlpsGeneticAlgorithmMainLoop()
72      : base() {
73      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false.") { Hidden = true });
74      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
75      Parameters.Add(new LookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations that the algorithm should process."));
76      Parameters.Add(new LookupParameter<IOperator>("Analyzer", "The operator used to the analyze all individuals."));
77      Parameters.Add(new LookupParameter<IOperator>("LayerAnalyzer", "The operator used to analyze each layer."));
78
79      var variableCreator = new VariableCreator() { Name = "Initialize" };
80      var initLayerAnalyzerProcessor = new SubScopesProcessor();
81      var layerVariableCreator = new VariableCreator() { Name = "Initialize Layer" };
82      var initLayerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
83      var initAnalyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
84      var resultsCollector = new ResultsCollector();
85      var matingPoolCreator = new MatingPoolCreator() { Name = "Create Mating Pools" };
86      var matingPoolProcessor = new UniformSubScopesProcessor();
87      var initializeLayer = new Assigner() { Name = "Reset LayerEvaluatedSolutions" };
88      var mainOperator = AlpsGeneticAlgorithmMainOperator.Create();
89      var generationsIcrementor = new IntCounter() { Name = "Increment Generations" };
90      var evaluatedSolutionsReducer = new DataReducer() { Name = "Increment EvaluatedSolutions" };
91      var eldersEmigrator = new EldersEmigrator() { Name = "Emigrate Elders" };
92      var layerOpener = new LayerOpener() { Name = "Open new Layer" };
93      var layerReseeder = new LayerReseeder() { Name = "Reseed Layer Zero" };
94      var layerAnalyzerProcessor = new UniformSubScopesProcessor();
95      var layerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
96      var analyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
97      var termination = new TerminationOperator();
98
99      OperatorGraph.InitialOperator = variableCreator;
100
101      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
102      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("GenerationsSinceLastRefresh", new IntValue(0)));
103      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("OpenLayers", new IntValue(1)));
104      variableCreator.Successor = initLayerAnalyzerProcessor;
105
106      initLayerAnalyzerProcessor.Operators.Add(layerVariableCreator);
107      initLayerAnalyzerProcessor.Successor = initAnalyzerPlaceholder;
108
109      layerVariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("LayerEvaluatedSolutions"));
110      layerVariableCreator.CollectedValues.Add(new ValueParameter<ResultCollection>("LayerResults"));
111      layerVariableCreator.Successor = initLayerAnalyzerPlaceholder;
112
113      initLayerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
114      initLayerAnalyzerPlaceholder.Successor = null;
115
116      initAnalyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
117      initAnalyzerPlaceholder.Successor = resultsCollector;
118
119      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
120      resultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("LayerResults", "Result set for each layer", "LayerResults"));
121      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("OpenLayers"));
122      resultsCollector.CopyValue = new BoolValue(false);
123      resultsCollector.Successor = matingPoolCreator;
124
125      matingPoolCreator.Successor = matingPoolProcessor;
126
127      matingPoolProcessor.Parallel.Value = true;
128      matingPoolProcessor.Operator = initializeLayer;
129      matingPoolProcessor.Successor = generationsIcrementor;
130
131      initializeLayer.LeftSideParameter.ActualName = "LayerEvaluatedSolutions";
132      initializeLayer.RightSideParameter.Value = new IntValue(0);
133      initializeLayer.Successor = mainOperator;
134
135      generationsIcrementor.ValueParameter.ActualName = "Generations";
136      generationsIcrementor.Increment = new IntValue(1);
137      generationsIcrementor.Successor = evaluatedSolutionsReducer;
138
139      evaluatedSolutionsReducer.ParameterToReduce.ActualName = "LayerEvaluatedSolutions";
140      evaluatedSolutionsReducer.TargetParameter.ActualName = "EvaluatedSolutions";
141      evaluatedSolutionsReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
142      evaluatedSolutionsReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Sum);
143      evaluatedSolutionsReducer.Successor = eldersEmigrator;
144
145      eldersEmigrator.Successor = layerOpener;
146
147      layerOpener.Successor = layerReseeder;
148
149      layerReseeder.Successor = layerAnalyzerProcessor;
150
151      layerAnalyzerProcessor.Operator = layerAnalyzerPlaceholder;
152      layerAnalyzerProcessor.Successor = analyzerPlaceholder;
153
154      layerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
155
156      analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
157      analyzerPlaceholder.Successor = termination;
158
159      termination.ContinueBranch = matingPoolCreator;
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.