Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/GEArtificialAntProblem.cs @ 10017

Last change on this file since 10017 was 10012, checked in by sawinkle, 11 years ago

#2109:
added initial version of Problem class and Evaluator class; GEArtificialAntProblem randomly initializes an IntegerVector and prepares the GE run; GEEvaluator currently only creates a simple SymbolicExpressionTree, consisting of the RootSymbol, StartSymbol and the MoveSymbol

File size: 21.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.IntegerVectorEncoding;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34using HeuristicLab.Problems.ArtificialAnt;
35using HeuristicLab.Problems.ArtificialAnt.Analyzers;
36
37namespace HeuristicLab.Problems.GrammaticalEvolution.ArtificialAnt {
38  [Item("Grammatical Evolution Artificial Ant Problem", "Represents the Artificial Ant problem, implemented in Grammatical Evolution.")]
39  [Creatable("Problems")]
40  [StorableClass]
41  public sealed class GEArtificialAntProblem : SingleObjectiveHeuristicOptimizationProblem<GEEvaluator, IIntegerVectorCreator>, IStorableContent {
42    public string Filename { get; set; }
43
44    #region constant for default world (Santa Fe)
45    private readonly bool[,] santaFeAntTrail = new bool[,] {
46      {false, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
47      {false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
48      {false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, false, false, false, false},
49      {false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, true, false, false},
50      {false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, true, false, false},
51      {false, false, false, true, true, true, true, false, true, true, true, true, true, false, false, false, false, false, false, false, false, true, true, false, false, false, false, false, false, false, false, false},
52      {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false},
53      {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},
54      {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},
55      {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, true, false, false},
56      {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},
57      {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
58      {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false},
59      {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},
60      {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, true, true, true, false, false, false},
61      {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, true, false, false, false, false, false, false, false, false},
62      {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
63      {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
64      {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false},
65      {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, true, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false},
66      {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
67      {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
68      {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false},
69      {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false},
70      {false, false, false, true, true, false, false, true, true, true, true, true, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
71      {false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
72      {false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
73      {false, true, false, false, false, false, false, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
74      {false, true, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
75      {false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
76      {false, false, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false },
77      {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false }
78    };
79    #endregion
80
81    #region Parameter Properties
82    public IValueParameter<ISymbolicExpressionGrammar> ArtificialAntExpressionGrammarParameter {
83      get { return (IValueParameter<ISymbolicExpressionGrammar>)Parameters["ArtificialAntExpressionGrammar"]; }
84    }
85    public IValueParameter<IntValue> MaxExpressionLengthParameter {
86      get { return (IValueParameter<IntValue>)Parameters["MaximumExpressionLength"]; }
87    }
88    public IValueParameter<IntValue> MaxExpressionDepthParameter {
89      get { return (IValueParameter<IntValue>)Parameters["MaximumExpressionDepth"]; }
90    }
91    public IValueParameter<IntValue> MaxFunctionDefinitionsParameter {
92      get { return (IValueParameter<IntValue>)Parameters["MaximumFunctionDefinitions"]; }
93    }
94    public IValueParameter<IntValue> MaxFunctionArgumentsParameter {
95      get { return (ValueParameter<IntValue>)Parameters["MaximumFunctionArguments"]; }
96    }
97    public IValueParameter<BoolMatrix> WorldParameter {
98      get { return (IValueParameter<BoolMatrix>)Parameters["World"]; }
99    }
100    public IValueParameter<IntValue> MaxTimeStepsParameter {
101      get { return (IValueParameter<IntValue>)Parameters["MaximumTimeSteps"]; }
102    }
103    public IValueParameter<IntMatrix> BoundsParameter {
104      get { return (IValueParameter<IntMatrix>)Parameters["Bounds"]; }
105    }
106   
107    #endregion
108
109    #region Properties
110    public BoolMatrix World {
111      get { return WorldParameter.Value; }
112      set { WorldParameter.Value = value; }
113    }
114    public IntValue MaxTimeSteps {
115      get { return MaxTimeStepsParameter.Value; }
116      set { MaxTimeStepsParameter.Value = value; }
117    }
118    public IntValue MaxExpressionLength {
119      get { return MaxExpressionLengthParameter.Value; }
120      set { MaxExpressionLengthParameter.Value = value; }
121    }
122    public IntValue MaxExpressionDepth {
123      get { return MaxExpressionDepthParameter.Value; }
124      set { MaxExpressionDepthParameter.Value = value; }
125    }
126    public IntValue MaxFunctionDefinitions {
127      get { return MaxFunctionDefinitionsParameter.Value; }
128      set { MaxFunctionDefinitionsParameter.Value = value; }
129    }
130    public IntValue MaxFunctionArguments {
131      get { return MaxFunctionArgumentsParameter.Value; }
132      set { MaxFunctionArgumentsParameter.Value = value; }
133    }
134    public ArtificialAntExpressionGrammar ArtificialAntExpressionGrammar {
135      get { return (ArtificialAntExpressionGrammar)ArtificialAntExpressionGrammarParameter.Value; }
136    }
137    public IEnumerable<IAntTrailAnalyzer> AntTrailAnalyzers {
138      get { return Operators.OfType<IAntTrailAnalyzer>(); }
139    }
140    public IntMatrix Bounds {
141      get { return BoundsParameter.Value; }
142      set { BoundsParameter.Value = value; }
143    }
144    #endregion
145
146    // BackwardsCompatibility3.3
147    #region Backwards compatible code, remove with 3.4
148    [Obsolete]
149    [Storable(Name = "operators")]
150    private IEnumerable<IOperator> oldOperators {
151      get { return null; }
152      set {
153        if (value != null && value.Any())
154          Operators.AddRange(value);
155      }
156    }
157    #endregion
158
159    [StorableConstructor]
160    private GEArtificialAntProblem(bool deserializing) : base(deserializing) { }
161    [StorableHook(HookType.AfterDeserialization)]
162    private void AfterDeserialization() {
163      RegisterEventHandlers();
164    }
165
166    private GEArtificialAntProblem(GEArtificialAntProblem original, Cloner cloner)
167      : base(original, cloner) {
168      RegisterEventHandlers();
169    }
170    public override IDeepCloneable Clone(Cloner cloner) {
171      return new GEArtificialAntProblem(this, cloner);
172    }
173    public GEArtificialAntProblem()
174      : base(new GEEvaluator(), new UniformRandomIntegerVectorCreator()) {
175      BoolMatrix world = new BoolMatrix(santaFeAntTrail);
176      Parameters.Add(new ValueParameter<IntValue>("MaximumExpressionLength", "Maximal length of the expression to control the artificial ant.", new IntValue(100)));
177      Parameters.Add(new ValueParameter<IntValue>("MaximumExpressionDepth", "Maximal depth of the expression to control the artificial ant.", new IntValue(10)));
178      Parameters.Add(new ValueParameter<IntValue>("MaximumFunctionDefinitions", "Maximal number of automatically defined functions in the expression to control the artificial ant.", new IntValue(3)));
179      Parameters.Add(new ValueParameter<IntValue>("MaximumFunctionArguments", "Maximal number of arguments of automatically defined functions in the expression to control the artificial ant.", new IntValue(3)));
180      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", new ArtificialAntExpressionGrammar()));
181      Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world));
182      Parameters.Add(new ValueParameter<IntValue>("MaximumTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
183      IntMatrix m = new IntMatrix(1,2);
184      m[0,0] = 1;
185      m[0,1] = 10;
186      Parameters.Add(new ValueParameter<IntMatrix>("Bounds", "", m));
187     
188      Maximization.Value = true;
189      MaximizationParameter.Hidden = true;
190      BestKnownQuality = new DoubleValue(89);
191     
192      SolutionCreator.IntegerVectorParameter.ActualName = "AntTrailSolutionIntegerVector";
193      Evaluator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolutionTree";
194      Evaluator.SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar";
195      Evaluator.QualityParameter.ActualName = "FoodEaten";
196     
197      InitializeOperators();
198      RegisterEventHandlers();
199    }
200
201    #region Events
202    protected override void OnSolutionCreatorChanged() {
203      base.OnSolutionCreatorChanged();
204     
205      SolutionCreator.IntegerVectorParameter.ActualName = "AntTrailSolutionIntegerVector";
206      SolutionCreator.IntegerVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
207     
208      ParameterizeAnalyzers();
209      ParameterizeOperators();
210    }
211   
212    protected override void OnEvaluatorChanged() {
213      base.OnEvaluatorChanged();
214     
215      Evaluator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolutionTree";
216      Evaluator.SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar";
217      Evaluator.QualityParameter.ActualName = "FoodEaten";
218     
219      Evaluator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged);
220      Evaluator.SymbolicExpressionTreeGrammarParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeGrammarParameter_ActualNameChanged);
221      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
222     
223      ParameterizeAnalyzers();
224      ParameterizeOperators();
225    }
226   
227    private void SolutionCreator_IntegerVectorParameter_ActualNameChanged(object sender, EventArgs e) {
228      ParameterizeAnalyzers();
229      ParameterizeOperators();
230    }
231   
232    private void Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
233      ParameterizeAnalyzers();
234      ParameterizeOperators();
235    }
236   
237    private void Evaluator_SymbolicExpressionTreeGrammarParameter_ActualNameChanged(object sender, EventArgs e) {
238      ParameterizeAnalyzers();
239      ParameterizeOperators();
240    }
241   
242    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
243      ParameterizeAnalyzers();
244      ParameterizeOperators();
245    }
246    #endregion
247
248    #region Helpers
249    private void RegisterEventHandlers() {
250      SolutionCreator.IntegerVectorParameter.ActualNameChanged           += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
251      Evaluator.SymbolicExpressionTreeParameter.ActualNameChanged        += new EventHandler(Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged);
252      Evaluator.SymbolicExpressionTreeGrammarParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeGrammarParameter_ActualNameChanged);
253      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
254      MaxFunctionArgumentsParameter.ValueChanged   += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
255      MaxFunctionArguments.ValueChanged            += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
256      MaxFunctionDefinitionsParameter.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
257      MaxFunctionDefinitions.ValueChanged          += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
258    }
259
260    private void MaxFunctionDefinitionsParameter_ValueChanged(object sender, EventArgs e) {
261      ArtificialAntExpressionGrammar.MaximumFunctionDefinitions = MaxFunctionDefinitions.Value;
262      ParameterizeOperators();
263      ParameterizeAnalyzers();
264    }
265    private void MaxFunctionArgumentsParameter_ValueChanged(object sender, EventArgs e) {
266      ArtificialAntExpressionGrammar.MaximumFunctionArguments = MaxFunctionArguments.Value;
267      ParameterizeOperators();
268      ParameterizeAnalyzers();
269    }
270
271    private void InitializeOperators() {
272      Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>().OfType<IOperator>());
273      Operators.Add(new BestAntTrailAnalyzer());
274      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
275      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
276      ParameterizeAnalyzers();
277      ParameterizeOperators();
278    }
279
280    private void ParameterizeAnalyzers() {
281      foreach (IAntTrailAnalyzer analyzer in AntTrailAnalyzers) {
282        analyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
283        analyzer.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
284        analyzer.WorldParameter.ActualName = WorldParameter.Name;
285        analyzer.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
286      }
287      foreach (ISymbolicExpressionTreeAnalyzer analyzer in Operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
288        analyzer.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
289      }
290    }
291
292    private void ParameterizeOperators() {
293      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
294
295      foreach (ISymbolicExpressionTreeGrammarBasedOperator op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
296        op.SymbolicExpressionTreeGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
297      }
298      foreach (GEEvaluator op in operators.OfType<GEEvaluator>()) {
299        op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
300        op.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
301        op.WorldParameter.ActualName = WorldParameter.Name;
302      }
303      foreach (IIntegerVectorCrossover op in operators.OfType<IIntegerVectorCrossover>()) {
304        op.ParentsParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
305        op.ChildParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
306      }
307      foreach (IIntegerVectorManipulator op in operators.OfType<IIntegerVectorManipulator>()) {
308        op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
309      }
310      foreach (IIntegerVectorCreator op in operators.OfType<IIntegerVectorCreator>()) {
311        op.BoundsParameter.ActualName = BoundsParameter.Name;
312        op.LengthParameter.ActualName = MaxExpressionLengthParameter.Name;
313      }
314    }
315    #endregion
316  }
317}
Note: See TracBrowser for help on using the repository browser.