[11580] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12018] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11580] | 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
|
---|
[12045] | 40 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 41 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 42 | }
|
---|
| 43 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 44 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 45 | }
|
---|
[11580] | 46 | public ILookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 47 | get { return (ILookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
| 48 | }
|
---|
| 49 | public ILookupParameter<IOperator> AnalyzerParameter {
|
---|
| 50 | get { return (ILookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
| 51 | }
|
---|
| 52 | public ILookupParameter<IOperator> LayerAnalyzerParameter {
|
---|
| 53 | get { return (ILookupParameter<IOperator>)Parameters["LayerAnalyzer"]; }
|
---|
| 54 | }
|
---|
| 55 | #endregion
|
---|
| 56 |
|
---|
[11583] | 57 | public GeneticAlgorithmMainLoop MainOperator {
|
---|
| 58 | get { return OperatorGraph.Iterate().OfType<GeneticAlgorithmMainLoop>().First(); }
|
---|
| 59 | }
|
---|
[11586] | 60 | public EldersEmigrator EldersEmigrator {
|
---|
| 61 | get { return OperatorGraph.Iterate().OfType<EldersEmigrator>().First(); }
|
---|
| 62 | }
|
---|
[11590] | 63 | public LayerUpdator LayerUpdator {
|
---|
| 64 | get { return OperatorGraph.Iterate().OfType<LayerUpdator>().First(); }
|
---|
| 65 | }
|
---|
[11583] | 66 |
|
---|
[11580] | 67 | [StorableConstructor]
|
---|
| 68 | private AlpsGeneticAlgorithmMainLoop(bool deserializing)
|
---|
| 69 | : base(deserializing) { }
|
---|
| 70 | private AlpsGeneticAlgorithmMainLoop(AlpsGeneticAlgorithmMainLoop original, Cloner cloner)
|
---|
| 71 | : base(original, cloner) { }
|
---|
| 72 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 73 | return new AlpsGeneticAlgorithmMainLoop(this, cloner);
|
---|
| 74 | }
|
---|
| 75 | public AlpsGeneticAlgorithmMainLoop()
|
---|
| 76 | : base() {
|
---|
[12997] | 77 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false.") { Hidden = true });
|
---|
[12045] | 78 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
[11580] | 79 | Parameters.Add(new LookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations that the algorithm should process."));
|
---|
| 80 | Parameters.Add(new LookupParameter<IOperator>("Analyzer", "The operator used to the analyze all individuals."));
|
---|
| 81 | Parameters.Add(new LookupParameter<IOperator>("LayerAnalyzer", "The operator used to analyze each layer."));
|
---|
| 82 |
|
---|
| 83 | var variableCreator = new VariableCreator() { Name = "Initialize" };
|
---|
| 84 | var initLayerAnalyzerProcessor = new SubScopesProcessor();
|
---|
[11590] | 85 | var layerVariableCreator = new VariableCreator() { Name = "Initialize Layer" };
|
---|
[11580] | 86 | var initLayerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
|
---|
[11609] | 87 | var initAnalyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
|
---|
| 88 | var resultsCollector = new ResultsCollector();
|
---|
[11583] | 89 | var matingPoolCreator = new MatingPoolCreator() { Name = "Create Mating Pools" };
|
---|
[12992] | 90 | var matingPoolProcessor = new UniformSubScopesProcessor();
|
---|
[11590] | 91 | var initializeLayer = new Assigner() { Name = "Reset LayerEvaluatedSolutions" };
|
---|
[11586] | 92 | var mainOperator = CreatePreparedGeneticAlgorithmMainLoop();
|
---|
[11583] | 93 | var generationsIcrementor = new IntCounter() { Name = "Increment Generations" };
|
---|
| 94 | var evaluatedSolutionsReducer = new DataReducer() { Name = "Increment EvaluatedSolutions" };
|
---|
[11585] | 95 | var eldersEmigrator = new EldersEmigrator() { Name = "Emigrate Elders" };
|
---|
[11586] | 96 | var layerUpdator = new LayerUpdator(mainOperator) { Name = "Update Layers" };
|
---|
[12992] | 97 | var layerAnalyzerProcessor = new UniformSubScopesProcessor();
|
---|
[12197] | 98 | var layerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
|
---|
[11580] | 99 | var analyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
|
---|
[12531] | 100 | var termination = new TerminationOperator();
|
---|
[11580] | 101 |
|
---|
| 102 | OperatorGraph.InitialOperator = variableCreator;
|
---|
| 103 |
|
---|
| 104 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
|
---|
| 105 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("GenerationsSinceLastRefresh", new IntValue(0)));
|
---|
[11586] | 106 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("OpenLayers", new IntValue(1)));
|
---|
[11609] | 107 | variableCreator.Successor = initLayerAnalyzerProcessor;
|
---|
[11580] | 108 |
|
---|
[11590] | 109 | initLayerAnalyzerProcessor.Operators.Add(layerVariableCreator);
|
---|
[11609] | 110 | initLayerAnalyzerProcessor.Successor = initAnalyzerPlaceholder;
|
---|
[11580] | 111 |
|
---|
[11590] | 112 | layerVariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("LayerEvaluatedSolutions"));
|
---|
[12071] | 113 | layerVariableCreator.CollectedValues.Add(new ValueParameter<ResultCollection>("LayerResults"));
|
---|
[11590] | 114 | layerVariableCreator.Successor = initLayerAnalyzerPlaceholder;
|
---|
| 115 |
|
---|
[11580] | 116 | initLayerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
|
---|
| 117 | initLayerAnalyzerPlaceholder.Successor = null;
|
---|
| 118 |
|
---|
[11609] | 119 | initAnalyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 120 | initAnalyzerPlaceholder.Successor = resultsCollector;
|
---|
| 121 |
|
---|
| 122 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
[12071] | 123 | resultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("LayerResults", "Result set for each layer", "LayerResults"));
|
---|
[11609] | 124 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("OpenLayers"));
|
---|
| 125 | resultsCollector.CopyValue = new BoolValue(false);
|
---|
[13031] | 126 | resultsCollector.Successor = matingPoolCreator;
|
---|
[11609] | 127 |
|
---|
[11580] | 128 | matingPoolCreator.Successor = matingPoolProcessor;
|
---|
| 129 |
|
---|
| 130 | matingPoolProcessor.Parallel.Value = true;
|
---|
[11590] | 131 | matingPoolProcessor.Operator = initializeLayer;
|
---|
[11583] | 132 | matingPoolProcessor.Successor = generationsIcrementor;
|
---|
[11580] | 133 |
|
---|
[11590] | 134 | initializeLayer.LeftSideParameter.ActualName = "LayerEvaluatedSolutions";
|
---|
| 135 | initializeLayer.RightSideParameter.Value = new IntValue(0);
|
---|
| 136 | initializeLayer.Successor = mainOperator;
|
---|
| 137 |
|
---|
[11583] | 138 | generationsIcrementor.ValueParameter.ActualName = "Generations";
|
---|
| 139 | generationsIcrementor.Increment = new IntValue(1);
|
---|
| 140 | generationsIcrementor.Successor = evaluatedSolutionsReducer;
|
---|
| 141 |
|
---|
[11676] | 142 | evaluatedSolutionsReducer.ParameterToReduce.ActualName = "LayerEvaluatedSolutions";
|
---|
[11583] | 143 | evaluatedSolutionsReducer.TargetParameter.ActualName = "EvaluatedSolutions";
|
---|
| 144 | evaluatedSolutionsReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
|
---|
| 145 | evaluatedSolutionsReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Sum);
|
---|
| 146 | evaluatedSolutionsReducer.Successor = eldersEmigrator;
|
---|
| 147 |
|
---|
[12197] | 148 | eldersEmigrator.Successor = layerUpdator;
|
---|
[11580] | 149 |
|
---|
[12197] | 150 | layerUpdator.Successor = layerAnalyzerProcessor;
|
---|
[11580] | 151 |
|
---|
[12197] | 152 | layerAnalyzerProcessor.Operator = layerAnalyzerPlaceholder;
|
---|
| 153 | layerAnalyzerProcessor.Successor = analyzerPlaceholder;
|
---|
[11580] | 154 |
|
---|
[12197] | 155 | layerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
|
---|
[11580] | 156 |
|
---|
| 157 | analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
[12531] | 158 | analyzerPlaceholder.Successor = termination;
|
---|
[11580] | 159 |
|
---|
[13031] | 160 | termination.ContinueBranch = matingPoolCreator;
|
---|
[11583] | 161 | }
|
---|
[11580] | 162 |
|
---|
[11586] | 163 | private GeneticAlgorithmMainLoop CreatePreparedGeneticAlgorithmMainLoop() {
|
---|
[11583] | 164 | var mainLoop = new GeneticAlgorithmMainLoop();
|
---|
[12186] | 165 |
|
---|
[12998] | 166 | var oldInitialOp = mainLoop.OperatorGraph.InitialOperator;
|
---|
[11583] | 167 | var selector = mainLoop.OperatorGraph.Iterate().OfType<Placeholder>().First(o => o.OperatorParameter.ActualName == "Selector");
|
---|
| 168 | var crossover = mainLoop.OperatorGraph.Iterate().OfType<Placeholder>().First(o => o.OperatorParameter.ActualName == "Crossover");
|
---|
[11590] | 169 | var subScopesCounter = mainLoop.OperatorGraph.Iterate().OfType<SubScopesCounter>().First();
|
---|
[11583] | 170 | var elitesMerger = mainLoop.OperatorGraph.Iterate().OfType<MergingReducer>().First();
|
---|
| 171 |
|
---|
[12186] | 172 | // Operator starts with calculating number of selected scopes base on plus/comma-selection replacement scheme
|
---|
| 173 | var numberOfSubScopesBranch = new ConditionalBranch() { Name = "PlusSelection?" };
|
---|
| 174 | var numberOfSelectedSubScopesPlusCalculator = new ExpressionCalculator() { Name = "NumberOfSelectedSubScopes = PopulationSize * 2" };
|
---|
| 175 | var numberOfSelectedSubScopesCalculator = new ExpressionCalculator() { Name = "NumberOfSelectedSubScopes = (PopulationSize - Elites) * 2" };
|
---|
| 176 | var replacementBranch = new ConditionalBranch() { Name = "PlusSelection?" };
|
---|
| 177 |
|
---|
[12998] | 178 | // Set new initial operator
|
---|
[12186] | 179 | mainLoop.OperatorGraph.InitialOperator = numberOfSubScopesBranch;
|
---|
| 180 |
|
---|
| 181 | numberOfSubScopesBranch.ConditionParameter.ActualName = "PlusSelection";
|
---|
| 182 | numberOfSubScopesBranch.TrueBranch = numberOfSelectedSubScopesPlusCalculator;
|
---|
| 183 | numberOfSubScopesBranch.FalseBranch = numberOfSelectedSubScopesCalculator;
|
---|
| 184 | numberOfSubScopesBranch.Successor = selector;
|
---|
| 185 |
|
---|
| 186 | numberOfSelectedSubScopesPlusCalculator.CollectedValues.Add(new LookupParameter<IntValue>("PopulationSize"));
|
---|
| 187 | numberOfSelectedSubScopesPlusCalculator.ExpressionResultParameter.ActualName = "NumberOfSelectedSubScopes";
|
---|
| 188 | numberOfSelectedSubScopesPlusCalculator.ExpressionParameter.Value = new StringValue("PopulationSize 2 * toint");
|
---|
| 189 |
|
---|
[12094] | 190 | numberOfSelectedSubScopesCalculator.CollectedValues.Add(new LookupParameter<IntValue>("PopulationSize"));
|
---|
| 191 | numberOfSelectedSubScopesCalculator.CollectedValues.Add(new LookupParameter<IntValue>("Elites"));
|
---|
| 192 | numberOfSelectedSubScopesCalculator.ExpressionResultParameter.ActualName = "NumberOfSelectedSubScopes";
|
---|
| 193 | numberOfSelectedSubScopesCalculator.ExpressionParameter.Value = new StringValue("PopulationSize Elites - 2 * toint");
|
---|
[11583] | 194 |
|
---|
[12186] | 195 | // Use Elitism or Plus-Selection as replacement strategy
|
---|
| 196 | var selectedProcessor = (SubScopesProcessor)selector.Successor;
|
---|
| 197 | var elitismReplacement = selectedProcessor.Successor;
|
---|
| 198 | selectedProcessor.Successor = replacementBranch;
|
---|
| 199 | replacementBranch.ConditionParameter.ActualName = "PlusSelection";
|
---|
| 200 | replacementBranch.FalseBranch = elitismReplacement;
|
---|
| 201 |
|
---|
| 202 | // Plus selection replacement
|
---|
| 203 | var replacementMergingReducer = new MergingReducer();
|
---|
| 204 | var replacementBestSelector = new BestSelector();
|
---|
| 205 | var replacementRightReducer = new RightReducer();
|
---|
| 206 | replacementBranch.TrueBranch = replacementMergingReducer;
|
---|
| 207 |
|
---|
| 208 | replacementMergingReducer.Successor = replacementBestSelector;
|
---|
| 209 |
|
---|
| 210 | replacementBestSelector.NumberOfSelectedSubScopesParameter.ActualName = "PopulationSize";
|
---|
| 211 | replacementBestSelector.CopySelected = new BoolValue(false);
|
---|
| 212 | replacementBestSelector.Successor = replacementRightReducer;
|
---|
| 213 |
|
---|
| 214 | replacementRightReducer.Successor = null;
|
---|
| 215 |
|
---|
| 216 | // Increment ages of all individuals after replacement
|
---|
| 217 | var incrementAgeProcessor = new UniformSubScopesProcessor();
|
---|
[13046] | 218 | var ageIncrementor = new DoubleCounter() { Name = "Increment Age" };
|
---|
[12186] | 219 | replacementBranch.Successor = incrementAgeProcessor;
|
---|
| 220 | incrementAgeProcessor.Operator = ageIncrementor;
|
---|
| 221 | incrementAgeProcessor.Successor = null;
|
---|
| 222 | ageIncrementor.ValueParameter.ActualName = "Age";
|
---|
[13046] | 223 | ageIncrementor.Increment = new DoubleValue(1.0);
|
---|
[12186] | 224 |
|
---|
[11583] | 225 | // Insert AgeCalculator between crossover and its successor
|
---|
| 226 | var crossoverSuccessor = crossover.Successor;
|
---|
[12997] | 227 | var ageCalculator = new WeightingReducer() { Name = "Calculate Age" };
|
---|
[12186] | 228 | crossover.Successor = ageCalculator;
|
---|
| 229 |
|
---|
[11583] | 230 | ageCalculator.ParameterToReduce.ActualName = "Age";
|
---|
| 231 | ageCalculator.TargetParameter.ActualName = "Age";
|
---|
[12997] | 232 | ageCalculator.WeightParameter.ActualName = "AgeInheritance";
|
---|
[11583] | 233 | ageCalculator.Successor = crossoverSuccessor;
|
---|
| 234 |
|
---|
[11590] | 235 | // When counting the evaluated solutions, write in LayerEvaluatedSolutions
|
---|
| 236 | subScopesCounter.ValueParameter.ActualName = "LayerEvaluatedSolutions";
|
---|
| 237 | subScopesCounter.AccumulateParameter.Value = new BoolValue(false);
|
---|
| 238 |
|
---|
[12186] | 239 | // Instead of generational loop after merging of elites, stop
|
---|
| 240 | elitesMerger.Successor = null;
|
---|
[11583] | 241 |
|
---|
| 242 | // Parameterize
|
---|
| 243 | foreach (var stochasticOperator in mainLoop.OperatorGraph.Iterate().OfType<IStochasticOperator>())
|
---|
| 244 | stochasticOperator.RandomParameter.ActualName = "LocalRandom";
|
---|
| 245 | foreach (var stochasticBranch in mainLoop.OperatorGraph.Iterate().OfType<StochasticBranch>())
|
---|
| 246 | stochasticBranch.RandomParameter.ActualName = "LocalRandom";
|
---|
| 247 |
|
---|
[13035] | 248 | // Remove unused operators
|
---|
| 249 | var usedOperators = mainLoop.OperatorGraph.Iterate();
|
---|
| 250 | var unusedOperators = mainLoop.OperatorGraph.Operators.Except(usedOperators);
|
---|
| 251 | foreach (var op in unusedOperators.ToList())
|
---|
| 252 | mainLoop.OperatorGraph.Operators.Remove(op);
|
---|
| 253 |
|
---|
[11583] | 254 | return mainLoop;
|
---|
[11580] | 255 | }
|
---|
| 256 | }
|
---|
| 257 | } |
---|