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