Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit 5777ef9

Browse files
committed
Allow objective constant terms for InteractiveLPProblem.
1 parent 62ddb03 commit 5777ef9

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

src/sage/numerical/interactive_simplex_method.py

+35-3
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,9 @@ class InteractiveLPProblem(SageObject):
592592
- ``is_primal`` -- (default: ``True``) whether this problem is primal or
593593
dual: each problem is of course dual to its own dual, this flag is mostly
594594
for internal use and affects default variable names only
595+
596+
- ``objective_constant_term`` -- (default: 0) a constant term of the
597+
objective added to ``c * x`` when computing optimal value
595598
596599
EXAMPLES:
597600
@@ -632,7 +635,7 @@ class InteractiveLPProblem(SageObject):
632635

633636
def __init__(self, A, b, c, x="x",
634637
constraint_type="<=", variable_type="", problem_type="max",
635-
base_ring=None, is_primal=True):
638+
base_ring=None, is_primal=True, objective_constant_term=0):
636639
r"""
637640
See :class:`InteractiveLPProblem` for documentation.
638641
@@ -671,6 +674,7 @@ def __init__(self, A, b, c, x="x",
671674
R = PolynomialRing(base_ring, x, order="neglex")
672675
x = vector(R, R.gens()) # All variables as a vector
673676
self._Abcx = A, b, c, x
677+
self._constant_term = objective_constant_term
674678

675679
if constraint_type in ["<=", ">=", "=="]:
676680
constraint_type = (constraint_type, ) * m
@@ -846,11 +850,12 @@ def _solve(self):
846850
M, S = -Infinity, None
847851
else:
848852
M, S = min((c * vector(R, v), v) for v in F.vertices())
849-
if self._is_negative:
850-
M = - M
851853
if S is not None:
852854
S = vector(R, S)
853855
S.set_immutable()
856+
M += self._constant_term
857+
if self._is_negative:
858+
M = - M
854859
return S, M
855860

856861
def Abcx(self):
@@ -1202,6 +1207,33 @@ def objective_coefficients(self):
12021207
(10, 5)
12031208
"""
12041209
return self._Abcx[2]
1210+
1211+
def objective_constant_term(self):
1212+
r"""
1213+
Return the constant term of the objective.
1214+
1215+
OUTPUT:
1216+
1217+
- a number
1218+
1219+
EXAMPLES::
1220+
1221+
sage: A = ([1, 1], [3, 1])
1222+
sage: b = (1000, 1500)
1223+
sage: c = (10, 5)
1224+
sage: P = InteractiveLPProblem(A, b, c, ["C", "B"], variable_type=">=")
1225+
sage: P.objective_constant_term()
1226+
0
1227+
sage: P.optimal_value()
1228+
6250
1229+
sage: P = InteractiveLPProblem(A, b, c, ["C", "B"],
1230+
....: variable_type=">=", objective_constant_term=-1250)
1231+
sage: P.objective_constant_term()
1232+
-1250
1233+
sage: P.optimal_value()
1234+
5000
1235+
"""
1236+
return self._constant_term
12051237

12061238
def optimal_solution(self):
12071239
r"""

0 commit comments

Comments
 (0)