Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added work in progress for the artificial ant problem #952 (Artificial Ant Problem for 3.3) and for the symbolic expression tree encoding #937 (Data types and operators for symbolic expression tree encoding).

File size: 10.0 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
130      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the Artificial Ant Problem is a maximization problem.", new BoolValue(true)));
131      Parameters.Add(new ValueParameter<SymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new artificial ant solutions.", creator));
132      Parameters.Add(new ValueParameter<Evaluator>("Evaluator", "The operator which should be used to evaluate artificial ant solutions.", evaluator));
133      Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this OneMax instance.", new DoubleValue(200)));
134      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", grammar));
135      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the expression to control the artificial ant.", new IntValue(100)));
136      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the expression to control the artificial ant.", new IntValue(10)));
137      Parameters.Add(new ValueParameter<IAntTrailVisualizer>("Visualizer", "The operator which should be used to visualize artificial ant solutions.", null));
138
139      creator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolution";
140      evaluator.QualityParameter.ActualName = "FoodEaten";
141      ParameterizeSolutionCreator();
142      ParameterizeEvaluator();
143
144      Initialize();
145    }
146
147    [StorableConstructor]
148    private ArtificialAntProblem(bool deserializing) : base() { }
149
150    public override IDeepCloneable Clone(Cloner cloner) {
151      ArtificialAntProblem clone = (ArtificialAntProblem)base.Clone(cloner);
152      clone.Initialize();
153      return clone;
154    }
155
156    #region Events
157    public event EventHandler SolutionCreatorChanged;
158    private void OnSolutionCreatorChanged() {
159      if (SolutionCreatorChanged != null)
160        SolutionCreatorChanged(this, EventArgs.Empty);
161    }
162    public event EventHandler EvaluatorChanged;
163    private void OnEvaluatorChanged() {
164      if (EvaluatorChanged != null)
165        EvaluatorChanged(this, EventArgs.Empty);
166    }
167    public event EventHandler VisualizerChanged;
168    private void OnVisualizerChanged() {
169      if (VisualizerChanged != null)
170        VisualizerChanged(this, EventArgs.Empty);
171    }
172
173    public event EventHandler OperatorsChanged;
174    private void OnOperatorsChanged() {
175      if (OperatorsChanged != null)
176        OperatorsChanged(this, EventArgs.Empty);
177    }
178
179    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
180      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
181      ParameterizeSolutionCreator();
182      ParameterizeEvaluator();
183      ParameterizeOperators();
184      OnSolutionCreatorChanged();
185    }
186    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
187      ParameterizeEvaluator();
188      ParameterizeOperators();
189    }
190    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
191      ParameterizeEvaluator();
192      OnEvaluatorChanged();
193    }
194    #endregion
195
196    #region Helpers
197    [StorableHook(HookType.AfterDeserialization)]
198    private void Initialize() {
199      InitializeOperators();
200      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
201      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
202      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
203    }
204    private void ParameterizeSolutionCreator() {
205      SolutionCreator.SymbolicExpressionGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
206    }
207    private void ParameterizeEvaluator() {
208      Evaluator.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
209    }
210    private void InitializeOperators() {
211      operators = new List<IOperator>();
212      operators.Add(Evaluator);
213      operators.Add(SolutionCreator);
214      ParameterizeOperators();
215    }
216
217    private void ParameterizeOperators() {
218      foreach (ProbabilisticTreeCreator op in Operators.OfType<ProbabilisticTreeCreator>()) {
219        op.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
220        op.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
221        op.SymbolicExpressionGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
222      }
223      foreach (Evaluator op in Operators.OfType<Evaluator>()) {
224        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
225      }
226    }
227    #endregion
228  }
229}
Note: See TracBrowser for help on using the repository browser.