Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11400 was 11400, checked in by abeham, 10 years ago

#2174: Added multi-objective programmable problem

File size: 8.6 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    dynamic IProblemDefinition.vars { get; set; }
116
117    void IProblemDefinition.Initialize() {
118
119    }
120
121    Configuration IProblemDefinition.GetConfiguration() {
122      return Configuration();
123    }
124
125    bool[] IMultiObjectiveProblemDefinition.Maximization {
126      get { return Maximization; }
127    }
128
129    double[] IMultiObjectiveProblemDefinition.Evaluate(IRandom random, ParameterVector vector) {
130      return Evaluate(random, vector);
131    }
132
133    void IMultiObjectiveProblemDefinition.Analyze(ParameterVector[] vectors, double[][] qualities, ResultCollection results) {
134      Analyze(vectors, qualities, results);
135    }
136
137    IEnumerable<ParameterVector> IMultiObjectiveProblemDefinition.GetNeighbors(IRandom random, ParameterVector vector) {
138      return Neighbors(random, vector);
139    }
140    #endregion
141
142    #region INamedItem
143    protected string name;
144    /// <inheritdoc/>
145    /// <remarks>Calls <see cref="OnNameChanging"/> and also <see cref="OnNameChanged"/>
146    /// eventually in the setter.</remarks>
147    public string Name {
148      get { return name; }
149      set {
150        if (!CanChangeName) throw new NotSupportedException("Name cannot be changed.");
151        if (!(name.Equals(value) || (value == null) && (name == string.Empty))) {
152          CancelEventArgs<string> e = value == null ? new CancelEventArgs<string>(string.Empty) : new CancelEventArgs<string>(value);
153          OnNameChanging(e);
154          if (!e.Cancel) {
155            name = value == null ? string.Empty : value;
156            OnNameChanged();
157          }
158        }
159      }
160    }
161    public virtual bool CanChangeName {
162      get { return true; }
163    }
164    protected string description;
165    public string Description {
166      get { return description; }
167      set {
168        if (!CanChangeDescription) throw new NotSupportedException("Description cannot be changed.");
169        if (!(description.Equals(value) || (value == null) && (description == string.Empty))) {
170          description = value == null ? string.Empty : value;
171          OnDescriptionChanged();
172        }
173      }
174    }
175    public virtual bool CanChangeDescription {
176      get { return true; }
177    }
178
179    /// <summary>
180    /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
181    /// </summary>
182    /// <returns>The current instance as a string.</returns>
183    public override string ToString() {
184      return Name;
185    }
186
187    /// <inheritdoc/>
188    public event EventHandler<CancelEventArgs<string>> NameChanging;
189    /// <summary>
190    /// Fires a new <c>NameChanging</c> event.
191    /// </summary>
192    /// <param name="e">The event arguments of the changing.</param>
193    protected virtual void OnNameChanging(CancelEventArgs<string> e) {
194      var handler = NameChanging;
195      if (handler != null) handler(this, e);
196    }
197    /// <inheritdoc/>
198    public event EventHandler NameChanged;
199    /// <summary>
200    /// Fires a new <c>NameChanged</c> event.
201    /// </summary>
202    /// <remarks>Calls <see cref="ItemBase.OnChanged"/>.</remarks>
203    protected virtual void OnNameChanged() {
204      var handler = NameChanged;
205      if (handler != null) handler(this, EventArgs.Empty);
206      OnToStringChanged();
207    }
208    /// <inheritdoc/>
209    public event EventHandler DescriptionChanged;
210    /// <summary>
211    /// Fires a new <c>DescriptionChanged</c> event.
212    /// </summary>
213    /// <remarks>Calls <see cref="ItemBase.OnChanged"/>.</remarks>
214    protected virtual void OnDescriptionChanged() {
215      var handler = DescriptionChanged;
216      if (handler != null) handler(this, EventArgs.Empty);
217    }
218    #endregion
219    #region IItem
220    public virtual string ItemName {
221      get { return ItemAttribute.GetName(this.GetType()); }
222    }
223    public virtual string ItemDescription {
224      get { return ItemAttribute.GetDescription(this.GetType()); }
225    }
226    public Version ItemVersion {
227      get { return ItemAttribute.GetVersion(this.GetType()); }
228    }
229    public static Image StaticItemImage {
230      get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
231    }
232    public virtual Image ItemImage {
233      get { return ItemAttribute.GetImage(this.GetType()); }
234    }
235    public object Clone() {
236      return Clone(new Cloner());
237    }
238
239    public event EventHandler ItemImageChanged;
240    protected virtual void OnItemImageChanged() {
241      var handler = ItemImageChanged;
242      if (handler != null) handler(this, EventArgs.Empty);
243    }
244    public event EventHandler ToStringChanged;
245    protected virtual void OnToStringChanged() {
246      var handler = ToStringChanged;
247      if (handler != null) handler(this, EventArgs.Empty);
248    }
249    #endregion
250  }
251}
Note: See TracBrowser for help on using the repository browser.