Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Optimization/3.3/Problems/UserDefinedProblem.cs @ 15401

Last change on this file since 15401 was 13656, checked in by ascheibe, 9 years ago

#2582 created branch for Hive Web Job Manager

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