Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionProblem.cs @ 3221

Last change on this file since 3221 was 3221, checked in by abeham, 13 years ago

updated permutation moves, fixed 3-opt #889

File size: 14.6 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.Collections.ObjectModel;
25using System.Drawing;
26using System.Linq;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Encodings.RealVectorEncoding;
31using HeuristicLab.Optimization;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.PluginInfrastructure;
35
36namespace HeuristicLab.Problems.TestFunctions {
37  [Item("Single Objective Test Function", "Test function with real valued inputs and a single objective.")]
38  [StorableClass]
39  [Creatable("Problems")]
40  public sealed class SingleObjectiveTestFunctionProblem : ParameterizedNamedItem, ISingleObjectiveProblem {
41    public override Image ItemImage {
42      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
43    }
44
45    #region Parameter Properties
46    public ValueParameter<BoolValue> MaximizationParameter {
47      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
48    }
49    IParameter ISingleObjectiveProblem.MaximizationParameter {
50      get { return MaximizationParameter; }
51    }
52    public ValueParameter<DoubleMatrix> BoundsParameter {
53      get { return (ValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
54    }
55    public ValueParameter<IntValue> ProblemSizeParameter {
56      get { return (ValueParameter<IntValue>)Parameters["ProblemSize"]; }
57    }
58    public ValueParameter<IRealVectorCreator> SolutionCreatorParameter {
59      get { return (ValueParameter<IRealVectorCreator>)Parameters["SolutionCreator"]; }
60    }
61    IParameter IProblem.SolutionCreatorParameter {
62      get { return SolutionCreatorParameter; }
63    }
64    public ValueParameter<ISingleObjectiveTestFunctionProblemEvaluator> EvaluatorParameter {
65      get { return (ValueParameter<ISingleObjectiveTestFunctionProblemEvaluator>)Parameters["Evaluator"]; }
66    }
67    IParameter IProblem.EvaluatorParameter {
68      get { return EvaluatorParameter; }
69    }
70    public OptionalValueParameter<ISingleObjectiveTestFunctionProblemSolutionsVisualizer> VisualizerParameter {
71      get { return (OptionalValueParameter<ISingleObjectiveTestFunctionProblemSolutionsVisualizer>)Parameters["Visualizer"]; }
72    }
73    IParameter IProblem.VisualizerParameter {
74      get { return VisualizerParameter; }
75    }
76    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
77      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
78    }
79    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
80      get { return BestKnownQualityParameter; }
81    }
82    #endregion
83
84    #region Properties
85    public BoolValue Maximization {
86      get { return MaximizationParameter.Value; }
87      set { MaximizationParameter.Value = value; }
88    }
89    public DoubleMatrix Bounds {
90      get { return BoundsParameter.Value; }
91      set { BoundsParameter.Value = value; }
92    }
93    public IntValue ProblemSize {
94      get { return ProblemSizeParameter.Value; }
95      set { ProblemSizeParameter.Value = value; }
96    }
97    public IRealVectorCreator SolutionCreator {
98      get { return SolutionCreatorParameter.Value; }
99      set { SolutionCreatorParameter.Value = value; }
100    }
101    ISolutionCreator IProblem.SolutionCreator {
102      get { return SolutionCreatorParameter.Value; }
103    }
104    public ISingleObjectiveTestFunctionProblemEvaluator Evaluator {
105      get { return EvaluatorParameter.Value; }
106      set { EvaluatorParameter.Value = value; }
107    }
108    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
109      get { return EvaluatorParameter.Value; }
110    }
111    IEvaluator IProblem.Evaluator {
112      get { return EvaluatorParameter.Value; }
113    }
114    public ISingleObjectiveTestFunctionProblemSolutionsVisualizer Visualizer {
115      get { return VisualizerParameter.Value; }
116      set { VisualizerParameter.Value = value; }
117    }
118    ISolutionsVisualizer IProblem.Visualizer {
119      get { return VisualizerParameter.Value; }
120    }
121    public DoubleValue BestKnownQuality {
122      get { return BestKnownQualityParameter.Value; }
123      set { BestKnownQualityParameter.Value = value; }
124    }
125    private List<IOperator> operators;
126    public IEnumerable<IOperator> Operators {
127      get { return operators; }
128    }
129    #endregion
130
131    [StorableConstructor]
132    private SingleObjectiveTestFunctionProblem(bool deserializing) : base() { }
133    public SingleObjectiveTestFunctionProblem()
134      : base() {
135      UniformRandomRealVectorCreator creator = new UniformRandomRealVectorCreator();
136      AckleyEvaluator evaluator = new AckleyEvaluator();
137
138      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as most test functions are minimization problems.", new BoolValue(evaluator.Maximization)));
139      Parameters.Add(new ValueParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension.", evaluator.Bounds));
140      Parameters.Add(new ValueParameter<IntValue>("ProblemSize", "The dimension of the problem.", new IntValue(2)));
141      Parameters.Add(new ValueParameter<IRealVectorCreator>("SolutionCreator", "The operator which should be used to create new TSP solutions.", creator));
142      Parameters.Add(new ValueParameter<ISingleObjectiveTestFunctionProblemEvaluator>("Evaluator", "The operator which should be used to evaluate TSP solutions.", evaluator));
143      Parameters.Add(new OptionalValueParameter<ISingleObjectiveTestFunctionProblemSolutionsVisualizer>("Visualizer", "The operator which should be used to visualize TSP solutions."));
144      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this TSP instance.", new DoubleValue(evaluator.BestKnownQuality)));
145
146      creator.RealVectorParameter.ActualName = "Point";
147      ParameterizeSolutionCreator();
148      ParameterizeEvaluator();
149      ParameterizeVisualizer();
150
151      Initialize();
152    }
153
154    public override IDeepCloneable Clone(Cloner cloner) {
155      SingleObjectiveTestFunctionProblem clone = (SingleObjectiveTestFunctionProblem)base.Clone(cloner);
156      clone.Initialize();
157      return clone;
158    }
159
160    #region Events
161    public event EventHandler SolutionCreatorChanged;
162    private void OnSolutionCreatorChanged() {
163      if (SolutionCreatorChanged != null)
164        SolutionCreatorChanged(this, EventArgs.Empty);
165    }
166    public event EventHandler EvaluatorChanged;
167    private void OnEvaluatorChanged() {
168      if (EvaluatorChanged != null)
169        EvaluatorChanged(this, EventArgs.Empty);
170    }
171    public event EventHandler VisualizerChanged;
172    private void OnVisualizerChanged() {
173      if (VisualizerChanged != null)
174        VisualizerChanged(this, EventArgs.Empty);
175    }
176    public event EventHandler OperatorsChanged;
177    private void OnOperatorsChanged() {
178      if (OperatorsChanged != null)
179        OperatorsChanged(this, EventArgs.Empty);
180    }
181    private void ProblemSizeParameter_ValueChanged(object sender, EventArgs e) {
182      ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
183      ProblemSize_ValueChanged(null, EventArgs.Empty);
184    }
185    private void ProblemSize_ValueChanged(object sender, EventArgs e) {
186      ParameterizeSolutionCreator();
187    }
188    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
189      ParameterizeSolutionCreator();
190      SolutionCreator_RealVectorParameter_ActualNameChanged(null, EventArgs.Empty);
191    }
192    private void SolutionCreator_RealVectorParameter_ActualNameChanged(object sender, EventArgs e) {
193      ParameterizeEvaluator();
194      ParameterizeOperators();
195      ParameterizeVisualizer();
196    }
197    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
198      ParameterizeEvaluator();
199      UpdateMoveEvaluators();
200      Maximization.Value = Evaluator.Maximization;
201      BoundsParameter.Value = Evaluator.Bounds;
202      if (ProblemSize.Value < Evaluator.MinimumProblemSize)
203        ProblemSize.Value = Evaluator.MinimumProblemSize;
204      else if (ProblemSize.Value > Evaluator.MaximumProblemSize)
205        ProblemSize.Value = Evaluator.MaximumProblemSize;
206      BestKnownQuality = new DoubleValue(Evaluator.BestKnownQuality);
207      Evaluator_QualityParameter_ActualNameChanged(null, EventArgs.Empty);
208    }
209    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
210      ParameterizeOperators();
211    }
212    private void VisualizerParameter_ValueChanged(object sender, EventArgs e) {
213      ParameterizeVisualizer();
214    }
215    private void BoundsParameter_ValueChanged(object sender, EventArgs e) {
216      Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
217      Bounds_ToStringChanged(null, EventArgs.Empty);
218    }
219    private void Bounds_ToStringChanged(object sender, EventArgs e) {
220      if (Bounds.Columns != 2 || Bounds.Rows < 1)
221        Bounds = new DoubleMatrix(1, 2);
222    }
223    private void Bounds_ItemChanged(object sender, EventArgs<int, int> e) {
224      if (e.Value2 == 0 && Bounds[e.Value, 1] <= Bounds[e.Value, 0])
225        Bounds[e.Value, 1] = Bounds[e.Value, 0] + 0.1;
226      if (e.Value2 == 1 && Bounds[e.Value, 0] >= Bounds[e.Value, 1])
227        Bounds[e.Value, 0] = Bounds[e.Value, 1] - 0.1;
228    }
229    private void MoveGenerator_AdditiveMoveParameter_ActualNameChanged(object sender, EventArgs e) {
230      string name = ((ILookupParameter<AdditiveMove>)sender).ActualName;
231      foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
232        op.AdditiveMoveParameter.ActualName = name;
233      }
234    }
235    #endregion
236
237    #region Helpers
238    [StorableHook(HookType.AfterDeserialization)]
239    private void Initialize() {
240      InitializeOperators();
241      ProblemSizeParameter.ValueChanged += new EventHandler(ProblemSizeParameter_ValueChanged);
242      ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
243      BoundsParameter.ValueChanged += new EventHandler(BoundsParameter_ValueChanged);
244      Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
245      Bounds.ItemChanged += new EventHandler<EventArgs<int, int>>(Bounds_ItemChanged);
246      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
247      SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged);
248      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
249      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
250      VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged);
251    }
252    private void InitializeOperators() {
253      operators = new List<IOperator>();
254      if (ApplicationManager.Manager != null) operators.AddRange(ApplicationManager.Manager.GetInstances<IRealVectorOperator>().Cast<IOperator>());
255      UpdateMoveEvaluators();
256      ParameterizeOperators();
257      InitializeMoveGenerators();
258    }
259    private void InitializeMoveGenerators() {
260      foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
261        if (op is IMoveGenerator) {
262          op.AdditiveMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_AdditiveMoveParameter_ActualNameChanged);
263        }
264      }
265    }
266    private void UpdateMoveEvaluators() {
267      if (ApplicationManager.Manager != null) {
268        foreach (ISingleObjectiveTestFunctionMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionMoveEvaluator>().ToList())
269          operators.Remove(op);
270        foreach (ISingleObjectiveTestFunctionMoveEvaluator op in ApplicationManager.Manager.GetInstances<ISingleObjectiveTestFunctionMoveEvaluator>())
271          if (op.EvaluatorType == Evaluator.GetType()) {
272            operators.Add(op);
273          }
274        ParameterizeOperators();
275        OnOperatorsChanged();
276      }
277    }
278    private void ParameterizeSolutionCreator() {
279      SolutionCreator.LengthParameter.Value = new IntValue(ProblemSize.Value);
280    }
281    private void ParameterizeEvaluator() {
282      Evaluator.PointParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
283    }
284    private void ParameterizeVisualizer() {
285      if (Visualizer != null) {
286        Visualizer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
287        Visualizer.PointParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
288      }
289    }
290    private void ParameterizeOperators() {
291      foreach (IRealVectorCrossover op in Operators.OfType<IRealVectorCrossover>()) {
292        op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
293        op.ChildParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
294        op.BoundsParameter.ActualName = BoundsParameter.Name;
295      }
296      foreach (IRealVectorManipulator op in Operators.OfType<IRealVectorManipulator>()) {
297        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
298        op.BoundsParameter.ActualName = BoundsParameter.Name;
299      }
300      foreach (IRealVectorMoveOperator op in Operators.OfType<IRealVectorMoveOperator>()) {
301        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
302      }
303      foreach (IRealVectorMoveGenerator op in Operators.OfType<IRealVectorMoveGenerator>()) {
304        op.BoundsParameter.ActualName = BoundsParameter.Name;
305      }
306      foreach (ISingleObjectiveTestFunctionAdditiveMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionAdditiveMoveEvaluator>()) {
307        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
308        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
309      }
310    }
311    #endregion
312  }
313}
Note: See TracBrowser for help on using the repository browser.