Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Optimization/3.3/UserDefinedProblem.cs @ 5247

Last change on this file since 5247 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

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.Drawing;
25using System.Linq;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Optimization {
34  /// <summary>
35  /// A problem which can be defined by the user.
36  /// </summary>
37  [Item("User-Defined Problem", "A problem which can be defined by the user.")]
38  [Creatable("Problems")]
39  [StorableClass]
40  public sealed class UserDefinedProblem : ParameterizedNamedItem, ISingleObjectiveProblem, IStorableContent {
41    public string Filename { get; set; }
42
43    public override Image ItemImage {
44      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
45    }
46    public new ParameterCollection Parameters {
47      get { return base.Parameters; }
48    }
49    IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
50      get { return Parameters; }
51    }
52
53    #region Parameters
54    public IValueParameter<ISingleObjectiveEvaluator> EvaluatorParameter {
55      get { return (IValueParameter<ISingleObjectiveEvaluator>)Parameters["Evaluator"]; }
56    }
57    public ValueParameter<BoolValue> MaximizationParameter {
58      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
59    }
60    IParameter ISingleObjectiveProblem.MaximizationParameter {
61      get { return MaximizationParameter; }
62    }
63    public ValueParameter<ISolutionCreator> SolutionCreatorParameter {
64      get { return (ValueParameter<ISolutionCreator>)Parameters["SolutionCreator"]; }
65    }
66    IParameter IProblem.SolutionCreatorParameter {
67      get { return SolutionCreatorParameter; }
68    }
69    IParameter IProblem.EvaluatorParameter {
70      get { return EvaluatorParameter; }
71    }
72    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
73      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
74    }
75    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
76      get { return BestKnownQualityParameter; }
77    }
78    public OptionalValueParameter<IScope> BestKnownSolutionParameter {
79      get { return (OptionalValueParameter<IScope>)Parameters["BestKnownSolution"]; }
80    }
81    public ValueParameter<ItemList<IOperator>> OperatorsParameter {
82      get { return (ValueParameter<ItemList<IOperator>>)Parameters["Operators"]; }
83    }
84    #endregion
85
86    #region Properties
87    public BoolValue Maximization {
88      get { return MaximizationParameter.Value; }
89      set { MaximizationParameter.Value = value; }
90    }
91    public ISolutionCreator SolutionCreator {
92      get { return SolutionCreatorParameter.Value; }
93      set { SolutionCreatorParameter.Value = value; }
94    }
95    ISolutionCreator IProblem.SolutionCreator {
96      get { return SolutionCreatorParameter.Value; }
97    }
98    public ISingleObjectiveEvaluator Evaluator {
99      get { return EvaluatorParameter.Value; }
100      set { EvaluatorParameter.Value = value; }
101    }
102    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
103      get { return EvaluatorParameter.Value; }
104    }
105    IEvaluator IProblem.Evaluator {
106      get { return EvaluatorParameter.Value; }
107    }
108    public DoubleValue BestKnownQuality {
109      get { return BestKnownQualityParameter.Value; }
110      set { BestKnownQualityParameter.Value = value; }
111    }
112    public IEnumerable<IOperator> Operators {
113      get { return OperatorsParameter.Value; }
114    }
115    #endregion
116
117    [StorableConstructor]
118    private UserDefinedProblem(bool deserializing) : base(deserializing) { }
119    [StorableHook(HookType.AfterDeserialization)]
120    private void AfterDeserialization() {
121      AttachEventHandlers();
122    }
123    public UserDefinedProblem()
124      : base() {
125      Parameters.Add(new ValueParameter<ISingleObjectiveEvaluator>("Evaluator", "The evaluator that collects the values to exchange.", new EmptyUserDefinedProblemEvaluator()));
126      Parameters.Add(new ValueParameter<ISolutionCreator>("SolutionCreator", "An operator to create the solution components."));
127      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as most test functions are minimization problems.", new BoolValue(false)));
128      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
129      Parameters.Add(new OptionalValueParameter<IScope>("BestKnownSolution", "The best known solution for this external evaluation problem."));
130      Parameters.Add(new ValueParameter<ItemList<IOperator>>("Operators", "The operators that are passed to the algorithm.", new ItemList<IOperator>()));
131
132      AttachEventHandlers();
133    }
134
135    private UserDefinedProblem(UserDefinedProblem original, Cloner cloner)
136      : base(original, cloner) {
137      AttachEventHandlers();
138    }
139    public override IDeepCloneable Clone(Cloner cloner) {
140      return new UserDefinedProblem(this, cloner);
141    }
142
143    #region Events
144    public event EventHandler SolutionCreatorChanged;
145    private void OnSolutionCreatorChanged() {
146      EventHandler handler = SolutionCreatorChanged;
147      if (handler != null) handler(this, EventArgs.Empty);
148    }
149    public event EventHandler EvaluatorChanged;
150    private void OnEvaluatorChanged() {
151      EventHandler handler = EvaluatorChanged;
152      if (handler != null) handler(this, EventArgs.Empty);
153    }
154    public event EventHandler OperatorsChanged;
155    private void OnOperatorsChanged() {
156      EventHandler handler = OperatorsChanged;
157      if (handler != null) handler(this, EventArgs.Empty);
158    }
159    public event EventHandler Reset;
160    private void OnReset() {
161      EventHandler handler = Reset;
162      if (handler != null) handler(this, EventArgs.Empty);
163    }
164    #endregion
165
166    #region Event handlers
167    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
168      OnSolutionCreatorChanged();
169    }
170    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
171      if (Evaluator != null)
172        Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
173      ParameterizeOperators();
174      OnEvaluatorChanged();
175    }
176    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
177      ParameterizeOperators();
178    }
179    private void OperatorsParameter_ValueChanged(object sender, EventArgs e) {
180      OnOperatorsChanged();
181    }
182    private void OperatorsParameter_Value_ItemsAdded(object sender, EventArgs e) {
183      OnOperatorsChanged();
184    }
185    private void OperatorsParameter_Value_ItemsRemoved(object sender, EventArgs e) {
186      OnOperatorsChanged();
187    }
188    private void OperatorsParameter_Value_CollectionReset(object sender, EventArgs e) {
189      OnOperatorsChanged();
190    }
191    #endregion
192
193    #region Helpers
194    private void AttachEventHandlers() {
195      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
196      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
197      if (Evaluator != null)
198        Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
199      OperatorsParameter.ValueChanged += new EventHandler(OperatorsParameter_ValueChanged);
200      OperatorsParameter.Value.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_ItemsAdded);
201      OperatorsParameter.Value.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_ItemsRemoved);
202      OperatorsParameter.Value.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_CollectionReset);
203    }
204
205    private void ParameterizeOperators() {
206      // A best effort approach to wiring
207      if (Evaluator != null) {
208        string qualityName = Evaluator.QualityParameter.ActualName;
209        foreach (IOperator op in OperatorsParameter.Value) {
210          foreach (ILookupParameter<DoubleValue> param in op.Parameters.OfType<ILookupParameter<DoubleValue>>()) {
211            if (param.Name.Equals("Quality")) param.ActualName = qualityName;
212          }
213          foreach (IScopeTreeLookupParameter<DoubleValue> param in op.Parameters.OfType<IScopeTreeLookupParameter<DoubleValue>>()) {
214            if (param.Name.Equals("Quality")) param.ActualName = qualityName;
215          }
216        }
217      }
218    }
219    #endregion
220
221    [Item("EmptyUserDefinedProblemEvaluator", "A dummy evaluator that will throw an exception when executed.")]
222    [StorableClass]
223    private sealed class EmptyUserDefinedProblemEvaluator : ParameterizedNamedItem, ISingleObjectiveEvaluator {
224
225      [StorableConstructor]
226      private EmptyUserDefinedProblemEvaluator(bool deserializing) : base(deserializing) { }
227      private EmptyUserDefinedProblemEvaluator(EmptyUserDefinedProblemEvaluator original, Cloner cloner)
228        : base(original, cloner) {
229      }
230      public override IDeepCloneable Clone(Cloner cloner) {
231        return new EmptyUserDefinedProblemEvaluator(this, cloner);
232      }
233
234      #region ISingleObjectiveEvaluator Members
235
236      public ILookupParameter<DoubleValue> QualityParameter {
237        get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
238      }
239
240      #endregion
241
242      public EmptyUserDefinedProblemEvaluator() {
243        Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The solution quality."));
244      }
245
246      #region IOperator Members
247
248      public bool Breakpoint { get; set; }
249
250      public IOperation Execute(IExecutionContext context) {
251        throw new InvalidOperationException("Please choose an appropriate evaluation operator.");
252      }
253
254      public void Abort() {
255        throw new InvalidOperationException("Please choose an appropriate evaluation operator.");
256      }
257
258#pragma warning disable 67
259      public event EventHandler BreakpointChanged;
260
261      public event EventHandler Executed;
262#pragma warning restore 67
263
264      #endregion
265
266      public override Image ItemImage {
267        get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Method; }
268      }
269    }
270  }
271}
Note: See TracBrowser for help on using the repository browser.