Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ImprovingStringConvertibleMatrix/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionSolution.cs @ 9286

Last change on this file since 9286 was 9286, checked in by sforsten, 11 years ago

#2018:

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