Ticket #11588: trac_11588.patch

File trac_11588.patch, 9.2 KB (added by ncohen, 10 years ago)
  • sage/numerical/backends/coin_backend.pxd

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1310576771 -7200
    # Node ID 841dcaf25aa29c02dafa371b90bdead0d7db9b0b
    # Parent  8532a2ad1e558cbc91ddaaa6b7cc79956dd1e8ba
    trac #11588 - Copying a Linear Program
    
    diff -r 8532a2ad1e55 -r 841dcaf25aa2 sage/numerical/backends/coin_backend.pxd
    a b  
    5252     c_CbcModel *new_c_CbcModel "new CbcModel" ()
    5353     void del_CbcModel "delete" (c_CbcModel *)
    5454
     55cdef extern from "../../local/include/coin/OsiSolverInterface.hpp":
     56     cdef cppclass OsiSolverInterface:
     57         pass
     58
    5559cdef extern from "../../local/include/coin/OsiCbcSolverInterface.hpp":
    56      ctypedef struct c_OsiCbcSolverInterface "OsiCbcSolverInterface":
     60     cdef cppclass c_OsiCbcSolverInterface "OsiCbcSolverInterface":
    5761         double getInfinity()
    5862         void loadProblem(c_CoinPackedMatrix, const_double_ptr, const_double_ptr, const_double_ptr, const_double_ptr, const_double_ptr)
    5963         void assignProblem(c_CoinPackedMatrix *, const_double_ptr, const_double_ptr, const_double_ptr, const_double_ptr, const_double_ptr)
     
    9397         void addCol (int numberElements, int *rows, double *elements, double collb, double colub, double obj)
    9498         void addRow (c_CoinPackedVector vec, double rowlb, double rowub)
    9599         c_CoinPackedMatrix * getMatrixByRow()
     100         c_OsiCbcSolverInterface * c_OsiCbcSolverInterface(OsiSolverInterface * solver)
    96101     c_OsiCbcSolverInterface *new_c_OsiCbcSolverInterface "new OsiCbcSolverInterface" ()
     102     c_OsiCbcSolverInterface * new2_c_OsiCbcSolverInterface "new OsiCbcSolverInterface" (OsiSolverInterface * solver)
    97103     void del_OsiCbcSolverInterface "delete" (c_OsiCbcSolverInterface *)
    98104
    99105cdef class CoinBackend(GenericBackend):
    100     cdef c_OsiCbcSolverInterface* si
     106    cdef c_OsiCbcSolverInterface * si
     107    cpdef CoinBackend copy(self)
  • sage/numerical/backends/coin_backend.pyx

    diff -r 8532a2ad1e55 -r 841dcaf25aa2 sage/numerical/backends/coin_backend.pyx
    a b  
    408408        cdef c_CoinPackedVector* row
    409409        row = new_c_CoinPackedVector();
    410410
     411
    411412        for i,c in coefficients:
    412413            row.insert(i, c)
    413414
     
    415416                        lower_bound if lower_bound != None else -self.si.getInfinity(),
    416417                        upper_bound if upper_bound != None else +self.si.getInfinity())
    417418
    418 
    419419    cpdef row(self, int index):
    420420        r"""
    421421        Returns a row
     
    963963            <BLANKLINE>
    964964        """
    965965        return ""
     966
     967    cpdef CoinBackend copy(self):
     968        """
     969        Returns a copy of self.
     970
     971        EXAMPLE::
     972
     973            sage: from sage.numerical.backends.generic_backend import get_solver
     974            sage: p = MixedIntegerLinearProgram(solver = "Coin")        # optional - Coin
     975            sage: b = p.new_variable()                         # optional - Coin
     976            sage: p.add_constraint(b[1] + b[2] <= 6)           # optional - Coin
     977            sage: p.set_objective(b[1] + b[2])                 # optional - Coin
     978            sage: copy(p).solve()                              # optional - Coin
     979            6.0
     980
     981        """
     982        cdef CoinBackend p = CoinBackend(maximization = (1 if self.is_maximization() else -1))
     983
     984        p.si = new2_c_OsiCbcSolverInterface(<OsiSolverInterface *> self.si)
     985
     986        return p
     987
  • sage/numerical/backends/cplex_backend.pxd

    diff -r 8532a2ad1e55 -r 841dcaf25aa2 sage/numerical/backends/cplex_backend.pxd
    a b  
    2121    cdef c_cpxlp * env
    2222    cdef c_cpxlp * lp
    2323    cdef current_sol
     24    cpdef CPLEXBackend copy(self)
    2425
    2526cdef extern from "../../local/include/cplex.h":
    2627
     
    157158     # Get the problem's type
    158159     int CPXgetprobtype(c_cpxlp * env, c_cpxlp * lp)
    159160
     161     # Set the problem's type
     162     int CPXchgprobtype(c_cpxlp * env, c_cpxlp * lp, int type)
     163
    160164     # Change a row's range
    161165     int CPXchgrngval(c_cpxlp * env, c_cpxlp * lp, int cnt, int * indices, double * values)
    162166
    163167     # Get a row's range
    164168     int CPXgetrngval(c_cpxlp * env, c_cpxlp * lp, double * rngval, int begin, int end)
    165169
     170     # Copy a LP
     171     c_cpxlp * CPXcloneprob(c_cpxlp * env, c_cpxlp * lp, int * status_p)
     172
    166173     # CONSTANTS
    167174     int CPX_ON = 1
    168175     int CPX_PARAM_SCRIND = 1035
  • sage/numerical/backends/cplex_backend.pyx

    diff -r 8532a2ad1e55 -r 841dcaf25aa2 sage/numerical/backends/cplex_backend.pyx
    a b  
    12351235        status = CPXwriteprob(self.env, self.lp, filename, ext)
    12361236        check(status)
    12371237
     1238    cpdef CPLEXBackend copy(self):
     1239        r"""
     1240        Returns a copy of self.
     1241
     1242        EXAMPLE::
     1243
     1244            sage: from sage.numerical.backends.generic_backend import get_solver
     1245            sage: p = MixedIntegerLinearProgram(solver = "CPLEX")        # optional - CPLEX
     1246            sage: b = p.new_variable()                         # optional - CPLEX
     1247            sage: p.add_constraint(b[1] + b[2] <= 6)           # optional - CPLEX
     1248            sage: p.set_objective(b[1] + b[2])                 # optional - CPLEX
     1249            sage: copy(p).solve()                              # optional - CPLEX
     1250            6.0
     1251        """
     1252        cdef CPLEXBackend p = CPLEXBackend()
     1253
     1254        p.lp = CPXcloneprob(p.env, self.lp, &status)
     1255        check(status)
     1256
     1257        p._mixed = self._mixed
     1258        p.current_sol = self.current_sol
     1259
     1260        status = CPXchgprobtype(p.env, p.lp, CPXgetprobtype(self.env, self.lp))
     1261        check(status)
     1262
     1263        return p
     1264
    12381265    def __dealloc__(self):
    12391266        r"""
    12401267        Destructor for the class
  • sage/numerical/backends/generic_backend.pxd

    diff -r 8532a2ad1e55 -r 841dcaf25aa2 sage/numerical/backends/generic_backend.pxd
    a b  
    3636    cpdef col_name(self, int index)
    3737    cpdef variable_upper_bound(self, int index, value = *)
    3838    cpdef variable_lower_bound(self, int index, value = *)
    39 
  • sage/numerical/backends/glpk_backend.pxd

    diff -r 8532a2ad1e55 -r 841dcaf25aa2 sage/numerical/backends/glpk_backend.pxd
    a b  
    8383     int glp_get_col_kind(c_glp_prob *lp, int)
    8484     double glp_get_obj_coef(c_glp_prob *lp, int)
    8585     int glp_get_obj_dir(c_glp_prob *lp)
     86     void glp_copy_prob(c_glp_prob *dst, c_glp_prob *src, int names)
    8687
    8788
    8889
     
    112113cdef class GLPKBackend(GenericBackend):
    113114    cdef c_glp_prob * lp
    114115    cdef c_glp_iocp * iocp
    115 
     116    cpdef GLPKBackend copy(self)
  • sage/numerical/backends/glpk_backend.pyx

    diff -r 8532a2ad1e55 -r 841dcaf25aa2 sage/numerical/backends/glpk_backend.pyx
    a b  
    10471047        """
    10481048        glp_write_mps(self.lp, modern, NULL,  filename)
    10491049
     1050    cpdef GLPKBackend copy(self):
     1051        """
     1052        Returns a copy of self.
     1053
     1054        EXAMPLE::
     1055
     1056            sage: from sage.numerical.backends.generic_backend import get_solver
     1057            sage: p = MixedIntegerLinearProgram(solver = "GLPK")
     1058            sage: b = p.new_variable()                         
     1059            sage: p.add_constraint(b[1] + b[2] <= 6)           
     1060            sage: p.set_objective(b[1] + b[2])                 
     1061            sage: copy(p).solve()                             
     1062            6.0
     1063        """
     1064        cdef GLPKBackend p = GLPKBackend(maximization = (1 if self.is_maximization() else -1))
     1065        glp_copy_prob(p.lp, self.lp, 1)
     1066        return p
     1067
     1068
    10501069    def __dealloc__(self):
    10511070        """
    10521071        Destructor
  • sage/numerical/mip.pyx

    diff -r 8532a2ad1e55 -r 841dcaf25aa2 sage/numerical/mip.pyx
    a b  
    8888include "../ext/stdsage.pxi"
    8989include "../ext/interrupt.pxi"
    9090include "../ext/cdefs.pxi"
    91 from copy import deepcopy
     91from copy import copy,deepcopy
    9292
    9393cdef class MixedIntegerLinearProgram:
    9494    r"""
     
    239239                 ", " + str(b.ncols()) + " variables, " +
    240240                 str(b.nrows()) + " constraints )")
    241241
     242    def __copy__(self):
     243        r"""
     244        Returns a copy of self
     245        """
     246        cdef MixedIntegerLinearProgram p = MixedIntegerLinearProgram(solver="GLPK")
     247        try:
     248            p._mipvariables = copy(self._mipvariables)
     249        except AttributeError:
     250            pass
     251
     252        try:
     253            p._variables = copy(self._variables)
     254        except AttributeError:
     255            pass
     256
     257        try:
     258            p._default_mipvariable = self._default_mipvariable
     259        except AttributeError:
     260            pass
     261
     262        p._backend = (<GenericBackend> self._backend).copy()
     263        return p
     264
    242265    def __getitem__(self, v):
    243266        r"""
    244267        Returns the symbolic variable corresponding to the key