1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.Binary {
|
---|
31 | [Item("Binary Knapsack Problem (BKSP)", "Represents a problem whose objective is to maximize the number of true values.")]
|
---|
32 | [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 999)]
|
---|
33 | [StorableClass]
|
---|
34 | public class BinaryKnapsackProblem : BinaryProblem {
|
---|
35 | public override bool Maximization {
|
---|
36 | get { return true; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | #region Parameter Properties
|
---|
40 | public ValueParameter<IntValue> KnapsackCapacityParameter {
|
---|
41 | get { return (ValueParameter<IntValue>)Parameters["KnapsackCapacity"]; }
|
---|
42 | }
|
---|
43 | public ValueParameter<IntArray> WeightsParameter {
|
---|
44 | get { return (ValueParameter<IntArray>)Parameters["Weights"]; }
|
---|
45 | }
|
---|
46 | public ValueParameter<IntArray> ValuesParameter {
|
---|
47 | get { return (ValueParameter<IntArray>)Parameters["Values"]; }
|
---|
48 | }
|
---|
49 | #endregion
|
---|
50 |
|
---|
51 | #region Properties
|
---|
52 | public IntValue KnapsackCapacity {
|
---|
53 | get { return KnapsackCapacityParameter.Value; }
|
---|
54 | set { KnapsackCapacityParameter.Value = value; }
|
---|
55 | }
|
---|
56 | public IntArray Weights {
|
---|
57 | get { return WeightsParameter.Value; }
|
---|
58 | set { WeightsParameter.Value = value; }
|
---|
59 | }
|
---|
60 | public IntArray Values {
|
---|
61 | get { return ValuesParameter.Value; }
|
---|
62 | set { ValuesParameter.Value = value; }
|
---|
63 | }
|
---|
64 | #endregion
|
---|
65 |
|
---|
66 | [StorableConstructor]
|
---|
67 | protected BinaryKnapsackProblem(bool deserializing) : base(deserializing) { }
|
---|
68 | protected BinaryKnapsackProblem(BinaryKnapsackProblem original, Cloner cloner) : base(original, cloner) { }
|
---|
69 | public BinaryKnapsackProblem()
|
---|
70 | : base() {
|
---|
71 | Encoding.Length = 5;
|
---|
72 | Parameters.Add(new ValueParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack.", new IntValue(0)));
|
---|
73 | Parameters.Add(new ValueParameter<IntArray>("Weights", "The weights of the items.", new IntArray(5)));
|
---|
74 | Parameters.Add(new ValueParameter<IntArray>("Values", "The values of the items.", new IntArray(5)));
|
---|
75 |
|
---|
76 | InitializeRandomKnapsackInstance();
|
---|
77 | }
|
---|
78 |
|
---|
79 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
80 | return new BinaryKnapsackProblem(this, cloner);
|
---|
81 | }
|
---|
82 |
|
---|
83 | public override double Evaluate(BinaryVector vector, IRandom random) {
|
---|
84 | var itemWeights = Weights;
|
---|
85 | var itemValues = Values;
|
---|
86 | var weight = 0;
|
---|
87 | var value = 0;
|
---|
88 | for (var i = 0; i < vector.Length; i++) {
|
---|
89 | if (!vector[i]) continue;
|
---|
90 | weight += itemWeights[i];
|
---|
91 | value += itemValues[i];
|
---|
92 | }
|
---|
93 | var cap = KnapsackCapacity.Value;
|
---|
94 | // assuming only positive values in the knapsack, maximizing in negative
|
---|
95 | // range makes solutions feasible, in positive range increases their value
|
---|
96 | return weight <= cap ? value : cap - weight;
|
---|
97 | }
|
---|
98 |
|
---|
99 | private void InitializeRandomKnapsackInstance() {
|
---|
100 | var rand = new System.Random();
|
---|
101 |
|
---|
102 | var itemCount = (int)Math.Pow(2, rand.Next(5, 10));
|
---|
103 | Weights = new IntArray(itemCount);
|
---|
104 | Values = new IntArray(itemCount);
|
---|
105 |
|
---|
106 | double totalWeight = 0;
|
---|
107 |
|
---|
108 | for (var i = 0; i < itemCount; i++) {
|
---|
109 | var value = rand.Next(1, 50);
|
---|
110 | var weight = rand.Next(1, 50);
|
---|
111 |
|
---|
112 | Values[i] = value;
|
---|
113 | Weights[i] = weight;
|
---|
114 | totalWeight += weight;
|
---|
115 | }
|
---|
116 |
|
---|
117 | KnapsackCapacity = new IntValue((int)Math.Round(0.6 * totalWeight));
|
---|
118 | Encoding.Length = itemCount;
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|