Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3627 was 3467, checked in by svonolfe, 14 years ago

Added simple view for the KnapsackProblem (#917)

File size: 7.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.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using System.Drawing;
29using HeuristicLab.Data;
30using HeuristicLab.Encodings.BinaryVectorEncoding;
31using HeuristicLab.Common;
32
33namespace 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    public KnapsackSolution() : base() { }
101    public KnapsackSolution(BinaryVector binaryVector, IntValue capacity, IntArray weights, IntArray values)
102      : base() {
103      this.binaryVector = binaryVector;
104      this.capacity = capacity;
105      this.weights = weights;
106      this.values = values;
107      Initialize();
108    }
109    [StorableConstructor]
110    private KnapsackSolution(bool deserializing) : base(deserializing) { }
111
112    [StorableHook(HookType.AfterDeserialization)]
113    private void Initialize() {
114      if (binaryVector != null) RegisterBinaryVectorEvents();
115      if (capacity != null) RegisterCapacityEvents();
116      if (weights != null) RegisterWeightsEvents();
117      if (values != null) RegisterValuesEvents();
118    }
119
120    public override IDeepCloneable Clone(Cloner cloner) {
121      KnapsackSolution clone = new KnapsackSolution();
122      cloner.RegisterClonedObject(this, clone);
123      clone.binaryVector = (BinaryVector)cloner.Clone(binaryVector);
124      clone.capacity = (IntValue)cloner.Clone(capacity);
125      clone.weights = (IntArray)cloner.Clone(weights);
126      clone.values = (IntArray)cloner.Clone(values);
127      clone.Initialize();
128      return clone;
129    }
130
131    #region Events
132    public event EventHandler BinaryVectorChanged;
133    private void OnBinaryVectorChanged() {
134      var changed = BinaryVectorChanged;
135      if (changed != null)
136        changed(this, EventArgs.Empty);
137    }
138
139    public event EventHandler CapacityChanged;
140    private void OnCapacityChanged() {
141      var changed = CapacityChanged;
142      if (changed != null)
143        changed(this, EventArgs.Empty);
144    }
145
146    public event EventHandler WeightsChanged;
147    private void OnWeightsChanged() {
148      var changed = WeightsChanged;
149      if (changed != null)
150        changed(this, EventArgs.Empty);
151    }
152
153    public event EventHandler ValuesChanged;
154    private void OnValuesChanged() {
155      var changed = ValuesChanged;
156      if (changed != null)
157        changed(this, EventArgs.Empty);
158    }
159
160    private void RegisterBinaryVectorEvents() {
161      BinaryVector.ItemChanged += new EventHandler<EventArgs<int>>(BinaryVector_ItemChanged);
162      BinaryVector.Reset += new EventHandler(BinaryVector_Reset);
163    }
164
165    private void DeregisterBinaryVectorEvents() {
166      BinaryVector.ItemChanged -= new EventHandler<EventArgs<int>>(BinaryVector_ItemChanged);
167      BinaryVector.Reset -= new EventHandler(BinaryVector_Reset);
168    }
169
170    private void RegisterCapacityEvents() {
171      Capacity.ValueChanged += new EventHandler(Capacity_ValueChanged);
172    }
173
174    private void DeregisterCapacityEvents() {
175      Capacity.ValueChanged -= new EventHandler(Capacity_ValueChanged);
176    }
177
178    private void RegisterWeightsEvents() {
179      Weights.ItemChanged += new EventHandler<EventArgs<int>>(Weights_ItemChanged);
180      Weights.Reset += new EventHandler(Weights_Reset);
181    }
182
183    private void DeregisterWeightsEvents() {
184      Weights.ItemChanged -= new EventHandler<EventArgs<int>>(Weights_ItemChanged);
185      Weights.Reset -= new EventHandler(Weights_Reset);
186    }
187
188    private void RegisterValuesEvents() {
189      Values.ItemChanged += new EventHandler<EventArgs<int>>(Values_ItemChanged);
190      Values.Reset += new EventHandler(Values_Reset);
191    }
192
193    private void DeregisterValuesEvents() {
194      Values.ItemChanged -= new EventHandler<EventArgs<int>>(Values_ItemChanged);
195      Values.Reset -= new EventHandler(Values_Reset);
196    }
197
198    private void BinaryVector_ItemChanged(object sender, EventArgs<int> e) {
199      OnBinaryVectorChanged();
200    }
201    private void BinaryVector_Reset(object sender, EventArgs e) {
202      OnBinaryVectorChanged();
203    }
204    void Capacity_ValueChanged(object sender, EventArgs e) {
205      OnCapacityChanged();
206    }
207    private void Weights_ItemChanged(object sender, EventArgs<int> e) {
208      OnWeightsChanged();
209    }
210    private void Weights_Reset(object sender, EventArgs e) {
211      OnWeightsChanged();
212    }
213    private void Values_ItemChanged(object sender, EventArgs<int> e) {
214      OnValuesChanged();
215    }
216    private void Values_Reset(object sender, EventArgs e) {
217      OnValuesChanged();
218    }
219    #endregion
220  }
221}
Note: See TracBrowser for help on using the repository browser.