Ticket #12884: trac_12884.patch

File trac_12884.patch, 6.6 KB (added by john_perry, 7 years ago)
  • sage/numerical/backends/cplex_backend.pyx

    # HG changeset patch
    # User John Perry <john.perry@usm.edu>
    # Date 1335469934 18000
    # Node ID 406c96a86d634a99d325874ab736f9e818b60125
    # Parent  8250db283716ccbf54a69a453d5dbb9c1004cef6
    fixing problems related to removing constraints
    
    diff --git a/sage/numerical/backends/cplex_backend.pyx b/sage/numerical/backends/cplex_backend.pyx
    a b  
    2020cdef class CPLEXBackend(GenericBackend):
    2121
    2222    def __cinit__(self, maximization = True):
     23        """
     24        Constructor
     25
     26        EXAMPLE::
     27
     28        sage: p = MixedIntegerLinearProgram(solver="CPLEX")                 # optional - CPLEX
     29        """
    2330
    2431        cdef int status
    2532        self.env = CPXopenCPLEX (&status)
  • sage/numerical/backends/generic_backend.pyx

    diff --git a/sage/numerical/backends/generic_backend.pyx b/sage/numerical/backends/generic_backend.pyx
    a b  
    254254        INPUT::
    255255
    256256        - ``i`` -- index of the constraint to remove.
     257
     258        EXAMPLE::
     259
     260            sage: from sage.numerical.backends.generic_backend import get_solver
     261            sage: p = get_solver(solver = "Nonexistent_LP_solver")  # optional - Nonexistent_LP_solver
     262            sage: p.add_constraint(p[0] + p[1], max = 10)           # optional - Nonexistent_LP_solver
     263            sage: p.remove_constraint(0)                            # optional - Nonexistent_LP_solver
    257264        """
    258265        raise NotImplementedError()
    259266
     
    264271        INPUT:
    265272
    266273        - ``constraints`` -- an iterable containing the indices of the rows to remove.
     274
     275        EXAMPLE::
     276
     277            sage: from sage.numerical.backends.generic_backend import get_solver
     278            sage: p = get_solver(solver = "Nonexistent_LP_solver")  # optional - Nonexistent_LP_solver
     279            sage: p.add_constraint(p[0] + p[1], max = 10)           # optional - Nonexistent_LP_solver
     280            sage: p.remove_constraints([0])                         # optional - Nonexistent_LP_solver
    267281        """
    268282        if type(constraints) == int: self.remove_constraint(constraints)
    269283
  • sage/numerical/backends/glpk_backend.pxd

    diff --git a/sage/numerical/backends/glpk_backend.pxd b/sage/numerical/backends/glpk_backend.pxd
    a b  
    7272     int glp_simplex(c_glp_prob *, c_glp_smcp *)
    7373     int glp_intopt(c_glp_prob *, c_glp_iocp *)
    7474     int lpx_intopt(c_glp_prob *)
     75     void glp_std_basis(c_glp_prob *)
    7576     void glp_delete_prob(c_glp_prob *)
    7677     double glp_get_col_prim(c_glp_prob *, int)
    7778     double glp_get_obj_val(c_glp_prob *)
  • sage/numerical/backends/glpk_backend.pyx

    diff --git a/sage/numerical/backends/glpk_backend.pyx b/sage/numerical/backends/glpk_backend.pyx
    a b  
    407407
    408408        rows[1] = i + 1
    409409        glp_del_rows(self.lp, 1, rows)
     410        glp_std_basis(self.lp)
    410411
    411412    cpdef remove_constraints(self, constraints):
    412413        r"""
     
    457458
    458459        glp_del_rows(self.lp, m, rows)
    459460        sage_free(rows)
     461        glp_std_basis(self.lp)
    460462
    461463    cpdef add_linear_constraint(self, coefficients, lower_bound, upper_bound, name=None):
    462464        """
  • sage/numerical/mip.pxd

    diff --git a/sage/numerical/mip.pxd b/sage/numerical/mip.pxd
    a b  
    2121    cdef int __INTEGER
    2222    cpdef int number_of_constraints(self)
    2323    cdef int _check_redundant
    24     cdef set _constraints
     24    cdef list _constraints
    2525
    2626cdef class MIPVariable:
    2727    cdef MixedIntegerLinearProgram _p
  • sage/numerical/mip.pyx

    diff --git a/sage/numerical/mip.pyx b/sage/numerical/mip.pyx
    a b  
    252252        # Check for redundant constraints
    253253        self._check_redundant = check_redundant
    254254        if check_redundant:
    255             self._constraints = set()
     255            self._constraints = list()
    256256
    257257    def __repr__(self):
    258258         r"""
     
    281281    def __copy__(self):
    282282        r"""
    283283        Returns a copy of the current ``MixedIntegerLinearProgram`` instance.
     284
     285        EXAMPLE::
     286
     287        sage: p = MixedIntegerLinearProgram()
     288        sage: p.add_constraint(p[0] + p[1], max = 10)
     289        sage: q = copy(p)
     290        sage: q.number_of_constraints()
     291        1
    284292        """
    285293        cdef MixedIntegerLinearProgram p = MixedIntegerLinearProgram(solver="GLPK")
    286294
     
    10461054              if (tuple(C),min,max) in self._constraints:
    10471055                return None
    10481056              else:
    1049                 self._constraints.add((tuple(C),min,max))
     1057                self._constraints.append((tuple(C),min,max))
    10501058            else:
    10511059              C = [(v,coeff) for (v,coeff) in f.iteritems() if v != -1]
    10521060
     
    11021110            sage: p.number_of_constraints()
    11031111            2
    11041112        """
     1113        if self._check_redundant: self._constraints.pop(i)
    11051114        self._backend.remove_constraint(i)
    11061115
    11071116    def remove_constraints(self, constraints):
     
    11361145            ...
    11371146            sage: p.number_of_constraints()
    11381147            1
     1148
     1149        When checking for redundant constraints, make sure you remove only
     1150        the constraints that were actually added. Problems could arise if
     1151        you have a function that builds lps non-interactively, but it fails
     1152        to check whether adding a constraint actually increases the number of
     1153        constraints. The function might later try to remove constraints that
     1154        are not actually there::
     1155
     1156            sage: p = MixedIntegerLinearProgram(check_redundant=True)
     1157            sage: x, y = p[0], p[1]
     1158            sage: p.add_constraint(x + y, max = 10)
     1159            sage: for each in xrange(10): p.add_constraint(x - y, max = 10)
     1160            sage: p.add_constraint(x, max = 4)
     1161            sage: p.number_of_constraints()
     1162            3
     1163            sage: p.remove_constraints(range(1,9))
     1164            Traceback (most recent call last):
     1165            ...
     1166            IndexError: pop index out of range
     1167            sage: p.remove_constraint(1)
     1168            sage: p.number_of_constraints()
     1169            2
     1170
     1171        We should now be able to add the old constraint back in::
     1172
     1173            sage: for each in xrange(10): p.add_constraint(x - y, max = 10)
     1174            sage: p.number_of_constraints()
     1175            3
    11391176        """
    1140         self._backend.remove_constraints(constraints)
     1177        if self._check_redundant:
     1178          for i in sorted(constraints,reverse=True):
     1179            self._constraints.pop(i)
     1180        self._backend.remove_constraints(constraints)
    11411181
    11421182    def set_binary(self, ee):
    11431183        r"""