Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3652 was 3651, checked in by gkronber, 15 years ago

Implemented analyzers for symbolic expression tree encoding, artificial ant problem and symbolic regression problem. #999 (Refactor algorithm analysis and tracing)

File size: 23.4 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;
34using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
35using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Crossovers;
36using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators;
37using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;
38using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
39using HeuristicLab.Problems.ArtificialAnt.Analyzers;
40using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Analyzers;
41
42namespace HeuristicLab.Problems.ArtificialAnt {
43  [Item("Artificial Ant Problem", "Represents the Artificial Ant problem.")]
44  [Creatable("Problems")]
45  [StorableClass]
46  public sealed class ArtificialAntProblem : ParameterizedNamedItem, ISingleObjectiveProblem {
47    public override Image ItemImage {
48      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
49    }
50
51    #region constant for default world (Santa Fe)
52    private readonly bool[,] santaFeAntTrail = new bool[,] {
53      {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},
54      {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},
55      {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},
56      {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},
57      {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},
58      {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},
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, true, 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, false, false, false, false, false, false},
61      {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, 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, false, false, false, true, false, false},
63      {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},
64      {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},
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, true, false, false},
66      {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},
67      {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},
68      {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},
69      {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},
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, false, false, false, false, false, false},
71      {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},
72      {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},
73      {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},
74      {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},
75      {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},
76      {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},
77      {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},
78      {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},
79      {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},
80      {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},
81      {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},
82      {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},
83      {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 },
84      {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 }
85    };
86    #endregion
87
88    #region Parameter Properties
89    public ValueParameter<BoolValue> MaximizationParameter {
90      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
91    }
92    IParameter ISingleObjectiveProblem.MaximizationParameter {
93      get { return MaximizationParameter; }
94    }
95    public ValueParameter<SymbolicExpressionTreeCreator> SolutionCreatorParameter {
96      get { return (ValueParameter<SymbolicExpressionTreeCreator>)Parameters["SolutionCreator"]; }
97    }
98    IParameter IProblem.SolutionCreatorParameter {
99      get { return SolutionCreatorParameter; }
100    }
101    public ValueParameter<Evaluator> EvaluatorParameter {
102      get { return (ValueParameter<Evaluator>)Parameters["Evaluator"]; }
103    }
104    IParameter IProblem.EvaluatorParameter {
105      get { return EvaluatorParameter; }
106    }
107    public ValueParameter<ISymbolicExpressionGrammar> ArtificialAntExpressionGrammarParameter {
108      get { return (ValueParameter<ISymbolicExpressionGrammar>)Parameters["ArtificialAntExpressionGrammar"]; }
109    }
110    public ValueParameter<IntValue> MaxExpressionLengthParameter {
111      get { return (ValueParameter<IntValue>)Parameters["MaxExpressionLength"]; }
112    }
113    public ValueParameter<IntValue> MaxExpressionDepthParameter {
114      get { return (ValueParameter<IntValue>)Parameters["MaxExpressionDepth"]; }
115    }
116    public ValueParameter<IntValue> MaxFunctionDefinitionsParameter {
117      get { return (ValueParameter<IntValue>)Parameters["MaxFunctionDefinitions"]; }
118    }
119    public ValueParameter<IntValue> MaxFunctionArgumentsParameter {
120      get { return (ValueParameter<IntValue>)Parameters["MaxFunctionArguments"]; }
121    }
122    public ValueParameter<BoolMatrix> WorldParameter {
123      get { return (ValueParameter<BoolMatrix>)Parameters["World"]; }
124    }
125    public ValueParameter<IntValue> MaxTimeStepsParameter {
126      get { return (ValueParameter<IntValue>)Parameters["MaxTimeSteps"]; }
127    }
128
129    public ValueParameter<DoubleValue> BestKnownQualityParameter {
130      get { return (ValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
131    }
132    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
133      get { return BestKnownQualityParameter; }
134    }
135    #endregion
136
137    #region Properties
138    public BoolMatrix World {
139      get { return WorldParameter.Value; }
140      set { WorldParameter.Value = value; }
141    }
142    public IntValue MaxTimeSteps {
143      get { return MaxTimeStepsParameter.Value; }
144      set { MaxTimeStepsParameter.Value = value; }
145    }
146    public IntValue MaxExpressionLength {
147      get { return MaxExpressionLengthParameter.Value; }
148      set { MaxExpressionLengthParameter.Value = value; }
149    }
150    public IntValue MaxExpressionDepth {
151      get { return MaxExpressionDepthParameter.Value; }
152      set { MaxExpressionDepthParameter.Value = value; }
153    }
154    public IntValue MaxFunctionDefinitions {
155      get { return MaxFunctionDefinitionsParameter.Value; }
156      set { MaxFunctionDefinitionsParameter.Value = value; }
157    }
158    public IntValue MaxFunctionArguments {
159      get { return MaxFunctionArgumentsParameter.Value; }
160      set { MaxFunctionArgumentsParameter.Value = value; }
161    }
162    public SymbolicExpressionTreeCreator SolutionCreator {
163      get { return SolutionCreatorParameter.Value; }
164      set { SolutionCreatorParameter.Value = value; }
165    }
166    ISolutionCreator IProblem.SolutionCreator {
167      get { return SolutionCreatorParameter.Value; }
168    }
169    public Evaluator Evaluator {
170      get { return EvaluatorParameter.Value; }
171      set { EvaluatorParameter.Value = value; }
172    }
173    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
174      get { return EvaluatorParameter.Value; }
175    }
176    IEvaluator IProblem.Evaluator {
177      get { return EvaluatorParameter.Value; }
178    }
179    public GlobalSymbolicExpressionGrammar ArtificialAntExpressionGrammar {
180      get { return (GlobalSymbolicExpressionGrammar)ArtificialAntExpressionGrammarParameter.Value; }
181    }
182    public DoubleValue BestKnownQuality {
183      get { return BestKnownQualityParameter.Value; }
184    }
185    private List<IOperator> operators;
186    public IEnumerable<IOperator> Operators {
187      get { return operators; }
188    }
189
190    public IEnumerable<IAntTrailPopulationAnalyzer> AntTrailAnalyzers {
191      get { return operators.OfType<IAntTrailPopulationAnalyzer>(); }
192    }
193    #endregion
194
195    public ArtificialAntProblem()
196      : base() {
197      SymbolicExpressionTreeCreator creator = new ProbabilisticTreeCreator();
198      Evaluator evaluator = new Evaluator();
199      BoolMatrix world = new BoolMatrix(santaFeAntTrail);
200      ISymbolicExpressionGrammar grammar = new GlobalSymbolicExpressionGrammar(new ArtificialAntExpressionGrammar());
201      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the Artificial Ant Problem is a maximization problem.", new BoolValue(true)));
202      Parameters.Add(new ValueParameter<SymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new artificial ant solutions.", creator));
203      Parameters.Add(new ValueParameter<Evaluator>("Evaluator", "The operator which should be used to evaluate artificial ant solutions.", evaluator));
204      Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this artificial ant instance.", new DoubleValue(89)));
205      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the expression to control the artificial ant.", new IntValue(100)));
206      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the expression to control the artificial ant.", new IntValue(10)));
207      Parameters.Add(new ValueParameter<IntValue>("MaxFunctionDefinitions", "Maximal number of automatically defined functions in the expression to control the artificial ant.", new IntValue(3)));
208      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)));
209      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", grammar));
210      Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world));
211      Parameters.Add(new ValueParameter<IntValue>("MaxTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
212
213      creator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolution";
214      evaluator.QualityParameter.ActualName = "FoodEaten";
215      ParameterizeSolutionCreator();
216      ParameterizeEvaluator();
217      Initialize();
218    }
219
220    [StorableConstructor]
221    private ArtificialAntProblem(bool deserializing) : base() { }
222
223    public override IDeepCloneable Clone(Cloner cloner) {
224      ArtificialAntProblem clone = (ArtificialAntProblem)base.Clone(cloner);
225      clone.Initialize();
226      return clone;
227    }
228
229    #region Events
230    public event EventHandler SolutionCreatorChanged;
231    private void OnSolutionCreatorChanged() {
232      var changed = SolutionCreatorChanged;
233      if (changed != null)
234        changed(this, EventArgs.Empty);
235    }
236    public event EventHandler EvaluatorChanged;
237    private void OnEvaluatorChanged() {
238      var changed = EvaluatorChanged;
239      if (changed != null)
240        changed(this, EventArgs.Empty);
241    }
242
243    public event EventHandler OperatorsChanged;
244    private void OnOperatorsChanged() {
245      var changed = OperatorsChanged;
246      if (changed != null)
247        changed(this, EventArgs.Empty);
248    }
249
250    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
251      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
252      ParameterizeSolutionCreator();
253      ParameterizeEvaluator();
254      ParameterizeAnalyzers();
255      ParameterizeOperators();
256      OnSolutionCreatorChanged();
257    }
258    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
259      ParameterizeEvaluator();
260      ParameterizeAnalyzers();
261      ParameterizeOperators();
262    }
263    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
264      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
265      ParameterizeEvaluator();
266      ParameterizeAnalyzers();
267      OnEvaluatorChanged();
268    }
269
270    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
271      ParameterizeAnalyzers();
272    }
273
274    #endregion
275
276    #region Helpers
277    [StorableHook(HookType.AfterDeserialization)]
278    private void Initialize() {
279      InitializeOperators();
280      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
281      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
282      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
283      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
284      MaxFunctionArgumentsParameter.ValueChanged += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
285      MaxFunctionArguments.ValueChanged += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
286      MaxFunctionDefinitionsParameter.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
287      MaxFunctionDefinitions.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
288    }
289
290    void MaxFunctionDefinitionsParameter_ValueChanged(object sender, EventArgs e) {
291      ArtificialAntExpressionGrammar.MaxFunctionDefinitions = MaxFunctionDefinitions.Value;
292    }
293
294    void MaxFunctionArgumentsParameter_ValueChanged(object sender, EventArgs e) {
295      ArtificialAntExpressionGrammar.MaxFunctionArguments = MaxFunctionArguments.Value;
296    }
297
298    private void InitializeOperators() {
299      operators = new List<IOperator>();
300      operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>().OfType<IOperator>());
301      operators.Add(new PopulationBestAntTrailAnalyzer());
302      operators.Add(new PopulationMinAvgMaxTreeSizeAnalyzer());
303      ParameterizeAnalyzers();
304      ParameterizeOperators();
305    }
306
307    private void ParameterizeSolutionCreator() {
308      SolutionCreator.SymbolicExpressionGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
309      SolutionCreator.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
310      SolutionCreator.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
311    }
312    private void ParameterizeEvaluator() {
313      Evaluator.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
314      Evaluator.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
315      Evaluator.WorldParameter.ActualName = WorldParameter.Name;
316    }
317    private void ParameterizeAnalyzers() {
318      foreach (IAntTrailPopulationAnalyzer analyzer in AntTrailAnalyzers) {
319        analyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
320        analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
321        analyzer.WorldParameter.ActualName = WorldParameter.Name;
322        analyzer.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
323      }
324      foreach (ISymbolicExpressionTreePopulationAnalyzer analyzer in Operators.OfType<ISymbolicExpressionTreePopulationAnalyzer>()) {
325        analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
326      }
327    }
328
329    private void ParameterizeOperators() {
330      foreach (ISymbolicExpressionTreeOperator op in Operators.OfType<ISymbolicExpressionTreeOperator>()) {
331        op.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
332        op.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
333        op.SymbolicExpressionGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
334      }
335      foreach (Evaluator op in Operators.OfType<Evaluator>()) {
336        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
337        op.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
338        op.WorldParameter.ActualName = WorldParameter.Name;
339      }
340      foreach (ISymbolicExpressionTreeCrossover op in Operators.OfType<ISymbolicExpressionTreeCrossover>()) {
341        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
342        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
343      }
344      foreach (ISymbolicExpressionTreeManipulator op in Operators.OfType<ISymbolicExpressionTreeManipulator>()) {
345        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
346      }
347      foreach (ISymbolicExpressionTreeArchitectureManipulator op in Operators.OfType<ISymbolicExpressionTreeArchitectureManipulator>()) {
348        op.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefinitionsParameter.Name;
349        op.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
350      }
351      foreach (SymbolicExpressionTreeCreator op in Operators.OfType<SymbolicExpressionTreeCreator>()) {
352        op.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
353        op.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefinitionsParameter.Name;
354      }
355    }
356
357
358    #endregion
359  }
360}
Note: See TracBrowser for help on using the repository browser.