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