Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionSolution.cs @ 4613

Last change on this file since 4613 was 3894, checked in by abeham, 15 years ago

#1029

  • Added Bounds to SingleObjectiveTestFunctionSolution
  • This would break persistence, so I added a CompatibilityMethod (which can be removed again once moving to 3.4)
File size: 10.4 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.Drawing;
24using HeuristicLab.Collections;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.RealVectorEncoding;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.TestFunctions {
32  /// <summary>
33  /// Represents a SingleObjectiveTestFunctionSolution solution.
34  /// </summary>
35  [Item("SingleObjectiveTestFunctionSolution", "Represents a SingleObjectiveTestFunction solution.")]
36  [StorableClass]
37  public class SingleObjectiveTestFunctionSolution : Item {
38    public override Image ItemImage {
39      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Image; }
40    }
41
42    [Storable]
43    private RealVector bestKnownRealVector;
44    public RealVector BestKnownRealVector {
45      get { return bestKnownRealVector; }
46      set {
47        if (bestKnownRealVector != value) {
48          if (bestKnownRealVector != null) DeregisterBestKnownRealVectorEvents();
49          bestKnownRealVector = value;
50          if (bestKnownRealVector != null) RegisterBestKnownRealVectorEvents();
51          OnBestKnownRealVectorChanged();
52        }
53      }
54    }
55
56    [Storable]
57    private RealVector bestRealVector;
58    public RealVector BestRealVector {
59      get { return bestRealVector; }
60      set {
61        if (bestRealVector != value) {
62          if (bestRealVector != null) DeregisterBestRealVectorEvents();
63          bestRealVector = value;
64          if (bestRealVector != null) RegisterBestRealVectorEvents();
65          OnBestRealVectorChanged();
66        }
67      }
68    }
69
70    [Storable]
71    private DoubleValue bestQuality;
72    public DoubleValue BestQuality {
73      get { return bestQuality; }
74      set {
75        if (bestQuality != value) {
76          if (bestQuality != null) DeregisterQualityEvents();
77          bestQuality = value;
78          if (bestQuality != null) RegisterQualityEvents();
79          OnQualityChanged();
80        }
81      }
82    }
83
84    [Storable]
85    private ItemArray<RealVector> population;
86    public ItemArray<RealVector> Population {
87      get { return population; }
88      set {
89        if (population != value) {
90          if (population != null) DeregisterPopulationEvents();
91          population = value;
92          if (population != null) RegisterPopulationEvents();
93          OnPopulationChanged();
94        }
95      }
96    }
97
98    [Storable]
99    private ISingleObjectiveTestFunctionProblemEvaluator evaluator;
100    public ISingleObjectiveTestFunctionProblemEvaluator Evaluator {
101      get { return evaluator; }
102      set {
103        if (evaluator != value) {
104          evaluator = value;
105          OnEvaluatorChanged();
106        }
107      }
108    }
109
110    [Storable]
111    private DoubleMatrix bounds;
112    public DoubleMatrix Bounds {
113      get { return bounds; }
114      set {
115        if (bounds != value) {
116          if (bounds != null) DeregisterBoundsEvents();
117          bounds = value;
118          if (bounds != null) RegisterBoundsEvents();
119          OnBoundsChanged();
120        }
121      }
122    }
123
124    public SingleObjectiveTestFunctionSolution() : base() { }
125    public SingleObjectiveTestFunctionSolution(RealVector realVector, DoubleValue quality, ISingleObjectiveTestFunctionProblemEvaluator evaluator)
126      : base() {
127      this.bestRealVector = realVector;
128      this.bestQuality = quality;
129      this.evaluator = evaluator;
130      Initialize();
131    }
132    [StorableConstructor]
133    private SingleObjectiveTestFunctionSolution(bool deserializing) : base(deserializing) { }
134
135    [StorableHook(HookType.AfterDeserialization)]
136    private void Initialize() {
137      if (bestKnownRealVector != null) RegisterBestKnownRealVectorEvents();
138      if (bestRealVector != null) RegisterBestRealVectorEvents();
139      if (bestQuality != null) RegisterQualityEvents();
140      if (population != null) RegisterPopulationEvents();
141      if (bounds != null) RegisterBoundsEvents();
142    }
143
144    public override IDeepCloneable Clone(Cloner cloner) {
145      SingleObjectiveTestFunctionSolution clone = new SingleObjectiveTestFunctionSolution();
146      cloner.RegisterClonedObject(this, clone);
147      clone.bestKnownRealVector = (RealVector)cloner.Clone(bestKnownRealVector);
148      clone.bestRealVector = (RealVector)cloner.Clone(bestRealVector);
149      clone.bestQuality = (DoubleValue)cloner.Clone(bestQuality);
150      clone.population = (ItemArray<RealVector>)cloner.Clone(population);
151      clone.evaluator = (ISingleObjectiveTestFunctionProblemEvaluator)cloner.Clone(evaluator);
152      clone.bounds = (DoubleMatrix)cloner.Clone(bounds);
153      clone.Initialize();
154      return clone;
155    }
156
157    #region Events
158    public event EventHandler BestKnownRealVectorChanged;
159    private void OnBestKnownRealVectorChanged() {
160      var changed = BestKnownRealVectorChanged;
161      if (changed != null)
162        changed(this, EventArgs.Empty);
163    }
164
165    public event EventHandler BestRealVectorChanged;
166    private void OnBestRealVectorChanged() {
167      var changed = BestRealVectorChanged;
168      if (changed != null)
169        changed(this, EventArgs.Empty);
170    }
171
172    public event EventHandler QualityChanged;
173    private void OnQualityChanged() {
174      var changed = QualityChanged;
175      if (changed != null)
176        changed(this, EventArgs.Empty);
177    }
178
179    public event EventHandler PopulationChanged;
180    private void OnPopulationChanged() {
181      var changed = PopulationChanged;
182      if (changed != null)
183        changed(this, EventArgs.Empty);
184    }
185
186    public event EventHandler EvaluatorChanged;
187    private void OnEvaluatorChanged() {
188      var changed = EvaluatorChanged;
189      if (changed != null)
190        changed(this, EventArgs.Empty);
191    }
192
193    public event EventHandler BoundsChanged;
194    private void OnBoundsChanged() {
195      var changed = BoundsChanged;
196      if (changed != null)
197        changed(this, EventArgs.Empty);
198    }
199
200    private void RegisterBestKnownRealVectorEvents() {
201      BestKnownRealVector.ItemChanged += new EventHandler<EventArgs<int>>(BestKnownRealVector_ItemChanged);
202      BestKnownRealVector.Reset += new EventHandler(BestKnownRealVector_Reset);
203    }
204    private void DeregisterBestKnownRealVectorEvents() {
205      BestKnownRealVector.ItemChanged -= new EventHandler<EventArgs<int>>(BestKnownRealVector_ItemChanged);
206      BestKnownRealVector.Reset -= new EventHandler(BestKnownRealVector_Reset);
207    }
208    private void RegisterBestRealVectorEvents() {
209      BestRealVector.ItemChanged += new EventHandler<EventArgs<int>>(BestRealVector_ItemChanged);
210      BestRealVector.Reset += new EventHandler(BestRealVector_Reset);
211    }
212    private void DeregisterBestRealVectorEvents() {
213      BestRealVector.ItemChanged -= new EventHandler<EventArgs<int>>(BestRealVector_ItemChanged);
214      BestRealVector.Reset -= new EventHandler(BestRealVector_Reset);
215    }
216    private void RegisterQualityEvents() {
217      BestQuality.ValueChanged += new EventHandler(Quality_ValueChanged);
218    }
219    private void DeregisterQualityEvents() {
220      BestQuality.ValueChanged -= new EventHandler(Quality_ValueChanged);
221    }
222    private void RegisterPopulationEvents() {
223      Population.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<RealVector>>(Population_CollectionReset);
224      Population.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<RealVector>>(Population_ItemsMoved);
225      Population.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<RealVector>>(Population_ItemsReplaced);
226    }
227    private void DeregisterPopulationEvents() {
228      Population.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<RealVector>>(Population_CollectionReset);
229      Population.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<RealVector>>(Population_ItemsMoved);
230      Population.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<RealVector>>(Population_ItemsReplaced);
231    }
232    private void RegisterBoundsEvents() {
233      Bounds.ItemChanged += new EventHandler<EventArgs<int, int>>(Bounds_ItemChanged);
234      Bounds.Reset += new EventHandler(Bounds_Reset);
235    }
236    private void DeregisterBoundsEvents() {
237      Bounds.ItemChanged -= new EventHandler<EventArgs<int, int>>(Bounds_ItemChanged);
238      Bounds.Reset -= new EventHandler(Bounds_Reset);
239    }
240
241    private void BestKnownRealVector_ItemChanged(object sender, EventArgs<int> e) {
242      OnBestKnownRealVectorChanged();
243    }
244    private void BestKnownRealVector_Reset(object sender, EventArgs e) {
245      OnBestKnownRealVectorChanged();
246    }
247    private void BestRealVector_ItemChanged(object sender, EventArgs<int> e) {
248      OnBestRealVectorChanged();
249    }
250    private void BestRealVector_Reset(object sender, EventArgs e) {
251      OnBestRealVectorChanged();
252    }
253    private void Quality_ValueChanged(object sender, EventArgs e) {
254      OnQualityChanged();
255    }
256    private void Population_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<RealVector>> e) {
257      OnPopulationChanged();
258    }
259    private void Population_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<RealVector>> e) {
260      OnPopulationChanged();
261    }
262    private void Population_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<RealVector>> e) {
263      OnPopulationChanged();
264    }
265    private void Bounds_ItemChanged(object sender, EventArgs<int, int> e) {
266      OnBoundsChanged();
267    }
268    private void Bounds_Reset(object sender, EventArgs e) {
269      OnBoundsChanged();
270    }
271    #endregion
272  }
273}
Note: See TracBrowser for help on using the repository browser.