Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Problems.Knapsack/3.3/KnapsackSolution.cs @ 6264

Last change on this file since 6264 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 8.3 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.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.BinaryVectorEncoding;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.Knapsack {
31  /// <summary>
32  /// Represents a knapsack solution which can be visualized in the GUI.
33  /// </summary>
34  [Item("KnapsackSolution", "Represents a Knapsack solution which can be visualized in the GUI.")]
35  [StorableClass]
36  public class KnapsackSolution : Item {
37    public override Image ItemImage {
38      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Image; }
39    }
40
41    [Storable]
42    private BinaryVector binaryVector;
43    public BinaryVector BinaryVector {
44      get { return binaryVector; }
45      set {
46        if (binaryVector != value) {
47          if (binaryVector != null) DeregisterBinaryVectorEvents();
48          binaryVector = value;
49          if (binaryVector != null) RegisterBinaryVectorEvents();
50          OnBinaryVectorChanged();
51        }
52      }
53    }
54
55    [Storable]
56    private IntValue capacity;
57    public IntValue Capacity {
58      get { return capacity; }
59      set {
60        if (capacity != value) {
61          if (capacity != null) DeregisterCapacityEvents();
62          capacity = value;
63          if (capacity != null) RegisterCapacityEvents();
64          OnCapacityChanged();
65        }
66      }
67    }
68
69    [Storable]
70    private IntArray weights;
71    public IntArray Weights {
72      get { return weights; }
73      set {
74        if (weights != value) {
75          if (weights != null) DeregisterWeightsEvents();
76          weights = value;
77          if (weights != null) RegisterWeightsEvents();
78          OnWeightsChanged();
79        }
80      }
81    }
82
83    [Storable]
84    private IntArray values;
85    public IntArray Values {
86      get { return values; }
87      set {
88        if (values != value) {
89          if (values != null) DeregisterValuesEvents();
90          values = value;
91          if (values != null) RegisterValuesEvents();
92          OnValuesChanged();
93        }
94      }
95    }
96
97    [Storable]
98    private DoubleValue quality;
99    public DoubleValue Quality {
100      get { return quality; }
101      set {
102        if (quality != value) {
103          if (quality != null) DeregisterQualityEvents();
104          quality = value;
105          if (quality != null) RegisterQualityEvents();
106          OnQualityChanged();
107        }
108      }
109    }
110
111    [StorableConstructor]
112    protected KnapsackSolution(bool deserializing) : base(deserializing) { }
113    protected KnapsackSolution(KnapsackSolution original, Cloner cloner)
114      : base(original, cloner) {
115      this.binaryVector = cloner.Clone(original.binaryVector);
116      this.quality = cloner.Clone(original.quality);
117      this.capacity = cloner.Clone(original.capacity);
118      this.weights = cloner.Clone(original.weights);
119      this.values = cloner.Clone(original.values);
120      Initialize();
121    }
122    public override IDeepCloneable Clone(Cloner cloner) {
123      return new KnapsackSolution(this, cloner);
124    }
125    public KnapsackSolution() : base() { }
126    public KnapsackSolution(BinaryVector binaryVector, DoubleValue quality, IntValue capacity, IntArray weights, IntArray values)
127      : base() {
128      this.binaryVector = binaryVector;
129      this.capacity = capacity;
130      this.weights = weights;
131      this.values = values;
132      this.quality = quality;
133      Initialize();
134    }
135
136    [StorableHook(HookType.AfterDeserialization)]
137    private void AfterDeserialization() {
138      Initialize();
139    }
140
141    private void Initialize() {
142      if (binaryVector != null) RegisterBinaryVectorEvents();
143      if (quality != null) RegisterQualityEvents();
144      if (capacity != null) RegisterCapacityEvents();
145      if (weights != null) RegisterWeightsEvents();
146      if (values != null) RegisterValuesEvents();
147    }
148
149    #region Events
150    public event EventHandler BinaryVectorChanged;
151    private void OnBinaryVectorChanged() {
152      var changed = BinaryVectorChanged;
153      if (changed != null)
154        changed(this, EventArgs.Empty);
155    }
156
157    public event EventHandler CapacityChanged;
158    private void OnCapacityChanged() {
159      var changed = CapacityChanged;
160      if (changed != null)
161        changed(this, EventArgs.Empty);
162    }
163
164    public event EventHandler WeightsChanged;
165    private void OnWeightsChanged() {
166      var changed = WeightsChanged;
167      if (changed != null)
168        changed(this, EventArgs.Empty);
169    }
170
171    public event EventHandler ValuesChanged;
172    private void OnValuesChanged() {
173      var changed = ValuesChanged;
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    private void RegisterBinaryVectorEvents() {
186      BinaryVector.ItemChanged += new EventHandler<EventArgs<int>>(BinaryVector_ItemChanged);
187      BinaryVector.Reset += new EventHandler(BinaryVector_Reset);
188    }
189
190    private void DeregisterBinaryVectorEvents() {
191      BinaryVector.ItemChanged -= new EventHandler<EventArgs<int>>(BinaryVector_ItemChanged);
192      BinaryVector.Reset -= new EventHandler(BinaryVector_Reset);
193    }
194
195    private void RegisterCapacityEvents() {
196      Capacity.ValueChanged += new EventHandler(Capacity_ValueChanged);
197    }
198
199    private void DeregisterCapacityEvents() {
200      Capacity.ValueChanged -= new EventHandler(Capacity_ValueChanged);
201    }
202
203    private void RegisterWeightsEvents() {
204      Weights.ItemChanged += new EventHandler<EventArgs<int>>(Weights_ItemChanged);
205      Weights.Reset += new EventHandler(Weights_Reset);
206    }
207
208    private void DeregisterWeightsEvents() {
209      Weights.ItemChanged -= new EventHandler<EventArgs<int>>(Weights_ItemChanged);
210      Weights.Reset -= new EventHandler(Weights_Reset);
211    }
212
213    private void RegisterValuesEvents() {
214      Values.ItemChanged += new EventHandler<EventArgs<int>>(Values_ItemChanged);
215      Values.Reset += new EventHandler(Values_Reset);
216    }
217
218    private void DeregisterValuesEvents() {
219      Values.ItemChanged -= new EventHandler<EventArgs<int>>(Values_ItemChanged);
220      Values.Reset -= new EventHandler(Values_Reset);
221    }
222
223    private void RegisterQualityEvents() {
224      Quality.ValueChanged += new EventHandler(Quality_ValueChanged);
225    }
226    private void DeregisterQualityEvents() {
227      Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);
228    }
229
230    private void BinaryVector_ItemChanged(object sender, EventArgs<int> e) {
231      OnBinaryVectorChanged();
232    }
233    private void BinaryVector_Reset(object sender, EventArgs e) {
234      OnBinaryVectorChanged();
235    }
236    void Capacity_ValueChanged(object sender, EventArgs e) {
237      OnCapacityChanged();
238    }
239    private void Weights_ItemChanged(object sender, EventArgs<int> e) {
240      OnWeightsChanged();
241    }
242    private void Weights_Reset(object sender, EventArgs e) {
243      OnWeightsChanged();
244    }
245    private void Values_ItemChanged(object sender, EventArgs<int> e) {
246      OnValuesChanged();
247    }
248    private void Values_Reset(object sender, EventArgs e) {
249      OnValuesChanged();
250    }
251    private void Quality_ValueChanged(object sender, EventArgs e) {
252      OnQualityChanged();
253    }
254    #endregion
255  }
256}
Note: See TracBrowser for help on using the repository browser.