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

Commit

Permalink
MIPVariable: Make it a subclass of FiniteFamily
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Koeppe committed Jan 3, 2023
1 parent 96366a6 commit 3beb977
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
7 changes: 4 additions & 3 deletions src/sage/numerical/mip.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ cdef extern from *:
cdef int REAL = -1
cdef int INTEGER = 0

from sage.sets.family cimport FiniteFamily
from sage.structure.sage_object cimport SageObject
from sage.numerical.backends.generic_backend cimport GenericBackend


cdef class MIPVariable


Expand All @@ -26,10 +29,8 @@ cdef class MixedIntegerLinearProgram(SageObject):
cpdef sum(self, L)


cdef class MIPVariable(SageObject):
cdef class MIPVariable(FiniteFamily):
cdef MixedIntegerLinearProgram _p
cdef dict _dict
cdef bint _dynamic_indices
cdef int _vtype
cdef str _name
cdef object _lower_bound
Expand Down
35 changes: 17 additions & 18 deletions src/sage/numerical/mip.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3131,7 +3131,7 @@ class MIPSolverException(RuntimeError):
pass


cdef class MIPVariable(SageObject):
cdef class MIPVariable(FiniteFamily):
r"""
``MIPVariable`` is a variable used by the class
``MixedIntegerLinearProgram``.
Expand Down Expand Up @@ -3179,17 +3179,16 @@ cdef class MIPVariable(SageObject):
sage: p.new_variable(nonnegative=True)
MIPVariable with 0 real components, >= 0
"""
self._dict = {}
super().__init__({})
self._p = mip
self._vtype = vtype
self._lower_bound = lower_bound
self._upper_bound = upper_bound
self._name = name
self._dynamic_indices = True
if indices is not None:
for i in indices:
self[i] # creates component
self._dynamic_indices = False
self._keys = indices

def __copy__(self):
r"""
Expand Down Expand Up @@ -3291,9 +3290,9 @@ cdef class MIPVariable(SageObject):
"""
cdef int j
if i in self._dict:
return self._dict[i]
if not self._dynamic_indices:
if i in self._dictionary:
return self._dictionary[i]
if self._keys is not None:
raise IndexError("{} does not index a component of {}".format(i, self))
zero = self._p._backend.zero()
name = self._name + "[" + str(i) + "]" if self._name else None
Expand All @@ -3308,7 +3307,7 @@ cdef class MIPVariable(SageObject):
name=name)
v = self._p.linear_functions_parent()({j : 1})
self._p._variables[v] = j
self._dict[i] = v
self._dictionary[i] = v
return v

def copy_for_mip(self, mip):
Expand Down Expand Up @@ -3354,8 +3353,8 @@ cdef class MIPVariable(SageObject):
"""
cdef MIPVariable cp = type(self)(mip, self._vtype, self._name,
self._lower_bound, self._upper_bound)
cp._dict = copy(self._dict)
cp._dynamic_indices = self._dynamic_indices
cp._dictionary = copy(self._dictionary)
cp._keys = self._keys
return cp

def set_min(self, min):
Expand Down Expand Up @@ -3394,7 +3393,7 @@ cdef class MIPVariable(SageObject):
"""
self._lower_bound = min
for v in self._dict.values():
for v in self._dictionary.values():
self._p.set_min(v,min)

def set_max(self, max):
Expand Down Expand Up @@ -3430,7 +3429,7 @@ cdef class MIPVariable(SageObject):
True
"""
self._upper_bound = max
for v in self._dict.values():
for v in self._dictionary.values():
self._p.set_max(v,max)

def _repr_(self):
Expand Down Expand Up @@ -3463,9 +3462,9 @@ cdef class MIPVariable(SageObject):
"""
s = 'MIPVariable{0} with {1} {2} component{3}'.format(
" " + self._name if self._name else "",
len(self._dict),
len(self._dictionary),
{0:"binary", -1:"real", 1:"integer"}[self._vtype],
"s" if len(self._dict) != 1 else "")
"s" if len(self._dictionary) != 1 else "")
if (self._vtype != 0) and (self._lower_bound is not None):
s += ', >= {0}'.format(self._lower_bound)
if (self._vtype != 0) and (self._upper_bound is not None):
Expand All @@ -3484,11 +3483,11 @@ cdef class MIPVariable(SageObject):
sage: sorted(v.keys())
[0, 1]
"""
return self._dict.keys()
return self._dictionary.keys()

def items(self):
r"""
Return the pairs (keys,value) contained in the dictionary.
Return the pairs (keys, value) contained in the dictionary.
EXAMPLES::
Expand All @@ -3498,7 +3497,7 @@ cdef class MIPVariable(SageObject):
sage: sorted(v.items())
[(0, x_0), (1, x_1)]
"""
return self._dict.items()
return self._dictionary.items()

def values(self):
r"""
Expand All @@ -3512,7 +3511,7 @@ cdef class MIPVariable(SageObject):
sage: sorted(v.values(), key=str)
[x_0, x_1]
"""
return self._dict.values()
return self._dictionary.values()

def mip(self):
r"""
Expand Down

0 comments on commit 3beb977

Please sign in to comment.