1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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 HEAL.Attic;
|
---|
26 | using HeuristicLab.Analysis;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Optimization.Operators;
|
---|
33 | using HeuristicLab.Parameters;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Problems.Knapsack {
|
---|
36 | [Item("Knapsack Problem (KSP)", "Represents a Knapsack problem.")]
|
---|
37 | [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 200)]
|
---|
38 | [StorableType("8CEDAFA2-6E0A-4D4B-B6C6-F85CC58B824E")]
|
---|
39 | public sealed class KnapsackProblem : BinaryVectorProblem {
|
---|
40 | public override bool Maximization { get { return true; } }
|
---|
41 |
|
---|
42 | #region Parameter Properties
|
---|
43 | public ValueParameter<IntValue> KnapsackCapacityParameter {
|
---|
44 | get { return (ValueParameter<IntValue>)Parameters["KnapsackCapacity"]; }
|
---|
45 | }
|
---|
46 | public ValueParameter<IntArray> WeightsParameter {
|
---|
47 | get { return (ValueParameter<IntArray>)Parameters["Weights"]; }
|
---|
48 | }
|
---|
49 | public ValueParameter<IntArray> ValuesParameter {
|
---|
50 | get { return (ValueParameter<IntArray>)Parameters["Values"]; }
|
---|
51 | }
|
---|
52 | public OptionalValueParameter<BinaryVector> BestKnownSolutionParameter {
|
---|
53 | get { return (OptionalValueParameter<BinaryVector>)Parameters["BestKnownSolution"]; }
|
---|
54 | }
|
---|
55 | #endregion
|
---|
56 |
|
---|
57 | #region Properties
|
---|
58 | public int KnapsackCapacity {
|
---|
59 | get { return KnapsackCapacityParameter.Value.Value; }
|
---|
60 | set { KnapsackCapacityParameter.Value.Value = value; }
|
---|
61 | }
|
---|
62 | public IntArray Weights {
|
---|
63 | get { return WeightsParameter.Value; }
|
---|
64 | set { WeightsParameter.Value = value; }
|
---|
65 | }
|
---|
66 | public IntArray Values {
|
---|
67 | get { return ValuesParameter.Value; }
|
---|
68 | set { ValuesParameter.Value = value; }
|
---|
69 | }
|
---|
70 | public BinaryVector BestKnownSolution {
|
---|
71 | get { return BestKnownSolutionParameter.Value; }
|
---|
72 | set { BestKnownSolutionParameter.Value = value; }
|
---|
73 | }
|
---|
74 | private BestKnapsackSolutionAnalyzer BestKnapsackSolutionAnalyzer {
|
---|
75 | get { return Operators.OfType<BestKnapsackSolutionAnalyzer>().FirstOrDefault(); }
|
---|
76 | }
|
---|
77 | #endregion
|
---|
78 |
|
---|
79 | [StorableConstructor]
|
---|
80 | private KnapsackProblem(StorableConstructorFlag _) : base(_) { }
|
---|
81 | private KnapsackProblem(KnapsackProblem original, Cloner cloner)
|
---|
82 | : base(original, cloner) {
|
---|
83 | RegisterEventHandlers();
|
---|
84 | }
|
---|
85 | public KnapsackProblem()
|
---|
86 | : base(new BinaryVectorEncoding("Selection")) {
|
---|
87 | Parameters.Add(new ValueParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack.", new IntValue(1)));
|
---|
88 | Parameters.Add(new ValueParameter<IntArray>("Weights", "The weights of the items.", new IntArray(5)));
|
---|
89 | Parameters.Add(new ValueParameter<IntArray>("Values", "The values of the items.", new IntArray(5)));
|
---|
90 | Parameters.Add(new OptionalValueParameter<BinaryVector>("BestKnownSolution", "The best known solution of this Knapsack instance."));
|
---|
91 |
|
---|
92 | InitializeRandomKnapsackInstance();
|
---|
93 |
|
---|
94 | InitializeOperators();
|
---|
95 | RegisterEventHandlers();
|
---|
96 | }
|
---|
97 |
|
---|
98 | public override double Evaluate(BinaryVector solution, IRandom random) {
|
---|
99 | var totalWeight = 0.0;
|
---|
100 | var totalValue = 0.0;
|
---|
101 | for (var i = 0; i < solution.Length; i++) {
|
---|
102 | if (!solution[i]) continue;
|
---|
103 | totalWeight += Weights[i];
|
---|
104 | totalValue += Values[i];
|
---|
105 | }
|
---|
106 | return totalWeight > KnapsackCapacity ? KnapsackCapacity - totalWeight : totalValue;
|
---|
107 | }
|
---|
108 |
|
---|
109 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
110 | return new KnapsackProblem(this, cloner);
|
---|
111 | }
|
---|
112 |
|
---|
113 | [StorableHook(HookType.AfterDeserialization)]
|
---|
114 | private void AfterDeserialization() {
|
---|
115 | RegisterEventHandlers();
|
---|
116 | }
|
---|
117 |
|
---|
118 | private void RegisterEventHandlers() {
|
---|
119 | Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
120 | KnapsackCapacityParameter.ValueChanged += KnapsackCapacityParameter_ValueChanged;
|
---|
121 | WeightsParameter.ValueChanged += WeightsParameter_ValueChanged;
|
---|
122 | WeightsParameter.Value.Reset += WeightsValue_Reset;
|
---|
123 | ValuesParameter.ValueChanged += ValuesParameter_ValueChanged;
|
---|
124 | ValuesParameter.Value.Reset += ValuesValue_Reset;
|
---|
125 | // TODO: There is no even to detect if the parameter itself was changed
|
---|
126 | Encoding.LengthParameter.ValueChanged += Encoding_LengthParameter_ValueChanged;
|
---|
127 | }
|
---|
128 |
|
---|
129 | #region Events
|
---|
130 | protected override void OnEncodingChanged() {
|
---|
131 | base.OnEncodingChanged();
|
---|
132 | Parameterize();
|
---|
133 | }
|
---|
134 | //TODO check with abeham if this is really necessary
|
---|
135 | //protected override void OnSolutionCreatorChanged() {
|
---|
136 | // base.OnSolutionCreatorChanged();
|
---|
137 | // Parameterize();
|
---|
138 | //}
|
---|
139 | protected override void OnEvaluatorChanged() {
|
---|
140 | base.OnEvaluatorChanged();
|
---|
141 | Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
142 | Parameterize();
|
---|
143 | }
|
---|
144 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
145 | Parameterize();
|
---|
146 | }
|
---|
147 | private void KnapsackCapacityParameter_ValueChanged(object sender, EventArgs e) {
|
---|
148 | Parameterize();
|
---|
149 | }
|
---|
150 | private void WeightsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
151 | Parameterize();
|
---|
152 | WeightsParameter.Value.Reset += WeightsValue_Reset;
|
---|
153 | }
|
---|
154 | private void WeightsValue_Reset(object sender, EventArgs e) {
|
---|
155 | if (WeightsParameter.Value != null && ValuesParameter.Value != null) {
|
---|
156 | ((IStringConvertibleArray)ValuesParameter.Value).Length = Weights.Length;
|
---|
157 | Encoding.Length = Weights.Length;
|
---|
158 | }
|
---|
159 | Parameterize();
|
---|
160 | }
|
---|
161 | private void ValuesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
162 | Parameterize();
|
---|
163 | ValuesParameter.Value.Reset += ValuesValue_Reset;
|
---|
164 | }
|
---|
165 | private void ValuesValue_Reset(object sender, EventArgs e) {
|
---|
166 | if (WeightsParameter.Value != null && ValuesParameter.Value != null) {
|
---|
167 | ((IStringConvertibleArray)WeightsParameter.Value).Length = Values.Length;
|
---|
168 | Encoding.Length = Values.Length;
|
---|
169 | }
|
---|
170 | Parameterize();
|
---|
171 | }
|
---|
172 | private void Encoding_LengthParameter_ValueChanged(object sender, EventArgs e) {
|
---|
173 | if (Weights.Length != Encoding.Length) {
|
---|
174 | ((IStringConvertibleArray)WeightsParameter.Value).Length = Encoding.Length;
|
---|
175 | }
|
---|
176 | if (Values.Length != Encoding.Length) {
|
---|
177 | ((IStringConvertibleArray)ValuesParameter.Value).Length = Encoding.Length;
|
---|
178 | }
|
---|
179 | Parameterize();
|
---|
180 | }
|
---|
181 | #endregion
|
---|
182 |
|
---|
183 | #region Helpers
|
---|
184 | private void InitializeOperators() {
|
---|
185 | Operators.Add(new KnapsackImprovementOperator());
|
---|
186 | Operators.Add(new KnapsackPathRelinker());
|
---|
187 | Operators.Add(new KnapsackSimultaneousPathRelinker());
|
---|
188 | Operators.Add(new KnapsackSimilarityCalculator());
|
---|
189 | Operators.Add(new QualitySimilarityCalculator());
|
---|
190 | Operators.Add(new NoSimilarityCalculator());
|
---|
191 |
|
---|
192 | Operators.Add(new BestKnapsackSolutionAnalyzer());
|
---|
193 | Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
|
---|
194 |
|
---|
195 | Parameterize();
|
---|
196 | }
|
---|
197 | private void Parameterize() {
|
---|
198 | var operators = new List<IItem>();
|
---|
199 |
|
---|
200 | if (BestKnapsackSolutionAnalyzer != null) {
|
---|
201 | operators.Add(BestKnapsackSolutionAnalyzer);
|
---|
202 | BestKnapsackSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
203 | BestKnapsackSolutionAnalyzer.MaximizationParameter.Hidden = true;
|
---|
204 | BestKnapsackSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
205 | BestKnapsackSolutionAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
206 | BestKnapsackSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
|
---|
207 | BestKnapsackSolutionAnalyzer.BestKnownSolutionParameter.Hidden = true;
|
---|
208 | BestKnapsackSolutionAnalyzer.KnapsackCapacityParameter.ActualName = KnapsackCapacityParameter.Name;
|
---|
209 | BestKnapsackSolutionAnalyzer.KnapsackCapacityParameter.Hidden = true;
|
---|
210 | BestKnapsackSolutionAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
211 | BestKnapsackSolutionAnalyzer.WeightsParameter.Hidden = true;
|
---|
212 | BestKnapsackSolutionAnalyzer.ValuesParameter.ActualName = ValuesParameter.Name;
|
---|
213 | BestKnapsackSolutionAnalyzer.ValuesParameter.Hidden = true;
|
---|
214 | }
|
---|
215 | foreach (var op in Operators.OfType<IKnapsackMoveEvaluator>()) {
|
---|
216 | operators.Add(op);
|
---|
217 | op.KnapsackCapacityParameter.ActualName = KnapsackCapacityParameter.Name;
|
---|
218 | op.KnapsackCapacityParameter.Hidden = true;
|
---|
219 | op.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
220 | op.WeightsParameter.Hidden = true;
|
---|
221 | op.ValuesParameter.ActualName = ValuesParameter.Name;
|
---|
222 | op.ValuesParameter.Hidden = true;
|
---|
223 |
|
---|
224 | var bitflipMoveEval = op as IKnapsackOneBitflipMoveEvaluator;
|
---|
225 | if (bitflipMoveEval != null) {
|
---|
226 | foreach (var moveOp in Encoding.Operators.OfType<IOneBitflipMoveQualityOperator>()) {
|
---|
227 | moveOp.MoveQualityParameter.ActualName = bitflipMoveEval.MoveQualityParameter.ActualName;
|
---|
228 | moveOp.MoveQualityParameter.Hidden = true;
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|
232 | foreach (var op in Operators.OfType<ISingleObjectiveImprovementOperator>()) {
|
---|
233 | operators.Add(op);
|
---|
234 | op.SolutionParameter.ActualName = Encoding.Name;
|
---|
235 | op.SolutionParameter.Hidden = true;
|
---|
236 | }
|
---|
237 | foreach (var op in Operators.OfType<ISingleObjectivePathRelinker>()) {
|
---|
238 | operators.Add(op);
|
---|
239 | op.ParentsParameter.ActualName = Encoding.Name;
|
---|
240 | op.ParentsParameter.Hidden = true;
|
---|
241 | }
|
---|
242 | foreach (var op in Operators.OfType<ISolutionSimilarityCalculator>()) {
|
---|
243 | operators.Add(op);
|
---|
244 | op.SolutionVariableName = Encoding.Name;
|
---|
245 | op.QualityVariableName = Evaluator.QualityParameter.ActualName;
|
---|
246 | }
|
---|
247 |
|
---|
248 | if (operators.Count > 0) Encoding.ConfigureOperators(Operators);
|
---|
249 | }
|
---|
250 | #endregion
|
---|
251 |
|
---|
252 | private void InitializeRandomKnapsackInstance() {
|
---|
253 | var sysrand = new System.Random();
|
---|
254 |
|
---|
255 | var itemCount = sysrand.Next(10, 100);
|
---|
256 | Weights = new IntArray(itemCount);
|
---|
257 | Values = new IntArray(itemCount);
|
---|
258 |
|
---|
259 | double totalWeight = 0;
|
---|
260 |
|
---|
261 | for (int i = 0; i < itemCount; i++) {
|
---|
262 | var value = sysrand.Next(1, 10);
|
---|
263 | var weight = sysrand.Next(1, 10);
|
---|
264 |
|
---|
265 | Values[i] = value;
|
---|
266 | Weights[i] = weight;
|
---|
267 | totalWeight += weight;
|
---|
268 | }
|
---|
269 |
|
---|
270 | KnapsackCapacity = (int)Math.Round(0.7 * totalWeight);
|
---|
271 | }
|
---|
272 | }
|
---|
273 | }
|
---|