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

Commit db6571e

Browse files
author
Matthias Koeppe
committed
MixedIntegerLinearProgram.polyhedron: Store backend variable names as _names attribute
1 parent 12be2d9 commit db6571e

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

src/sage/geometry/abc.pyx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,29 @@ class Polyhedron:
7979
"""
8080

8181
pass
82+
83+
84+
class PolyhedronFace:
85+
r"""
86+
Abstract base class for :class:`~sage.geometry.polyhedron.base.Polyhedron_base`
87+
88+
This class is defined for the purpose of ``isinstance`` tests. It should not be
89+
instantiated.
90+
91+
EXAMPLES::
92+
93+
sage: import sage.geometry.abc
94+
sage: P = polytopes.cube() # optional - sage.geometry.polyhedron
95+
sage: isinstance(P, sage.geometry.abc.Polyhedron) # optional - sage.geometry.polyhedron
96+
True
97+
98+
By design, there is a unique direct subclass::
99+
100+
sage: sage.geometry.abc.Polyhedron.__subclasses__() # optional - sage.geometry.polyhedron
101+
[<class 'sage.geometry.polyhedron.base0.Polyhedron_base0'>]
102+
103+
sage: len(sage.geometry.abc.Polyhedron.__subclasses__()) <= 1
104+
True
105+
"""
106+
107+
pass

src/sage/numerical/mip.pyx

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,8 +1031,9 @@ cdef class MixedIntegerLinearProgram(SageObject):
10311031
10321032
OUTPUT:
10331033
1034-
A :func:`Polyhedron` object whose `i`-th variable represents the `i`-th
1035-
variable of ``self``.
1034+
A :func:`Polyhedron` object whose `i`-th coordinate represents the `i`-th
1035+
backend variable of ``self``. The attribute ``_names`` contains the names of
1036+
the variables.
10361037
10371038
.. warning::
10381039
@@ -1060,6 +1061,8 @@ cdef class MixedIntegerLinearProgram(SageObject):
10601061
sage: p.add_constraint(0 <= 3*p['y'] + p['x'] <= 2)
10611062
sage: P = p.polyhedron(); P
10621063
A 2-dimensional polyhedron in RDF^2 defined as the convex hull of 4 vertices
1064+
sage: P._names
1065+
['x_0', 'x_1']
10631066
10641067
3-D Polyhedron::
10651068
@@ -1069,6 +1072,8 @@ cdef class MixedIntegerLinearProgram(SageObject):
10691072
sage: p.add_constraint(0 <= 2*p['z'] + p['x'] + 3*p['y'] <= 1)
10701073
sage: P = p.polyhedron(); P
10711074
A 3-dimensional polyhedron in RDF^3 defined as the convex hull of 8 vertices
1075+
sage: P._names
1076+
['x_0', 'x_1', 'x_2']
10721077
10731078
An empty polyhedron::
10741079
@@ -1079,13 +1084,17 @@ cdef class MixedIntegerLinearProgram(SageObject):
10791084
sage: p.add_constraint(2*v['z'] + v['x'] + 3*v['y'] >= 2)
10801085
sage: P = p.polyhedron(); P
10811086
The empty polyhedron in RDF^3
1087+
sage: P._names
1088+
['x_0', 'x_1', 'x_2']
10821089
10831090
An unbounded polyhedron::
10841091
10851092
sage: p = MixedIntegerLinearProgram(solver='GLPK')
10861093
sage: p.add_constraint(2*p['x'] + p['y'] - p['z'] <= 1)
10871094
sage: P = p.polyhedron(); P
10881095
A 3-dimensional polyhedron in RDF^3 defined as the convex hull of 1 vertex, 1 ray, 2 lines
1096+
sage: P._names
1097+
['x_0', 'x_1', 'x_2']
10891098
10901099
A square (see :trac:`14395`) ::
10911100
@@ -1097,6 +1106,8 @@ cdef class MixedIntegerLinearProgram(SageObject):
10971106
sage: p.add_constraint( y >= -1 )
10981107
sage: p.polyhedron()
10991108
A 2-dimensional polyhedron in RDF^2 defined as the convex hull of 4 vertices
1109+
sage: P._names
1110+
['x_0', 'x_1', 'x_2']
11001111
11011112
We can also use a backend that supports exact arithmetic::
11021113
@@ -1108,6 +1119,8 @@ cdef class MixedIntegerLinearProgram(SageObject):
11081119
sage: p.add_constraint( y >= -1 )
11091120
sage: p.polyhedron()
11101121
A 2-dimensional polyhedron in QQ^2 defined as the convex hull of 4 vertices
1122+
sage: P._names
1123+
['x_0', 'x_1', 'x_2']
11111124
11121125
TESTS:
11131126
@@ -1152,9 +1165,10 @@ cdef class MixedIntegerLinearProgram(SageObject):
11521165
linear_function.insert(0,ub)
11531166
inequalities.append(linear_function)
11541167

1155-
# Variable bounds
1168+
# Variable bounds and names
11561169
zero = [0] * nvar
1157-
for 0<= i < nvar:
1170+
names = []
1171+
for 0 <= i < nvar:
11581172
lb, ub = b.col_bounds(i)
11591173
# Fixed variable
11601174
if (not lb is None) and lb == ub:
@@ -1175,7 +1189,15 @@ cdef class MixedIntegerLinearProgram(SageObject):
11751189
linear_function[i] = -1
11761190
linear_function.insert(0,ub)
11771191
inequalities.append(linear_function)
1178-
return Polyhedron(ieqs = inequalities, eqns = equalities, **kwds)
1192+
name = b.col_name(i)
1193+
if not name:
1194+
# same default as used in "show"
1195+
name = str(self.linear_functions_parent()({i: 1}))
1196+
names.append(name)
1197+
1198+
polyhedron = Polyhedron(ieqs=inequalities, eqns=equalities, **kwds)
1199+
polyhedron._names = names
1200+
return polyhedron
11791201

11801202
def show(self):
11811203
r"""
@@ -1247,7 +1269,7 @@ cdef class MixedIntegerLinearProgram(SageObject):
12471269
# varid_name associates variables id to names
12481270
varid_name = {}
12491271
varid_explainer = {}
1250-
for 0<= i < b.ncols():
1272+
for 0 <= i < b.ncols():
12511273
s = b.col_name(i)
12521274
default_name = str(self.linear_functions_parent()({i: 1}))
12531275
if s and s != default_name:
@@ -1260,7 +1282,7 @@ cdef class MixedIntegerLinearProgram(SageObject):
12601282
print("Maximization:" if b.is_maximization() else "Minimization:")
12611283
print(" ", end=" ")
12621284
first = True
1263-
for 0<= i< b.ncols():
1285+
for 0 <= i< b.ncols():
12641286
c = b.objective_coefficient(i)
12651287
if c == 0:
12661288
continue

0 commit comments

Comments
 (0)