[11580] | 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 |
|
---|
[11583] | 22 | using System.Linq;
|
---|
| 23 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
[11580] | 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;
|
---|
[11583] | 32 | using HeuristicLab.Selection;
|
---|
[11580] | 33 |
|
---|
| 34 | namespace HeuristicLab.Algorithms.ALPS {
|
---|
| 35 |
|
---|
| 36 | [Item("AlpsGeneticAlgorithmMainLoop", "An ALPS genetic algorithm main loop operator.")]
|
---|
[11585] | 37 | [StorableClass]
|
---|
[11583] | 38 | public sealed class AlpsGeneticAlgorithmMainLoop : AlgorithmOperator {
|
---|
[11580] | 39 | #region Parameter Properties
|
---|
| 40 | public ILookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 41 | get { return (ILookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
| 42 | }
|
---|
| 43 | public ILookupParameter<IOperator> AnalyzerParameter {
|
---|
| 44 | get { return (ILookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
| 45 | }
|
---|
| 46 | public ILookupParameter<IOperator> LayerAnalyzerParameter {
|
---|
| 47 | get { return (ILookupParameter<IOperator>)Parameters["LayerAnalyzer"]; }
|
---|
| 48 | }
|
---|
| 49 | #endregion
|
---|
| 50 |
|
---|
[11583] | 51 | public GeneticAlgorithmMainLoop MainOperator {
|
---|
| 52 | get { return OperatorGraph.Iterate().OfType<GeneticAlgorithmMainLoop>().First(); }
|
---|
| 53 | }
|
---|
[11586] | 54 | public EldersEmigrator EldersEmigrator {
|
---|
| 55 | get { return OperatorGraph.Iterate().OfType<EldersEmigrator>().First(); }
|
---|
| 56 | }
|
---|
[11583] | 57 |
|
---|
[11580] | 58 | [StorableConstructor]
|
---|
| 59 | private AlpsGeneticAlgorithmMainLoop(bool deserializing)
|
---|
| 60 | : base(deserializing) { }
|
---|
| 61 | private AlpsGeneticAlgorithmMainLoop(AlpsGeneticAlgorithmMainLoop original, Cloner cloner)
|
---|
| 62 | : base(original, cloner) { }
|
---|
| 63 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 64 | return new AlpsGeneticAlgorithmMainLoop(this, cloner);
|
---|
| 65 | }
|
---|
| 66 | public AlpsGeneticAlgorithmMainLoop()
|
---|
| 67 | : base() {
|
---|
| 68 | Parameters.Add(new LookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations that the algorithm should process."));
|
---|
| 69 | Parameters.Add(new LookupParameter<IOperator>("Analyzer", "The operator used to the analyze all individuals."));
|
---|
| 70 | Parameters.Add(new LookupParameter<IOperator>("LayerAnalyzer", "The operator used to analyze each layer."));
|
---|
| 71 |
|
---|
| 72 | var variableCreator = new VariableCreator() { Name = "Initialize" };
|
---|
| 73 | var resultsCollector = new ResultsCollector();
|
---|
| 74 | var initAnalyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
|
---|
| 75 | var initLayerAnalyzerProcessor = new SubScopesProcessor();
|
---|
| 76 | var initLayerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
|
---|
[11583] | 77 | var matingPoolCreator = new MatingPoolCreator() { Name = "Create Mating Pools" };
|
---|
[11580] | 78 | var matingPoolProcessor = new UniformSubScopesProcessor();
|
---|
[11586] | 79 | var mainOperator = CreatePreparedGeneticAlgorithmMainLoop();
|
---|
[11580] | 80 | var layerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
|
---|
[11583] | 81 | var generationsIcrementor = new IntCounter() { Name = "Increment Generations" };
|
---|
| 82 | var evaluatedSolutionsReducer = new DataReducer() { Name = "Increment EvaluatedSolutions" };
|
---|
[11585] | 83 | var eldersEmigrator = new EldersEmigrator() { Name = "Emigrate Elders" };
|
---|
[11586] | 84 | var layerUpdator = new LayerUpdator(mainOperator) { Name = "Update Layers" };
|
---|
[11580] | 85 | var analyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
|
---|
| 86 | var generationsComparator = new Comparator() { Name = "Generations >= MaximumGenerations" };
|
---|
| 87 | var terminateBranch = new ConditionalBranch() { Name = "Terminate?" };
|
---|
| 88 |
|
---|
| 89 | OperatorGraph.InitialOperator = variableCreator;
|
---|
| 90 |
|
---|
| 91 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
|
---|
| 92 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("GenerationsSinceLastRefresh", new IntValue(0)));
|
---|
[11586] | 93 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("OpenLayers", new IntValue(1)));
|
---|
[11580] | 94 | variableCreator.Successor = resultsCollector;
|
---|
| 95 |
|
---|
| 96 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
| 97 | resultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("LayerResults", "Result set for each layer", "Results"));
|
---|
| 98 | resultsCollector.Successor = initAnalyzerPlaceholder;
|
---|
| 99 |
|
---|
| 100 | initAnalyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 101 | initAnalyzerPlaceholder.Successor = initLayerAnalyzerProcessor;
|
---|
| 102 |
|
---|
| 103 | initLayerAnalyzerProcessor.Operators.Add(initLayerAnalyzerPlaceholder);
|
---|
| 104 | initLayerAnalyzerProcessor.Successor = matingPoolCreator;
|
---|
| 105 |
|
---|
| 106 | initLayerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
|
---|
| 107 | initLayerAnalyzerPlaceholder.Successor = null;
|
---|
| 108 |
|
---|
| 109 | matingPoolCreator.Successor = matingPoolProcessor;
|
---|
| 110 |
|
---|
| 111 | matingPoolProcessor.Parallel.Value = true;
|
---|
| 112 | matingPoolProcessor.Operator = mainOperator;
|
---|
[11583] | 113 | matingPoolProcessor.Successor = generationsIcrementor;
|
---|
[11580] | 114 |
|
---|
[11583] | 115 | generationsIcrementor.ValueParameter.ActualName = "Generations";
|
---|
| 116 | generationsIcrementor.Increment = new IntValue(1);
|
---|
| 117 | generationsIcrementor.Successor = evaluatedSolutionsReducer;
|
---|
| 118 |
|
---|
| 119 | evaluatedSolutionsReducer.ParameterToReduce.ActualName = "EvaluatedSolutions";
|
---|
| 120 | evaluatedSolutionsReducer.TargetParameter.ActualName = "EvaluatedSolutions";
|
---|
| 121 | evaluatedSolutionsReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
|
---|
| 122 | evaluatedSolutionsReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Sum);
|
---|
| 123 | evaluatedSolutionsReducer.Successor = eldersEmigrator;
|
---|
| 124 |
|
---|
[11580] | 125 | mainOperator.Successor = layerAnalyzerPlaceholder;
|
---|
| 126 |
|
---|
| 127 | layerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
|
---|
| 128 | layerAnalyzerPlaceholder.Successor = null;
|
---|
| 129 |
|
---|
| 130 | eldersEmigrator.Successor = layerUpdator;
|
---|
| 131 |
|
---|
| 132 | layerUpdator.Successor = analyzerPlaceholder;
|
---|
| 133 |
|
---|
| 134 | analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 135 | analyzerPlaceholder.Successor = generationsComparator;
|
---|
| 136 |
|
---|
| 137 | generationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
| 138 | generationsComparator.LeftSideParameter.ActualName = "Generations";
|
---|
| 139 | generationsComparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
| 140 | generationsComparator.ResultParameter.ActualName = "TerminateGenerations";
|
---|
| 141 | generationsComparator.Successor = terminateBranch;
|
---|
| 142 |
|
---|
[11583] | 143 | terminateBranch.ConditionParameter.ActualName = "TerminateGenerations";
|
---|
[11580] | 144 | terminateBranch.FalseBranch = matingPoolCreator;
|
---|
[11583] | 145 | }
|
---|
[11580] | 146 |
|
---|
[11586] | 147 | private GeneticAlgorithmMainLoop CreatePreparedGeneticAlgorithmMainLoop() {
|
---|
[11583] | 148 | var mainLoop = new GeneticAlgorithmMainLoop();
|
---|
| 149 | var selector = mainLoop.OperatorGraph.Iterate().OfType<Placeholder>().First(o => o.OperatorParameter.ActualName == "Selector");
|
---|
| 150 | var crossover = mainLoop.OperatorGraph.Iterate().OfType<Placeholder>().First(o => o.OperatorParameter.ActualName == "Crossover");
|
---|
| 151 | var elitesMerger = mainLoop.OperatorGraph.Iterate().OfType<MergingReducer>().First();
|
---|
| 152 |
|
---|
| 153 | // Operator starts with selector
|
---|
| 154 | mainLoop.OperatorGraph.InitialOperator = selector;
|
---|
| 155 |
|
---|
| 156 | // Insert AgeCalculator between crossover and its successor
|
---|
| 157 | var crossoverSuccessor = crossover.Successor;
|
---|
| 158 | var ageCalculator = new DataReducer() { Name = "Calculate Age" };
|
---|
| 159 | ageCalculator.ParameterToReduce.ActualName = "Age";
|
---|
| 160 | ageCalculator.TargetParameter.ActualName = "Age";
|
---|
| 161 | ageCalculator.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Max);
|
---|
| 162 | ageCalculator.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign);
|
---|
| 163 | crossover.Successor = ageCalculator;
|
---|
| 164 | ageCalculator.Successor = crossoverSuccessor;
|
---|
| 165 |
|
---|
| 166 | // Instead of generational loop after merging of elites, increment ages of all individuals
|
---|
| 167 | var processor = new UniformSubScopesProcessor();
|
---|
| 168 | var incrementor = new IntCounter() { Name = "Increment Age" };
|
---|
| 169 | processor.Operator = incrementor;
|
---|
| 170 | processor.Successor = null;
|
---|
| 171 | incrementor.ValueParameter.ActualName = "Age";
|
---|
| 172 | incrementor.Increment = new IntValue(1);
|
---|
| 173 | incrementor.Successor = null;
|
---|
| 174 | elitesMerger.Successor = processor;
|
---|
| 175 |
|
---|
| 176 | // Parameterize
|
---|
| 177 | foreach (var stochasticOperator in mainLoop.OperatorGraph.Iterate().OfType<IStochasticOperator>())
|
---|
| 178 | stochasticOperator.RandomParameter.ActualName = "LocalRandom";
|
---|
| 179 | foreach (var stochasticBranch in mainLoop.OperatorGraph.Iterate().OfType<StochasticBranch>())
|
---|
| 180 | stochasticBranch.RandomParameter.ActualName = "LocalRandom";
|
---|
| 181 |
|
---|
| 182 | return mainLoop;
|
---|
[11580] | 183 | }
|
---|
| 184 | }
|
---|
| 185 | } |
---|