Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GBT/HeuristicLab.Problems.GrammaticalEvolution/3.3/ArtificialAnt/GEArtificialAntProblem.cs @ 12417

Last change on this file since 12417 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

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