[12331] | 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.OffspringSelectionGeneticAlgorithm;
|
---|
| 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 | using HeuristicLab.Selection;
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.Algorithms.ALPS.OffspringSelection {
|
---|
| 35 | [Item("AlpsOffspringSelectionGeneticAlgorithmMainLoop", "An ALPS OS GA Main Loop Operator.")]
|
---|
| 36 | [StorableClass]
|
---|
| 37 | public sealed class AlpsOffspringSelectionGeneticAlgorithmMainLoop : 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 | public LookupParameter<DoubleValue> ComparisonFactorParameter {
|
---|
| 55 | get { return (LookupParameter<DoubleValue>)Parameters["ComparisonFactor"]; }
|
---|
| 56 | }
|
---|
| 57 | public ValueLookupParameter<DoubleValue> ComparisonFactorStartParameter {
|
---|
| 58 | get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorStart"]; }
|
---|
| 59 | }
|
---|
| 60 | public ValueLookupParameter<IOperator> ComparisonFactorModifierParameter {
|
---|
| 61 | get { return (ValueLookupParameter<IOperator>)Parameters["ComparisonFactorModifier"]; }
|
---|
| 62 | }
|
---|
| 63 | #endregion
|
---|
| 64 |
|
---|
| 65 | public OffspringSelectionGeneticAlgorithmMainOperator MainOperator {
|
---|
| 66 | get { return OperatorGraph.Iterate().OfType<OffspringSelectionGeneticAlgorithmMainOperator>().First(); }
|
---|
| 67 | }
|
---|
| 68 | public EldersEmigrator EldersEmigrator {
|
---|
| 69 | get { return OperatorGraph.Iterate().OfType<EldersEmigrator>().First(); }
|
---|
| 70 | }
|
---|
| 71 | public LayerUpdator LayerUpdator {
|
---|
| 72 | get { return OperatorGraph.Iterate().OfType<LayerUpdator>().First(); }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | [StorableConstructor]
|
---|
| 76 | private AlpsOffspringSelectionGeneticAlgorithmMainLoop(bool deserializing)
|
---|
| 77 | : base(deserializing) { }
|
---|
| 78 | private AlpsOffspringSelectionGeneticAlgorithmMainLoop(AlpsOffspringSelectionGeneticAlgorithmMainLoop original, Cloner cloner)
|
---|
| 79 | : base(original, cloner) { }
|
---|
| 80 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 81 | return new AlpsOffspringSelectionGeneticAlgorithmMainLoop(this, cloner);
|
---|
| 82 | }
|
---|
| 83 | public AlpsOffspringSelectionGeneticAlgorithmMainLoop()
|
---|
| 84 | : base() {
|
---|
| 85 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
| 86 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
| 87 | Parameters.Add(new LookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations that the algorithm should process."));
|
---|
| 88 | Parameters.Add(new LookupParameter<IOperator>("Analyzer", "The operator used to the analyze all individuals."));
|
---|
| 89 | Parameters.Add(new LookupParameter<IOperator>("LayerAnalyzer", "The operator used to analyze each layer."));
|
---|
| 90 | Parameters.Add(new LookupParameter<DoubleValue>("ComparisonFactor", "The comparison factor is used to determine whether the offspring should be compared to the better parent, the worse parent or a quality value linearly interpolated between them. It is in the range [0;1]."));
|
---|
| 91 | Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorStart", "The initial value for the comparison factor."));
|
---|
| 92 | Parameters.Add(new ValueLookupParameter<IOperator>("ComparisonFactorModifier", "The operator used to modify the comparison factor."));
|
---|
| 93 |
|
---|
| 94 | var variableCreator = new VariableCreator() { Name = "Initialize" };
|
---|
| 95 | var initializeComparisonFactor = new Assigner() { Name = "Initialize ComparisonFactor" };
|
---|
| 96 | var initLayerAnalyzerProcessor = new SubScopesProcessor();
|
---|
| 97 | var layerVariableCreator = new VariableCreator() { Name = "Initialize Layer" };
|
---|
| 98 | var initLayerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
|
---|
| 99 | var initAnalyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
|
---|
| 100 | var resultsCollector = new ResultsCollector();
|
---|
| 101 | var matingPoolPreProcessor = new UniformSubScopesProcessor() { Name = "MatingPoolPreProcessor" };
|
---|
| 102 | var matingPoolPreSorter = new SubScopesSorter() { Name = "MatingPoolPreSorter" };
|
---|
| 103 | var matingPoolCreator = new MatingPoolCreator() { Name = "Create Mating Pools" };
|
---|
| 104 | var matingPoolProcessor = new LayerUniformSubScopesProcessor();
|
---|
| 105 | var initializeLayer = new Assigner() { Name = "Reset LayerEvaluatedSolutions" };
|
---|
| 106 | var mainOperator = CreatePreparedOSGAMainOperator();
|
---|
| 107 | var generationsIcrementor = new IntCounter() { Name = "Increment Generations" };
|
---|
| 108 | var evaluatedSolutionsReducer = new DataReducer() { Name = "Increment EvaluatedSolutions" };
|
---|
| 109 | var eldersEmigrator = new EldersEmigrator() { Name = "Emigrate Elders" };
|
---|
| 110 | var updateComparisonFactor = new Placeholder() { Name = "Update ComparisonFactor (Placeholder)" };
|
---|
| 111 | var layerUpdator = new LayerUpdator(mainOperator) { Name = "Update Layers" };
|
---|
| 112 | var layerAnalyzerProcessor = new LayerUniformSubScopesProcessor();
|
---|
| 113 | var layerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
|
---|
| 114 | var analyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
|
---|
[12550] | 115 | var termination = new TerminationOperator();
|
---|
[12331] | 116 |
|
---|
| 117 | OperatorGraph.InitialOperator = variableCreator;
|
---|
| 118 |
|
---|
| 119 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
|
---|
| 120 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("GenerationsSinceLastRefresh", new IntValue(0)));
|
---|
| 121 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("OpenLayers", new IntValue(1)));
|
---|
| 122 | variableCreator.Successor = initializeComparisonFactor;
|
---|
| 123 |
|
---|
| 124 | initializeComparisonFactor.LeftSideParameter.ActualName = "ComparisonFactor";
|
---|
| 125 | initializeComparisonFactor.RightSideParameter.ActualName = "ComparisonFactorStart";
|
---|
| 126 | initializeComparisonFactor.Successor = initLayerAnalyzerProcessor;
|
---|
| 127 |
|
---|
| 128 | initLayerAnalyzerProcessor.Operators.Add(layerVariableCreator);
|
---|
| 129 | initLayerAnalyzerProcessor.Successor = initAnalyzerPlaceholder;
|
---|
| 130 |
|
---|
| 131 | layerVariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("LayerEvaluatedSolutions"));
|
---|
| 132 | layerVariableCreator.CollectedValues.Add(new ValueParameter<ResultCollection>("LayerResults"));
|
---|
[12348] | 133 | layerVariableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("SelectionPressure", new DoubleValue(0)));
|
---|
| 134 | layerVariableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("CurrentSuccessRatio", new DoubleValue(0)));
|
---|
[12331] | 135 | layerVariableCreator.Successor = initLayerAnalyzerPlaceholder;
|
---|
| 136 |
|
---|
| 137 | initLayerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
|
---|
| 138 | initLayerAnalyzerPlaceholder.Successor = null;
|
---|
| 139 |
|
---|
| 140 | initAnalyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 141 | initAnalyzerPlaceholder.Successor = resultsCollector;
|
---|
| 142 |
|
---|
| 143 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
| 144 | resultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("LayerResults", "Result set for each layer", "LayerResults"));
|
---|
| 145 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("OpenLayers"));
|
---|
| 146 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Comparison Factor", null, ComparisonFactorParameter.Name));
|
---|
| 147 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Selection Pressure", "Displays the rising selection pressure during a generation.", "SelectionPressure"));
|
---|
| 148 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Success Ratio", "Indicates how many successful children were already found during a generation (relative to the population size).", "CurrentSuccessRatio"));
|
---|
| 149 | resultsCollector.CopyValue = new BoolValue(false);
|
---|
| 150 | resultsCollector.Successor = matingPoolPreProcessor;
|
---|
| 151 |
|
---|
| 152 | matingPoolPreProcessor.Operator = matingPoolPreSorter;
|
---|
| 153 | matingPoolPreProcessor.Successor = matingPoolCreator;
|
---|
| 154 |
|
---|
| 155 | matingPoolPreSorter.ValueParameter.ActualName = QualityParameter.Name;
|
---|
| 156 | matingPoolPreSorter.DescendingParameter.ActualName = MaximizationParameter.Name;
|
---|
| 157 |
|
---|
| 158 | matingPoolCreator.Successor = matingPoolProcessor;
|
---|
| 159 |
|
---|
| 160 | matingPoolProcessor.Parallel.Value = true;
|
---|
| 161 | matingPoolProcessor.Operator = initializeLayer;
|
---|
| 162 | matingPoolProcessor.Successor = generationsIcrementor;
|
---|
| 163 |
|
---|
| 164 | initializeLayer.LeftSideParameter.ActualName = "LayerEvaluatedSolutions";
|
---|
| 165 | initializeLayer.RightSideParameter.Value = new IntValue(0);
|
---|
| 166 | initializeLayer.Successor = mainOperator;
|
---|
| 167 |
|
---|
| 168 | generationsIcrementor.ValueParameter.ActualName = "Generations";
|
---|
| 169 | generationsIcrementor.Increment = new IntValue(1);
|
---|
| 170 | generationsIcrementor.Successor = evaluatedSolutionsReducer;
|
---|
| 171 |
|
---|
| 172 | evaluatedSolutionsReducer.ParameterToReduce.ActualName = "LayerEvaluatedSolutions";
|
---|
| 173 | evaluatedSolutionsReducer.TargetParameter.ActualName = "EvaluatedSolutions";
|
---|
| 174 | evaluatedSolutionsReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
|
---|
| 175 | evaluatedSolutionsReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Sum);
|
---|
| 176 | evaluatedSolutionsReducer.Successor = updateComparisonFactor;
|
---|
| 177 |
|
---|
| 178 | updateComparisonFactor.OperatorParameter.ActualName = "ComparisonFactorModifier";
|
---|
| 179 | updateComparisonFactor.Successor = eldersEmigrator;
|
---|
| 180 |
|
---|
| 181 | eldersEmigrator.Successor = layerUpdator;
|
---|
| 182 |
|
---|
| 183 | layerUpdator.Successor = layerAnalyzerProcessor;
|
---|
| 184 |
|
---|
| 185 | layerAnalyzerProcessor.Operator = layerAnalyzerPlaceholder;
|
---|
| 186 | layerAnalyzerProcessor.Successor = analyzerPlaceholder;
|
---|
| 187 |
|
---|
| 188 | layerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
|
---|
| 189 |
|
---|
| 190 | analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
[12550] | 191 | analyzerPlaceholder.Successor = termination;
|
---|
[12331] | 192 |
|
---|
[12550] | 193 | termination.ContinueBranch = matingPoolPreProcessor;
|
---|
[12331] | 194 | }
|
---|
| 195 |
|
---|
| 196 | private OffspringSelectionGeneticAlgorithmMainOperator CreatePreparedOSGAMainOperator() {
|
---|
| 197 | var mainLoop = new OffspringSelectionGeneticAlgorithmMainOperator();
|
---|
| 198 |
|
---|
| 199 | // Increment ages of all individuals after replacement (merging of elites)
|
---|
| 200 | var elitesMerger = mainLoop.OperatorGraph.Iterate().OfType<MergingReducer>().Last();
|
---|
| 201 | var incrementAgeProcessor = new UniformSubScopesProcessor();
|
---|
| 202 | var ageIncrementor = new IntCounter() { Name = "Increment Age" };
|
---|
| 203 | elitesMerger.Successor = incrementAgeProcessor;
|
---|
| 204 | incrementAgeProcessor.Operator = ageIncrementor;
|
---|
| 205 | incrementAgeProcessor.Successor = null;
|
---|
| 206 | ageIncrementor.ValueParameter.ActualName = "Age";
|
---|
| 207 | ageIncrementor.Increment = new IntValue(1);
|
---|
| 208 |
|
---|
| 209 | // Insert AgeCalculator between crossover and its successor
|
---|
| 210 | var crossovers = mainLoop.OperatorGraph.Iterate().OfType<Placeholder>().Where(o => o.OperatorParameter.ActualName == "Crossover");
|
---|
| 211 | foreach (var crossover in crossovers) {
|
---|
| 212 | var crossoverSuccessor = crossover.Successor;
|
---|
| 213 | var ageCalculator = new DataReducer() { Name = "Calculate Age" };
|
---|
| 214 | crossover.Successor = ageCalculator;
|
---|
| 215 |
|
---|
| 216 | ageCalculator.ParameterToReduce.ActualName = "Age";
|
---|
| 217 | ageCalculator.TargetParameter.ActualName = "Age";
|
---|
| 218 | ageCalculator.ReductionOperation.Value = null;
|
---|
| 219 | ageCalculator.ReductionOperation.ActualName = "AgeInheritanceReduction";
|
---|
| 220 | ageCalculator.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign);
|
---|
| 221 | ageCalculator.Successor = crossoverSuccessor;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | // When counting the evaluated solutions, write in LayerEvaluatedSolutions
|
---|
| 225 | var subScopesCounters = mainLoop.OperatorGraph.Iterate().OfType<SubScopesCounter>();
|
---|
| 226 | foreach (var subScopesCounter in subScopesCounters) {
|
---|
| 227 | subScopesCounter.ValueParameter.ActualName = "LayerEvaluatedSolutions";
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | // Parameterize
|
---|
| 231 | foreach (var stochasticOperator in mainLoop.OperatorGraph.Iterate().OfType<IStochasticOperator>())
|
---|
| 232 | stochasticOperator.RandomParameter.ActualName = "LocalRandom";
|
---|
| 233 | foreach (var stochasticBranch in mainLoop.OperatorGraph.Iterate().OfType<StochasticBranch>())
|
---|
| 234 | stochasticBranch.RandomParameter.ActualName = "LocalRandom";
|
---|
| 235 |
|
---|
| 236 | return mainLoop;
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
| 239 | } |
---|