Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.ArtificialAnt/3.4/ArtificialAntProblem.cs @ 5547

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

#1418: Updated ArtificialAntProblem.

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