[3130] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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.Analysis;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Operators;
|
---|
| 27 | using HeuristicLab.Optimization.Operators;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 | using HeuristicLab.Selection;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Algorithms.EvolutionStrategy {
|
---|
| 33 | /// <summary>
|
---|
| 34 | /// An operator which represents the main loop of an evolution strategy (EvolutionStrategy).
|
---|
| 35 | /// </summary>
|
---|
| 36 | [Item("EvolutionStrategyMainLoop", "An operator which represents the main loop of an evolution strategy (EvolutionStrategy).")]
|
---|
| 37 | [StorableClass]
|
---|
| 38 | public sealed class EvolutionStrategyMainLoop : AlgorithmOperator {
|
---|
| 39 | #region Parameter properties
|
---|
| 40 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
| 41 | get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 42 | }
|
---|
| 43 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 44 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 45 | }
|
---|
| 46 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 47 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 48 | }
|
---|
| 49 | public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 50 | get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 51 | }
|
---|
| 52 | public ValueLookupParameter<IntValue> PopulationSizeParameter {
|
---|
| 53 | get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
| 54 | }
|
---|
| 55 | public ValueLookupParameter<IntValue> ParentsPerChildParameter {
|
---|
| 56 | get { return (ValueLookupParameter<IntValue>)Parameters["ParentsPerChild"]; }
|
---|
| 57 | }
|
---|
| 58 | public ValueLookupParameter<IntValue> ChildrenParameter {
|
---|
| 59 | get { return (ValueLookupParameter<IntValue>)Parameters["Children"]; }
|
---|
| 60 | }
|
---|
| 61 | public ValueLookupParameter<BoolValue> PlusSelectionParameter {
|
---|
| 62 | get { return (ValueLookupParameter<BoolValue>)Parameters["PlusSelection"]; }
|
---|
| 63 | }
|
---|
| 64 | public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 65 | get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
| 66 | }
|
---|
| 67 | public ValueLookupParameter<IOperator> MutatorParameter {
|
---|
| 68 | get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
|
---|
| 69 | }
|
---|
| 70 | public ValueLookupParameter<IOperator> RecombinatorParameter {
|
---|
| 71 | get { return (ValueLookupParameter<IOperator>)Parameters["Recombinator"]; }
|
---|
| 72 | }
|
---|
| 73 | public ValueLookupParameter<IOperator> EvaluatorParameter {
|
---|
| 74 | get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
|
---|
| 75 | }
|
---|
| 76 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
| 77 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
| 78 | }
|
---|
| 79 | public ValueLookupParameter<IOperator> AnalyzerParameter {
|
---|
| 80 | get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
| 81 | }
|
---|
| 82 | private ScopeParameter CurrentScopeParameter {
|
---|
| 83 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
| 84 | }
|
---|
| 85 | private ValueLookupParameter<IOperator> StrategyParameterManipulatorParameter {
|
---|
| 86 | get { return (ValueLookupParameter<IOperator>)Parameters["StrategyParameterManipulator"]; }
|
---|
| 87 | }
|
---|
| 88 | private ValueLookupParameter<IOperator> StrategyParameterCrossoverParameter {
|
---|
| 89 | get { return (ValueLookupParameter<IOperator>)Parameters["StrategyParameterCrossover"]; }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | public IScope CurrentScope {
|
---|
| 93 | get { return CurrentScopeParameter.ActualValue; }
|
---|
| 94 | }
|
---|
| 95 | #endregion
|
---|
| 96 |
|
---|
| 97 | [StorableConstructor]
|
---|
| 98 | private EvolutionStrategyMainLoop(bool deserializing) : base() { }
|
---|
| 99 | public EvolutionStrategyMainLoop()
|
---|
| 100 | : base() {
|
---|
| 101 | Initialize();
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | private void Initialize() {
|
---|
| 105 | #region Create parameters
|
---|
| 106 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
| 107 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
| 108 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
| 109 | Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
|
---|
| 110 | Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "µ (mu) - the size of the population."));
|
---|
| 111 | Parameters.Add(new ValueLookupParameter<IntValue>("ParentsPerChild", "ρ (rho) - how many parents should be recombined."));
|
---|
| 112 | Parameters.Add(new ValueLookupParameter<IntValue>("Children", "λ (lambda) - the size of the offspring population."));
|
---|
| 113 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
|
---|
| 114 | Parameters.Add(new ValueLookupParameter<BoolValue>("PlusSelection", "True for plus selection (elitist population), false for comma selection (non-elitist population)."));
|
---|
| 115 | Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
|
---|
| 116 | Parameters.Add(new ValueLookupParameter<IOperator>("Recombinator", "The operator used to cross solutions."));
|
---|
| 117 | Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
|
---|
| 118 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
| 119 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
|
---|
| 120 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the EvolutionStrategy should be applied."));
|
---|
| 121 | Parameters.Add(new ValueLookupParameter<IOperator>("StrategyParameterManipulator", "The operator to mutate the endogeneous strategy parameters."));
|
---|
| 122 | Parameters.Add(new ValueLookupParameter<IOperator>("StrategyParameterCrossover", "The operator to cross the endogeneous strategy parameters."));
|
---|
| 123 | #endregion
|
---|
| 124 |
|
---|
| 125 | #region Create operators
|
---|
| 126 | VariableCreator variableCreator = new VariableCreator();
|
---|
| 127 | ResultsCollector resultsCollector1 = new ResultsCollector();
|
---|
| 128 | Placeholder analyzer1 = new Placeholder();
|
---|
| 129 | WithoutRepeatingBatchedRandomSelector selector = new WithoutRepeatingBatchedRandomSelector();
|
---|
| 130 | SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
|
---|
| 131 | Comparator useRecombinationComparator = new Comparator();
|
---|
| 132 | ConditionalBranch useRecombinationBranch = new ConditionalBranch();
|
---|
| 133 | ChildrenCreator childrenCreator = new ChildrenCreator();
|
---|
| 134 | UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
|
---|
| 135 | Placeholder recombinator = new Placeholder();
|
---|
| 136 | Placeholder strategyRecombinator = new Placeholder();
|
---|
| 137 | Placeholder strategyMutator1 = new Placeholder();
|
---|
| 138 | Placeholder mutator1 = new Placeholder();
|
---|
| 139 | Placeholder evaluator1 = new Placeholder();
|
---|
| 140 | SubScopesRemover subScopesRemover = new SubScopesRemover();
|
---|
| 141 | UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
|
---|
| 142 | Placeholder strategyMutator2 = new Placeholder();
|
---|
| 143 | Placeholder mutator2 = new Placeholder();
|
---|
| 144 | Placeholder evaluator2 = new Placeholder();
|
---|
| 145 | ConditionalBranch plusOrCommaReplacementBranch = new ConditionalBranch();
|
---|
| 146 | MergingReducer plusReplacement = new MergingReducer();
|
---|
| 147 | RightReducer commaReplacement = new RightReducer();
|
---|
| 148 | BestSelector bestSelector = new BestSelector();
|
---|
| 149 | RightReducer rightReducer = new RightReducer();
|
---|
| 150 | IntCounter intCounter = new IntCounter();
|
---|
| 151 | Comparator comparator = new Comparator();
|
---|
| 152 | ResultsCollector resultsCollector2 = new ResultsCollector();
|
---|
| 153 | Placeholder analyzer2 = new Placeholder();
|
---|
| 154 | ConditionalBranch conditionalBranch = new ConditionalBranch();
|
---|
| 155 |
|
---|
| 156 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class EvolutionStrategy expects this to be called Generations
|
---|
| 157 |
|
---|
| 158 | resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
| 159 | resultsCollector1.ResultsParameter.ActualName = "Results";
|
---|
| 160 |
|
---|
| 161 | analyzer1.Name = "Analyzer (placeholder)";
|
---|
| 162 | analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 163 |
|
---|
| 164 | selector.Name = "ES Random Selector";
|
---|
| 165 | selector.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
|
---|
| 166 | selector.ChildrenParameter.ActualName = ChildrenParameter.Name;
|
---|
| 167 |
|
---|
| 168 | useRecombinationComparator.Name = "ParentsPerChild > 1";
|
---|
| 169 | useRecombinationComparator.LeftSideParameter.ActualName = ParentsPerChildParameter.Name;
|
---|
| 170 | useRecombinationComparator.RightSideParameter.Value = new IntValue(1);
|
---|
| 171 | useRecombinationComparator.Comparison = new Comparison(ComparisonType.Greater);
|
---|
| 172 | useRecombinationComparator.ResultParameter.ActualName = "UseRecombination";
|
---|
| 173 |
|
---|
| 174 | useRecombinationBranch.Name = "Use Recombination?";
|
---|
| 175 | useRecombinationBranch.ConditionParameter.ActualName = "UseRecombination";
|
---|
| 176 |
|
---|
| 177 | childrenCreator.ParentsPerChild = null;
|
---|
| 178 | childrenCreator.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
|
---|
| 179 |
|
---|
| 180 | recombinator.Name = "Recombinator (placeholder)";
|
---|
| 181 | recombinator.OperatorParameter.ActualName = RecombinatorParameter.Name;
|
---|
| 182 |
|
---|
| 183 | strategyRecombinator.Name = "Strategy Parameter Recombinator (placeholder)";
|
---|
| 184 | strategyRecombinator.OperatorParameter.ActualName = StrategyParameterCrossoverParameter.Name;
|
---|
| 185 |
|
---|
| 186 | strategyMutator1.Name = "Strategy Parameter Manipulator (placeholder)";
|
---|
| 187 | strategyMutator1.OperatorParameter.ActualName = StrategyParameterManipulatorParameter.Name;
|
---|
| 188 |
|
---|
| 189 | mutator1.Name = "Mutator (placeholder)";
|
---|
| 190 | mutator1.OperatorParameter.ActualName = MutatorParameter.Name;
|
---|
| 191 |
|
---|
| 192 | evaluator1.Name = "Evaluator (placeholder)";
|
---|
| 193 | evaluator1.OperatorParameter.ActualName = EvaluatorParameter.Name;
|
---|
| 194 |
|
---|
| 195 | subScopesRemover.RemoveAllSubScopes = true;
|
---|
| 196 |
|
---|
| 197 | strategyMutator2.Name = "Strategy Parameter Manipulator (placeholder)";
|
---|
| 198 | strategyMutator2.OperatorParameter.ActualName = StrategyParameterManipulatorParameter.Name;
|
---|
| 199 |
|
---|
| 200 | mutator2.Name = "Mutator (placeholder)";
|
---|
| 201 | mutator2.OperatorParameter.ActualName = MutatorParameter.Name;
|
---|
| 202 |
|
---|
| 203 | evaluator2.Name = "Evaluator (placeholder)";
|
---|
| 204 | evaluator2.OperatorParameter.ActualName = EvaluatorParameter.Name;
|
---|
| 205 |
|
---|
| 206 | plusOrCommaReplacementBranch.ConditionParameter.ActualName = PlusSelectionParameter.Name;
|
---|
| 207 |
|
---|
| 208 | bestSelector.CopySelected = new BoolValue(false);
|
---|
| 209 | bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 210 | bestSelector.NumberOfSelectedSubScopesParameter.ActualName = PopulationSizeParameter.Name;
|
---|
| 211 | bestSelector.QualityParameter.ActualName = QualityParameter.Name;
|
---|
| 212 |
|
---|
| 213 | intCounter.Increment = new IntValue(1);
|
---|
| 214 | intCounter.ValueParameter.ActualName = "Generations";
|
---|
| 215 |
|
---|
| 216 | comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
| 217 | comparator.LeftSideParameter.ActualName = "Generations";
|
---|
| 218 | comparator.ResultParameter.ActualName = "Terminate";
|
---|
| 219 | comparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
| 220 |
|
---|
| 221 | resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
| 222 | resultsCollector2.ResultsParameter.ActualName = "Results";
|
---|
| 223 |
|
---|
| 224 | analyzer2.Name = "Analyzer (placeholder)";
|
---|
| 225 | analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 226 |
|
---|
| 227 | conditionalBranch.ConditionParameter.ActualName = "Terminate";
|
---|
| 228 | #endregion
|
---|
| 229 |
|
---|
| 230 | #region Create operator graph
|
---|
| 231 | OperatorGraph.InitialOperator = variableCreator;
|
---|
| 232 | variableCreator.Successor = resultsCollector1;
|
---|
| 233 | resultsCollector1.Successor = analyzer1;
|
---|
| 234 | analyzer1.Successor = selector;
|
---|
| 235 | selector.Successor = subScopesProcessor1;
|
---|
| 236 | subScopesProcessor1.Operators.Add(new EmptyOperator());
|
---|
| 237 | subScopesProcessor1.Operators.Add(useRecombinationComparator);
|
---|
| 238 | subScopesProcessor1.Successor = plusOrCommaReplacementBranch;
|
---|
| 239 | useRecombinationComparator.Successor = useRecombinationBranch;
|
---|
| 240 | useRecombinationBranch.TrueBranch = childrenCreator;
|
---|
| 241 | useRecombinationBranch.FalseBranch = uniformSubScopesProcessor2;
|
---|
| 242 | useRecombinationBranch.Successor = null;
|
---|
| 243 | childrenCreator.Successor = uniformSubScopesProcessor1;
|
---|
| 244 | uniformSubScopesProcessor1.Operator = recombinator;
|
---|
| 245 | uniformSubScopesProcessor1.Successor = null;
|
---|
| 246 | recombinator.Successor = strategyRecombinator;
|
---|
| 247 | strategyRecombinator.Successor = strategyMutator1;
|
---|
| 248 | strategyMutator1.Successor = mutator1;
|
---|
| 249 | mutator1.Successor = evaluator1;
|
---|
| 250 | evaluator1.Successor = subScopesRemover;
|
---|
| 251 | subScopesRemover.Successor = null;
|
---|
| 252 | uniformSubScopesProcessor2.Operator = strategyMutator2;
|
---|
| 253 | uniformSubScopesProcessor2.Successor = null;
|
---|
| 254 | strategyMutator2.Successor = mutator2;
|
---|
| 255 | mutator2.Successor = evaluator2;
|
---|
| 256 | plusOrCommaReplacementBranch.TrueBranch = plusReplacement;
|
---|
| 257 | plusOrCommaReplacementBranch.FalseBranch = commaReplacement;
|
---|
| 258 | plusOrCommaReplacementBranch.Successor = bestSelector;
|
---|
| 259 | bestSelector.Successor = rightReducer;
|
---|
| 260 | rightReducer.Successor = intCounter;
|
---|
| 261 | intCounter.Successor = comparator;
|
---|
| 262 | comparator.Successor = resultsCollector2;
|
---|
| 263 | resultsCollector2.Successor = analyzer2;
|
---|
| 264 | analyzer2.Successor = conditionalBranch;
|
---|
| 265 | conditionalBranch.FalseBranch = selector;
|
---|
| 266 | conditionalBranch.TrueBranch = null;
|
---|
| 267 | conditionalBranch.Successor = null;
|
---|
| 268 | #endregion
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | public override IOperation Apply() {
|
---|
| 272 | if (MutatorParameter.ActualValue == null)
|
---|
| 273 | return null;
|
---|
| 274 | return base.Apply();
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 | }
|
---|