Exemplo n.º 1
0
def test_constraint_ancestors(op):
    x, y = Var(), Var()
    constr = op(x + y, 1)
    variables = constr.get_variables()
    assert x in variables
    assert y in variables
    assert Var() not in variables
Exemplo n.º 2
0
def test_solving_trivial_two_var_problem():
    bip = Knapsack(capacity=3)
    bip.add_var(Var(), weight=1, value=1)
    bip.add_var(Var(), weight=1, value=1)
    bip.solve()
    assert all([v.value == 1 for v in bip.variables])
    assert bip.optimal_value == 2
Exemplo n.º 3
0
def test_get_terms():
    x = Var()
    y = 5 - 2 * x
    z = y + 4
    family = z.terms
    assert x in family
    assert Var() not in family
Exemplo n.º 4
0
def test_knapsack_with_negative_values():
    bip = Knapsack(capacity=10)
    bip.add_var(Var(), weight=4, value=-2)
    bip.add_var(Var(), weight=20, value=5)
    bip.solve()
    assert [v.value for v in bip.variables] == [0, 0]
    assert bip.optimal_value == 0
Exemplo n.º 5
0
def test_with_offsets():
    x, y = Var(), Var()
    bip = InequalityConstrained(objective=2 * x + 3 * y + 5,
                                constraint=x + y + 1 <= 2)
    bip.solve()
    assert x.value == 0
    assert y.value == 1
    assert bip.optimal_value == 8
Exemplo n.º 6
0
def test_knapsack_with_negative_values_neg_weights():
    bip = Knapsack(capacity=10)
    x, y = Var(), Var()
    bip.add_var(x, weight=15, value=-2)
    bip.add_var(y, weight=-5, value=5)
    bip.solve()
    assert [x.value, y.value] == [0, 1]
    assert bip.optimal_value == 5
Exemplo n.º 7
0
def test_solving_nontrivial_two_var_problem():
    bip = Knapsack(capacity=3)
    bip.add_var(Var(), weight=4, value=5)
    bip.add_var(Var(), weight=1, value=1)
    bip.solve()
    assert bip.variables[0].value == 0
    assert bip.variables[1].value == 1
    assert bip.optimal_value == 1
Exemplo n.º 8
0
def test_solving_two_var_problem_with_negative_weights():
    bip = Knapsack(capacity=3)
    x, y = Var(), Var()
    bip.add_var(x, weight=-4, value=5)
    bip.add_var(y, weight=1, value=1)
    bip.solve()
    assert x.value == 1
    assert y.value == 1
    assert bip.optimal_value == 6
Exemplo n.º 9
0
def test_solving_nontrivial_four_var_problem():
    bip = Knapsack(capacity=14)
    bip.add_var(Var(), weight=5, value=8)
    bip.add_var(Var(), weight=7, value=11)
    bip.add_var(Var(), weight=4, value=6)
    bip.add_var(Var(), weight=3, value=4)
    bip.solve()
    values = [v.value for v in bip.variables]
    assert values == [0, 1, 1, 1]
    assert bip.optimal_value == 21
Exemplo n.º 10
0
def test_trivial_solves():
    x, y = Var(), Var()
    bip = InequalityConstrained(objective=2 * x + 3 * y, constraint=x + y <= 1)
    bip.solve()
    assert x.value == 0
    assert y.value == 1

    x, y = Var(), Var()
    bip = InequalityConstrained(objective=2 * x + 3 * y, constraint=x + y >= 1)
    bip.solve()
    assert x.value == 1
    assert y.value == 1
Exemplo n.º 11
0
def test_inversion():
    x = Var()
    y = 3 * x
    y.set_value(9)
    np.testing.assert_almost_equal(x.value, 3)

    x = Var()
    y = 3 * x + 6
    y.set_value(9)
    np.testing.assert_almost_equal(x.value, 1)

    x, z = Var(), Var()
    y = 3 * x + z
    with pytest.raises(ValueError):
        y.set_value(9)
Exemplo n.º 12
0
def test_solving_nontrivial_eight_var_problem():
    bip = Knapsack(capacity=104)
    for weight, value in zip([25, 35, 45, 5, 25, 3, 2, 2],
                             [350, 400, 450, 20, 70, 8, 5, 5]):
        bip.add_var(Var(), weight=weight, value=value)

    bip.solve()
    values = [v.value for v in bip.variables]
    assert values == [1, 0, 1, 1, 1, 0, 1, 1]
    assert bip.optimal_value == 900
Exemplo n.º 13
0
def test_var_has_value():
    x = Var()
    assert x.value == None
Exemplo n.º 14
0
def test_derived_var_value_cascade_right():
    x = Var('x')
    y = x - 1
    y.set_value(1)
    assert x.value == 2
Exemplo n.º 15
0
def test_derived_var_value_cascade_left():
    x = Var('x')
    y = 1 - x
    y.set_value(1)
    assert x.value == 0
Exemplo n.º 16
0
def test_adding_variables():
    bip = Knapsack(capacity=3)
    bip.add_var(Var(), weight=1, value=1)
Exemplo n.º 17
0
def test_init_with_kwargs():
    x, y = Var(), Var()
    InequalityConstrained(objective=2 * x + 3 * y, constraint=x + y <= 1)
Exemplo n.º 18
0
def test_var_set_value():
    x = Var()
    x.set_value(1)
    assert x.value == 1
Exemplo n.º 19
0
def test_downstream_variable_setting():
    x = Var()
    y = 5 - 2 * x
    z = y + 4
    z.set_value(10)
    np.testing.assert_almost_equal(x.value, -0.5)
Exemplo n.º 20
0
def test_terms():
    x, y = Var(), Var()
    exp = x + y
    assert x in exp.terms
    assert y in exp.terms
    assert Var() not in exp.terms
Exemplo n.º 21
0
def test_var_initializes_with_name():
    x = Var('x')
    assert x.name == 'x'
Exemplo n.º 22
0
def test_add_same_terms():
    x = Var()
    y = 3 * x + 5 * x - 3
    assert len(y.terms) == 1
    assert y.coefs == [8]
    assert y.offset == -3