Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2269 Implemented AlpsGeneticAlgorithmMainLoop.

File size: 9.6 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Optimization.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Algorithms.ALPS {
32
33  [Item("AlpsGeneticAlgorithmMainLoop", "An ALPS genetic algorithm main loop operator.")]
34  public class AlpsGeneticAlgorithmMainLoop : AlgorithmOperator {
35    #region Parameter Properties
36    public ILookupParameter<IRandom> RandomParameter {
37      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
38    }
39    public ILookupParameter<BoolValue> MaximizationParameter {
40      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
41    }
42    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
43      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
44    }
45    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
46      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
47    }
48    public ILookupParameter<IntValue> PopulationSizeParameter {
49      get { return (ILookupParameter<IntValue>)Parameters["PopulationSize"]; }
50    }
51    public ILookupParameter<IntValue> MaximumGenerationsParameter {
52      get { return (ILookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
53    }
54    public ILookupParameter<IOperator> SelectorParameter {
55      get { return (ILookupParameter<IOperator>)Parameters["Selector"]; }
56    }
57    public ILookupParameter<IOperator> CrossoverParameter {
58      get { return (ILookupParameter<IOperator>)Parameters["Crossover"]; }
59    }
60    public ILookupParameter<PercentValue> MutationProbabilityParameter {
61      get { return (ILookupParameter<PercentValue>)Parameters["MutationProbability"]; }
62    }
63    public ILookupParameter<IOperator> MutatorParameter {
64      get { return (ILookupParameter<IOperator>)Parameters["Mutator"]; }
65    }
66    public ILookupParameter<IOperator> EvaluatorParameter {
67      get { return (ILookupParameter<IOperator>)Parameters["Evaluator"]; }
68    }
69    public ILookupParameter<IntValue> ElitesParameter {
70      get { return (ILookupParameter<IntValue>)Parameters["Elites"]; }
71    }
72    public IValueLookupParameter<BoolValue> ReevaluateElitesParameter {
73      get { return (IValueLookupParameter<BoolValue>)Parameters["ReevaluateElites"]; }
74    }
75    public ILookupParameter<ResultCollection> ResultsParameter {
76      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
77    }
78    public ILookupParameter<IOperator> AnalyzerParameter {
79      get { return (ILookupParameter<IOperator>)Parameters["Analyzer"]; }
80    }
81    public ILookupParameter<IOperator> LayerAnalyzerParameter {
82      get { return (ILookupParameter<IOperator>)Parameters["LayerAnalyzer"]; }
83    }
84    #endregion
85
86    [StorableConstructor]
87    private AlpsGeneticAlgorithmMainLoop(bool deserializing)
88      : base(deserializing) { }
89    private AlpsGeneticAlgorithmMainLoop(AlpsGeneticAlgorithmMainLoop original, Cloner cloner)
90      : base(original, cloner) { }
91    public override IDeepCloneable Clone(Cloner cloner) {
92      return new AlpsGeneticAlgorithmMainLoop(this, cloner);
93    }
94    public AlpsGeneticAlgorithmMainLoop()
95      : base() {
96      Parameters.Add(new LookupParameter<IRandom>("Random", "A pseudo random number generator."));
97      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
98      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
99      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
100      Parameters.Add(new LookupParameter<IntValue>("PopulationSize", "The size of the population of solutions."));
101      Parameters.Add(new LookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations that the algorithm should process."));
102      Parameters.Add(new LookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
103      Parameters.Add(new LookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
104      Parameters.Add(new LookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
105      Parameters.Add(new LookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
106      Parameters.Add(new LookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
107      Parameters.Add(new LookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
108      Parameters.Add(new LookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
109      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The results collection to store the results."));
110      Parameters.Add(new LookupParameter<IOperator>("Analyzer", "The operator used to the analyze all individuals."));
111      Parameters.Add(new LookupParameter<IOperator>("LayerAnalyzer", "The operator used to analyze each layer."));
112
113
114      var variableCreator = new VariableCreator() { Name = "Initialize" };
115      var resultsCollector = new ResultsCollector();
116      var initAnalyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
117      var initLayerAnalyzerProcessor = new SubScopesProcessor();
118      var initLayerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
119      var matingPoolCreator = new CombinedOperator() { Name = "Create Mating Pools" };
120      var matingPoolProcessor = new UniformSubScopesProcessor();
121      var mainOperator = new CombinedOperator() { Name = "Perform Generation" };
122      var layerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
123      var eldersEmigrator = new CombinedOperator() { Name = "Emigrate Elders" };
124      var layerUpdator = new CombinedOperator() { Name = "Update Layers" };
125      var analyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
126      var generationsComparator = new Comparator() { Name = "Generations >= MaximumGenerations" };
127      var terminateBranch = new ConditionalBranch() { Name = "Terminate?" };
128
129      OperatorGraph.InitialOperator = variableCreator;
130
131      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
132      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("GenerationsSinceLastRefresh", new IntValue(0)));
133      variableCreator.Successor = resultsCollector;
134
135      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
136      resultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("LayerResults", "Result set for each layer", "Results"));
137      resultsCollector.Successor = initAnalyzerPlaceholder;
138
139      initAnalyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
140      initAnalyzerPlaceholder.Successor = initLayerAnalyzerProcessor;
141
142      initLayerAnalyzerProcessor.Operators.Add(initLayerAnalyzerPlaceholder);
143      initLayerAnalyzerProcessor.Successor = matingPoolCreator;
144
145      initLayerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
146      initLayerAnalyzerPlaceholder.Successor = null;
147
148      matingPoolCreator.Successor = matingPoolProcessor;
149
150      matingPoolProcessor.Parallel.Value = true;
151      matingPoolProcessor.Operator = mainOperator;
152      matingPoolProcessor.Successor = eldersEmigrator;
153
154      mainOperator.Successor = layerAnalyzerPlaceholder;
155
156      layerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
157      layerAnalyzerPlaceholder.Successor = null;
158
159      eldersEmigrator.Successor = layerUpdator;
160
161      layerUpdator.Successor = analyzerPlaceholder;
162
163      analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
164      analyzerPlaceholder.Successor = generationsComparator;
165
166      generationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
167      generationsComparator.LeftSideParameter.ActualName = "Generations";
168      generationsComparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
169      generationsComparator.ResultParameter.ActualName = "TerminateGenerations";
170      generationsComparator.Successor = terminateBranch;
171
172      terminateBranch.ConditionParameter.ActualName = "Terminategenerations";
173      terminateBranch.FalseBranch = matingPoolCreator;
174
175    }
176
177  }
178}
Note: See TracBrowser for help on using the repository browser.