Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/Problems/BinaryKnapsackProblem.cs @ 14783

Last change on this file since 14783 was 14601, checked in by jkarder, 8 years ago

#2205: worked on optimization networks

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