Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntProblem.cs @ 5625

Last change on this file since 5625 was 5625, checked in by mkommend, 14 years ago

#1418: Reorganized branch and removed CreateAble-Attribute from outdated classes.

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