Ticket #11607: trac_11607_read_constraints_from_lp.patch

File trac_11607_read_constraints_from_lp.patch, 3.1 KB (added by john_perry, 10 years ago)
  • sage/numerical/mip.pxd

    # HG changeset patch
    # User John Perry <john.perry@usm.edu>
    # Date 1314070284 25200
    # Node ID c2f79876b38121da9aa5eff961fc97db2e8c40fb
    # Parent  3b02e04bca54660e3f4b5efb6858e99cd20ceceb
    Trac 11607: Read constraints from linear program
    
    diff -r 3b02e04bca54 -r c2f79876b381 sage/numerical/mip.pxd
    a b  
    1818    cdef int __BINARY
    1919    cdef int __REAL
    2020    cdef int __INTEGER
     21    cpdef int number_of_constraints(self)
    2122
    2223cdef class MIPVariable:
    2324    cdef MixedIntegerLinearProgram _p
  • sage/numerical/mip.pyx

    diff -r 3b02e04bca54 -r c2f79876b381 sage/numerical/mip.pyx
    a b  
    372372        self._mipvariables.append(v)
    373373        return v
    374374
     375    cpdef int number_of_constraints(self):
     376      r"""
     377      Returns the number of constraints assigned so far.
     378     
     379      EXAMPLE::
     380            sage: p = MixedIntegerLinearProgram()
     381            sage: p.add_constraint(p[0] - p[2], min = 1, max = 4)
     382            sage: p.add_constraint(p[0] - 2*p[1], min = 1)
     383            sage: p.number_of_constraints()
     384            2
     385      """
     386      return self._backend.nrows()
     387   
     388    def constraints(self, indices = None):
     389        r"""
     390        Returns a list of constraints, as 3-tuples.
     391        If `n` is `None`, this returns all constraints.
     392        If `n` is an integer, this is similar (but not equivalent) to ``self.constraint(n)``.
     393        If `n` is a list of integers, this is equivalent to ``[self.constraint
     394        The first entry in each tuple is the lower bound;
     395        the second entry is a pair ``(indices, coeffs)``
     396        (see ``row`` for details); and
     397        the third entry is the upper bound.
     398       
     399        EXAMPLE::
     400            sage: p = MixedIntegerLinearProgram()
     401            sage: p.add_constraint(p[0] - p[2], min = 1, max = 4)
     402            sage: p.add_constraint(p[0] - 2*p[1], min = 1)
     403            sage: p.constraints()
     404            [(1.0, ([1, 0], [-1.0, 1.0]), 4.0), (1.0, ([2, 0], [-2.0, 1.0]), None)]
     405            sage: p.constraints(0)
     406            [(1.0, ([1, 0], [-1.0, 1.0]), 4.0)]
     407            sage: p.constraints([1])
     408            [(1.0, ([2, 0], [-2.0, 1.0]), None)]
     409        """
     410        cdef int i
     411        cdef str s
     412        cdef GenericBackend b = self._backend
     413       
     414        from sage.rings.integer import Integer as Integer
     415       
     416        result = list()
     417        if indices == None:
     418          indices = xrange(b.nrows())
     419        elif isinstance(indices, int) or isinstance(indices, Integer):
     420            indices = [indices]
     421        elif not isinstance(indices, list):
     422          raise TypeError, "constraints() requires a list of integers, though it will accommodate None or an integer."
     423        for i in indices:
     424          lb, ub = b.row_bounds(i)
     425          result.append((lb, b.row(i), ub))
     426       
     427        return result
     428
    375429    def show(self):
    376430        r"""
    377431        Displays the ``MixedIntegerLinearProgram`` in a human-readable