Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.GrammaticalEvolution/3.3/ArtificialAnt/GEArtificialAntProblem.cs @ 13348

Last change on this file since 13348 was 12916, checked in by gkronber, 9 years ago

#2472:

  • deleted unused files
  • removed createable attribute from outdated implementation of GrammaticalEvolution problems
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  [StorableClass]
43  public sealed class GEArtificialAntProblem : SingleObjectiveHeuristicOptimizationProblem<GEArtificialAntEvaluator, IIntegerVectorCreator>, IStorableContent {
44    public string Filename { get; set; }
45
46    #region constant for default world (Santa Fe)
47    private readonly bool[,] santaFeAntTrail = new bool[,] {
48      {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},
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, 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, true, true, true, 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, true, false, false, false, false, true, 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, 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},
54      {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},
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, 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, true, false, false},
58      {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},
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, 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, true, 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, false, false, false, false, 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, true, true, true, false, false, false},
63      {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},
64      {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},
65      {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},
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, true, 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, true, 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, 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, false, false, false, false, false, false, false, false, false, false, true, 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, true, false, false, false, false, false, false, false, false},
72      {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},
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, 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, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
76      {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},
77      {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},
78      {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 },
79      {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 }
80    };
81    #endregion
82
83    #region Parameter Properties
84    public IValueParameter<ISymbolicExpressionGrammar> ArtificialAntExpressionGrammarParameter {
85      get { return (IValueParameter<ISymbolicExpressionGrammar>)Parameters["ArtificialAntExpressionGrammar"]; }
86    }
87    public IValueParameter<IntValue> MaxExpressionLengthParameter {
88      get { return (IValueParameter<IntValue>)Parameters["MaximumExpressionLength"]; }
89    }
90    public IValueParameter<BoolMatrix> WorldParameter {
91      get { return (IValueParameter<BoolMatrix>)Parameters["World"]; }
92    }
93    public IValueParameter<IntValue> MaxTimeStepsParameter {
94      get { return (IValueParameter<IntValue>)Parameters["MaximumTimeSteps"]; }
95    }
96    public IValueParameter<IntMatrix> BoundsParameter {
97      get { return (IValueParameter<IntMatrix>)Parameters["Bounds"]; }
98    }
99    public IValueParameter<IGenotypeToPhenotypeMapper> GenotypeToPhenotypeMapperParameter {
100      get { return (IValueParameter<IGenotypeToPhenotypeMapper>)Parameters["GenotypeToPhenotypeMapper"]; }
101    }
102    #endregion
103
104    #region Properties
105    public BoolMatrix World {
106      get { return WorldParameter.Value; }
107      set { WorldParameter.Value = value; }
108    }
109    public IntValue MaxTimeSteps {
110      get { return MaxTimeStepsParameter.Value; }
111      set { MaxTimeStepsParameter.Value = value; }
112    }
113    public IntValue MaxExpressionLength {
114      get { return MaxExpressionLengthParameter.Value; }
115      set { MaxExpressionLengthParameter.Value = value; }
116    }
117    public ArtificialAntExpressionGrammar ArtificialAntExpressionGrammar {
118      get { return (ArtificialAntExpressionGrammar)ArtificialAntExpressionGrammarParameter.Value; }
119    }
120    public IEnumerable<IAntTrailAnalyzer> AntTrailAnalyzers {
121      get { return Operators.OfType<IAntTrailAnalyzer>(); }
122    }
123    public IntMatrix Bounds {
124      get { return BoundsParameter.Value; }
125      set { BoundsParameter.Value = value; }
126    }
127    #endregion
128
129    [StorableConstructor]
130    private GEArtificialAntProblem(bool deserializing) : base(deserializing) { }
131    [StorableHook(HookType.AfterDeserialization)]
132    private void AfterDeserialization() {
133      RegisterEventHandlers();
134    }
135
136    private GEArtificialAntProblem(GEArtificialAntProblem original, Cloner cloner)
137      : base(original, cloner) {
138      RegisterEventHandlers();
139    }
140
141    public override IDeepCloneable Clone(Cloner cloner) {
142      return new GEArtificialAntProblem(this, cloner);
143    }
144
145    public GEArtificialAntProblem()
146      : base(new GEArtificialAntEvaluator(), new UniformRandomIntegerVectorCreator()) {
147      BoolMatrix world = new BoolMatrix(santaFeAntTrail);
148      Parameters.Add(new ValueParameter<IntValue>("MaximumExpressionLength", "Maximal length of the expression to control the artificial ant (genotype length).", new IntValue(30)));
149      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", new ArtificialAntExpressionGrammar()));
150      Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world));
151      Parameters.Add(new ValueParameter<IntValue>("MaximumTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
152      IntMatrix m = new IntMatrix(new int[,] { { 0, 100 } });
153      Parameters.Add(new ValueParameter<IntMatrix>("Bounds", "The integer number range in which the single genomes of a genotype are created.", m));
154      Parameters.Add(new ValueParameter<IGenotypeToPhenotypeMapper>("GenotypeToPhenotypeMapper", "Maps the genotype (an integer vector) to the phenotype (a symbolic expression tree).", new DepthFirstMapper()));
155
156      Maximization.Value = true;
157      MaximizationParameter.Hidden = true;
158      BestKnownQuality = new DoubleValue(89);
159
160      SolutionCreator.IntegerVectorParameter.ActualName = "AntTrailSolutionIntegerVector";
161      Evaluator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolutionTree";
162      Evaluator.SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar";
163      Evaluator.QualityParameter.ActualName = "FoodEaten";
164
165      InitializeOperators();
166      RegisterEventHandlers();
167    }
168
169    #region Events
170    protected override void OnSolutionCreatorChanged() {
171      base.OnSolutionCreatorChanged();
172
173      SolutionCreator.IntegerVectorParameter.ActualName = "AntTrailSolutionIntegerVector";
174      SolutionCreator.IntegerVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
175
176      ParameterizeAnalyzers();
177      ParameterizeOperators();
178    }
179
180    protected override void OnEvaluatorChanged() {
181      base.OnEvaluatorChanged();
182
183      Evaluator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolutionTree";
184      Evaluator.SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar";
185      Evaluator.QualityParameter.ActualName = "FoodEaten";
186
187      Evaluator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged);
188      Evaluator.SymbolicExpressionTreeGrammarParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeGrammarParameter_ActualNameChanged);
189      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
190
191      ParameterizeAnalyzers();
192      ParameterizeOperators();
193    }
194
195    private void SolutionCreator_IntegerVectorParameter_ActualNameChanged(object sender, EventArgs e) {
196      ParameterizeAnalyzers();
197      ParameterizeOperators();
198    }
199
200    private void Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
201      ParameterizeAnalyzers();
202      ParameterizeOperators();
203    }
204
205    private void Evaluator_SymbolicExpressionTreeGrammarParameter_ActualNameChanged(object sender, EventArgs e) {
206      ParameterizeAnalyzers();
207      ParameterizeOperators();
208    }
209
210    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
211      ParameterizeAnalyzers();
212      ParameterizeOperators();
213    }
214    #endregion
215
216    #region Helpers
217    private void RegisterEventHandlers() {
218      SolutionCreator.IntegerVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
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
224    private void InitializeOperators() {
225      Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>().OfType<IOperator>());
226      Operators.Add(new BestAntTrailAnalyzer());
227      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
228      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
229      ParameterizeAnalyzers();
230      ParameterizeOperators();
231    }
232
233    private void ParameterizeAnalyzers() {
234      foreach (IAntTrailAnalyzer analyzer in AntTrailAnalyzers) {
235        analyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
236        analyzer.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
237        analyzer.WorldParameter.ActualName = WorldParameter.Name;
238        analyzer.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
239      }
240      foreach (ISymbolicExpressionTreeAnalyzer analyzer in Operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
241        analyzer.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
242      }
243    }
244
245    private void ParameterizeOperators() {
246      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
247
248      foreach (ISymbolicExpressionTreeGrammarBasedOperator op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
249        op.SymbolicExpressionTreeGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
250      }
251      foreach (GEArtificialAntEvaluator op in operators.OfType<GEArtificialAntEvaluator>()) {
252        op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
253        op.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
254        op.WorldParameter.ActualName = WorldParameter.Name;
255      }
256      foreach (IIntegerVectorCrossover op in operators.OfType<IIntegerVectorCrossover>()) {
257        op.ParentsParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
258        op.ChildParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
259      }
260      foreach (IIntegerVectorManipulator op in operators.OfType<IIntegerVectorManipulator>()) {
261        op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
262      }
263      foreach (IIntegerVectorCreator op in operators.OfType<IIntegerVectorCreator>()) {
264        op.BoundsParameter.ActualName = BoundsParameter.Name;
265        op.LengthParameter.ActualName = MaxExpressionLengthParameter.Name;
266      }
267    }
268    #endregion
269  }
270}
Note: See TracBrowser for help on using the repository browser.