def test_knapsack_unbounded_for_approx(self):
        # http://math.stackexchange.com/questions/720001/

        # Item (Value, Weight)
        i1 = Item (90, 9)
        i2 = Item (19, 2)
        i3 = Item (1, 1)
        self.assertEqual((95,[0,5,0]), knapsack_unbounded([i1,i2,i3], 10))
        
        # approximation chooses this one
        self.assertEqual((91,[1,0,1]), knapsack_approximate([i1,i2,i3], 10))
Beispiel #2
0
    def test_knapsack_unbounded_for_approx(self):
        # http://math.stackexchange.com/questions/720001/

        # Item (Value, Weight)
        i1 = Item (90, 9)
        i2 = Item (19, 2)
        i3 = Item (1, 1)
        self.assertEqual((95,[0,5,0]), knapsack_unbounded([i1,i2,i3], 10))
        
        # approximation chooses this one
        self.assertEqual((91,[1,0,1]), knapsack_approximate([i1,i2,i3], 10))
def reportOne():
    """
    To see the generates matrices, modify the knapsack methods temporarily to
    print out the matrices.
    """
    items = [Item(4, 4), Item(8, 8), Item(9, 9), Item(10, 10)]
    W = 33

    print("Knapsack Unbounded")
    w_un = knapsack_unbounded(items[:], W)
    print(w_un)

    print("Knapsack Approximate")
    w_ap = knapsack_approximate(items[:], W)
    print(w_ap)

    print("Knapsack 0/1")
    w_01 = knapsack_01(items[:], W)
    print(w_01)
def reportOne():
    """
    To see the generates matrices, modify the knapsack methods temporarily to
    print out the matrices.
    """
    items = [Item(4,4), Item(8,8), Item(9,9), Item(10,10)]
    W = 33
    
    print ("Knapsack Unbounded")
    w_un = knapsack_unbounded(items[:], W);
    print (w_un)
    
    print ("Knapsack Approximate")
    w_ap = knapsack_approximate(items[:], W);
    print (w_ap)
    
    print ("Knapsack 0/1")
    w_01 = knapsack_01(items[:], W);
    print (w_01)
def trials():
    """
    Locate knapsack items that has different results for :
       1) knapsack_unbounded
       2) knapsack_01
       3) knapsack_approximate
       
    Found this nice example:
    
    W = 33
    (33, [6, 0, 0, 1]) (32, [8, 0, 0, 0]) (31, [1, 1, 1, 1])

        (val= 4 , weight= 4
        (val= 8 , weight= 8
        (val= 10 , weight= 10
        (val= 9 , weight= 9

    """
    # 103 207 4 2 523

    for t in range(10000):
        items = []
        # Item (Value, Weight)
        for _ in range(5):
            i = random.randint(3, 11)
            items.append(Item(i, i))

        W = 33

        w_un = knapsack_unbounded(items[:], W)
        w_ap = knapsack_approximate(items[:], W)
        w_01 = knapsack_01(items[:], W)

        if w_un[0] != w_ap[0] and w_ap[0] != w_01[0] and w_un[0] != w_01[0]:
            print(w_un, w_ap, w_01)
            for i in items:
                print('(val=', i.value, ', weight=', i.weight)
            return
def trials():
    """
    Locate knapsack items that has different results for :
       1) knapsack_unbounded
       2) knapsack_01
       3) knapsack_approximate
       
    Found this nice example:
    
    W = 33
    (33, [6, 0, 0, 1]) (32, [8, 0, 0, 0]) (31, [1, 1, 1, 1])

        (val= 4 , weight= 4
        (val= 8 , weight= 8
        (val= 10 , weight= 10
        (val= 9 , weight= 9

    """
    # 103 207 4 2 523
    
    for t in range(10000):
        items = []
        # Item (Value, Weight)
        for _ in range(5):
            i = random.randint(3,11)
            items.append(Item(i,i))
                       
        W = 33
    
        w_un = knapsack_unbounded(items[:], W);
        w_ap = knapsack_approximate(items[:], W);
        w_01 = knapsack_01(items[:], W);
                        
        if w_un[0] != w_ap[0] and w_ap[0] != w_01[0] and w_un[0] != w_01[0]:
            print (w_un, w_ap, w_01)
            for i in items:
                print ('(val=', i.value, ', weight=', i.weight);
            return