Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Problems.Knapsack/3.3/KnapsackSolution.cs

Last change on this file was 13656, checked in by ascheibe, 9 years ago

#2582 created branch for Hive Web Job Manager

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