Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2109:

  • Renamed GEEvaluator.cs to GEArtificialAntEvaluator.cs, because a further Evaluator for the Symbolic Regression problem (single objective) is planned to be implemented.
  • Excluded the mapping process from GEArtificialAntEvaluator.cs and created several separated mapper classes. Created stubs for breath-first, depth-first, random and PIGE mappers. These mapper implementations should later be easily usable together with different problems. The mapper can be selected in the GUI, via a Problem parameter. The depth-first mapper is already usable, the others still cause exceptions.
File size: 21.1 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;
36using HeuristicLab.Problems.GrammaticalEvolution.Mappers;
37
38namespace HeuristicLab.Problems.GrammaticalEvolution {
39  [Item("Grammatical Evolution Artificial Ant Problem", "Represents the Artificial Ant problem, implemented in Grammatical Evolution.")]
40  [Creatable("Problems")]
41  [StorableClass]
42  public sealed class GEArtificialAntProblem : SingleObjectiveHeuristicOptimizationProblem<GEArtificialAntEvaluator, IIntegerVectorCreator>, IStorableContent {
43    public string Filename { get; set; }
44   
45    #region constant for default world (Santa Fe)
46    private readonly bool[,] santaFeAntTrail = new bool[,] {
47      {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},
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, false, false, false, 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, false, true, true, true, false, false, 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, 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},
52      {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},
53      {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},
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, false, false, false},
56      {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},
57      {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},
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, false, false, false},
59      {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},
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, false, false, false, false, false, false},
61      {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},
62      {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},
63      {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},
64      {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},
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, true, false, false, false, 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, true, 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, true, false, false, false, false, false, false, false, false, false, false, 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, false, false, false, true, false, false, false, false, false},
70      {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},
71      {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},
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, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
74      {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},
75      {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},
76      {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},
77      {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 },
78      {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 }
79    };
80    #endregion
81
82    #region Parameter Properties
83    public IValueParameter<ISymbolicExpressionGrammar> ArtificialAntExpressionGrammarParameter {
84      get { return (IValueParameter<ISymbolicExpressionGrammar>)Parameters["ArtificialAntExpressionGrammar"]; }
85    }
86    public IValueParameter<IntValue> MaxExpressionLengthParameter {
87      get { return (IValueParameter<IntValue>)Parameters["MaximumExpressionLength"]; }
88    }
89    public IValueParameter<IntValue> MaxFunctionDefinitionsParameter {
90      get { return (IValueParameter<IntValue>)Parameters["MaximumFunctionDefinitions"]; }
91    }
92    public IValueParameter<IntValue> MaxFunctionArgumentsParameter {
93      get { return (ValueParameter<IntValue>)Parameters["MaximumFunctionArguments"]; }
94    }
95    public IValueParameter<BoolMatrix> WorldParameter {
96      get { return (IValueParameter<BoolMatrix>)Parameters["World"]; }
97    }
98    public IValueParameter<IntValue> MaxTimeStepsParameter {
99      get { return (IValueParameter<IntValue>)Parameters["MaximumTimeSteps"]; }
100    }
101    public IValueParameter<IntMatrix> BoundsParameter {
102      get { return (IValueParameter<IntMatrix>)Parameters["Bounds"]; }
103    }
104    public IValueParameter<IGenotypeToPhenotypeMapper> GenotypeToPhenotypeMapperParameter {
105      get { return (IValueParameter<IGenotypeToPhenotypeMapper>)Parameters["GenotypeToPhenotypeMapper"]; }
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 MaxFunctionDefinitions {
123      get { return MaxFunctionDefinitionsParameter.Value; }
124      set { MaxFunctionDefinitionsParameter.Value = value; }
125    }
126    public IntValue MaxFunctionArguments {
127      get { return MaxFunctionArgumentsParameter.Value; }
128      set { MaxFunctionArgumentsParameter.Value = value; }
129    }
130    public ArtificialAntExpressionGrammar ArtificialAntExpressionGrammar {
131      get { return (ArtificialAntExpressionGrammar)ArtificialAntExpressionGrammarParameter.Value; }
132    }
133    public IEnumerable<IAntTrailAnalyzer> AntTrailAnalyzers {
134      get { return Operators.OfType<IAntTrailAnalyzer>(); }
135    }
136    public IntMatrix Bounds {
137      get { return BoundsParameter.Value; }
138      set { BoundsParameter.Value = value; }
139    }
140    #endregion
141
142    // BackwardsCompatibility3.3
143    #region Backwards compatible code, remove with 3.4
144    [Obsolete]
145    [Storable(Name = "operators")]
146    private IEnumerable<IOperator> oldOperators {
147      get { return null; }
148      set {
149        if (value != null && value.Any())
150          Operators.AddRange(value);
151      }
152    }
153    #endregion
154
155    [StorableConstructor]
156    private GEArtificialAntProblem(bool deserializing) : base(deserializing) { }
157    [StorableHook(HookType.AfterDeserialization)]
158    private void AfterDeserialization() {
159      RegisterEventHandlers();
160    }
161
162    private GEArtificialAntProblem(GEArtificialAntProblem original, Cloner cloner)
163      : base(original, cloner) {
164      RegisterEventHandlers();
165    }
166    public override IDeepCloneable Clone(Cloner cloner) {
167      return new GEArtificialAntProblem(this, cloner);
168    }
169    public GEArtificialAntProblem()
170      : base(new GEArtificialAntEvaluator(), new UniformRandomIntegerVectorCreator()) {
171      BoolMatrix world = new BoolMatrix(santaFeAntTrail);
172      Parameters.Add(new ValueParameter<IntValue>("MaximumExpressionLength", "Maximal length of the expression to control the artificial ant.", new IntValue(30)));
173      Parameters.Add(new ValueParameter<IntValue>("MaximumFunctionDefinitions", "Maximal number of automatically defined functions in the expression to control the artificial ant.", new IntValue(3)));
174      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)));
175      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", new ArtificialAntExpressionGrammar()));
176      Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world));
177      Parameters.Add(new ValueParameter<IntValue>("MaximumTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
178      IntMatrix m = new IntMatrix(new int[,]{{0,100}});
179      Parameters.Add(new ValueParameter<IntMatrix>("Bounds", "The integer number range in which the single genomes of a genotype are created.", m));
180      Parameters.Add(new ValueParameter<IGenotypeToPhenotypeMapper>("GenotypeToPhenotypeMapper", "Maps the genotype (an integer vector) to the phenotype (a symbolic expression tree).", new DepthFirstMapper()));
181     
182      Maximization.Value = true;
183      MaximizationParameter.Hidden = true;
184      BestKnownQuality = new DoubleValue(89);
185     
186      SolutionCreator.IntegerVectorParameter.ActualName = "AntTrailSolutionIntegerVector";
187      Evaluator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolutionTree";
188      Evaluator.SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar";
189      Evaluator.QualityParameter.ActualName = "FoodEaten";
190     
191      InitializeOperators();
192      RegisterEventHandlers();
193    }
194
195    #region Events
196    protected override void OnSolutionCreatorChanged() {
197      base.OnSolutionCreatorChanged();
198     
199      SolutionCreator.IntegerVectorParameter.ActualName = "AntTrailSolutionIntegerVector";
200      SolutionCreator.IntegerVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
201     
202      ParameterizeAnalyzers();
203      ParameterizeOperators();
204    }
205   
206    protected override void OnEvaluatorChanged() {
207      base.OnEvaluatorChanged();
208     
209      Evaluator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolutionTree";
210      Evaluator.SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar";
211      Evaluator.QualityParameter.ActualName = "FoodEaten";
212     
213      Evaluator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged);
214      Evaluator.SymbolicExpressionTreeGrammarParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeGrammarParameter_ActualNameChanged);
215      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
216     
217      ParameterizeAnalyzers();
218      ParameterizeOperators();
219    }
220   
221    private void SolutionCreator_IntegerVectorParameter_ActualNameChanged(object sender, EventArgs e) {
222      ParameterizeAnalyzers();
223      ParameterizeOperators();
224    }
225   
226    private void Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
227      ParameterizeAnalyzers();
228      ParameterizeOperators();
229    }
230   
231    private void Evaluator_SymbolicExpressionTreeGrammarParameter_ActualNameChanged(object sender, EventArgs e) {
232      ParameterizeAnalyzers();
233      ParameterizeOperators();
234    }
235   
236    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
237      ParameterizeAnalyzers();
238      ParameterizeOperators();
239    }
240    #endregion
241
242    #region Helpers
243    private void RegisterEventHandlers() {
244      SolutionCreator.IntegerVectorParameter.ActualNameChanged           += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
245      Evaluator.SymbolicExpressionTreeParameter.ActualNameChanged        += new EventHandler(Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged);
246      Evaluator.SymbolicExpressionTreeGrammarParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeGrammarParameter_ActualNameChanged);
247      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
248      MaxFunctionArgumentsParameter.ValueChanged   += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
249      MaxFunctionArguments.ValueChanged            += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
250      MaxFunctionDefinitionsParameter.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
251      MaxFunctionDefinitions.ValueChanged          += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
252    }
253
254    private void MaxFunctionDefinitionsParameter_ValueChanged(object sender, EventArgs e) {
255      ArtificialAntExpressionGrammar.MaximumFunctionDefinitions = MaxFunctionDefinitions.Value;
256      ParameterizeOperators();
257      ParameterizeAnalyzers();
258    }
259    private void MaxFunctionArgumentsParameter_ValueChanged(object sender, EventArgs e) {
260      ArtificialAntExpressionGrammar.MaximumFunctionArguments = MaxFunctionArguments.Value;
261      ParameterizeOperators();
262      ParameterizeAnalyzers();
263    }
264
265    private void InitializeOperators() {
266      Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>().OfType<IOperator>());
267      Operators.Add(new BestAntTrailAnalyzer());
268      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
269      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
270      ParameterizeAnalyzers();
271      ParameterizeOperators();
272    }
273
274    private void ParameterizeAnalyzers() {
275      foreach (IAntTrailAnalyzer analyzer in AntTrailAnalyzers) {
276        analyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
277        analyzer.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
278        analyzer.WorldParameter.ActualName = WorldParameter.Name;
279        analyzer.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
280      }
281      foreach (ISymbolicExpressionTreeAnalyzer analyzer in Operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
282        analyzer.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
283      }
284    }
285
286    private void ParameterizeOperators() {
287      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
288
289      foreach (ISymbolicExpressionTreeGrammarBasedOperator op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
290        op.SymbolicExpressionTreeGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
291      }
292      foreach (GEArtificialAntEvaluator op in operators.OfType<GEArtificialAntEvaluator>()) {
293        op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
294        op.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
295        op.WorldParameter.ActualName = WorldParameter.Name;
296      }
297      foreach (IIntegerVectorCrossover op in operators.OfType<IIntegerVectorCrossover>()) {
298        op.ParentsParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
299        op.ChildParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
300      }
301      foreach (IIntegerVectorManipulator op in operators.OfType<IIntegerVectorManipulator>()) {
302        op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
303      }
304      foreach (IIntegerVectorCreator op in operators.OfType<IIntegerVectorCreator>()) {
305        op.BoundsParameter.ActualName = BoundsParameter.Name;
306        op.LengthParameter.ActualName = MaxExpressionLengthParameter.Name;
307      }
308    }
309    #endregion
310  }
311}
Note: See TracBrowser for help on using the repository browser.