[3467] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16453] | 3 | * Copyright (C) 2002-2019 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 |
|
---|
| 22 | using System;
|
---|
[4068] | 23 | using System.Drawing;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
[3467] | 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
[16559] | 28 | using HEAL.Attic;
|
---|
[3467] | 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.Knapsack {
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// Represents a knapsack solution which can be visualized in the GUI.
|
---|
| 33 | /// </summary>
|
---|
[4513] | 34 | [Item("KnapsackSolution", "Represents a Knapsack solution which can be visualized in the GUI.")]
|
---|
[16462] | 35 | [StorableType("C959079D-3D92-4C88-8734-9DADEE851169")]
|
---|
[3467] | 36 | public class KnapsackSolution : Item {
|
---|
[7201] | 37 | public static new Image StaticItemImage {
|
---|
[5287] | 38 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Image; }
|
---|
[3467] | 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 |
|
---|
[3641] | 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 |
|
---|
[4722] | 111 | [StorableConstructor]
|
---|
[16462] | 112 | protected KnapsackSolution(StorableConstructorFlag _) : base(_) { }
|
---|
[4722] | 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 | }
|
---|
[3467] | 125 | public KnapsackSolution() : base() { }
|
---|
[3641] | 126 | public KnapsackSolution(BinaryVector binaryVector, DoubleValue quality, IntValue capacity, IntArray weights, IntArray values)
|
---|
[3467] | 127 | : base() {
|
---|
| 128 | this.binaryVector = binaryVector;
|
---|
| 129 | this.capacity = capacity;
|
---|
| 130 | this.weights = weights;
|
---|
| 131 | this.values = values;
|
---|
[3641] | 132 | this.quality = quality;
|
---|
[3467] | 133 | Initialize();
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[4722] | 137 | private void AfterDeserialization() {
|
---|
| 138 | Initialize();
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[3467] | 141 | private void Initialize() {
|
---|
| 142 | if (binaryVector != null) RegisterBinaryVectorEvents();
|
---|
[3641] | 143 | if (quality != null) RegisterQualityEvents();
|
---|
[3467] | 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 |
|
---|
[3641] | 178 | public event EventHandler QualityChanged;
|
---|
| 179 | private void OnQualityChanged() {
|
---|
| 180 | var changed = QualityChanged;
|
---|
| 181 | if (changed != null)
|
---|
| 182 | changed(this, EventArgs.Empty);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[3467] | 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 |
|
---|
[3641] | 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 |
|
---|
[3467] | 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 | }
|
---|
[3641] | 251 | private void Quality_ValueChanged(object sender, EventArgs e) {
|
---|
| 252 | OnQualityChanged();
|
---|
| 253 | }
|
---|
[3467] | 254 | #endregion
|
---|
| 255 | }
|
---|
| 256 | }
|
---|