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

Commit aa5fa1d

Browse files
committed
Make _repr_ of MILP more informative
1 parent 037272c commit aa5fa1d

File tree

4 files changed

+54
-20
lines changed

4 files changed

+54
-20
lines changed

src/sage/cpython/wrapperdescr.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def wrapperdescr_call(slotwrapper, self, *args, **kwds):
6767
63/5
6868
sage: from sage.numerical.mip import MixedIntegerLinearProgram
6969
sage: wrapperdescr_call(type.__call__, MixedIntegerLinearProgram, maximization=False)
70-
Mixed Integer Program ( minimization, 0 variables, 0 constraints )
70+
Mixed Integer Program (no objective, 0 variables, 0 constraints)
7171
7272
TESTS::
7373

src/sage/geometry/polyhedron/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ def to_linear_program(self, solver=None, return_variable=False, base_ring=None):
11451145
11461146
sage: p = polytopes.cube()
11471147
sage: p.to_linear_program()
1148-
Mixed Integer Program ( maximization, 3 variables, 6 constraints )
1148+
Linear Program (no objective, 3 variables, 6 constraints)
11491149
sage: lp, x = p.to_linear_program(return_variable=True)
11501150
sage: lp.set_objective(2*x[0] + 1*x[1] + 39*x[2])
11511151
sage: lp.solve()

src/sage/numerical/backends/cvxopt_backend.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ cdef class CVXOPTBackend(GenericBackend):
3939
:trac:`20332`::
4040
4141
sage: p
42-
Mixed Integer Program ( maximization, 0 variables, 0 constraints )
42+
Mixed Integer Program (no objective, 0 variables, 0 constraints)
4343
4444
General backend testsuite::
4545

src/sage/numerical/mip.pyx

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -528,28 +528,62 @@ cdef class MixedIntegerLinearProgram(SageObject):
528528
linear_function = __call__
529529

530530
def _repr_(self):
531-
r"""
532-
Returns a short description of the ``MixedIntegerLinearProgram``.
531+
r"""
532+
Return a short description of the ``MixedIntegerLinearProgram``.
533533
534-
EXAMPLES::
534+
EXAMPLES::
535535
536-
sage: p = MixedIntegerLinearProgram(solver='GLPK')
537-
sage: v = p.new_variable(nonnegative=True)
538-
sage: p.add_constraint(v[1] + v[2], max=2)
539-
sage: print(p)
540-
Mixed Integer Program ( maximization, 2 variables, 1 constraints )
541-
"""
542-
cdef GenericBackend b = self._backend
536+
sage: p = MixedIntegerLinearProgram(solver='GLPK')
537+
sage: v = p.new_variable(binary=True)
538+
sage: p.add_constraint(v[1] + v[2], max=1)
539+
sage: p
540+
Boolean Program (no objective, 2 variables, 1 constraint)
541+
"""
542+
cdef GenericBackend b = self._backend
543543

544-
return ("Mixed Integer Program "+
544+
cdef int nvars = b.ncols()
545+
cdef int i
546+
547+
cdef int have_int = 0, have_bool = 0, have_cont = 0
548+
for i in range(nvars):
549+
if b.is_variable_binary(i):
550+
have_bool = 1
551+
elif b.is_variable_integer(i):
552+
have_int = 1
553+
else:
554+
have_cont = 1
555+
556+
if have_cont:
557+
if have_int or have_bool:
558+
kind = "Mixed Integer Program"
559+
else:
560+
kind = "Linear Program"
561+
elif have_int:
562+
kind = "Integer Program"
563+
elif have_bool:
564+
kind = "Boolean Program"
565+
else:
566+
# We have no variables...
567+
kind = "Mixed Integer Program"
568+
569+
if (all(b.objective_coefficient(i) == 0 for i in range(nvars))
570+
and b.objective_constant_term() == 0):
571+
minmax = "no objective"
572+
elif b.is_maximization():
573+
minmax = "maximization"
574+
else:
575+
minmax = "minimization"
545576

546-
( "\"" +self._backend.problem_name()+ "\""
547-
if (str(self._backend.problem_name()) != "") else "")+
577+
def plural(num, noun):
578+
if num != 1:
579+
noun = noun + "s"
580+
return f"{num} {noun}"
548581

549-
" ( " + ("maximization" if b.is_maximization() else "minimization" ) +
582+
name = b.problem_name()
583+
if name:
584+
kind += f' "{name}"'
550585

551-
", " + str(b.ncols()) + " variables, " +
552-
str(b.nrows()) + " constraints )")
586+
return f"{kind} ({minmax}, {plural(b.ncols(), 'variable')}, {plural(b.nrows(), 'constraint')})"
553587

554588
def __copy__(self):
555589
r"""
@@ -687,7 +721,7 @@ cdef class MixedIntegerLinearProgram(SageObject):
687721
sage: p = MixedIntegerLinearProgram(solver='GLPK')
688722
sage: p.set_problem_name("Test program")
689723
sage: p
690-
Mixed Integer Program "Test program" ( maximization, 0 variables, 0 constraints )
724+
Mixed Integer Program "Test program" (no objective, 0 variables, 0 constraints)
691725
"""
692726
self._backend.problem_name(name)
693727

0 commit comments

Comments
 (0)