Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/UserDefinedProblem.cs @ 4047

Last change on this file since 4047 was 3880, checked in by abeham, 15 years ago

#1023

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