Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added first version of architecture altering operators for ADFs. #290 (Implement ADFs)

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