Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProblemDefinition.cs @ 11402

Last change on this file since 11402 was 11402, checked in by abeham, 8 years ago

#2174: cleaned up API a little

File size: 8.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Optimization;
28
29namespace HeuristicLab.Problems.Programmable {
30  [Item("Multi-objective Problem Definition", "This definition can be dynamically created through code and assigned to the problem.")]
31  public class MultiObjectiveProblemDefinition : IMultiObjectiveProblemDefinitionHost, IMultiObjectiveProblemDefinition {
32    private bool[] maximization;
33    public bool[] Maximization {
34      get { return maximization; }
35      set {
36        maximization = value;
37        OnInstanceChanged();
38      }
39    }
40
41    private Func<Configuration> configuration;
42    public Func<Configuration> Configuration {
43      get { return configuration; }
44      set {
45        configuration = value;
46        OnInstanceChanged();
47      }
48    }
49
50    private Func<IRandom, ParameterVector, double[]> evaluate;
51    public Func<IRandom, ParameterVector, double[]> Evaluate {
52      get { return evaluate; }
53      set {
54        evaluate = value;
55        OnInstanceChanged();
56      }
57    }
58
59    private Action<ParameterVector[], double[][], ResultCollection> analyze;
60    public Action<ParameterVector[], double[][], ResultCollection> Analyze {
61      get { return analyze; }
62      set {
63        analyze = value;
64        OnInstanceChanged();
65      }
66    }
67
68    private Func<IRandom, ParameterVector, IEnumerable<ParameterVector>> neighbors;
69    public Func<IRandom, ParameterVector, IEnumerable<ParameterVector>> Neighbors {
70      get { return neighbors; }
71      set {
72        neighbors = value;
73        OnInstanceChanged();
74      }
75    }
76
77    protected MultiObjectiveProblemDefinition(MultiObjectiveProblemDefinition original, Cloner cloner) {
78      cloner.RegisterClonedObject(original, this);
79      name = original.name;
80      description = original.description;
81      maximization = original.maximization;
82      configuration = original.configuration;
83      evaluate = original.evaluate;
84      analyze = original.analyze;
85      neighbors = original.neighbors;
86    }
87
88    public MultiObjectiveProblemDefinition() : this((string)null, null) { }
89    public MultiObjectiveProblemDefinition(string name) : this(name, null) { }
90    public MultiObjectiveProblemDefinition(string name, string description) {
91      this.name = name ?? ItemName;
92      this.description = description ?? ItemDescription;
93      configuration = () => new Configuration();
94      evaluate = (random, vector) => new[] { 0.0, 0.0 };
95      analyze = (vectors, doubles, results) => { };
96      neighbors = (random, vector) => new[] { (ParameterVector)vector.Clone() };
97    }
98
99
100    public IDeepCloneable Clone(Cloner cloner) {
101      return new MultiObjectiveProblemDefinition(this, cloner);
102    }
103
104    public IMultiObjectiveProblemDefinition Instance {
105      get { return this; }
106    }
107
108    public event EventHandler InstanceChanged;
109    protected void OnInstanceChanged() {
110      var handler = InstanceChanged;
111      if (handler != null) handler(this, EventArgs.Empty);
112    }
113
114    #region Problem definition explicit interface implementations
115    Configuration IProblemDefinition.GetConfiguration() {
116      return Configuration();
117    }
118
119    bool[] IMultiObjectiveProblemDefinition.Maximization {
120      get { return Maximization; }
121    }
122
123    double[] IMultiObjectiveProblemDefinition.Evaluate(IRandom random, ParameterVector vector) {
124      return Evaluate(random, vector);
125    }
126
127    void IMultiObjectiveProblemDefinition.Analyze(ParameterVector[] vectors, double[][] qualities, ResultCollection results) {
128      Analyze(vectors, qualities, results);
129    }
130
131    IEnumerable<ParameterVector> IMultiObjectiveProblemDefinition.GetNeighbors(IRandom random, ParameterVector vector) {
132      return Neighbors(random, vector);
133    }
134    #endregion
135
136    #region INamedItem
137    protected string name;
138    /// <inheritdoc/>
139    /// <remarks>Calls <see cref="OnNameChanging"/> and also <see cref="OnNameChanged"/>
140    /// eventually in the setter.</remarks>
141    public string Name {
142      get { return name; }
143      set {
144        if (!CanChangeName) throw new NotSupportedException("Name cannot be changed.");
145        if (!(name.Equals(value) || (value == null) && (name == string.Empty))) {
146          CancelEventArgs<string> e = value == null ? new CancelEventArgs<string>(string.Empty) : new CancelEventArgs<string>(value);
147          OnNameChanging(e);
148          if (!e.Cancel) {
149            name = value == null ? string.Empty : value;
150            OnNameChanged();
151          }
152        }
153      }
154    }
155    public virtual bool CanChangeName {
156      get { return true; }
157    }
158    protected string description;
159    public string Description {
160      get { return description; }
161      set {
162        if (!CanChangeDescription) throw new NotSupportedException("Description cannot be changed.");
163        if (!(description.Equals(value) || (value == null) && (description == string.Empty))) {
164          description = value == null ? string.Empty : value;
165          OnDescriptionChanged();
166        }
167      }
168    }
169    public virtual bool CanChangeDescription {
170      get { return true; }
171    }
172
173    /// <summary>
174    /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
175    /// </summary>
176    /// <returns>The current instance as a string.</returns>
177    public override string ToString() {
178      return Name;
179    }
180
181    /// <inheritdoc/>
182    public event EventHandler<CancelEventArgs<string>> NameChanging;
183    /// <summary>
184    /// Fires a new <c>NameChanging</c> event.
185    /// </summary>
186    /// <param name="e">The event arguments of the changing.</param>
187    protected virtual void OnNameChanging(CancelEventArgs<string> e) {
188      var handler = NameChanging;
189      if (handler != null) handler(this, e);
190    }
191    /// <inheritdoc/>
192    public event EventHandler NameChanged;
193    /// <summary>
194    /// Fires a new <c>NameChanged</c> event.
195    /// </summary>
196    /// <remarks>Calls <see cref="ItemBase.OnChanged"/>.</remarks>
197    protected virtual void OnNameChanged() {
198      var handler = NameChanged;
199      if (handler != null) handler(this, EventArgs.Empty);
200      OnToStringChanged();
201    }
202    /// <inheritdoc/>
203    public event EventHandler DescriptionChanged;
204    /// <summary>
205    /// Fires a new <c>DescriptionChanged</c> event.
206    /// </summary>
207    /// <remarks>Calls <see cref="ItemBase.OnChanged"/>.</remarks>
208    protected virtual void OnDescriptionChanged() {
209      var handler = DescriptionChanged;
210      if (handler != null) handler(this, EventArgs.Empty);
211    }
212    #endregion
213    #region IItem
214    public virtual string ItemName {
215      get { return ItemAttribute.GetName(this.GetType()); }
216    }
217    public virtual string ItemDescription {
218      get { return ItemAttribute.GetDescription(this.GetType()); }
219    }
220    public Version ItemVersion {
221      get { return ItemAttribute.GetVersion(this.GetType()); }
222    }
223    public static Image StaticItemImage {
224      get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
225    }
226    public virtual Image ItemImage {
227      get { return ItemAttribute.GetImage(this.GetType()); }
228    }
229    public object Clone() {
230      return Clone(new Cloner());
231    }
232
233    public event EventHandler ItemImageChanged;
234    protected virtual void OnItemImageChanged() {
235      var handler = ItemImageChanged;
236      if (handler != null) handler(this, EventArgs.Empty);
237    }
238    public event EventHandler ToStringChanged;
239    protected virtual void OnToStringChanged() {
240      var handler = ToStringChanged;
241      if (handler != null) handler(this, EventArgs.Empty);
242    }
243    #endregion
244  }
245}
Note: See TracBrowser for help on using the repository browser.