Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4681 was 4419, checked in by swagner, 14 years ago

Enabled saving only for some specific items (#1193)

File size: 23.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.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  [Creatable("Problems")]
42  [StorableClass]
43  public sealed class ArtificialAntProblem : ParameterizedNamedItem, ISingleObjectiveProblem, IStorableContent {
44    public string Filename { get; set; }
45
46    public override Image ItemImage {
47      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.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 ISingleObjectiveProblem.MaximizationParameter {
92      get { return MaximizationParameter; }
93    }
94    public ValueParameter<SymbolicExpressionTreeCreator> SolutionCreatorParameter {
95      get { return (ValueParameter<SymbolicExpressionTreeCreator>)Parameters["SolutionCreator"]; }
96    }
97    IParameter IProblem.SolutionCreatorParameter {
98      get { return SolutionCreatorParameter; }
99    }
100    public ValueParameter<Evaluator> EvaluatorParameter {
101      get { return (ValueParameter<Evaluator>)Parameters["Evaluator"]; }
102    }
103    IParameter IProblem.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 ISingleObjectiveProblem.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 IProblem.SolutionCreator {
166      get { return SolutionCreatorParameter.Value; }
167    }
168    public Evaluator Evaluator {
169      get { return EvaluatorParameter.Value; }
170      set { EvaluatorParameter.Value = value; }
171    }
172    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
173      get { return EvaluatorParameter.Value; }
174    }
175    IEvaluator IProblem.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    public ArtificialAntProblem()
199      : base() {
200      SymbolicExpressionTreeCreator creator = new ProbabilisticTreeCreator();
201      Evaluator evaluator = new Evaluator();
202      BoolMatrix world = new BoolMatrix(santaFeAntTrail);
203      ISymbolicExpressionGrammar grammar = new GlobalSymbolicExpressionGrammar(new ArtificialAntExpressionGrammar());
204      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the Artificial Ant Problem is a maximization problem.", new BoolValue(true)));
205      Parameters.Add(new ValueParameter<SymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new artificial ant solutions.", creator));
206      Parameters.Add(new ValueParameter<Evaluator>("Evaluator", "The operator which should be used to evaluate artificial ant solutions.", evaluator));
207      Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this artificial ant instance.", new DoubleValue(89)));
208      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the expression to control the artificial ant.", new IntValue(100)));
209      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the expression to control the artificial ant.", new IntValue(10)));
210      Parameters.Add(new ValueParameter<IntValue>("MaxFunctionDefinitions", "Maximal number of automatically defined functions in the expression to control the artificial ant.", new IntValue(3)));
211      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)));
212      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", grammar));
213      Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world));
214      Parameters.Add(new ValueParameter<IntValue>("MaxTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
215
216      creator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolution";
217      evaluator.QualityParameter.ActualName = "FoodEaten";
218      ParameterizeSolutionCreator();
219      ParameterizeEvaluator();
220      InitializeOperators();
221      AttachEventHandlers();
222    }
223
224    public override IDeepCloneable Clone(Cloner cloner) {
225      ArtificialAntProblem clone = (ArtificialAntProblem)base.Clone(cloner);
226      clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
227      clone.AttachEventHandlers();
228      return clone;
229    }
230
231    #region Events
232    public event EventHandler SolutionCreatorChanged;
233    private void OnSolutionCreatorChanged() {
234      EventHandler handler = SolutionCreatorChanged;
235      if (handler != null) handler(this, EventArgs.Empty);
236    }
237    public event EventHandler EvaluatorChanged;
238    private void OnEvaluatorChanged() {
239      EventHandler handler = EvaluatorChanged;
240      if (handler != null) handler(this, EventArgs.Empty);
241    }
242    public event EventHandler OperatorsChanged;
243    private void OnOperatorsChanged() {
244      EventHandler handler = OperatorsChanged;
245      if (handler != null) handler(this, EventArgs.Empty);
246    }
247    public event EventHandler Reset;
248    private void OnReset() {
249      EventHandler handler = Reset;
250      if (handler != null) handler(this, EventArgs.Empty);
251    }
252
253    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
254      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
255      ParameterizeSolutionCreator();
256      ParameterizeEvaluator();
257      ParameterizeAnalyzers();
258      ParameterizeOperators();
259      OnSolutionCreatorChanged();
260    }
261    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
262      ParameterizeEvaluator();
263      ParameterizeAnalyzers();
264      ParameterizeOperators();
265    }
266    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
267      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
268      ParameterizeEvaluator();
269      ParameterizeAnalyzers();
270      OnEvaluatorChanged();
271    }
272
273    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
274      ParameterizeAnalyzers();
275    }
276
277    #endregion
278
279    #region Helpers
280    [StorableHook(HookType.AfterDeserialization)]
281    private void AfterDeserializationHook() {
282      // BackwardsCompatibility3.3
283      #region Backwards compatible code (remove with 3.4)
284      if (operators == null) InitializeOperators();
285      #endregion
286      AttachEventHandlers();
287    }
288
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      ParameterizeAnalyzers();
314      ParameterizeOperators();
315    }
316
317    private void ParameterizeSolutionCreator() {
318      SolutionCreator.SymbolicExpressionGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
319      SolutionCreator.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
320      SolutionCreator.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
321    }
322    private void ParameterizeEvaluator() {
323      Evaluator.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
324      Evaluator.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
325      Evaluator.WorldParameter.ActualName = WorldParameter.Name;
326    }
327    private void ParameterizeAnalyzers() {
328      foreach (IAntTrailAnalyzer analyzer in AntTrailAnalyzers) {
329        analyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
330        analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
331        analyzer.WorldParameter.ActualName = WorldParameter.Name;
332        analyzer.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
333      }
334      foreach (ISymbolicExpressionTreeAnalyzer analyzer in Operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
335        analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
336      }
337    }
338
339    private void ParameterizeOperators() {
340      foreach (ISymbolicExpressionTreeOperator op in Operators.OfType<ISymbolicExpressionTreeOperator>()) {
341        op.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
342        op.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
343        op.SymbolicExpressionGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
344      }
345      foreach (Evaluator op in Operators.OfType<Evaluator>()) {
346        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
347        op.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
348        op.WorldParameter.ActualName = WorldParameter.Name;
349      }
350      foreach (ISymbolicExpressionTreeCrossover op in Operators.OfType<ISymbolicExpressionTreeCrossover>()) {
351        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
352        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
353      }
354      foreach (ISymbolicExpressionTreeManipulator op in Operators.OfType<ISymbolicExpressionTreeManipulator>()) {
355        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
356      }
357      foreach (ISymbolicExpressionTreeArchitectureManipulator op in Operators.OfType<ISymbolicExpressionTreeArchitectureManipulator>()) {
358        op.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefinitionsParameter.Name;
359        op.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
360      }
361      foreach (SymbolicExpressionTreeCreator op in Operators.OfType<SymbolicExpressionTreeCreator>()) {
362        op.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
363        op.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefinitionsParameter.Name;
364      }
365    }
366
367
368    #endregion
369  }
370}
Note: See TracBrowser for help on using the repository browser.