Skip to content

Commit

Permalink
Constant constraints (#82)
Browse files Browse the repository at this point in the history
Constraint without any decision variable like following causes
segmentation fault in CBC solver with Python-MIP 1.15.
https://github.com/Jij-Inc/ommx/actions/runs/9698846012/job/26766373894

```python
    x = DecisionVariable.continuous(0)
    instance = Instance.from_components(
        decision_variables=[x],
        objective=x,
        constraints=[
            # 1 >= 0 is always true
            Linear(terms={}, constant=1) >= 0,
            x <= 1,
        ],
        sense=Instance.MAXIMIZE,
    )
```

This has been reported in
coin-or/python-mip#213 and some linked issues,
and fixed in coin-or/python-mip#237.

We use latest commit of Python-MIP
coin-or/python-mip@0ccb811
until the next version is released
coin-or/python-mip#384
  • Loading branch information
termoshtt authored Jun 27, 2024
1 parent 7b652e1 commit 1233f0e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion python/ommx-python-mip-adapter/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ classifiers = [
]
dependencies = [
"ommx >= 0.5.0, < 0.6.0",
"mip",
"mip @ git+https://github.com/coin-or/python-mip.git@0ccb81115543e737ab74a4f1309891ce5650c8d5",
]

[project.urls]
Expand Down
40 changes: 40 additions & 0 deletions python/ommx-python-mip-adapter/tests/test_constant_constraint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import ommx_python_mip_adapter as adapter
from ommx.v1 import Instance, DecisionVariable, Linear


def test_constant_constraint_feasible():
x = DecisionVariable.continuous(0)
instance = Instance.from_components(
decision_variables=[x],
objective=x,
constraints=[
# 1 >= 0 is always true
Linear(terms={}, constant=1) >= 0,
x <= 1,
],
sense=Instance.MAXIMIZE,
)
result = adapter.solve(instance)
assert result.HasField("solution")

solution = result.solution
assert solution.state.entries == {0: 1.0}
assert solution.objective == 1.0

assert len(solution.evaluated_constraints) == 2


def test_constant_constraint_infeasible():
x = DecisionVariable.continuous(0)
instance = Instance.from_components(
decision_variables=[x],
objective=x,
constraints=[
# -1 >= 0 is always false
Linear(terms={}, constant=-1) >= 0,
x <= 1,
],
sense=Instance.MAXIMIZE,
)
result = adapter.solve(instance)
assert result.HasField("infeasible")

0 comments on commit 1233f0e

Please sign in to comment.