Free cookie consent management tool by TermsFeed Policy Generator

source: branches/QAPAlgorithms/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntProblem.cs @ 6350

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

#1418: Reintegrated branch into trunk.

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