Exemplo n.º 1
0
def test_estimate_value_1():
    input_text = "4 11\n8 4\n10 5\n15 8\n4 3\n"
    choice = [1,1,0]
    kp = KnapsackProblemData(input_text)
    num_chosen = len(choice)
    current_weight = np.sum(np.multiply(kp.weights[0:num_chosen],np.array(choice)))
    current_value  = np.sum(np.multiply(kp.values[0:num_chosen],np.array(choice)))
    # value vector  [ 10 8  15 4 ]
    # weight vector [ 5  4  8  3 ]
    # choice gets weight 5+4 = 9 with remaining capacity 2
    # choice gets value 10+8 = 18
    # we can take 2/3 of item 4, giving us added value of (2/3)*4 = 2.67
    # estimated value == 20.67
    estimated_value = estimate_value(kp,choice,current_weight,current_value)
    assert choice == [1,1,0] # choice should not mutate
    assert estimated_value == 18 + (2*4)/float(3)
Exemplo n.º 2
0
def test_estimate_value_2():
    input_text = "4 11\n8 4\n10 5\n15 8\n4 3\n"
    choice = [0,0]
    kp = KnapsackProblemData(input_text)
    num_chosen = len(choice)
    current_weight = np.sum(np.multiply(kp.weights[0:num_chosen],np.array(choice)))
    current_value  = np.sum(np.multiply(kp.values[0:num_chosen],np.array(choice)))
    # value vector  [ 10 8  15 4 ]
    # weight vector [ 5  4  8  3 ]
    # choice gets weight 0 with remaining capacity 11
    # choice gets value 0
    # we take all of item 3
    #	value = 15
    #	weight = 8
    # we can take all of item 4, giving us added value of 4
    # estimated value == 19
    estimated_value = estimate_value(kp,choice,current_weight,current_value)
    assert choice == [0,0] # choice should not mutate
    assert estimated_value == 19