[8059] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8059] | 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;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 | using HeuristicLab.PluginInfrastructure;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Problems.LawnMower {
|
---|
| 34 | [StorableClass]
|
---|
| 35 | [Item("Lawn Mower Problem", "The lawn mower demo problem for genetic programming.")]
|
---|
| 36 | public class Problem : SingleObjectiveHeuristicOptimizationProblem<Evaluator, ISymbolicExpressionTreeCreator> {
|
---|
| 37 | private const string LawnWidthParameterName = "LawnWidth";
|
---|
| 38 | private const string LawnLengthParameterName = "LawnLength";
|
---|
| 39 | private const string LawnMowerProgramParameterName = "Program";
|
---|
| 40 | private const string MaxLawnMowerProgramLengthParameterName = "MaxProgramLength";
|
---|
| 41 | private const string MaxLawnMowerProgramDepthParameterName = "MaxProgramDepth";
|
---|
| 42 | private const string LawnMowerGrammarParameterName = "Grammar";
|
---|
[8131] | 43 | private const string MaxFunctionDefinitionsParameterName = "MaxFunctionDefinitions";
|
---|
| 44 | private const string MaxArgumentDefinitionsParameterName = "MaxArgumentDefinitions";
|
---|
[8059] | 45 |
|
---|
| 46 | public IFixedValueParameter<IntValue> LawnWidthParameter {
|
---|
| 47 | get { return (IFixedValueParameter<IntValue>)Parameters[LawnWidthParameterName]; }
|
---|
| 48 | }
|
---|
| 49 | public IFixedValueParameter<IntValue> LawnLengthParameter {
|
---|
| 50 | get { return (IFixedValueParameter<IntValue>)Parameters[LawnLengthParameterName]; }
|
---|
| 51 | }
|
---|
| 52 | public IFixedValueParameter<IntValue> MaxLawnMowerProgramLengthParameter {
|
---|
| 53 | get { return (IFixedValueParameter<IntValue>)Parameters[MaxLawnMowerProgramLengthParameterName]; }
|
---|
| 54 | }
|
---|
| 55 | public IFixedValueParameter<IntValue> MaxLawnMowerProgramDepthParameter {
|
---|
| 56 | get { return (IFixedValueParameter<IntValue>)Parameters[MaxLawnMowerProgramDepthParameterName]; }
|
---|
| 57 | }
|
---|
| 58 | public IValueParameter<Grammar> GrammarParameter {
|
---|
| 59 | get { return (IValueParameter<Grammar>)Parameters[LawnMowerGrammarParameterName]; }
|
---|
| 60 | }
|
---|
[8131] | 61 | public IFixedValueParameter<IntValue> MaxFunctionDefinitionsParameter {
|
---|
| 62 | get { return (IFixedValueParameter<IntValue>)Parameters[MaxFunctionDefinitionsParameterName]; }
|
---|
| 63 | }
|
---|
| 64 | public IFixedValueParameter<IntValue> MaxArgumentDefinitionsParameter {
|
---|
| 65 | get { return (IFixedValueParameter<IntValue>)Parameters[MaxArgumentDefinitionsParameterName]; }
|
---|
| 66 | }
|
---|
[8059] | 67 |
|
---|
| 68 | [StorableConstructor]
|
---|
| 69 | protected Problem(bool deserializing)
|
---|
| 70 | : base(deserializing) {
|
---|
| 71 | }
|
---|
| 72 | protected Problem(Problem original, Cloner cloner)
|
---|
| 73 | : base(original, cloner) {
|
---|
| 74 | RegisterEventHandlers();
|
---|
| 75 | }
|
---|
| 76 | public Problem()
|
---|
| 77 | : base(new Evaluator(), new RampedHalfAndHalfTreeCreator()) {
|
---|
| 78 | Parameters.Add(new FixedValueParameter<IntValue>(LawnWidthParameterName, "Width of the lawn.", new IntValue(8)));
|
---|
| 79 | Parameters.Add(new FixedValueParameter<IntValue>(LawnLengthParameterName, "Length of the lawn.", new IntValue(8)));
|
---|
| 80 | Parameters.Add(new FixedValueParameter<IntValue>(MaxLawnMowerProgramDepthParameterName, "Maximal depth of the lawn mower program.", new IntValue(13)));
|
---|
| 81 | Parameters.Add(new FixedValueParameter<IntValue>(MaxLawnMowerProgramLengthParameterName, "Maximal length of the lawn mower program.", new IntValue(1000)));
|
---|
[8131] | 82 | Parameters.Add(new FixedValueParameter<IntValue>(MaxFunctionDefinitionsParameterName, "Maximal number of automatically defined functions (ADF).", new IntValue(3)));
|
---|
| 83 | Parameters.Add(new FixedValueParameter<IntValue>(MaxArgumentDefinitionsParameterName, "Maximal number of automatically defined arguments.", new IntValue(3)));
|
---|
[8059] | 84 | Parameters.Add(new ValueParameter<Grammar>(LawnMowerGrammarParameterName, "Grammar for the lawn mower program.",
|
---|
| 85 | new Grammar()));
|
---|
| 86 | Maximization.Value = true;
|
---|
| 87 |
|
---|
[11504] | 88 | GrammarParameter.Value.MaximumFunctionDefinitions = MaxFunctionDefinitionsParameter.Value.Value;
|
---|
| 89 | GrammarParameter.Value.MaximumFunctionArguments = MaxArgumentDefinitionsParameter.Value.Value;
|
---|
| 90 |
|
---|
[12504] | 91 | InitializeOperators();
|
---|
[8059] | 92 | RegisterEventHandlers();
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 97 | private void AfterDeserialization() {
|
---|
| 98 | RegisterEventHandlers();
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 102 | return new Problem(this, cloner);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | private void InitializeOperators() {
|
---|
| 106 | Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
|
---|
| 107 | Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
|
---|
| 108 | Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
|
---|
| 109 | Operators.Add(new BestSolutionAnalyzer());
|
---|
| 110 | ParameterizeOperators();
|
---|
| 111 | ParameterizeAnalyzers();
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 |
|
---|
| 115 | private void RegisterEventHandlers() {
|
---|
| 116 | Evaluator.QualityParameter.ActualNameChanged += QualityParameterOnActualNameChanged;
|
---|
| 117 | SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged +=
|
---|
| 118 | SymbolicExpressionTreeParameterOnActualNameChanged;
|
---|
[8131] | 119 | MaxArgumentDefinitionsParameter.ValueChanged += ParameterizeGrammar;
|
---|
| 120 | MaxFunctionDefinitionsParameter.ValueChanged += ParameterizeGrammar;
|
---|
[8059] | 121 | }
|
---|
| 122 |
|
---|
| 123 | protected override void OnEvaluatorChanged() {
|
---|
| 124 | Evaluator.LawnMowerProgramParameter.ActualName = LawnMowerProgramParameterName;
|
---|
| 125 | Evaluator.LawnLengthParameter.ActualName = LawnLengthParameterName;
|
---|
| 126 | Evaluator.LawnWidthParameter.ActualName = LawnWidthParameterName;
|
---|
| 127 | Evaluator.QualityParameter.ActualNameChanged += QualityParameterOnActualNameChanged;
|
---|
| 128 | ParameterizeAnalyzers();
|
---|
| 129 | ParameterizeOperators();
|
---|
[8163] | 130 | base.OnEvaluatorChanged();
|
---|
[8059] | 131 | }
|
---|
| 132 |
|
---|
| 133 | protected override void OnSolutionCreatorChanged() {
|
---|
| 134 | SolutionCreator.SymbolicExpressionTreeParameter.ActualName = LawnMowerProgramParameterName;
|
---|
| 135 | SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += SymbolicExpressionTreeParameterOnActualNameChanged;
|
---|
| 136 | ParameterizeAnalyzers();
|
---|
| 137 | ParameterizeOperators();
|
---|
[8163] | 138 | base.OnSolutionCreatorChanged();
|
---|
[8059] | 139 | }
|
---|
| 140 |
|
---|
| 141 | private void SymbolicExpressionTreeParameterOnActualNameChanged(object sender, EventArgs eventArgs) {
|
---|
| 142 | ParameterizeAnalyzers();
|
---|
| 143 | ParameterizeOperators();
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | private void QualityParameterOnActualNameChanged(object sender, EventArgs eventArgs) {
|
---|
| 147 | ParameterizeAnalyzers();
|
---|
| 148 | ParameterizeOperators();
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[8131] | 151 | private void ParameterizeGrammar(object sender, EventArgs eventArgs) {
|
---|
| 152 | GrammarParameter.Value.MaximumFunctionArguments = MaxArgumentDefinitionsParameter.Value.Value;
|
---|
| 153 | GrammarParameter.Value.MaximumFunctionDefinitions = MaxFunctionDefinitionsParameter.Value.Value;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[8059] | 156 | private void ParameterizeAnalyzers() {
|
---|
| 157 | var analyzers = Operators.OfType<IAnalyzer>();
|
---|
| 158 | foreach (var o in analyzers.OfType<ISymbolicExpressionTreeAnalyzer>()) {
|
---|
| 159 | o.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
| 160 | }
|
---|
| 161 | foreach (var o in analyzers.OfType<BestSolutionAnalyzer>()) {
|
---|
| 162 | o.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | private void ParameterizeOperators() {
|
---|
| 167 | var operators = Parameters
|
---|
| 168 | .OfType<IValueParameter>()
|
---|
| 169 | .Select(p => p.Value)
|
---|
| 170 | .OfType<IOperator>()
|
---|
| 171 | .Union(Operators);
|
---|
| 172 | foreach (var o in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
|
---|
| 173 | o.SymbolicExpressionTreeGrammarParameter.ActualName = LawnMowerGrammarParameterName;
|
---|
| 174 | }
|
---|
| 175 | foreach (var o in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
|
---|
| 176 | o.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaxLawnMowerProgramDepthParameterName;
|
---|
| 177 | o.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaxLawnMowerProgramLengthParameterName;
|
---|
| 178 | }
|
---|
| 179 | foreach (var op in operators.OfType<Evaluator>()) {
|
---|
| 180 | op.LawnMowerProgramParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
| 181 | op.LawnLengthParameter.ActualName = LawnLengthParameterName;
|
---|
| 182 | op.LawnWidthParameter.ActualName = LawnWidthParameterName;
|
---|
| 183 | }
|
---|
| 184 | foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
|
---|
| 185 | op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
[12422] | 186 | op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
[8059] | 187 | }
|
---|
| 188 | foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
|
---|
| 189 | op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
| 190 | }
|
---|
| 191 | foreach (var op in operators.OfType<ISymbolicExpressionTreeCreator>()) {
|
---|
| 192 | op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
| 193 | }
|
---|
[8131] | 194 | foreach (ISymbolicExpressionTreeArchitectureAlteringOperator op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
|
---|
| 195 | op.MaximumFunctionDefinitionsParameter.ActualName = MaxFunctionDefinitionsParameter.Name;
|
---|
| 196 | op.MaximumFunctionArgumentsParameter.ActualName = MaxArgumentDefinitionsParameter.Name;
|
---|
| 197 | }
|
---|
[8059] | 198 | }
|
---|
| 199 | }
|
---|
| 200 | }
|
---|