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