Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3238 was 3238, checked in by gkronber, 14 years ago

Implemented a solution visualizer for the artificial ant problem. #952 (Artificial Ant Problem for 3.3)

File size: 11.1 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.Linq;
25using System.Drawing;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
34
35namespace HeuristicLab.Problems.ArtificialAnt {
36  [Item("ArtificialAntProblem", "Represents the Artificial Ant problem.")]
37  [Creatable("Problems")]
38  [StorableClass]
39  public sealed class ArtificialAntProblem : ParameterizedNamedItem, ISingleObjectiveProblem {
40    public override Image ItemImage {
41      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
42    }
43
44    #region Parameter Properties
45    public ValueParameter<BoolValue> MaximizationParameter {
46      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
47    }
48    IParameter ISingleObjectiveProblem.MaximizationParameter {
49      get { return MaximizationParameter; }
50    }
51    public ValueParameter<SymbolicExpressionTreeCreator> SolutionCreatorParameter {
52      get { return (ValueParameter<SymbolicExpressionTreeCreator>)Parameters["SolutionCreator"]; }
53    }
54    IParameter IProblem.SolutionCreatorParameter {
55      get { return SolutionCreatorParameter; }
56    }
57    public ValueParameter<Evaluator> EvaluatorParameter {
58      get { return (ValueParameter<Evaluator>)Parameters["Evaluator"]; }
59    }
60    IParameter IProblem.EvaluatorParameter {
61      get { return EvaluatorParameter; }
62    }
63    public ValueParameter<ISymbolicExpressionGrammar> ArtificialAntExpressionGrammarParameter {
64      get { return (ValueParameter<ISymbolicExpressionGrammar>)Parameters["ArtificialAntExpressionGrammar"]; }
65    }
66    public ValueParameter<IntValue> MaxExpressionLengthParameter {
67      get { return (ValueParameter<IntValue>)Parameters["MaxExpressionLength"]; }
68    }
69    public ValueParameter<IntValue> MaxExpressionDepthParameter {
70      get { return (ValueParameter<IntValue>)Parameters["MaxExpressionDepth"]; }
71    }
72
73    public OptionalValueParameter<IAntTrailVisualizer> VisualizerParameter {
74      get { return (OptionalValueParameter<IAntTrailVisualizer>)Parameters["Visualizer"]; }
75    }
76    IParameter IProblem.VisualizerParameter {
77      get { return VisualizerParameter; }
78    }
79    public ValueParameter<DoubleValue> BestKnownQualityParameter {
80      get { return (ValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
81    }
82    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
83      get { return BestKnownQualityParameter; }
84    }
85    #endregion
86
87    #region Properties
88    public SymbolicExpressionTreeCreator SolutionCreator {
89      get { return SolutionCreatorParameter.Value; }
90      set { SolutionCreatorParameter.Value = value; }
91    }
92    ISolutionCreator IProblem.SolutionCreator {
93      get { return SolutionCreatorParameter.Value; }
94    }
95    public Evaluator Evaluator {
96      get { return EvaluatorParameter.Value; }
97      set { EvaluatorParameter.Value = value; }
98    }
99    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
100      get { return EvaluatorParameter.Value; }
101    }
102    IEvaluator IProblem.Evaluator {
103      get { return EvaluatorParameter.Value; }
104    }
105    public ISymbolicExpressionGrammar ArtificialAntExpressionGrammar {
106      get { return ArtificialAntExpressionGrammarParameter.Value; }
107    }
108    public IAntTrailVisualizer Visualizer {
109      get { return VisualizerParameter.Value; }
110      set { VisualizerParameter.Value = value; }
111    }
112    ISolutionsVisualizer IProblem.Visualizer {
113      get { return VisualizerParameter.Value; }
114    }
115    public DoubleValue BestKnownQuality {
116      get { return BestKnownQualityParameter.Value; }
117    }
118    private List<IOperator> operators;
119    public IEnumerable<IOperator> Operators {
120      get { return operators; }
121    }
122    #endregion
123
124    public ArtificialAntProblem()
125      : base() {
126      SymbolicExpressionTreeCreator creator = new ProbabilisticTreeCreator();
127      Evaluator evaluator = new Evaluator();
128      ArtificialAntExpressionGrammar grammar = new ArtificialAntExpressionGrammar();
129      BestAntTrailVisualizer visualizer = new BestAntTrailVisualizer();
130
131      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the Artificial Ant Problem is a maximization problem.", new BoolValue(true)));
132      Parameters.Add(new ValueParameter<SymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new artificial ant solutions.", creator));
133      Parameters.Add(new ValueParameter<Evaluator>("Evaluator", "The operator which should be used to evaluate artificial ant solutions.", evaluator));
134      Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this OneMax instance.", new DoubleValue(89)));
135      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", grammar));
136      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the expression to control the artificial ant.", new IntValue(100)));
137      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the expression to control the artificial ant.", new IntValue(10)));
138      Parameters.Add(new ValueParameter<IAntTrailVisualizer>("Visualizer", "The operator which should be used to visualize artificial ant solutions.", visualizer));
139
140      creator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolution";
141      evaluator.QualityParameter.ActualName = "FoodEaten";
142      ParameterizeSolutionCreator();
143      ParameterizeEvaluator();
144      ParameterizeVisualizer();
145
146      Initialize();
147    }
148
149    [StorableConstructor]
150    private ArtificialAntProblem(bool deserializing) : base() { }
151
152    public override IDeepCloneable Clone(Cloner cloner) {
153      ArtificialAntProblem clone = (ArtificialAntProblem)base.Clone(cloner);
154      clone.Initialize();
155      return clone;
156    }
157
158    #region Events
159    public event EventHandler SolutionCreatorChanged;
160    private void OnSolutionCreatorChanged() {
161      if (SolutionCreatorChanged != null)
162        SolutionCreatorChanged(this, EventArgs.Empty);
163    }
164    public event EventHandler EvaluatorChanged;
165    private void OnEvaluatorChanged() {
166      if (EvaluatorChanged != null)
167        EvaluatorChanged(this, EventArgs.Empty);
168    }
169    public event EventHandler VisualizerChanged;
170    private void OnVisualizerChanged() {
171      if (VisualizerChanged != null)
172        VisualizerChanged(this, EventArgs.Empty);
173    }
174
175    public event EventHandler OperatorsChanged;
176    private void OnOperatorsChanged() {
177      if (OperatorsChanged != null)
178        OperatorsChanged(this, EventArgs.Empty);
179    }
180
181    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
182      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
183      ParameterizeSolutionCreator();
184      ParameterizeEvaluator();
185      ParameterizeOperators();
186      OnSolutionCreatorChanged();
187    }
188    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
189      ParameterizeEvaluator();
190      ParameterizeOperators();
191    }
192    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
193      ParameterizeEvaluator();
194      OnEvaluatorChanged();
195    }
196    #endregion
197
198    #region Helpers
199    [StorableHook(HookType.AfterDeserialization)]
200    private void Initialize() {
201      InitializeOperators();
202      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
203      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
204      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
205    }
206    private void ParameterizeSolutionCreator() {
207      SolutionCreator.SymbolicExpressionGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
208    }
209    private void ParameterizeEvaluator() {
210      Evaluator.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
211    }
212    private void InitializeOperators() {
213      operators = new List<IOperator>();
214      operators.Add(Evaluator);
215      operators.Add(SolutionCreator);
216      operators.Add(new SubtreeCrossover());
217      ParameterizeOperators();
218    }
219
220    private void ParameterizeOperators() {
221      foreach (ProbabilisticTreeCreator op in Operators.OfType<ProbabilisticTreeCreator>()) {
222        op.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
223        op.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
224        op.SymbolicExpressionGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
225      }
226      foreach (Evaluator op in Operators.OfType<Evaluator>()) {
227        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
228      }
229      foreach (SymbolicExpressionTreeCrossover op in Operators.OfType<SymbolicExpressionTreeCrossover>()) {
230        op.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
231        op.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
232        op.SymbolicExpressionGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
233        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
234        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
235      }
236    }
237    private void ParameterizeVisualizer() {
238      if (Visualizer != null) {
239        Visualizer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
240        if (Visualizer is IAntTrailVisualizer)
241          ((IAntTrailVisualizer)Visualizer).SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
242      }
243    }
244
245    #endregion
246  }
247}
Note: See TracBrowser for help on using the repository browser.