ommx-python-mip-adapter 3.0.0b7__tar.gz → 3.0.0b8__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (19) hide show
  1. {ommx_python_mip_adapter-3.0.0b7 → ommx_python_mip_adapter-3.0.0b8}/PKG-INFO +2 -2
  2. {ommx_python_mip_adapter-3.0.0b7 → ommx_python_mip_adapter-3.0.0b8}/ommx_python_mip_adapter/adapter.py +15 -10
  3. {ommx_python_mip_adapter-3.0.0b7 → ommx_python_mip_adapter-3.0.0b8}/ommx_python_mip_adapter.egg-info/PKG-INFO +2 -2
  4. {ommx_python_mip_adapter-3.0.0b7 → ommx_python_mip_adapter-3.0.0b8}/ommx_python_mip_adapter.egg-info/SOURCES.txt +2 -1
  5. {ommx_python_mip_adapter-3.0.0b7 → ommx_python_mip_adapter-3.0.0b8}/ommx_python_mip_adapter.egg-info/requires.txt +1 -1
  6. {ommx_python_mip_adapter-3.0.0b7 → ommx_python_mip_adapter-3.0.0b8}/pyproject.toml +2 -2
  7. {ommx_python_mip_adapter-3.0.0b7 → ommx_python_mip_adapter-3.0.0b8}/tests/test_adapter.py +15 -0
  8. {ommx_python_mip_adapter-3.0.0b7 → ommx_python_mip_adapter-3.0.0b8}/tests/test_constant_constraint.py +4 -1
  9. {ommx_python_mip_adapter-3.0.0b7 → ommx_python_mip_adapter-3.0.0b8}/tests/test_model_to_instance.py +5 -5
  10. ommx_python_mip_adapter-3.0.0b8/tests/test_unknown_constraint_family.py +24 -0
  11. {ommx_python_mip_adapter-3.0.0b7 → ommx_python_mip_adapter-3.0.0b8}/README.md +0 -0
  12. {ommx_python_mip_adapter-3.0.0b7 → ommx_python_mip_adapter-3.0.0b8}/ommx_python_mip_adapter/__init__.py +0 -0
  13. {ommx_python_mip_adapter-3.0.0b7 → ommx_python_mip_adapter-3.0.0b8}/ommx_python_mip_adapter/exception.py +0 -0
  14. {ommx_python_mip_adapter-3.0.0b7 → ommx_python_mip_adapter-3.0.0b8}/ommx_python_mip_adapter/python_mip_to_ommx.py +0 -0
  15. {ommx_python_mip_adapter-3.0.0b7 → ommx_python_mip_adapter-3.0.0b8}/ommx_python_mip_adapter.egg-info/dependency_links.txt +0 -0
  16. {ommx_python_mip_adapter-3.0.0b7 → ommx_python_mip_adapter-3.0.0b8}/ommx_python_mip_adapter.egg-info/top_level.txt +0 -0
  17. {ommx_python_mip_adapter-3.0.0b7 → ommx_python_mip_adapter-3.0.0b8}/setup.cfg +0 -0
  18. {ommx_python_mip_adapter-3.0.0b7 → ommx_python_mip_adapter-3.0.0b8}/tests/test_integration.py +0 -0
  19. {ommx_python_mip_adapter-3.0.0b7 → ommx_python_mip_adapter-3.0.0b8}/tests/test_tracing.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ommx_python_mip_adapter
3
- Version: 3.0.0b7
3
+ Version: 3.0.0b8
4
4
  Summary: An adapter for the Python-MIP from/to OMMX.
5
5
  Author-email: "Jij Inc." <info@j-ij.com>
6
6
  Project-URL: Repository, https://github.com/Jij-Inc/ommx
@@ -15,7 +15,7 @@ Classifier: License :: OSI Approved :: Apache Software License
15
15
  Classifier: License :: OSI Approved :: MIT License
16
16
  Requires-Python: >=3.10
17
17
  Description-Content-Type: text/markdown
18
- Requires-Dist: ommx<4.0.0,>=3.0.0b7
18
+ Requires-Dist: ommx<4.0.0,>=3.0.0b8
19
19
  Requires-Dist: mip<2.0.0,>=1.17.0
20
20
  Requires-Dist: cffi<2.0.0,>=1.15.0
21
21
 
@@ -14,6 +14,7 @@ from ommx.adapter import (
14
14
  NoSolutionReturned,
15
15
  )
