Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Problems.LawnMower/3.3/Problem.cs @ 11845

Last change on this file since 11845 was 11576, checked in by swagner, 10 years ago

#2205: Merged changes r11062:11557 from trunk/sources into branches/OptimizationNetworks

File size: 10.0 KB
RevLine 
[8059]1#region License Information
2/* HeuristicLab
[11576]3 * Copyright (C) 2002-2014 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
22using System;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.PluginInfrastructure;
32
33namespace 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";
[8131]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    }
[8131]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)));
[8131]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
[11576]89      GrammarParameter.Value.MaximumFunctionDefinitions = MaxFunctionDefinitionsParameter.Value.Value;
90      GrammarParameter.Value.MaximumFunctionArguments = MaxArgumentDefinitionsParameter.Value.Value;
91
92      InitializeOperators();     
[8059]93      RegisterEventHandlers();
94    }
95
96
97    [StorableHook(HookType.AfterDeserialization)]
98    private void AfterDeserialization() {
99      RegisterEventHandlers();
100    }
101
102    public override IDeepCloneable Clone(Cloner cloner) {
103      return new Problem(this, cloner);
104    }
105
106    private void InitializeOperators() {
107      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
108      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
109      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
110      Operators.Add(new BestSolutionAnalyzer());
111      ParameterizeOperators();
112      ParameterizeAnalyzers();
113    }
114
115
116    private void RegisterEventHandlers() {
117      Evaluator.QualityParameter.ActualNameChanged += QualityParameterOnActualNameChanged;
118      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged +=
119        SymbolicExpressionTreeParameterOnActualNameChanged;
[8131]120      MaxArgumentDefinitionsParameter.ValueChanged += ParameterizeGrammar;
121      MaxFunctionDefinitionsParameter.ValueChanged += ParameterizeGrammar;
[8059]122    }
123
124    protected override void OnEvaluatorChanged() {
125      Evaluator.LawnMowerProgramParameter.ActualName = LawnMowerProgramParameterName;
126      Evaluator.LawnLengthParameter.ActualName = LawnLengthParameterName;
127      Evaluator.LawnWidthParameter.ActualName = LawnWidthParameterName;
128      Evaluator.QualityParameter.ActualNameChanged += QualityParameterOnActualNameChanged;
129      ParameterizeAnalyzers();
130      ParameterizeOperators();
[8163]131      base.OnEvaluatorChanged();
[8059]132    }
133
134    protected override void OnSolutionCreatorChanged() {
135      SolutionCreator.SymbolicExpressionTreeParameter.ActualName = LawnMowerProgramParameterName;
136      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += SymbolicExpressionTreeParameterOnActualNameChanged;
137      ParameterizeAnalyzers();
138      ParameterizeOperators();
[8163]139      base.OnSolutionCreatorChanged();
[8059]140    }
141
142    private void SymbolicExpressionTreeParameterOnActualNameChanged(object sender, EventArgs eventArgs) {
143      ParameterizeAnalyzers();
144      ParameterizeOperators();
145    }
146
147    private void QualityParameterOnActualNameChanged(object sender, EventArgs eventArgs) {
148      ParameterizeAnalyzers();
149      ParameterizeOperators();
150    }
151
[8131]152    private void ParameterizeGrammar(object sender, EventArgs eventArgs) {
153      GrammarParameter.Value.MaximumFunctionArguments = MaxArgumentDefinitionsParameter.Value.Value;
154      GrammarParameter.Value.MaximumFunctionDefinitions = MaxFunctionDefinitionsParameter.Value.Value;
155    }
156
[8059]157    private void ParameterizeAnalyzers() {
158      var analyzers = Operators.OfType<IAnalyzer>();
159      foreach (var o in analyzers.OfType<ISymbolicExpressionTreeAnalyzer>()) {
160        o.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
161      }
162      foreach (var o in analyzers.OfType<BestSolutionAnalyzer>()) {
163        o.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
164      }
165    }
166
167    private void ParameterizeOperators() {
168      var operators = Parameters
169        .OfType<IValueParameter>()
170        .Select(p => p.Value)
171        .OfType<IOperator>()
172        .Union(Operators);
173      foreach (var o in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
174        o.SymbolicExpressionTreeGrammarParameter.ActualName = LawnMowerGrammarParameterName;
175      }
176      foreach (var o in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
177        o.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaxLawnMowerProgramDepthParameterName;
178        o.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaxLawnMowerProgramLengthParameterName;
179      }
180      foreach (var op in operators.OfType<Evaluator>()) {
181        op.LawnMowerProgramParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
182        op.LawnLengthParameter.ActualName = LawnLengthParameterName;
183        op.LawnWidthParameter.ActualName = LawnWidthParameterName;
184      }
185      foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
186        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
187        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
188      }
189      foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
190        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
191      }
192      foreach (var op in operators.OfType<ISymbolicExpressionTreeCreator>()) {
193        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
194      }
[8131]195      foreach (ISymbolicExpressionTreeArchitectureAlteringOperator op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
196        op.MaximumFunctionDefinitionsParameter.ActualName = MaxFunctionDefinitionsParameter.Name;
197        op.MaximumFunctionArgumentsParameter.ActualName = MaxArgumentDefinitionsParameter.Name;
198      }
[8059]199    }
200  }
201}
Note: See TracBrowser for help on using the repository browser.