Exemplo n.º 1
0
def test_constraint_matrix():
    m = Model()
    x = m.add_variables(coords=[range(10)])
    y = m.add_variables()
    m.add_constraints(x, "=", 0)
    A = m.constraints.to_matrix()
    assert A.shape == (10, 11)
Exemplo n.º 2
0
def create_model(N):
    m = Model()
    coords = [arange(N), arange(N)]
    x = m.add_variables(coords=coords)
    y = m.add_variables(coords=coords)
    m.add_constraints(x - y >= arange(N))
    m.add_constraints(x + y >= 0)
    m.add_objective((2 * x).sum() + y.sum())
    return m
Exemplo n.º 3
0
def milp_model_r():
    m = Model()

    lower = pd.Series(0, range(10))
    x = m.add_variables(coords=[lower.index], name="x", binary=True)
    y = m.add_variables(lower, name="y")

    m.add_constraints(x + y, ">=", 10)

    m.add_objective(2 * x + y)
    return m
Exemplo n.º 4
0
def model_chunked():
    m = Model(chunk="auto")

    x = m.add_variables(name="x")
    y = m.add_variables(name="y")

    m.add_constraints(2 * x + 6 * y, ">=", 10)
    m.add_constraints(4 * x + 2 * y, ">=", 3)

    m.add_objective(2 * y + x)
    return m
Exemplo n.º 5
0
def model_anonymous_constraint():
    m = Model(chunk=None)

    x = m.add_variables(name="x")
    y = m.add_variables(name="y")

    m.add_constraints(2 * x + 6 * y >= 10)
    m.add_constraints(4 * x + 2 * y >= 3)

    m.add_objective(2 * y + x)
    return m
Exemplo n.º 6
0
def test_nan_in_objective():
    m = Model()

    x = m.add_variables(name="x")
    y = m.add_variables(name="y")

    m.add_constraints(2 * x + 6 * y, ">=", np.nan)
    m.add_constraints(4 * x + 2 * y, ">=", 3)

    m.add_objective(np.nan * y + x)
    with pytest.raises(ValueError):
        m.solve()
Exemplo n.º 7
0
def model_with_inf():
    m = Model()

    lower = pd.Series(0, range(10))
    x = m.add_variables(coords=[lower.index], name="x", binary=True)
    y = m.add_variables(lower, name="y")

    m.add_constraints(x + y, ">=", 10)
    m.add_constraints(1 * x, "<=", np.inf)

    m.objective = 2 * x + y

    return m
Exemplo n.º 8
0
def model(n, solver, integerlabels):
    m = Model()
    if integerlabels:
        N, M = [arange(n), arange(n)]
    else:
        N, M = [arange(n).astype(float), arange(n).astype(str)]
    x = m.add_variables(coords=[N, M])
    y = m.add_variables(coords=[N, M])
    m.add_constraints(x - y >= N)
    m.add_constraints(x + y >= 0)
    m.add_objective((2 * x).sum() + y.sum())
    m.solve(solver)
    return
Exemplo n.º 9
0
def test_constraints_accessor():
    m = Model()

    lower = xr.DataArray(np.zeros((10, 10)), coords=[range(10), range(10)])
    upper = xr.DataArray(np.ones((10, 10)), coords=[range(10), range(10)])
    x = m.add_variables(lower, upper)
    y = m.add_variables()
    m.add_constraints(1 * x + 10 * y, "=", 0)
    assert m.constraints["con0"].shape == (10, 10)
    assert isinstance(m.constraints[["con0"]], linopy.constraints.Constraints)
    assert isinstance(m.constraints.inequalities,
                      linopy.constraints.Constraints)
    assert isinstance(m.constraints.equalities, linopy.constraints.Constraints)
Exemplo n.º 10
0
def masked_constraint_model():
    m = Model()

    lower = pd.Series(0, range(10))
    x = m.add_variables(lower, name="x")
    y = m.add_variables(lower, name="y")

    mask = pd.Series([True] * 8 + [False, False])
    m.add_constraints(x + y, ">=", 10, mask=mask)
    # for the last two entries only the following constraint will be active
    m.add_constraints(x + y, ">=", 5)

    m.add_objective(2 * x + y)
    return m
Exemplo n.º 11
0
def masked_variable_model():
    m = Model()

    lower = pd.Series(0, range(10))
    x = m.add_variables(lower, name="x")
    mask = pd.Series([True] * 8 + [False, False])
    y = m.add_variables(lower, name="y", mask=mask)

    m.add_constraints(x + y, ">=", 10)

    m.add_constraints(y, ">=", 0)

    m.add_objective(2 * x + y)
    return m
Exemplo n.º 12
0
def test_to_blocks(tmp_path):
    m = Model()

    lower = pd.Series(range(20))
    upper = pd.Series(range(30, 50))
    x = m.add_variables(lower, upper)
    y = m.add_variables(lower, upper)

    m.add_constraints(x + y, "<=", 10)

    m.add_objective(2 * x + 3 * y)

    m.blocks = xr.DataArray([1] * 10 + [2] * 10)

    m.to_block_files(tmp_path)
Exemplo n.º 13
0
def test_constraint_repr():
    m = Model()

    x = m.add_variables()
    c = m.add_constraints(x, ">=", 0)
    c.__repr__()
    c._repr_html_()
