Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntProblem.cs @ 3245

Last change on this file since 3245 was 3245, checked in by gkronber, 13 years ago

Added another visualizer that displays the symbolic expression tree of the best artificial ant solution. #952 (Artificial Ant Problem for 3.3)

File size: 21.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 System.Drawing;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
34
35namespace HeuristicLab.Problems.ArtificialAnt {
36  [Item("ArtificialAntProblem", "Represents the Artificial Ant problem.")]
37  [Creatable("Problems")]
38  [StorableClass]
39  public sealed class ArtificialAntProblem : ParameterizedNamedItem, ISingleObjectiveProblem {
40    public override Image ItemImage {
41      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
42    }
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 ValueParameter<BoolValue> MaximizationParameter {
83      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
84    }
85    IParameter ISingleObjectiveProblem.MaximizationParameter {
86      get { return MaximizationParameter; }
87    }
88    public ValueParameter<SymbolicExpressionTreeCreator> SolutionCreatorParameter {
89      get { return (ValueParameter<SymbolicExpressionTreeCreator>)Parameters["SolutionCreator"]; }
90    }
91    IParameter IProblem.SolutionCreatorParameter {
92      get { return SolutionCreatorParameter; }
93    }
94    public ValueParameter<Evaluator> EvaluatorParameter {
95      get { return (ValueParameter<Evaluator>)Parameters["Evaluator"]; }
96    }
97    IParameter IProblem.EvaluatorParameter {
98      get { return EvaluatorParameter; }
99    }
100    public ValueParameter<ISymbolicExpressionGrammar> ArtificialAntExpressionGrammarParameter {
101      get { return (ValueParameter<ISymbolicExpressionGrammar>)Parameters["ArtificialAntExpressionGrammar"]; }
102    }
103    public ValueParameter<IntValue> MaxExpressionLengthParameter {
104      get { return (ValueParameter<IntValue>)Parameters["MaxExpressionLength"]; }
105    }
106    public ValueParameter<IntValue> MaxExpressionDepthParameter {
107      get { return (ValueParameter<IntValue>)Parameters["MaxExpressionDepth"]; }
108    }
109    public ValueParameter<BoolMatrix> WorldParameter {
110      get { return (ValueParameter<BoolMatrix>)Parameters["World"]; }
111    }
112    public ValueParameter<IntValue> MaxTimeStepsParameter {
113      get { return (ValueParameter<IntValue>)Parameters["MaxTimeSteps"]; }
114    }
115
116    public OptionalValueParameter<ISingleObjectiveSolutionsVisualizer> VisualizerParameter {
117      get { return (OptionalValueParameter<ISingleObjectiveSolutionsVisualizer>)Parameters["Visualizer"]; }
118    }
119    IParameter IProblem.VisualizerParameter {
120      get { return VisualizerParameter; }
121    }
122    public ValueParameter<DoubleValue> BestKnownQualityParameter {
123      get { return (ValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
124    }
125    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
126      get { return BestKnownQualityParameter; }
127    }
128    #endregion
129
130    #region Properties
131    public BoolMatrix World {
132      get { return WorldParameter.Value; }
133      set { WorldParameter.Value = value; }
134    }
135    public IntValue MaxTimeSteps {
136      get { return MaxTimeStepsParameter.Value; }
137      set { MaxTimeStepsParameter.Value = value; }
138    }
139    public IntValue MaxExpressionLength {
140      get { return MaxExpressionLengthParameter.Value; }
141      set { MaxExpressionLengthParameter.Value = value; }
142    }
143    public IntValue MaxExpressionDepth {
144      get { return MaxExpressionDepthParameter.Value; }
145      set { MaxExpressionDepthParameter.Value = value; }
146    }
147    public SymbolicExpressionTreeCreator SolutionCreator {
148      get { return SolutionCreatorParameter.Value; }
149      set { SolutionCreatorParameter.Value = value; }
150    }
151    ISolutionCreator IProblem.SolutionCreator {
152      get { return SolutionCreatorParameter.Value; }
153    }
154    public Evaluator Evaluator {
155      get { return EvaluatorParameter.Value; }
156      set { EvaluatorParameter.Value = value; }
157    }
158    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
159      get { return EvaluatorParameter.Value; }
160    }
161    IEvaluator IProblem.Evaluator {
162      get { return EvaluatorParameter.Value; }
163    }
164    public ISymbolicExpressionGrammar ArtificialAntExpressionGrammar {
165      get { return ArtificialAntExpressionGrammarParameter.Value; }
166    }
167    public ISingleObjectiveSolutionsVisualizer Visualizer {
168      get { return VisualizerParameter.Value; }
169      set { VisualizerParameter.Value = value; }
170    }
171    ISolutionsVisualizer IProblem.Visualizer {
172      get { return VisualizerParameter.Value; }
173    }
174    public DoubleValue BestKnownQuality {
175      get { return BestKnownQualityParameter.Value; }
176    }
177    private List<ISymbolicExpressionTreeOperator> operators;
178    public IEnumerable<IOperator> Operators {
179      get { return operators.Cast<IOperator>(); }
180    }
181    #endregion
182
183    public ArtificialAntProblem()
184      : base() {
185      SymbolicExpressionTreeCreator creator = new ProbabilisticTreeCreator();
186      Evaluator evaluator = new Evaluator();
187      ArtificialAntExpressionGrammar grammar = new ArtificialAntExpressionGrammar();
188      BestAntTrailVisualizer visualizer = new BestAntTrailVisualizer();
189      BoolMatrix world = new BoolMatrix(santaFeAntTrail);
190      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the Artificial Ant Problem is a maximization problem.", new BoolValue(true)));
191      Parameters.Add(new ValueParameter<SymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new artificial ant solutions.", creator));
192      Parameters.Add(new ValueParameter<Evaluator>("Evaluator", "The operator which should be used to evaluate artificial ant solutions.", evaluator));
193      Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this OneMax instance.", new DoubleValue(89)));
194      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", grammar));
195      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the expression to control the artificial ant.", new IntValue(100)));
196      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the expression to control the artificial ant.", new IntValue(10)));
197      Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world));
198      Parameters.Add(new ValueParameter<IntValue>("MaxTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
199      Parameters.Add(new ValueParameter<ISingleObjectiveSolutionsVisualizer>("Visualizer", "The operator which should be used to visualize artificial ant solutions.", visualizer));
200
201      creator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolution";
202      evaluator.QualityParameter.ActualName = "FoodEaten";
203      ParameterizeSolutionCreator();
204      ParameterizeEvaluator();
205      ParameterizeVisualizer();
206
207      Initialize();
208    }
209
210    [StorableConstructor]
211    private ArtificialAntProblem(bool deserializing) : base() { }
212
213    public override IDeepCloneable Clone(Cloner cloner) {
214      ArtificialAntProblem clone = (ArtificialAntProblem)base.Clone(cloner);
215      clone.Initialize();
216      return clone;
217    }
218
219    #region Events
220    public event EventHandler SolutionCreatorChanged;
221    private void OnSolutionCreatorChanged() {
222      var changed = SolutionCreatorChanged;
223      if (changed != null)
224        changed(this, EventArgs.Empty);
225    }
226    public event EventHandler EvaluatorChanged;
227    private void OnEvaluatorChanged() {
228      var changed = EvaluatorChanged;
229      if (changed != null)
230        changed(this, EventArgs.Empty);
231    }
232    public event EventHandler VisualizerChanged;
233    private void OnVisualizerChanged() {
234      var changed = VisualizerChanged;
235      if (changed != null)
236        changed(this, EventArgs.Empty);
237    }
238
239    public event EventHandler OperatorsChanged;
240    private void OnOperatorsChanged() {
241      var changed = OperatorsChanged;
242      if (changed != null)
243        changed(this, EventArgs.Empty);
244    }
245
246    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
247      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
248      ParameterizeSolutionCreator();
249      ParameterizeEvaluator();
250      ParameterizeVisualizer();
251      ParameterizeOperators();
252      OnSolutionCreatorChanged();
253    }
254    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
255      ParameterizeEvaluator();
256      ParameterizeVisualizer();
257      ParameterizeOperators();
258    }
259    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
260      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
261      ParameterizeEvaluator();
262      ParameterizeVisualizer();
263      OnEvaluatorChanged();
264    }
265
266    private void VisualizerParameter_ValueChanged(object sender, EventArgs e) {
267      ParameterizeVisualizer();
268      OnVisualizerChanged();
269    }
270
271    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
272      ParameterizeVisualizer();
273    }
274
275    #endregion
276
277    #region Helpers
278    [StorableHook(HookType.AfterDeserialization)]
279    private void Initialize() {
280      InitializeOperators();
281      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
282      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
283      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
284      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
285      VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged);
286    }
287
288    private void InitializeOperators() {
289      operators = new List<ISymbolicExpressionTreeOperator>();
290      operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
291      ParameterizeOperators();
292    }
293
294    private void ParameterizeSolutionCreator() {
295      SolutionCreator.SymbolicExpressionGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
296      SolutionCreator.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
297      SolutionCreator.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
298    }
299    private void ParameterizeEvaluator() {
300      Evaluator.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
301      Evaluator.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
302      Evaluator.WorldParameter.ActualName = WorldParameter.Name;
303    }
304    private void ParameterizeVisualizer() {
305      if (Visualizer != null) {
306        Visualizer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
307        var antTrailVisualizer = Visualizer as IAntTrailVisualizer;
308        if (antTrailVisualizer != null) {
309          antTrailVisualizer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
310          antTrailVisualizer.WorldParameter.ActualName = WorldParameter.Name;
311          antTrailVisualizer.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
312        }
313        var bestSymExpressionVisualizer = Visualizer as BestSymbolicExpressionTreeVisualizer;
314        if (bestSymExpressionVisualizer != null) {
315          bestSymExpressionVisualizer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
316        }
317      }
318    }
319
320    private void ParameterizeOperators() {
321      foreach (ISymbolicExpressionTreeOperator op in Operators.OfType<ISymbolicExpressionTreeOperator>()) {
322        op.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
323        op.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
324        op.SymbolicExpressionGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
325      }
326      foreach (Evaluator op in Operators.OfType<Evaluator>()) {
327        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
328        op.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
329        op.WorldParameter.ActualName = WorldParameter.Name;
330      }
331      foreach (SymbolicExpressionTreeCrossover op in Operators.OfType<SymbolicExpressionTreeCrossover>()) {
332        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
333        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
334      }
335    }
336
337
338    #endregion
339  }
340}
Note: See TracBrowser for help on using the repository browser.