Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3338 was 3338, checked in by gkronber, 12 years ago

Fixed bugs related to dynamic symbol constraints with ADFs. #290 (Implement ADFs)

File size: 24.1 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<IntValue> MaxFunctionDefinitionsParameter {
110      get { return (ValueParameter<IntValue>)Parameters["MaxFunctionDefinitions"]; }
111    }
112    public ValueParameter<IntValue> MaxFunctionArgumentsParameter {
113      get { return (ValueParameter<IntValue>)Parameters["MaxFunctionArguments"]; }
114    }
115    public ValueParameter<BoolMatrix> WorldParameter {
116      get { return (ValueParameter<BoolMatrix>)Parameters["World"]; }
117    }
118    public ValueParameter<IntValue> MaxTimeStepsParameter {
119      get { return (ValueParameter<IntValue>)Parameters["MaxTimeSteps"]; }
120    }
121
122    public OptionalValueParameter<ISingleObjectiveSolutionsVisualizer> VisualizerParameter {
123      get { return (OptionalValueParameter<ISingleObjectiveSolutionsVisualizer>)Parameters["Visualizer"]; }
124    }
125    IParameter IProblem.VisualizerParameter {
126      get { return VisualizerParameter; }
127    }
128    public ValueParameter<DoubleValue> BestKnownQualityParameter {
129      get { return (ValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
130    }
131    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
132      get { return BestKnownQualityParameter; }
133    }
134    #endregion
135
136    #region Properties
137    public BoolMatrix World {
138      get { return WorldParameter.Value; }
139      set { WorldParameter.Value = value; }
140    }
141    public IntValue MaxTimeSteps {
142      get { return MaxTimeStepsParameter.Value; }
143      set { MaxTimeStepsParameter.Value = value; }
144    }
145    public IntValue MaxExpressionLength {
146      get { return MaxExpressionLengthParameter.Value; }
147      set { MaxExpressionLengthParameter.Value = value; }
148    }
149    public IntValue MaxExpressionDepth {
150      get { return MaxExpressionDepthParameter.Value; }
151      set { MaxExpressionDepthParameter.Value = value; }
152    }
153    public IntValue MaxFunctionDefinitions {
154      get { return MaxFunctionDefinitionsParameter.Value; }
155      set { MaxFunctionDefinitionsParameter.Value = value; }
156    }
157    public IntValue MaxFunctionArguments {
158      get { return MaxFunctionArgumentsParameter.Value; }
159      set { MaxFunctionArgumentsParameter.Value = value; }
160    }
161    public SymbolicExpressionTreeCreator SolutionCreator {
162      get { return SolutionCreatorParameter.Value; }
163      set { SolutionCreatorParameter.Value = value; }
164    }
165    ISolutionCreator IProblem.SolutionCreator {
166      get { return SolutionCreatorParameter.Value; }
167    }
168    public Evaluator Evaluator {
169      get { return EvaluatorParameter.Value; }
170      set { EvaluatorParameter.Value = value; }
171    }
172    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
173      get { return EvaluatorParameter.Value; }
174    }
175    IEvaluator IProblem.Evaluator {
176      get { return EvaluatorParameter.Value; }
177    }
178    public GlobalSymbolicExpressionGrammar ArtificialAntExpressionGrammar {
179      get { return (GlobalSymbolicExpressionGrammar)ArtificialAntExpressionGrammarParameter.Value; }
180    }
181    public ISingleObjectiveSolutionsVisualizer Visualizer {
182      get { return VisualizerParameter.Value; }
183      set { VisualizerParameter.Value = value; }
184    }
185    ISolutionsVisualizer IProblem.Visualizer {
186      get { return VisualizerParameter.Value; }
187    }
188    public DoubleValue BestKnownQuality {
189      get { return BestKnownQualityParameter.Value; }
190    }
191    private List<ISymbolicExpressionTreeOperator> operators;
192    public IEnumerable<IOperator> Operators {
193      get { return operators.Cast<IOperator>(); }
194    }
195    #endregion
196
197    public ArtificialAntProblem()
198      : base() {
199      SymbolicExpressionTreeCreator creator = new ProbabilisticTreeCreator();
200      Evaluator evaluator = new Evaluator();
201      BestAntTrailVisualizer visualizer = new BestAntTrailVisualizer();
202      BoolMatrix world = new BoolMatrix(santaFeAntTrail);
203      ISymbolicExpressionGrammar grammar = new GlobalSymbolicExpressionGrammar(new ArtificialAntExpressionGrammar());
204      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the Artificial Ant Problem is a maximization problem.", new BoolValue(true)));
205      Parameters.Add(new ValueParameter<SymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new artificial ant solutions.", creator));
206      Parameters.Add(new ValueParameter<Evaluator>("Evaluator", "The operator which should be used to evaluate artificial ant solutions.", evaluator));
207      Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this artificial ant instance.", new DoubleValue(89)));
208      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the expression to control the artificial ant.", new IntValue(100)));
209      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the expression to control the artificial ant.", new IntValue(10)));
210      Parameters.Add(new ValueParameter<IntValue>("MaxFunctionDefinitions", "Maximal number of automatically defined functions in the expression to control the artificial ant.", new IntValue(3)));
211      Parameters.Add(new ValueParameter<IntValue>("MaxFunctionArguments", "Maximal number of arguments of automatically defined functions in the expression to control the artificial ant.", new IntValue(3)));
212      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", grammar));
213      Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world));
214      Parameters.Add(new ValueParameter<IntValue>("MaxTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
215      Parameters.Add(new ValueParameter<ISingleObjectiveSolutionsVisualizer>("Visualizer", "The operator which should be used to visualize artificial ant solutions.", visualizer));
216
217      creator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolution";
218      evaluator.QualityParameter.ActualName = "FoodEaten";
219      ParameterizeSolutionCreator();
220      ParameterizeEvaluator();
221      ParameterizeVisualizer();
222
223      Initialize();
224    }
225
226    [StorableConstructor]
227    private ArtificialAntProblem(bool deserializing) : base() { }
228
229    public override IDeepCloneable Clone(Cloner cloner) {
230      ArtificialAntProblem clone = (ArtificialAntProblem)base.Clone(cloner);
231      clone.Initialize();
232      return clone;
233    }
234
235    #region Events
236    public event EventHandler SolutionCreatorChanged;
237    private void OnSolutionCreatorChanged() {
238      var changed = SolutionCreatorChanged;
239      if (changed != null)
240        changed(this, EventArgs.Empty);
241    }
242    public event EventHandler EvaluatorChanged;
243    private void OnEvaluatorChanged() {
244      var changed = EvaluatorChanged;
245      if (changed != null)
246        changed(this, EventArgs.Empty);
247    }
248    public event EventHandler VisualizerChanged;
249    private void OnVisualizerChanged() {
250      var changed = VisualizerChanged;
251      if (changed != null)
252        changed(this, EventArgs.Empty);
253    }
254
255    public event EventHandler OperatorsChanged;
256    private void OnOperatorsChanged() {
257      var changed = OperatorsChanged;
258      if (changed != null)
259        changed(this, EventArgs.Empty);
260    }
261
262    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
263      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
264      ParameterizeSolutionCreator();
265      ParameterizeEvaluator();
266      ParameterizeVisualizer();
267      ParameterizeOperators();
268      OnSolutionCreatorChanged();
269    }
270    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
271      ParameterizeEvaluator();
272      ParameterizeVisualizer();
273      ParameterizeOperators();
274    }
275    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
276      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
277      ParameterizeEvaluator();
278      ParameterizeVisualizer();
279      OnEvaluatorChanged();
280    }
281
282    private void VisualizerParameter_ValueChanged(object sender, EventArgs e) {
283      ParameterizeVisualizer();
284      OnVisualizerChanged();
285    }
286
287    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
288      ParameterizeVisualizer();
289    }
290
291    #endregion
292
293    #region Helpers
294    [StorableHook(HookType.AfterDeserialization)]
295    private void Initialize() {
296      InitializeOperators();
297      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
298      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
299      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
300      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
301      VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged);
302      MaxFunctionArgumentsParameter.ValueChanged += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
303      MaxFunctionArguments.ValueChanged += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
304      MaxFunctionDefinitionsParameter.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
305      MaxFunctionDefinitions.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
306    }
307
308    void MaxFunctionDefinitionsParameter_ValueChanged(object sender, EventArgs e) {
309      ArtificialAntExpressionGrammar.MaxFunctionDefinitions = MaxFunctionDefinitions.Value;
310    }
311
312    void MaxFunctionArgumentsParameter_ValueChanged(object sender, EventArgs e) {
313      ArtificialAntExpressionGrammar.MaxFunctionArguments = MaxFunctionArguments.Value;
314    }
315
316    private void InitializeOperators() {
317      operators = new List<ISymbolicExpressionTreeOperator>();
318      operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
319      ParameterizeOperators();
320    }
321
322    private void ParameterizeSolutionCreator() {
323      SolutionCreator.SymbolicExpressionGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
324      SolutionCreator.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
325      SolutionCreator.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
326    }
327    private void ParameterizeEvaluator() {
328      Evaluator.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
329      Evaluator.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
330      Evaluator.WorldParameter.ActualName = WorldParameter.Name;
331    }
332    private void ParameterizeVisualizer() {
333      if (Visualizer != null) {
334        Visualizer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
335        var antTrailVisualizer = Visualizer as IAntTrailVisualizer;
336        if (antTrailVisualizer != null) {
337          antTrailVisualizer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
338          antTrailVisualizer.WorldParameter.ActualName = WorldParameter.Name;
339          antTrailVisualizer.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
340        }
341        var bestSymExpressionVisualizer = Visualizer as BestSymbolicExpressionTreeVisualizer;
342        if (bestSymExpressionVisualizer != null) {
343          bestSymExpressionVisualizer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
344        }
345      }
346    }
347
348    private void ParameterizeOperators() {
349      foreach (ISymbolicExpressionTreeOperator op in Operators.OfType<ISymbolicExpressionTreeOperator>()) {
350        op.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
351        op.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
352        op.SymbolicExpressionGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
353      }
354      foreach (Evaluator op in Operators.OfType<Evaluator>()) {
355        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
356        op.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
357        op.WorldParameter.ActualName = WorldParameter.Name;
358      }
359      foreach (SymbolicExpressionTreeCrossover op in Operators.OfType<SymbolicExpressionTreeCrossover>()) {
360        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
361        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
362      }
363      foreach (SymbolicExpressionTreeManipulator op in Operators.OfType<SymbolicExpressionTreeManipulator>()) {
364        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
365      }
366      foreach (SymbolicExpressionTreeArchitectureAlteringOperator op in Operators.OfType<SymbolicExpressionTreeArchitectureAlteringOperator>()) {
367        op.MaxFunctionDefiningBranchesParameter.ActualName = MaxFunctionDefinitionsParameter.Name;
368        op.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
369      }
370      foreach (SymbolicExpressionTreeCreator op in Operators.OfType<SymbolicExpressionTreeCreator>()) {
371        op.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
372        op.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefinitionsParameter.Name;
373      }
374    }
375
376
377    #endregion
378  }
379}
Note: See TracBrowser for help on using the repository browser.