Exemplo n.º 14
0
def test_get_name_by_label():
    m = Model()
    x = m.add_variables(coords=[range(10)])
    y = m.add_variables(coords=[range(10)])

    m.add_constraints(x + y <= 10, name="first")
    m.add_constraints(x - y >= 5, name="second")

    assert m.constraints.get_name_by_label(4) == "first"
    assert m.constraints.get_name_by_label(14) == "second"

    with pytest.raises(ValueError):
        m.constraints.get_name_by_label(30)

    with pytest.raises(ValueError):
        m.constraints.get_name_by_label("first")
Exemplo n.º 15
0
def test_constraint_matrix_masked_constraints_and_variables():
    """
    Test constraint matrix with missing constraints.
    """
    # now with missing variables
    m = Model()
    mask = pd.Series([False] * 5 + [True] * 5)
    x = m.add_variables(coords=[range(10)], mask=mask)
    m.add_variables()
    m.add_constraints(x, "=", 0, mask=mask)
    A = m.constraints.to_matrix(filter_missings=True)
    assert A.shape == (5, 6)
    assert A.shape == (m.ncons, m.nvars)

    A = m.constraints.to_matrix(filter_missings=False)
    assert A.shape == (m._cCounter, m._xCounter)
Exemplo n.º 16
0
def test_to_file(tmp_path):
    import gurobipy

    m = Model()

    x = m.add_variables(4, pd.Series([8, 10]))
    y = m.add_variables(0, pd.DataFrame([[1, 2], [3, 4], [5, 6]]))

    m.add_constraints(x + y, "<=", 10)

    m.add_objective(2 * x + 3 * y)

    fn = tmp_path / "test.lp"
    m.to_file(fn)

    gurobipy.read(str(fn))
Exemplo n.º 17
0
def test_to_netcdf(tmp_path):
    m = Model()

    x = m.add_variables(4, pd.Series([8, 10]))
    y = m.add_variables(0, pd.DataFrame([[1, 2], [3, 4], [5, 6]]))
    m.add_constraints(x + y, "<=", 10)
    m.add_objective(2 * x + 3 * y)

    fn = tmp_path / "test.nc"
    m.to_netcdf(fn)
    p = read_netcdf(fn)

    for k in m.scalar_attrs:
        if k != "objective_value":
            assert getattr(m, k) == getattr(p, k)
    for k in m.dataset_attrs:
        assert_equal(getattr(m, k), getattr(p, k))
Exemplo n.º 18
0
def test_constraint_matrix_masked_variables():
    """
    Test constraint matrix with missing variables.

    In this case the variables that are used in the constraints are
    missing. The matrix shoud not be built for constraints which have
    variables which are missing.
    """
    # now with missing variables
    m = Model()
    mask = pd.Series([False] * 5 + [True] * 5)
    x = m.add_variables(coords=[range(10)], mask=mask)
    m.add_variables()
    m.add_constraints(x, "=", 0)
    A = m.constraints.to_matrix(filter_missings=True)
    assert A.shape == (5, 6)
    assert A.shape == (m.ncons, m.nvars)

    A = m.constraints.to_matrix(filter_missings=False)
    assert A.shape == (m._cCounter, m._xCounter)
Exemplo n.º 19
0
def modified_model():
    m = Model()

    lower = pd.Series(0, range(10))
    x = m.add_variables(coords=[lower.index], name="x", binary=True)
    y = m.add_variables(lower, name="y")

    c = m.add_constraints(x + y, ">=", 10)

    y.lower = 9
    c.lhs = 2 * x + y
    m.objective = 2 * x + y

    return m
Exemplo n.º 20
0
def test_constraint_accessor():
    m = Model()
    x = m.add_variables()
    y = m.add_variables()
    c = m.add_constraints(x, ">=", 0)
    assert c.rhs.item() == 0
    assert c.vars.item() == 0
    assert c.coeffs.item() == 1
    assert c.sign.item() == ">="

    c.rhs = 2
    assert c.rhs.item() == 2

    c.vars = y
    assert c.vars.item() == 1

    c.coeffs = 3
    assert c.coeffs.item() == 3

    c.sign = "=="
    assert c.sign.item() == "=="
Exemplo n.º 21
0
def test_constraint_accessor_M():
    m = Model()
    lower = pd.Series(range(10), range(10))
    upper = pd.Series(range(10, 20), range(10))
    x = m.add_variables(lower, upper)
    y = m.add_variables(lower, upper)
    c = m.add_constraints(x, ">=", 0)
    assert c.rhs.item() == 0
    assert (c.rhs == 0).all()

    assert c.vars.shape == (10, 1)
    assert c.coeffs.shape == (10, 1)

    assert c.sign.item() == ">="

    c.rhs = 2
    assert c.rhs.item() == 2

    c.lhs = 3 * y
    assert (c.vars.squeeze() == y.data).all()
    assert (c.coeffs == 3).all()
    assert isinstance(c.lhs, linopy.LinearExpression)
Exemplo n.º 22
0
def test_constraints_repr():
    m = Model()
    m.constraints.__repr__()
    x = m.add_variables()
    m.add_constraints(x, ">=", 0)
    m.constraints.__repr__()