Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Problems.ArtificialAnt/3.4/ArtificialAntProblem.cs @ 12648

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

#1418: Reintegrated branch into trunk.

File size: 23.5 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, ISingleObjectiveHeuristicOptimizationProblem, 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 ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
89      get { return MaximizationParameter; }
90    }
91    public IValueParameter<ISymbolicExpressionTreeCreator> SolutionCreatorParameter {
92      get { return (IValueParameter<ISymbolicExpressionTreeCreator>)Parameters["SolutionCreator"]; }
93    }
94    IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
95      get { return SolutionCreatorParameter; }
96    }
97    public IValueParameter<Evaluator> EvaluatorParameter {
98      get { return (IValueParameter<Evaluator>)Parameters["Evaluator"]; }
99    }
100    IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
101      get { return EvaluatorParameter; }
102    }
103    public IValueParameter<ISymbolicExpressionGrammar> ArtificialAntExpressionGrammarParameter {
104      get { return (IValueParameter<ISymbolicExpressionGrammar>)Parameters["ArtificialAntExpressionGrammar"]; }
105    }
106    public IValueParameter<IntValue> MaxExpressionLengthParameter {
107      get { return (IValueParameter<IntValue>)Parameters["MaximumExpressionLength"]; }
108    }
109    public IValueParameter<IntValue> MaxExpressionDepthParameter {
110      get { return (IValueParameter<IntValue>)Parameters["MaximumExpressionDepth"]; }
111    }
112    public IValueParameter<IntValue> MaxFunctionDefinitionsParameter {
113      get { return (IValueParameter<IntValue>)Parameters["MaximumFunctionDefinitions"]; }
114    }
115    public IValueParameter<IntValue> MaxFunctionArgumentsParameter {
116      get { return (ValueParameter<IntValue>)Parameters["MaximumFunctionArguments"]; }
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["MaximumTimeSteps"]; }
123    }
124
125    public IValueParameter<DoubleValue> BestKnownQualityParameter {
126      get { return (IValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
127    }
128    IParameter ISingleObjectiveHeuristicOptimizationProblem.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 IHeuristicOptimizationProblem.SolutionCreator {
163      get { return SolutionCreatorParameter.Value; }
164    }
165    public Evaluator Evaluator {
166      get { return EvaluatorParameter.Value; }
167      set { EvaluatorParameter.Value = value; }
168    }
169    ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
170      get { return EvaluatorParameter.Value; }
171    }
172    IEvaluator IHeuristicOptimizationProblem.Evaluator {
173      get { return EvaluatorParameter.Value; }
174    }
175    public ArtificialAntExpressionGrammar ArtificialAntExpressionGrammar {
176      get { return (ArtificialAntExpressionGrammar)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      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the Artificial Ant Problem is a maximization problem.", new BoolValue(true)));
214      Parameters.Add(new ValueParameter<ISymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new artificial ant solutions.", creator));
215      Parameters.Add(new ValueParameter<Evaluator>("Evaluator", "The operator which should be used to evaluate artificial ant solutions.", evaluator));
216      Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this artificial ant instance.", new DoubleValue(89)));
217      Parameters.Add(new ValueParameter<IntValue>("MaximumExpressionLength", "Maximal length of the expression to control the artificial ant.", new IntValue(100)));
218      Parameters.Add(new ValueParameter<IntValue>("MaximumExpressionDepth", "Maximal depth of the expression to control the artificial ant.", new IntValue(10)));
219      Parameters.Add(new ValueParameter<IntValue>("MaximumFunctionDefinitions", "Maximal number of automatically defined functions in the expression to control the artificial ant.", new IntValue(3)));
220      Parameters.Add(new ValueParameter<IntValue>("MaximumFunctionArguments", "Maximal number of arguments of automatically defined functions in the expression to control the artificial ant.", new IntValue(3)));
221      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", new ArtificialAntExpressionGrammar()));
222      Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world));
223      Parameters.Add(new ValueParameter<IntValue>("MaximumTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
224
225      creator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolution";
226      creator.SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar";
227      evaluator.QualityParameter.ActualName = "FoodEaten";
228      InitializeOperators();
229      AttachEventHandlers();
230    }
231
232    #region Events
233    public event EventHandler SolutionCreatorChanged;
234    private void OnSolutionCreatorChanged() {
235      EventHandler handler = SolutionCreatorChanged;
236      if (handler != null) handler(this, EventArgs.Empty);
237    }
238    public event EventHandler EvaluatorChanged;
239    private void OnEvaluatorChanged() {
240      EventHandler handler = EvaluatorChanged;
241      if (handler != null) handler(this, EventArgs.Empty);
242    }
243    public event EventHandler OperatorsChanged;
244    private void OnOperatorsChanged() {
245      EventHandler handler = OperatorsChanged;
246      if (handler != null) handler(this, EventArgs.Empty);
247    }
248    public event EventHandler Reset;
249    private void OnReset() {
250      EventHandler handler = Reset;
251      if (handler != null) handler(this, EventArgs.Empty);
252    }
253
254    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
255      SolutionCreator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolution";
256      var grammarBased = SolutionCreator as ISymbolicExpressionTreeGrammarBasedOperator;
257      if (grammarBased != null)
258        grammarBased.SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar";
259
260      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
261      ParameterizeAnalyzers();
262      ParameterizeOperators();
263      OnSolutionCreatorChanged();
264    }
265    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
266      ParameterizeAnalyzers();
267      ParameterizeOperators();
268    }
269    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
270      Evaluator.QualityParameter.ActualName = "FoodEaten";
271      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
272      ParameterizeAnalyzers();
273      ParameterizeOperators();
274      OnEvaluatorChanged();
275    }
276    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
277      ParameterizeAnalyzers();
278      ParameterizeOperators();
279    }
280    #endregion
281
282    #region Helpers
283    private void AttachEventHandlers() {
284      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
285      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
286      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
287      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
288      MaxFunctionArgumentsParameter.ValueChanged += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
289      MaxFunctionArguments.ValueChanged += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
290      MaxFunctionDefinitionsParameter.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
291      MaxFunctionDefinitions.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
292    }
293
294    private void MaxFunctionDefinitionsParameter_ValueChanged(object sender, EventArgs e) {
295      ArtificialAntExpressionGrammar.MaximumFunctionDefinitions = MaxFunctionDefinitions.Value;
296      ParameterizeOperators();
297      ParameterizeAnalyzers();
298    }
299    private void MaxFunctionArgumentsParameter_ValueChanged(object sender, EventArgs e) {
300      ArtificialAntExpressionGrammar.MaximumFunctionArguments = MaxFunctionArguments.Value;
301      ParameterizeOperators();
302      ParameterizeAnalyzers();
303    }
304
305    private void InitializeOperators() {
306      operators = new List<IOperator>();
307      operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>().OfType<IOperator>());
308      operators.Add(new BestAntTrailAnalyzer());
309      operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
310      operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
311      ParameterizeAnalyzers();
312      ParameterizeOperators();
313    }
314
315    private void ParameterizeAnalyzers() {
316      foreach (IAntTrailAnalyzer analyzer in AntTrailAnalyzers) {
317        analyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
318        analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
319        analyzer.WorldParameter.ActualName = WorldParameter.Name;
320        analyzer.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
321      }
322      foreach (ISymbolicExpressionTreeAnalyzer analyzer in Operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
323        analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
324      }
325    }
326
327    private void ParameterizeOperators() {
328      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
329
330      foreach (ISymbolicExpressionTreeGrammarBasedOperator op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
331        op.SymbolicExpressionTreeGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
332      }
333      foreach (ISymbolicExpressionTreeSizeConstraintOperator op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
334        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaxExpressionDepthParameter.Name;
335        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaxExpressionLengthParameter.Name;
336
337      }
338      foreach (Evaluator op in operators.OfType<Evaluator>()) {
339        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
340        op.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
341        op.WorldParameter.ActualName = WorldParameter.Name;
342      }
343      foreach (ISymbolicExpressionTreeCrossover op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
344        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
345        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
346      }
347      foreach (ISymbolicExpressionTreeManipulator op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
348        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
349      }
350      foreach (ISymbolicExpressionTreeArchitectureAlteringOperator op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
351        op.MaximumFunctionDefinitionsParameter.ActualName = MaxFunctionDefinitionsParameter.Name;
352        op.MaximumFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
353      }
354    }
355    #endregion
356  }
357}
Note: See TracBrowser for help on using the repository browser.