Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7049 was 5954, checked in by mkommend, 14 years ago

#1462: Added algorithms and problems folder to HeuristicLab.Optimization.

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