16
16
  from ommx import (
17
+ ConstraintID,
17
18
  Equality,
18
19
  Function,
19
20
  Instance,
@@ -334,14 +335,10 @@ class OMMXPythonMIPAdapter(SolverAdapter):
334
335
  # Duals certify the active formulation and need an explicit mapping
335
336
  # before they can be attached to projected output semantics.
336
337
  if self.instance.output_objective is None:
337
- dual_variables = {}
338
- for constraint in data.constrs:
339
- pi = constraint.pi
340
- if pi is not None:
341
- id = int(constraint.name)
342
- dual_variables[id] = pi
343
- for constraint_id, dual_value in dual_variables.items():
344
- solution.set_dual_variable(constraint_id, dual_value)
338
+ for constraint_id, name in self._constraint_names.items():
339
+ constraint = data.constr_by_name(name)
340
+ if constraint is not None and constraint.pi is not None:
341
+ solution.set_dual_variable(constraint_id, float(constraint.pi))
345
342
 
346
343
  if data.status == mip.OptimizationStatus.OPTIMAL:
347
344
  solution.optimality = self.instance.map_active_optimality(
@@ -473,7 +470,13 @@ class OMMXPythonMIPAdapter(SolverAdapter):
473
470
  self.model.objective = self._as_lin_expr(self.instance.objective)
474
471
 
475
472
  def _set_constraints(self):
476
- for cid, constraint in self.instance.constraints.items():
473
+ self._constraint_names: dict[ConstraintID, str] = {}
474
+ for cid in self.instance.constraints:
475
+ if not isinstance(cid, ConstraintID):
476
+ raise OMMXPythonMIPAdapterError(
477
+ f"Unsupported constraint family: {type(cid).__name__} (id: {cid})"
478
+ )
479
+ constraint = self.instance.constraints[cid]
477
480
  lin_expr = self._as_lin_expr(constraint.function)
478
481
  if constraint.equality == Equality.EqualToZero:
479
482
  constr_expr = lin_expr == 0
@@ -484,4 +487,6 @@ class OMMXPythonMIPAdapter(SolverAdapter):
484
487
  f"Not supported constraint equality: "
485
488
  f"id: {cid}, equality: {constraint.equality}"
486
489
  )
487
- self.model.add_constr(constr_expr, name=str(cid))
490
+ name = str(cid)
491
+ self.model.add_constr(constr_expr, name=name)
492
+ self._constraint_names[cid] = name
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ommx_python_mip_adapter
3
- Version: 3.0.0b7
3
+ Version: 3.0.0b8
4
4
  Summary: An adapter for the Python-MIP from/to OMMX.
5
5
  Author-email: "Jij Inc." <info@j-ij.com>
6
6
  Project-URL: Repository, https://github.com/Jij-Inc/ommx
@@ -15,7 +15,7 @@ Classifier: License :: OSI Approved :: Apache Software License
15
15
  Classifier: License :: OSI Approved :: MIT License
16
16
  Requires-Python: >=3.10
17
17
  Description-Content-Type: text/markdown
18
- Requires-Dist: ommx<4.0.0,>=3.0.0b7
18
+ Requires-Dist: ommx<4.0.0,>=3.0.0b8
19
19
  Requires-Dist: mip<2.0.0,>=1.17.0
20
20
  Requires-Dist: cffi<2.0.0,>=1.15.0
21
21
 
@@ -13,4 +13,5 @@ tests/test_adapter.py
13
13
  tests/test_constant_constraint.py
14
14
  tests/test_integration.py
15
15
  tests/test_model_to_instance.py
16
- tests/test_tracing.py
16
+ tests/test_tracing.py
17
+ tests/test_unknown_constraint_family.py
@@ -1,3 +1,3 @@
1
- ommx<4.0.0,>=3.0.0b7
1
+ ommx<4.0.0,>=3.0.0b8
2
2
  mip<2.0.0,>=1.17.0
3
3
  cffi<2.0.0,>=1.15.0
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "ommx_python_mip_adapter"
7
- version = "3.0.0b7"
7
+ version = "3.0.0b8"
8
8
 
9
9
  description = "An adapter for the Python-MIP from/to OMMX."
10
10
  authors = [{ name = "Jij Inc.", email = "info@j-ij.com" }]
@@ -23,7 +23,7 @@ classifiers = [
23
23
  ]
24
24
 
25
25
  dependencies = [
26
- "ommx >= 3.0.0b7, < 4.0.0",
26
+ "ommx >= 3.0.0b8, < 4.0.0",
27
27
  "mip >= 1.17.0, < 2.0.0",
28
28
  "cffi >= 1.15.0, < 2.0.0",
29
29
  ]
@@ -226,3 +226,18 @@ def test_recommended_preparation_reaches_the_python_mip_input_class() -> None:
226
226
  assert instance.prepare(input_class, policy) is None
227
227
  assert input_class.contains(instance)
228
228
  assert OMMXPythonMIPAdapter.check_applicability(instance).is_member
229
+
230
+
231
+ def test_dual_mapping_preserves_typed_sparse_constraint_ids():
232
+ x = DecisionVariable.continuous(0, lower=0, upper=10)
233
+ instance = Instance.from_components(
234
+ decision_variables=[x],
235
+ objective=x,
236
+ sense=Sense.Maximize,
237
+ constraints={42: x <= 2},
238
+ )
239
+ adapter = OMMXPythonMIPAdapter(instance)
240
+ model = adapter.solver_input
241
+ model.optimize()
242
+ solution = adapter.decode(model)
243
+ assert solution.get_dual_variable(42) == pytest.approx(1)
@@ -1,3 +1,4 @@
1
+ from ommx import ConstraintID
1
2
  from ommx_python_mip_adapter import OMMXPythonMIPAdapter
2
3
  from ommx import DecisionVariable, Instance, Linear, Sense
3
4
  from ommx.adapter import InfeasibleDetected
@@ -24,7 +25,9 @@ def test_constant_constraint_feasible():
24
25
  assert solution.state.entries == {0: 1.0}
25
26
  assert solution.objective == 1.0
26
27
 
27
- assert len(solution.constraints) == 2
28
+ assert (
29
+ len({cid for cid in solution.constraints if isinstance(cid, ConstraintID)}) == 2
30
+ )
28
31
 
29
32
 
30
33
  @pytest.mark.skip(
@@ -80,7 +80,7 @@ def test_milp():
80
80
  # Check the constraints
81
81
  assert len(ommx_instance.constraints) == 3
82
82
 
83
- constraint1 = ommx_instance.get_constraint_by_id(0)
83
+ constraint1 = ommx_instance.constraints.of_kind("regular")[0]
84
84
  assert constraint1.equality == Equality.EqualToZero
85
85
  assert constraint1.function.degree() == 1
86
86
  assert constraint1.function.constant_term == 6
@@ -89,7 +89,7 @@ def test_milp():
89
89
  assert constraint1_terms[0] == 4
90
90
  assert constraint1_terms[1] == -5
91
91
 
92
- constraint2 = ommx_instance.get_constraint_by_id(1)
92
+ constraint2 = ommx_instance.constraints.of_kind("regular")[1]
93
93
  assert constraint2.equality == Equality.LessThanOrEqualToZero
94
94
  assert constraint2.function.degree() == 1
95
95
  assert constraint2.function.constant_term == -9
@@ -98,7 +98,7 @@ def test_milp():
98
98
  assert constraint2_terms[0] == -7
99
99
  assert constraint2_terms[2] == 8
100
100
 
101
- constraint3 = ommx_instance.get_constraint_by_id(2)
101
+ constraint3 = ommx_instance.constraints.of_kind("regular")[2]
102
102
  assert constraint3.equality == Equality.LessThanOrEqualToZero
103
103
  assert constraint3.function.degree() == 1
104
104
  assert constraint3.function.constant_term == -12
@@ -162,7 +162,7 @@ def test_no_objective_model():
162
162
  # Check the constraints
163
163
  assert len(ommx_instance.constraints) == 2
164
164
 
165
- constraint1 = ommx_instance.get_constraint_by_id(0)
165
+ constraint1 = ommx_instance.constraints.of_kind("regular")[0]
166
166
  assert constraint1.equality == Equality.EqualToZero
167
167
  assert constraint1.function.degree() == 1
168
168
  assert constraint1.function.constant_term == -5
@@ -171,7 +171,7 @@ def test_no_objective_model():
171
171
  assert constraint1_terms[0] == 1
172
172
  assert constraint1_terms[1] == 2
173
173
 
174
- constraint2 = ommx_instance.get_constraint_by_id(1)
174
+ constraint2 = ommx_instance.constraints.of_kind("regular")[1]
175
175
  assert constraint2.equality == Equality.EqualToZero
176
176
  assert constraint2.function.degree() == 1
177
177
  assert constraint2.function.constant_term == -10
@@ -0,0 +1,24 @@
1
+ """An adapter must reject future families even at its translation boundary."""
2
+
3
+ from types import SimpleNamespace
4
+ from typing import cast
5
+
6
+ import pytest
7
+ from ommx import ConstraintIDBase, Instance, OneHotConstraintID
8
+ from ommx_python_mip_adapter import OMMXPythonMIPAdapter, OMMXPythonMIPAdapterError
9
+
10
+
11
+ class FutureConstraintID(ConstraintIDBase):
12
+ pass
13
+
14
+
15
+ @pytest.mark.parametrize("key", [FutureConstraintID(), OneHotConstraintID(1)])
16
+ def test_unknown_family_is_rejected_instead_of_omitted(key: ConstraintIDBase):
17
+ # Exercise the consumer boundary independently of SDK applicability checks:
18
+ # a newer SDK may recognize a family that this adapter cannot translate.
19
+ adapter = object.__new__(OMMXPythonMIPAdapter)
20
+ adapter.instance = cast(Instance, SimpleNamespace(constraints=[key]))
21
+ with pytest.raises(
22
+ OMMXPythonMIPAdapterError, match="Unsupported constraint family"
23
+ ):
24
+ adapter._set_constraints()