Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Knapsack/3.3/KnapsackSolution.cs @ 4068

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

Sorted usings and removed unused usings in entire solution (#1094)

File size: 8.2 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    public KnapsackSolution() : base() { }
112    public KnapsackSolution(BinaryVector binaryVector, DoubleValue quality, IntValue capacity, IntArray weights, IntArray values)
113      : base() {
114      this.binaryVector = binaryVector;
115      this.capacity = capacity;
116      this.weights = weights;
117      this.values = values;
118      this.quality = quality;
119      Initialize();
120    }
121    [StorableConstructor]
122    private KnapsackSolution(bool deserializing) : base(deserializing) { }
123
124    [StorableHook(HookType.AfterDeserialization)]
125    private void Initialize() {
126      if (binaryVector != null) RegisterBinaryVectorEvents();
127      if (quality != null) RegisterQualityEvents();
128      if (capacity != null) RegisterCapacityEvents();
129      if (weights != null) RegisterWeightsEvents();
130      if (values != null) RegisterValuesEvents();
131    }
132
133    public override IDeepCloneable Clone(Cloner cloner) {
134      KnapsackSolution clone = new KnapsackSolution();
135      cloner.RegisterClonedObject(this, clone);
136      clone.binaryVector = (BinaryVector)cloner.Clone(binaryVector);
137      clone.quality = (DoubleValue)cloner.Clone(quality);
138      clone.capacity = (IntValue)cloner.Clone(capacity);
139      clone.weights = (IntArray)cloner.Clone(weights);
140      clone.values = (IntArray)cloner.Clone(values);
141      clone.Initialize();
142      return clone;
143    }
144
145    #region Events
146    public event EventHandler BinaryVectorChanged;
147    private void OnBinaryVectorChanged() {
148      var changed = BinaryVectorChanged;
149      if (changed != null)
150        changed(this, EventArgs.Empty);
151    }
152
153    public event EventHandler CapacityChanged;
154    private void OnCapacityChanged() {
155      var changed = CapacityChanged;
156      if (changed != null)
157        changed(this, EventArgs.Empty);
158    }
159
160    public event EventHandler WeightsChanged;
161    private void OnWeightsChanged() {
162      var changed = WeightsChanged;
163      if (changed != null)
164        changed(this, EventArgs.Empty);
165    }
166
167    public event EventHandler ValuesChanged;
168    private void OnValuesChanged() {
169      var changed = ValuesChanged;
170      if (changed != null)
171        changed(this, EventArgs.Empty);
172    }
173
174    public event EventHandler QualityChanged;
175    private void OnQualityChanged() {
176      var changed = QualityChanged;
177      if (changed != null)
178        changed(this, EventArgs.Empty);
179    }
180
181    private void RegisterBinaryVectorEvents() {
182      BinaryVector.ItemChanged += new EventHandler<EventArgs<int>>(BinaryVector_ItemChanged);
183      BinaryVector.Reset += new EventHandler(BinaryVector_Reset);
184    }
185
186    private void DeregisterBinaryVectorEvents() {
187      BinaryVector.ItemChanged -= new EventHandler<EventArgs<int>>(BinaryVector_ItemChanged);
188      BinaryVector.Reset -= new EventHandler(BinaryVector_Reset);
189    }
190
191    private void RegisterCapacityEvents() {
192      Capacity.ValueChanged += new EventHandler(Capacity_ValueChanged);
193    }
194
195    private void DeregisterCapacityEvents() {
196      Capacity.ValueChanged -= new EventHandler(Capacity_ValueChanged);
197    }
198
199    private void RegisterWeightsEvents() {
200      Weights.ItemChanged += new EventHandler<EventArgs<int>>(Weights_ItemChanged);
201      Weights.Reset += new EventHandler(Weights_Reset);
202    }
203
204    private void DeregisterWeightsEvents() {
205      Weights.ItemChanged -= new EventHandler<EventArgs<int>>(Weights_ItemChanged);
206      Weights.Reset -= new EventHandler(Weights_Reset);
207    }
208
209    private void RegisterValuesEvents() {
210      Values.ItemChanged += new EventHandler<EventArgs<int>>(Values_ItemChanged);
211      Values.Reset += new EventHandler(Values_Reset);
212    }
213
214    private void DeregisterValuesEvents() {
215      Values.ItemChanged -= new EventHandler<EventArgs<int>>(Values_ItemChanged);
216      Values.Reset -= new EventHandler(Values_Reset);
217    }
218
219    private void RegisterQualityEvents() {
220      Quality.ValueChanged += new EventHandler(Quality_ValueChanged);
221    }
222    private void DeregisterQualityEvents() {
223      Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);
224    }
225
226    private void BinaryVector_ItemChanged(object sender, EventArgs<int> e) {
227      OnBinaryVectorChanged();
228    }
229    private void BinaryVector_Reset(object sender, EventArgs e) {
230      OnBinaryVectorChanged();
231    }
232    void Capacity_ValueChanged(object sender, EventArgs e) {
233      OnCapacityChanged();
234    }
235    private void Weights_ItemChanged(object sender, EventArgs<int> e) {
236      OnWeightsChanged();
237    }
238    private void Weights_Reset(object sender, EventArgs e) {
239      OnWeightsChanged();
240    }
241    private void Values_ItemChanged(object sender, EventArgs<int> e) {
242      OnValuesChanged();
243    }
244    private void Values_Reset(object sender, EventArgs e) {
245      OnValuesChanged();
246    }
247    private void Quality_ValueChanged(object sender, EventArgs e) {
248      OnQualityChanged();
249    }
250    #endregion
251  }
252}
Note: See TracBrowser for help on using the repository browser.