Пример #1
0
 def test_eight_items(self):
     self.assertEqual(
         solve_knapsack(104, [{
             "weight": 25,
             "value": 350
         }, {
             "weight": 35,
             "value": 400
         }, {
             "weight": 45,
             "value": 450
         }, {
             "weight": 5,
             "value": 20
         }, {
             "weight": 25,
             "value": 70
         }, {
             "weight": 3,
             "value": 8
         }, {
             "weight": 2,
             "value": 5
         }, {
             "weight": 2,
             "value": 5
         }]), 900)
Пример #2
0
 def test_knapsack(self):
     w = [1, 2, 3, 5, 6]
     v = [2, 3, 4, 8, 9]
     b = 5
     ans1 = 8
     ans2 = [4]
     self.assertEqual((ans1, ans2), knapsack.solve_knapsack(w, v, b))
     pass
Пример #3
0
    def _exact_solver_driver(self, data_file, max_weight, expected_energy,
                             expected_item_indices):
        """Utility routine to perform a test on the exact solver and compare to expected solution"""
        sampler = dimod.ExactSolver()

        df = pd.read_csv(data_file, names=['cost', 'weight'])

        selected_item_indices, energy = knapsack.solve_knapsack(
            df['cost'], df['weight'], max_weight, sampler=sampler)

        self.assertEqual(energy, expected_energy)
        self.assertEqual(selected_item_indices, expected_item_indices)
Пример #4
0
 def test_example_knapsack(self):
     self.assertEqual(
         solve_knapsack(10, [{
             "weight": 5,
             "value": 10
         }, {
             "weight": 4,
             "value": 40
         }, {
             "weight": 6,
             "value": 30
         }, {
             "weight": 4,
             "value": 50
         }]), 90)
Пример #5
0
 def test_fifteen_items(self):
     self.assertEqual(
         solve_knapsack(750, [{
             "weight": 70,
             "value": 135
         }, {
             "weight": 73,
             "value": 139
         }, {
             "weight": 77,
             "value": 149
         }, {
             "weight": 80,
             "value": 150
         }, {
             "weight": 82,
             "value": 156
         }, {
             "weight": 87,
             "value": 163
         }, {
             "weight": 90,
             "value": 173
         }, {
             "weight": 94,
             "value": 184
         }, {
             "weight": 98,
             "value": 192
         }, {
             "weight": 106,
             "value": 201
         }, {
             "weight": 110,
             "value": 210
         }, {
             "weight": 113,
             "value": 214
         }, {
             "weight": 115,
             "value": 221
         }, {
             "weight": 118,
             "value": 229
         }, {
             "weight": 120,
             "value": 240
         }]), 1458)
Пример #6
0
 def test_cannot_be_greedy_by_value(self):
     self.assertEqual(
         solve_knapsack(10, [{
             "weight": 2,
             "value": 20
         }, {
             "weight": 2,
             "value": 20
         }, {
             "weight": 2,
             "value": 20
         }, {
             "weight": 2,
             "value": 20
         }, {
             "weight": 10,
             "value": 50
         }]), 80)
Пример #7
0
 def test_cannot_be_greedy_by_weight(self):
     self.assertEqual(
         solve_knapsack(10, [{
             "weight": 2,
             "value": 5
         }, {
             "weight": 2,
             "value": 5
         }, {
             "weight": 2,
             "value": 5
         }, {
             "weight": 2,
             "value": 5
         }, {
             "weight": 10,
             "value": 21
         }]), 21)
Пример #8
0
 def test_no_items(self):
     self.assertEqual(solve_knapsack(100, []), 0)
Пример #9
0
 def test_one_item_too_heavy(self):
     self.assertEqual(solve_knapsack(10, [{"weight": 100, "value": 1}]), 0)
Пример #10
0
To find such d and w, the biggest value must be equal to out.
'''
import numpy as np
from numpy.random import randint
import knapsack

#d: list of w/v. d is sorted from larger one.
d = np.sort([randint(100) for i in range(5)])[::-1]

check = 1
while (check == 1):
    #w: weight of a bag
    w = [randint(1, 100) for i in range(5)]
    #v: value of a bag
    v = [d[i] * w[i] for i in range(5)]
    #l: index of the biggest value
    l = v.index(max(v))
    if (l > 1):
        check = 0

max_weight = w[0] + w[1] + w[2] - 1
out = max(v[0] + v[1], v[l])
opt, used = knapsack.solve_knapsack(w, v, max_weight)

print(d)
print(w)
print(v, l)
print("b=%d opt=%d out=%d rate=%f " %
      (max_weight, opt, out, float(opt) / float(out)))
print(used)