// $Id: osi.i 88 2006-10-23 23:57:50Z leolopes $ // SWIG interface for part of the Osi, using Cbc. %module osi %include "carrays.i" %array_class(double, doubleArray); %array_class(int, intArray); %typemap(in) double* { int i; if (!PySequence_Check($input)) { PyErr_SetString(PyExc_ValueError,"Expected a sequence"); return NULL; } int len = PySequence_Length($input); $1 = (double *) malloc(len*sizeof(double)); for (i = 0; i < len; i++) { PyObject *o = PySequence_GetItem($input,i); if (PyNumber_Check(o)) { $1[i] = PyFloat_AsDouble(o); } else { PyErr_SetString(PyExc_ValueError,"Sequence elements must be numbers"); free($1); return NULL; } } } %typemap(freearg) double* { if ($1) free($1); } %typemap(in) int* { int i; if (!PySequence_Check($input)) { PyErr_SetString(PyExc_ValueError,"Expected a sequence"); return NULL; } int len = PySequence_Length($input); $1 = (int *) malloc(len*sizeof(int)); for (i = 0; i < len; i++) { PyObject *o = PySequence_GetItem($input,i); if (PyNumber_Check(o)) { $1[i] = PyInt_AsLong(o); } else { PyErr_SetString(PyExc_ValueError,"Sequence elements must be numbers"); free($1); return NULL; } } } %typemap(freearg) int* { if ($1) free($1); } %typemap(in) CoinBigIndex* = int*; %typemap(freearg) CoinBigIndex* = int*; %{ #include #include #include #include #include %} class OsiCbcSolverInterface { public: virtual void initialSolve() ; virtual void resolve() ; virtual void branchAndBound() ; ///@name Methods returning info on how the solution process terminated //@{ /// Are there numerical difficulties? virtual bool isAbandoned() const ; /// Is optimality proven? virtual bool isProvenOptimal() const ; /// Is primal infeasiblity proven? virtual bool isProvenPrimalInfeasible() const ; /// Is dual infeasiblity proven? virtual bool isProvenDualInfeasible() const ; /// Is the given primal objective limit reached? virtual bool isPrimalObjectiveLimitReached() const ; /// Is the given dual objective limit reached? virtual bool isDualObjectiveLimitReached() const ; /// Iteration limit reached? virtual bool isIterationLimitReached() const ; //@} // Get solvers value for infinity virtual double getInfinity() const ; /**@name Solution query methods */ //@{ /// Get number of columns virtual int getNumCols() const; /// Get number of rows virtual int getNumRows() const; /// Get number of nonzero elements virtual int getNumElements() const; /// Get pointer to array[getNumCols()] of column lower bounds virtual const double * getColLower() const; /// Get pointer to array[getNumCols()] of column upper bounds virtual const double * getColUpper() const; /// Get pointer to array[getNumCols()] of primal variable values virtual const double * getColSolution() const ; /// Get pointer to array[getNumRows()] of dual variable values virtual const double * getRowPrice() const ; /// Get a pointer to array[getNumCols()] of reduced costs virtual const double * getReducedCost() const ; /// Get pointer to array[getNumCols()] of objective function coefficients virtual const double * getObjCoefficients() const; /// Get objective function sense (1 for min (default), -1 for max) virtual double getObjSense() const ; /// Get pointer to array[getNumRows()] of row lower bounds virtual const double * getRowLower() const ; /// Get pointer to array[getNumRows()] of row upper bounds virtual const double * getRowUpper() const ; /* Get pointer to array[getNumRows()] of row activity levels (constraint matrix times the solution vector). */ virtual const double * getRowActivity() const ; /// Get objective function value virtual double getObjValue() const ; /** Get the number of iterations it took to solve the problem (whatever ``iteration'' means to the solver). */ virtual int getIterationCount() const ; //------------------------------------------------------------------------- /**@name Methods to modify the objective, bounds, and solution For functions which take a set of indices as parameters (\c setObjCoeffSet(), \c setColSetBounds(), \c setRowSetBounds(), \c setRowSetTypes()), the parameters follow the C++ STL iterator convention: \c indexFirst points to the first index in the set, and \c indexLast points to a position one past the last index in the set. */ //@{ /** Set an objective function coefficient */ virtual void setObjCoeff( int elementIndex, double elementValue ) ; /** Set a set of objective function coefficients */ virtual void setObjCoeffSet(const int* indexFirst, const int* indexLast, const double* coeffList); /** Set a single column lower and upper bound. */ virtual void setColBounds( int elementIndex, double lower, double upper ) ; /** Set the upper and lower bounds of a set of columns. The default implementation just invokes setColBounds() over and over again. For each column, boundList must contain both a lower and upper bound, in that order. */ virtual void setColSetBounds(const int* indexFirst, const int* indexLast, const double* boundList); /** Set a single row lower and upper bound. */ virtual void setRowBounds( int elementIndex, double lower, double upper ); /** Set the bounds on a set of rows. The default implementation just invokes setRowBounds() over and over again. */ virtual void setRowSetBounds(const int* indexFirst, const int* indexLast, const double* boundList); /// Set the objective function sense. /// (1 for min (default), -1 for max) virtual void setObjSense(double s) ; /** Set the objective coefficients for all columns array [getNumCols()] is an array of values for the objective. This defaults to a series of set operations and is here for speed. */ virtual void setObjective(const double * array); //@} //------------------------------------------------------------------------- /**@name Methods to set variable type */ //@{ /** Set the variables listed in indices (which is of length len) to be continuous variables */ virtual void setContinuous(const int* indices, int len); /** Set the variables listed in indices (which is of length len) to be integer variables */ virtual void setInteger(const int* indices, int len); //@} //------------------------------------------------------------------------- /**@name Methods to modify the constraint system. Note that new columns are added as continuous variables. */ //@{ /** Add a column (primal variable) to the problem. */ virtual void addCol(int numberElements, const int * rows, const double * elements, const double collb, const double colub, const double obj) ; /** Add a set of columns (primal variables) to the problem. */ /* virtual void addCols(const int numcols, const int * columnStarts, const int * rows, const double * elements, const double* collb, const double* colub, const double* obj); */ /** Add a row (constraint) to the problem. */ /* virtual void addRow(int numberElements, const int * columns, const double * element, const double rowlb, const double rowub) ; */ /** Add a set of rows (constraints) to the problem. */ /* virtual void addRows(const int numrows, const int * rowStarts, const int * columns, const double * element, const double* rowlb, const double* rowub); */ //----------------------------------------------------------------------- /**@name Methods to input a problem */ //@{ /** Load in an problem by copying the arguments (the constraints on the rows are given by sense/rhs/range triplets). If a pointer is 0 then the following values are the default:
  • colub: all columns have upper bound infinity
  • collb: all columns have lower bound 0
  • obj: all variables have 0 objective coefficient
  • rowsen: all rows are >=
  • rowrhs: all right hand sides are 0
  • rowrng: 0 for the ranged rows
