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 |
|
---|
22 | using System.Linq;
|
---|
23 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Optimization.Operators;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | namespace 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<IOperator> AnalyzerParameter {
|
---|
46 | get { return (ILookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
47 | }
|
---|
48 | public ILookupParameter<IOperator> LayerAnalyzerParameter {
|
---|
49 | get { return (ILookupParameter<IOperator>)Parameters["LayerAnalyzer"]; }
|
---|
50 | }
|
---|
51 | #endregion
|
---|
52 |
|
---|
53 | public GeneticAlgorithmMainLoop MainOperator {
|
---|
54 | get { return OperatorGraph.Iterate().OfType<GeneticAlgorithmMainLoop>().First(); }
|
---|
55 | }
|
---|
56 |
|
---|
57 | [StorableConstructor]
|
---|
58 | private AlpsGeneticAlgorithmMainLoop(bool deserializing)
|
---|
59 | : base(deserializing) { }
|
---|
60 | private AlpsGeneticAlgorithmMainLoop(AlpsGeneticAlgorithmMainLoop original, Cloner cloner)
|
---|
61 | : base(original, cloner) { }
|
---|
62 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
63 | return new AlpsGeneticAlgorithmMainLoop(this, cloner);
|
---|
64 | }
|
---|
65 | public AlpsGeneticAlgorithmMainLoop()
|
---|
66 | : base() {
|
---|
67 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false.") { Hidden = true });
|
---|
68 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
69 | Parameters.Add(new LookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations that the algorithm should process."));
|
---|
70 | Parameters.Add(new LookupParameter<IOperator>("Analyzer", "The operator used to the analyze all individuals."));
|
---|
71 | Parameters.Add(new LookupParameter<IOperator>("LayerAnalyzer", "The operator used to analyze each layer."));
|
---|
72 |
|
---|
73 | var variableCreator = new VariableCreator() { Name = "Initialize" };
|
---|
74 | var initLayerAnalyzerProcessor = new SubScopesProcessor();
|
---|
75 | var layerVariableCreator = new VariableCreator() { Name = "Initialize Layer" };
|
---|
76 | var initLayerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
|
---|
77 | var initAnalyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
|
---|
78 | var resultsCollector = new ResultsCollector();
|
---|
79 | var matingPoolCreator = new MatingPoolCreator() { Name = "Create Mating Pools" };
|
---|
80 | var matingPoolProcessor = new UniformSubScopesProcessor();
|
---|
81 | var initializeLayer = new Assigner() { Name = "Reset LayerEvaluatedSolutions" };
|
---|
82 | var mainOperator = AlpsGeneticAlgorithmMainOperator.Create();
|
---|
83 | var generationsIcrementor = new IntCounter() { Name = "Increment Generations" };
|
---|
84 | var evaluatedSolutionsReducer = new DataReducer() { Name = "Increment EvaluatedSolutions" };
|
---|
85 | var eldersEmigrator = new EldersEmigrator() { Name = "Emigrate Elders" };
|
---|
86 | var layerOpener = new LayerOpener() { Name = "Open new Layer" };
|
---|
87 | var layerReseeder = new LayerReseeder() { Name = "Reseed Layer Zero" };
|
---|
88 | var layerAnalyzerProcessor = new UniformSubScopesProcessor();
|
---|
89 | var layerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
|
---|
90 | var analyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
|
---|
91 | var termination = new TerminationOperator();
|
---|
92 |
|
---|
93 | OperatorGraph.InitialOperator = variableCreator;
|
---|
94 |
|
---|
95 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
|
---|
96 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("GenerationsSinceLastRefresh", new IntValue(0)));
|
---|
97 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("OpenLayers", new IntValue(1)));
|
---|
98 | variableCreator.Successor = initLayerAnalyzerProcessor;
|
---|
99 |
|
---|
100 | initLayerAnalyzerProcessor.Operators.Add(layerVariableCreator);
|
---|
101 | initLayerAnalyzerProcessor.Successor = initAnalyzerPlaceholder;
|
---|
102 |
|
---|
103 | layerVariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("LayerEvaluatedSolutions"));
|
---|
104 | layerVariableCreator.CollectedValues.Add(new ValueParameter<ResultCollection>("LayerResults"));
|
---|
105 | layerVariableCreator.Successor = initLayerAnalyzerPlaceholder;
|
---|
106 |
|
---|
107 | initLayerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
|
---|
108 | initLayerAnalyzerPlaceholder.Successor = null;
|
---|
109 |
|
---|
110 | initAnalyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
111 | initAnalyzerPlaceholder.Successor = resultsCollector;
|
---|
112 |
|
---|
113 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
114 | resultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("LayerResults", "Result set for each layer", "LayerResults"));
|
---|
115 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("OpenLayers"));
|
---|
116 | resultsCollector.CopyValue = new BoolValue(false);
|
---|
117 | resultsCollector.Successor = matingPoolCreator;
|
---|
118 |
|
---|
119 | matingPoolCreator.Successor = matingPoolProcessor;
|
---|
120 |
|
---|
121 | matingPoolProcessor.Parallel.Value = true;
|
---|
122 | matingPoolProcessor.Operator = initializeLayer;
|
---|
123 | matingPoolProcessor.Successor = generationsIcrementor;
|
---|
124 |
|
---|
125 | initializeLayer.LeftSideParameter.ActualName = "LayerEvaluatedSolutions";
|
---|
126 | initializeLayer.RightSideParameter.Value = new IntValue(0);
|
---|
127 | initializeLayer.Successor = mainOperator;
|
---|
128 |
|
---|
129 | generationsIcrementor.ValueParameter.ActualName = "Generations";
|
---|
130 | generationsIcrementor.Increment = new IntValue(1);
|
---|
131 | generationsIcrementor.Successor = evaluatedSolutionsReducer;
|
---|
132 |
|
---|
133 | evaluatedSolutionsReducer.ParameterToReduce.ActualName = "LayerEvaluatedSolutions";
|
---|
134 | evaluatedSolutionsReducer.TargetParameter.ActualName = "EvaluatedSolutions";
|
---|
135 | evaluatedSolutionsReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
|
---|
136 | evaluatedSolutionsReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Sum);
|
---|
137 | evaluatedSolutionsReducer.Successor = eldersEmigrator;
|
---|
138 |
|
---|
139 | eldersEmigrator.Successor = layerOpener;
|
---|
140 |
|
---|
141 | layerOpener.Successor = layerReseeder;
|
---|
142 |
|
---|
143 | layerReseeder.Successor = layerAnalyzerProcessor;
|
---|
144 |
|
---|
145 | layerAnalyzerProcessor.Operator = layerAnalyzerPlaceholder;
|
---|
146 | layerAnalyzerProcessor.Successor = analyzerPlaceholder;
|
---|
147 |
|
---|
148 | layerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
|
---|
149 |
|
---|
150 | analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
151 | analyzerPlaceholder.Successor = termination;
|
---|
152 |
|
---|
153 | termination.ContinueBranch = matingPoolCreator;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | } |
---|