| 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2012 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 HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Operators;
|
---|
| 26 | using HeuristicLab.Optimization.Operators;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 | using HeuristicLab.Selection;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Algorithms.CMAEvolutionStrategy {
|
---|
| 32 | [Item("CMAEvolutionStrategyMainLoop", "An operator which represents the main loop of a CMA evolution strategy.")]
|
---|
| 33 | [StorableClass]
|
---|
| 34 | public sealed class CMAEvolutionStrategyMainLoop : AlgorithmOperator {
|
---|
| 35 | #region Parameter properties
|
---|
| 36 | public IValueLookupParameter<IRandom> RandomParameter {
|
---|
| 37 | get { return (IValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 38 | }
|
---|
| 39 | public IValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 40 | get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 41 | }
|
---|
| 42 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 43 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 44 | }
|
---|
| 45 | public IValueLookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 46 | get { return (IValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 47 | }
|
---|
| 48 | public IValueLookupParameter<IntValue> PopulationSizeParameter {
|
---|
| 49 | get { return (IValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
| 50 | }
|
---|
| 51 | public IValueLookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 52 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
| 53 | }
|
---|
| 54 | public IValueLookupParameter<IntValue> MaximumEvaluatedSolutionsParameter {
|
---|
| 55 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumEvaluatedSolutions"]; }
|
---|
| 56 | }
|
---|
| 57 | public IValueLookupParameter<IOperator> MutatorParameter {
|
---|
| 58 | get { return (IValueLookupParameter<IOperator>)Parameters["Mutator"]; }
|
---|
| 59 | }
|
---|
| 60 | public IValueLookupParameter<IOperator> RecombinatorParameter {
|
---|
| 61 | get { return (IValueLookupParameter<IOperator>)Parameters["Recombinator"]; }
|
---|
| 62 | }
|
---|
| 63 | public IValueLookupParameter<IOperator> EvaluatorParameter {
|
---|
| 64 | get { return (IValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
|
---|
| 65 | }
|
---|
| 66 | public IValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
| 67 | get { return (IValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
| 68 | }
|
---|
| 69 | public IValueLookupParameter<IOperator> AnalyzerParameter {
|
---|
| 70 | get { return (IValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
| 71 | }
|
---|
| 72 | public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
| 73 | get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
|
---|
| 74 | }
|
---|
| 75 | public ILookupParameter<IntValue> GenerationsParameter {
|
---|
| 76 | get { return (ILookupParameter<IntValue>)Parameters["Generations"]; }
|
---|
| 77 | }
|
---|
| 78 | public ILookupParameter<IntValue> MuParameter {
|
---|
| 79 | get { return (ILookupParameter<IntValue>)Parameters["Mu"]; }
|
---|
| 80 | }
|
---|
| 81 | public IValueLookupParameter<IOperator> CMAESUpdaterParameter {
|
---|
| 82 | get { return (IValueLookupParameter<IOperator>)Parameters["CMAESUpdater"]; }
|
---|
| 83 | }
|
---|
| 84 | #endregion
|
---|
| 85 |
|
---|
| 86 | [StorableConstructor]
|
---|
| 87 | private CMAEvolutionStrategyMainLoop(bool deserializing) : base(deserializing) { }
|
---|
| 88 | private CMAEvolutionStrategyMainLoop(CMAEvolutionStrategyMainLoop original, Cloner cloner)
|
---|
| 89 | : base(original, cloner) {
|
---|
| 90 | }
|
---|
| 91 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 92 | return new CMAEvolutionStrategyMainLoop(this, cloner);
|
---|
| 93 | }
|
---|
| 94 | public CMAEvolutionStrategyMainLoop()
|
---|
| 95 | : base() {
|
---|
| 96 | Initialize();
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | private void Initialize() {
|
---|
| 100 | #region Create parameters
|
---|
| 101 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
| 102 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
| 103 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
| 104 | Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
|
---|
| 105 | Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "λ (lambda) - the size of the population."));
|
---|
| 106 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
|
---|
| 107 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumEvaluatedSolutions", "The maximum number of evaluated solutions that should be computed."));
|
---|
| 108 | Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
|
---|
| 109 | Parameters.Add(new ValueLookupParameter<IOperator>("Recombinator", "The operator used to cross solutions."));
|
---|
| 110 | Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions. This operator is executed in parallel, if an engine is used which supports parallelization."));
|
---|
| 111 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
| 112 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
|
---|
| 113 | Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
|
---|
| 114 | Parameters.Add(new LookupParameter<IntValue>("Generations", "The current generation that is being processed."));
|
---|
| 115 | Parameters.Add(new LookupParameter<IntValue>("Mu", "The number of offspring considered for updating of the strategy parameters."));
|
---|
| 116 | Parameters.Add(new ValueLookupParameter<IOperator>("CMAESUpdater", "The operator to update the strategy parameters."));
|
---|
| 117 | #endregion
|
---|
| 118 |
|
---|
| 119 | #region Create operators
|
---|
| 120 | var analyzer = new Placeholder();
|
---|
| 121 | var selector = new LeftSelector();
|
---|
| 122 | var ssp1 = new SubScopesProcessor();
|
---|
| 123 | var eo1 = new EmptyOperator();
|
---|
| 124 | var childrenCreator = new ChildrenCreator();
|
---|
| 125 | var ssp2 = new SubScopesProcessor();
|
---|
| 126 | var ussp1 = new UniformSubScopesProcessor();
|
---|
| 127 | var mutator = new Placeholder();
|
---|
| 128 | var ussp2 = new UniformSubScopesProcessor();
|
---|
| 129 | var evaluator1 = new Placeholder();
|
---|
| 130 | var evaluationsCounter = new IntCounter();
|
---|
| 131 | var sorter = new SubScopesSorter();
|
---|
| 132 | var selector2 = new LeftSelector();
|
---|
| 133 | var reducer2 = new RightReducer();
|
---|
| 134 | var recombinator = new Placeholder();
|
---|
| 135 | var evaluator2 = new Placeholder();
|
---|
| 136 | var updater = new Placeholder();
|
---|
| 137 | var reducer1 = new RightReducer();
|
---|
| 138 | var ssp3 = new SubScopesProcessor();
|
---|
| 139 | var subScopesRemover = new SubScopesRemover();
|
---|
| 140 | var generationsCounter = new IntCounter();
|
---|
| 141 | var comparatorEvaluations = new Comparator();
|
---|
| 142 | var branchEvaluations = new ConditionalBranch();
|
---|
| 143 | var comparatorGenerations = new Comparator();
|
---|
| 144 | var branchGenerations = new ConditionalBranch();
|
---|
| 145 | var analyzer2 = new Placeholder();
|
---|
| 146 |
|
---|
| 147 | analyzer.Name = "Analyzer (placeholder)";
|
---|
| 148 | analyzer.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 149 |
|
---|
| 150 | selector.CopySelected = new BoolValue(true);
|
---|
| 151 | selector.NumberOfSelectedSubScopesParameter.ActualName = PopulationSizeParameter.Name;
|
---|
| 152 |
|
---|
| 153 | childrenCreator.ParentsPerChild = null;
|
---|
| 154 | childrenCreator.ParentsPerChildParameter.ActualName = PopulationSizeParameter.Name;
|
---|
| 155 |
|
---|
| 156 | ussp1.Name = "Mutate Offspring";
|
---|
| 157 | ussp1.Parallel = new BoolValue(false);
|
---|
| 158 |
|
---|
| 159 | mutator.Name = "Mutator (placeholder)";
|
---|
| 160 | mutator.OperatorParameter.ActualName = MutatorParameter.Name;
|
---|
| 161 |
|
---|
| 162 | ussp2.Name = "Evaluate Offspring";
|
---|
| 163 | ussp2.Parallel = new BoolValue(true);
|
---|
| 164 |
|
---|
| 165 | evaluator1.Name = "Evaluator (placeholder)";
|
---|
| 166 | evaluator1.OperatorParameter.ActualName = EvaluatorParameter.Name;
|
---|
| 167 |
|
---|
| 168 | evaluationsCounter.Name = "Count EvaluatedSolutions";
|
---|
| 169 | evaluationsCounter.Increment = null;
|
---|
| 170 | evaluationsCounter.IncrementParameter.ActualName = PopulationSizeParameter.Name;
|
---|
| 171 |
|
---|
| 172 | sorter.Name = "Sort Scopes";
|
---|
| 173 | sorter.ValueParameter.ActualName = QualityParameter.Name;
|
---|
| 174 | sorter.ValueParameter.Depth = 1;
|
---|
| 175 | sorter.DescendingParameter.ActualName = MaximizationParameter.Name;
|
---|
| 176 |
|
---|
| 177 | selector2.NumberOfSelectedSubScopesParameter.ActualName = MuParameter.Name;
|
---|
| 178 | selector2.CopySelected = new BoolValue(false);
|
---|
| 179 |
|
---|
| 180 | reducer2.Name = "Drop Worst";
|
---|
| 181 |
|
---|
| 182 | recombinator.Name = "Update Mean (placeholder)";
|
---|
| 183 | recombinator.OperatorParameter.ActualName = RecombinatorParameter.Name;
|
---|
| 184 |
|
---|
| 185 | evaluator2.Name = "Evaluator (placeholder)";
|
---|
| 186 | evaluator2.OperatorParameter.ActualName = EvaluatorParameter.Name;
|
---|
| 187 |
|
---|
| 188 | updater.Name = "Update Strategy Parameters";
|
---|
| 189 | updater.OperatorParameter.ActualName = CMAESUpdaterParameter.Name;
|
---|
| 190 |
|
---|
| 191 | reducer1.Name = "Move to new mean";
|
---|
| 192 |
|
---|
| 193 | ssp3.Name = "Cleanup";
|
---|
| 194 |
|
---|
| 195 | subScopesRemover.Name = "Drop Best Offspring";
|
---|
| 196 | subScopesRemover.RemoveAllSubScopes = true;
|
---|
| 197 |
|
---|
| 198 | generationsCounter.Name = "Generations++";
|
---|
| 199 | generationsCounter.Increment = new IntValue(1);
|
---|
| 200 | generationsCounter.ValueParameter.ActualName = "Generations";
|
---|
| 201 |
|
---|
| 202 | comparatorEvaluations.Name = "Compare EvaluatedSolutions";
|
---|
| 203 | comparatorEvaluations.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
| 204 | comparatorEvaluations.LeftSideParameter.ActualName = "EvaluatedSolutions";
|
---|
| 205 | comparatorEvaluations.ResultParameter.ActualName = "Terminate";
|
---|
| 206 | comparatorEvaluations.RightSideParameter.ActualName = MaximumEvaluatedSolutionsParameter.Name;
|
---|
| 207 |
|
---|
| 208 | branchEvaluations.Name = "Terminate on MaximumEvaluations";
|
---|
| 209 | branchEvaluations.ConditionParameter.ActualName = "Terminate";
|
---|
| 210 |
|
---|
| 211 | comparatorGenerations.Name = "Compare Generations";
|
---|
| 212 | comparatorGenerations.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
| 213 | comparatorGenerations.LeftSideParameter.ActualName = "Generations";
|
---|
| 214 | comparatorGenerations.ResultParameter.ActualName = "Terminate";
|
---|
| 215 | comparatorGenerations.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
| 216 |
|
---|
| 217 | branchGenerations.Name = "Terminate on MaximumGenerations";
|
---|
| 218 | branchGenerations.ConditionParameter.ActualName = "Terminate";
|
---|
| 219 |
|
---|
| 220 | analyzer2.Name = "Analyzer (Placeholder)";
|
---|
| 221 | analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 222 | #endregion
|
---|
| 223 |
|
---|
| 224 | #region Create operator graph
|
---|
| 225 | OperatorGraph.InitialOperator = analyzer;
|
---|
| 226 | analyzer.Successor = selector;
|
---|
| 227 | selector.Successor = ssp1;
|
---|
| 228 | ssp1.Operators.Add(eo1);
|
---|
| 229 | ssp1.Operators.Add(childrenCreator);
|
---|
| 230 | ssp1.Successor = generationsCounter;
|
---|
| 231 | eo1.Successor = null;
|
---|
| 232 | childrenCreator.Successor = ssp2;
|
---|
| 233 | ssp2.Operators.Add(ussp1);
|
---|
| 234 | ssp2.Successor = null;
|
---|
| 235 | ussp1.Operator = mutator;
|
---|
| 236 | ussp1.Successor = ussp2;
|
---|
| 237 | mutator.Successor = null;
|
---|
| 238 | ussp2.Operator = evaluator1;
|
---|
| 239 | ussp2.Successor = evaluationsCounter;
|
---|
| 240 | evaluator1.Successor = null;
|
---|
| 241 | evaluationsCounter.Successor = sorter;
|
---|
| 242 | sorter.Successor = selector2;
|
---|
| 243 | selector2.Successor = reducer2;
|
---|
| 244 | reducer2.Successor = recombinator;
|
---|
| 245 | recombinator.Successor = evaluator2;
|
---|
| 246 | evaluator2.Successor = null;
|
---|
| 247 | generationsCounter.Successor = updater;
|
---|
| 248 | updater.Successor = reducer1;
|
---|
| 249 | reducer1.Successor = ssp3;
|
---|
| 250 | ssp3.Operators.Add(subScopesRemover);
|
---|
| 251 | ssp3.Successor = comparatorEvaluations;
|
---|
| 252 | subScopesRemover.Successor = null;
|
---|
| 253 | comparatorEvaluations.Successor = branchEvaluations;
|
---|
| 254 | branchEvaluations.FalseBranch = comparatorGenerations;
|
---|
| 255 | branchEvaluations.TrueBranch = analyzer2;
|
---|
| 256 | branchEvaluations.Successor = null;
|
---|
| 257 | comparatorGenerations.Successor = branchGenerations;
|
---|
| 258 | branchGenerations.FalseBranch = analyzer;
|
---|
| 259 | branchGenerations.TrueBranch = analyzer2;
|
---|
| 260 | branchGenerations.Successor = null;
|
---|
| 261 | analyzer2.Successor = null;
|
---|
| 262 | #endregion
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | public override IOperation Apply() {
|
---|
| 266 | if (MutatorParameter.ActualValue == null || RecombinatorParameter.ActualValue == null)
|
---|
| 267 | return null;
|
---|
| 268 | return base.Apply();
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
| 271 | }
|
---|