*/ /** Just like the other loadProblem() methods except that the matrix is given in a standard column major ordered format (without gaps). */ virtual void loadProblem(const int numcols, const int numrows, const CoinBigIndex * start, const int* index, const double* value, const double* collb, const double* colub, const double* obj, const double* rowlb, const double* rowub) ; /** Read a problem in MPS format from the given filename. The default implementation uses CoinMpsIO::readMps() to read the MPS file and returns the number of errors encountered. */ virtual int readMps(const char *filename, const char *extension = "mps") ; /** Read a problem in GMPL format from the given filenames. Will only work if glpk installed */ virtual int readGMPL(const char *filename, const char * dataname=NULL); /** Write the problem in MPS format to the specified file. If objSense is non-zero, a value of -1.0 causes the problem to be written with a maximization objective; +1.0 forces a minimization objective. If objSense is zero, the choice is left to implementation. */ virtual void writeMps(const char *filename, const char *extension = "mps", double objSense=0.0) const ; /** Write the problem in MPS format to the specified file. Row and column names may be null. formatType is
  • 0 - normal
  • 1 - extra accuracy
  • 2 - IEEE hex
Returns non-zero on I/O error */ int writeMpsNative(const char *filename, const char ** rowNames, const char ** columnNames, int formatType=0,int numberAcross=2, double objSense=0.0) const ; //--------------------------------------------------------------------------- /** Simplex Interface Abstract Base Class Abstract Base Class for describing an advanced interface to a simplex solver. When switched on allows great control of simplex iterations. Also allows access to tableau. */ /** Get basic indices (order of indices corresponds to the order of elements in a vector retured by getBInvACol() and getBInvCol()). */ virtual void getBasics(int* index) const ; //--------------------------------------------------------------------------- ///@name Constructors and destructors //@{ /// Default Constructor OsiCbcSolverInterface(); /** Clone The result of calling clone(false) is defined to be equivalent to calling the default constructor OsiCbcSolverInterface(). */ // virtual OsiCbcSolverInterface * clone(bool copyData = true) const ; /// Copy constructor OsiCbcSolverInterface(const OsiCbcSolverInterface &); /// Destructor virtual ~OsiCbcSolverInterface (); /** Reset the solver interface. A call to reset() returns the solver interface to the same state as it would have if it had just been constructed by calling the default constructor OsiCbcSolverInterface(). */ virtual void reset(); //@} %extend{ long stop(void){ CbcEventHandler *eh = self->getModelPtr()->getEventHandler(); if (eh) eh->setDfltAction(CbcEventHandler::stop); else return 0; return (long)eh; //This is temporary debug info } long go(void){ CbcEventHandler *eh = self->getModelPtr()->getEventHandler(); if (eh) eh->setDfltAction(CbcEventHandler::noAction); else return 0; return (long)eh; //This is temporary debug info } long setLogLevel(int newLevel){ self->getModelPtr()->setLogLevel(newLevel); return 0; } } //--------------------------------------------------------------------------- }; %pythoncode %{ def _test(): import doctest doctest.testfile('testOsi.txt') if __name__ == "__main__": _test() %}