PyMODINIT_FUNC PyInit_gsl(void) { PyObject *m; if (!(m = PyModule_Create(&gsl_module))) return NULL; if (import_cvxopt() < 0) return NULL; return m; }
PyMODINIT_FUNC initcholmod(void) { CHOL(start) (&Common); cholmod_module = Py_InitModule3("cvxopt.cholmod", cholmod_functions, cholmod__doc__); PyModule_AddObject(cholmod_module, "options", PyDict_New()); if (import_cvxopt() < 0) return; }
PyMODINIT_FUNC PyInit_cholmod(void) { CHOL(start) (&Common); if (!(cholmod_module = PyModule_Create(&cholmod_module_def))) return NULL; PyModule_AddObject(cholmod_module, "options", PyDict_New()); if (import_cvxopt() < 0) return NULL; return cholmod_module; }
PyMODINIT_FUNC PyInit_misc(void) { PyObject *misc_mod; misc_mod = PyModule_Create(&misc_module); if (misc_mod == NULL) return NULL; if (import_cvxopt() < 0) return NULL; return misc_mod; }
// Python 2.x PyMODINIT_FUNC initmisc(void) { PyObject *m; m = Py_InitModule3("misc", misc_functions, misc__doc__); if (import_cvxopt() < 0) return; }
static PyObject *integer(PyObject *self, PyObject *args, PyObject *kwrds) { matrix *c, *h, *b=NULL, *x=NULL; PyObject *G, *A=NULL, *IntSet=NULL, *BinSet = NULL; PyObject *t=NULL; pyiocp *iocpParm = NULL;; glp_iocp *options = NULL; glp_prob *lp; int m, n, p, i, j, k, nnz, nnzmax, *rn=NULL, *cn=NULL; double *a=NULL, val; char *kwlist[] = {"c", "G", "h", "A", "b", "I", "B","iocp", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwrds, "OOO|OOOOO!", kwlist, &c, &G, &h, &A, &b, &IntSet, &BinSet,iocp_t,&iocpParm)) return NULL; if(!iocpParm) { iocpParm = (pyiocp*)malloc(sizeof(*iocpParm)); glp_init_iocp(&(iocpParm->obj)); } if(iocpParm) { Py_INCREF(iocpParm); options = &iocpParm->obj; options->presolve = 1; } if ((Matrix_Check(G) && MAT_ID(G) != DOUBLE) || (SpMatrix_Check(G) && SP_ID(G) != DOUBLE) || (!Matrix_Check(G) && !SpMatrix_Check(G))){ PyErr_SetString(PyExc_TypeError, "G must be a 'd' matrix"); return NULL; } if ((m = Matrix_Check(G) ? MAT_NROWS(G) : SP_NROWS(G)) <= 0) err_p_int("m"); if ((n = Matrix_Check(G) ? MAT_NCOLS(G) : SP_NCOLS(G)) <= 0) err_p_int("n"); if (!Matrix_Check(h) || h->id != DOUBLE) err_dbl_mtrx("h"); if (h->nrows != m || h->ncols != 1){ PyErr_SetString(PyExc_ValueError, "incompatible dimensions"); return NULL; } if (A){ if ((Matrix_Check(A) && MAT_ID(A) != DOUBLE) || (SpMatrix_Check(A) && SP_ID(A) != DOUBLE) || (!Matrix_Check(A) && !SpMatrix_Check(A))){ PyErr_SetString(PyExc_ValueError, "A must be a dense " "'d' matrix or a general sparse matrix"); return NULL; } if ((p = Matrix_Check(A) ? MAT_NROWS(A) : SP_NROWS(A)) < 0) err_p_int("p"); if ((Matrix_Check(A) ? MAT_NCOLS(A) : SP_NCOLS(A)) != n){ PyErr_SetString(PyExc_ValueError, "incompatible " "dimensions"); return NULL; } } else p = 0; if (b && (!Matrix_Check(b) || b->id != DOUBLE)) err_dbl_mtrx("b"); if ((b && (b->nrows != p || b->ncols != 1)) || (!b && p !=0 )){ PyErr_SetString(PyExc_ValueError, "incompatible dimensions"); return NULL; } if ((IntSet) && (!PyAnySet_Check(IntSet))) PY_ERR_TYPE("invalid integer index set"); if ((BinSet) && (!PyAnySet_Check(BinSet))) PY_ERR_TYPE("invalid binary index set"); lp = glp_create_prob(); glp_add_rows(lp, m+p); glp_add_cols(lp, n); for (i=0; i<n; i++){ glp_set_obj_coef(lp, i+1, MAT_BUFD(c)[i]); glp_set_col_bnds(lp, i+1, GLP_FR, 0.0, 0.0); } for (i=0; i<m; i++) glp_set_row_bnds(lp, i+1, GLP_UP, 0.0, MAT_BUFD(h)[i]); for (i=0; i<p; i++) glp_set_row_bnds(lp, i+m+1, GLP_FX, MAT_BUFD(b)[i], MAT_BUFD(b)[i]); nnzmax = (SpMatrix_Check(G) ? SP_NNZ(G) : m*n ) + ((A && SpMatrix_Check(A)) ? SP_NNZ(A) : p*n); a = (double *) calloc(nnzmax+1, sizeof(double)); rn = (int *) calloc(nnzmax+1, sizeof(int)); cn = (int *) calloc(nnzmax+1, sizeof(int)); if (!a || !rn || !cn){ free(a); free(rn); free(cn); glp_delete_prob(lp); return PyErr_NoMemory(); } nnz = 0; if (SpMatrix_Check(G)) { for (j=0; j<n; j++) for (k=SP_COL(G)[j]; k<SP_COL(G)[j+1]; k++) if ((val = SP_VALD(G)[k]) != 0.0){ a[1+nnz] = val; rn[1+nnz] = SP_ROW(G)[k]+1; cn[1+nnz] = j+1; nnz++; } } else for (j=0; j<n; j++) for (i=0; i<m; i++) if ((val = MAT_BUFD(G)[i+j*m]) != 0.0){ a[1+nnz] = val; rn[1+nnz] = i+1; cn[1+nnz] = j+1; nnz++; } if (A && SpMatrix_Check(A)){ for (j=0; j<n; j++) for (k=SP_COL(A)[j]; k<SP_COL(A)[j+1]; k++) if ((val = SP_VALD(A)[k]) != 0.0){ a[1+nnz] = val; rn[1+nnz] = m+SP_ROW(A)[k]+1; cn[1+nnz] = j+1; nnz++; } } else for (j=0; j<n; j++) for (i=0; i<p; i++) if ((val = MAT_BUFD(A)[i+j*p]) != 0.0){ a[1+nnz] = val; rn[1+nnz] = m+i+1; cn[1+nnz] = j+1; nnz++; } glp_load_matrix(lp, nnz, rn, cn, a); free(rn); free(cn); free(a); if (!(t = PyTuple_New(2))) { glp_delete_prob(lp); return PyErr_NoMemory(); } if (IntSet) { PyObject *iter = PySequence_Fast(IntSet, "Critical error: not sequence"); for (i=0; i<PySet_GET_SIZE(IntSet); i++) { PyObject *tmp = PySequence_Fast_GET_ITEM(iter, i); #if PY_MAJOR_VERSION >= 3 if (!PyLong_Check(tmp)) { #else if (!PyInt_Check(tmp)) { #endif glp_delete_prob(lp); Py_DECREF(iter); PY_ERR_TYPE("non-integer element in I"); } #if PY_MAJOR_VERSION >= 3 int k = PyLong_AS_LONG(tmp); #else int k = PyInt_AS_LONG(tmp); #endif if ((k < 0) || (k >= n)) { glp_delete_prob(lp); Py_DECREF(iter); PY_ERR(PyExc_IndexError, "index element out of range in I"); } glp_set_col_kind(lp, k+1, GLP_IV); } Py_DECREF(iter); } if (BinSet) { PyObject *iter = PySequence_Fast(BinSet, "Critical error: not sequence"); for (i=0; i<PySet_GET_SIZE(BinSet); i++) { PyObject *tmp = PySequence_Fast_GET_ITEM(iter, i); #if PY_MAJOR_VERSION >= 3 if (!PyLong_Check(tmp)) { #else if (!PyInt_Check(tmp)) { #endif glp_delete_prob(lp); Py_DECREF(iter); PY_ERR_TYPE("non-binary element in I"); } #if PY_MAJOR_VERSION >= 3 int k = PyLong_AS_LONG(tmp); #else int k = PyInt_AS_LONG(tmp); #endif if ((k < 0) || (k >= n)) { glp_delete_prob(lp); Py_DECREF(iter); PY_ERR(PyExc_IndexError, "index element out of range in B"); } glp_set_col_kind(lp, k+1, GLP_BV); } Py_DECREF(iter); } switch (glp_intopt(lp,options)){ case 0: x = (matrix *) Matrix_New(n,1,DOUBLE); if (!x) { Py_XDECREF(iocpParm); Py_XDECREF(t); glp_delete_prob(lp); return PyErr_NoMemory(); } set_output_string(t,"optimal"); set_output_string(t,"optimal"); for (i=0; i<n; i++) MAT_BUFD(x)[i] = glp_mip_col_val(lp, i+1); PyTuple_SET_ITEM(t, 1, (PyObject *) x); Py_XDECREF(iocpParm); glp_delete_prob(lp); return (PyObject *) t; case GLP_ETMLIM: x = (matrix *) Matrix_New(n,1,DOUBLE); if (!x) { Py_XDECREF(t); Py_XDECREF(iocpParm); glp_delete_prob(lp); return PyErr_NoMemory(); } set_output_string(t,"time limit exceeded"); for (i=0; i<n; i++) MAT_BUFD(x)[i] = glp_mip_col_val(lp, i+1); PyTuple_SET_ITEM(t, 1, (PyObject *) x); Py_XDECREF(iocpParm); glp_delete_prob(lp); return (PyObject *) t; case GLP_EBOUND: set_output_string(t,"incorrect bounds"); break; case GLP_EFAIL: set_output_string(t,"invalid MIP formulation"); break; case GLP_ENOPFS: set_output_string(t,"primal infeasible"); break; case GLP_ENODFS: set_output_string(t,"dual infeasible"); break; case GLP_EMIPGAP: set_output_string(t,"Relative mip gap tolerance reached"); break; /*case LPX_E_ITLIM: set_output_string(t,"maxiters exceeded"); break;*/ /*case LPX_E_SING: set_output_string(t,"singular or ill-conditioned basis"); break;*/ default: set_output_string(t,"unknown"); } Py_XDECREF(iocpParm); glp_delete_prob(lp); PyTuple_SET_ITEM(t, 1, Py_BuildValue("")); return (PyObject *) t; } static PyMethodDef glpk_functions[] = { {"lp", (PyCFunction) simplex, METH_VARARGS|METH_KEYWORDS, doc_simplex}, {"ilp", (PyCFunction) integer, METH_VARARGS|METH_KEYWORDS, doc_integer}, {NULL} /* Sentinel */ }; #if PY_MAJOR_VERSION >= 3 static PyModuleDef glpk_module_def = { PyModuleDef_HEAD_INIT, "glpk", glpk__doc__, -1, glpk_functions, NULL, NULL, NULL, NULL }; void addglpkConstants (void) { PyModule_AddIntMacro(glpk_module, GLP_ON); PyModule_AddIntMacro(glpk_module,GLP_OFF); /* reason codes: */ PyModule_AddIntMacro(glpk_module,GLP_IROWGEN); PyModule_AddIntMacro(glpk_module,GLP_IBINGO); PyModule_AddIntMacro(glpk_module,GLP_IHEUR); PyModule_AddIntMacro(glpk_module,GLP_ICUTGEN); PyModule_AddIntMacro(glpk_module,GLP_IBRANCH); PyModule_AddIntMacro(glpk_module,GLP_ISELECT); PyModule_AddIntMacro(glpk_module,GLP_IPREPRO); /* branch selection indicator: */ PyModule_AddIntMacro(glpk_module,GLP_NO_BRNCH); PyModule_AddIntMacro(glpk_module,GLP_DN_BRNCH); PyModule_AddIntMacro(glpk_module,GLP_UP_BRNCH); /* return codes: */ PyModule_AddIntMacro(glpk_module,GLP_EBADB); PyModule_AddIntMacro(glpk_module,GLP_ESING); PyModule_AddIntMacro(glpk_module,GLP_ECOND); PyModule_AddIntMacro(glpk_module,GLP_EBOUND); PyModule_AddIntMacro(glpk_module,GLP_EFAIL); PyModule_AddIntMacro(glpk_module,GLP_EOBJLL); PyModule_AddIntMacro(glpk_module,GLP_EOBJUL); PyModule_AddIntMacro(glpk_module,GLP_EITLIM); PyModule_AddIntMacro(glpk_module,GLP_ETMLIM); PyModule_AddIntMacro(glpk_module,GLP_ENOPFS); PyModule_AddIntMacro(glpk_module,GLP_ENODFS); PyModule_AddIntMacro(glpk_module,GLP_EROOT); PyModule_AddIntMacro(glpk_module,GLP_ESTOP); PyModule_AddIntMacro(glpk_module,GLP_EMIPGAP); PyModule_AddIntMacro(glpk_module,GLP_ENOFEAS); PyModule_AddIntMacro(glpk_module,GLP_ENOCVG); PyModule_AddIntMacro(glpk_module,GLP_EINSTAB); PyModule_AddIntMacro(glpk_module,GLP_EDATA); PyModule_AddIntMacro(glpk_module,GLP_ERANGE); /* condition indicator: */ PyModule_AddIntMacro(glpk_module,GLP_KKT_PE); PyModule_AddIntMacro(glpk_module,GLP_KKT_PB); PyModule_AddIntMacro(glpk_module,GLP_KKT_DE); PyModule_AddIntMacro(glpk_module,GLP_KKT_DB); PyModule_AddIntMacro(glpk_module,GLP_KKT_CS); /* MPS file format: */ PyModule_AddIntMacro(glpk_module,GLP_MPS_DECK); PyModule_AddIntMacro(glpk_module,GLP_MPS_FILE); /* simplex method control parameters */ /* message level: */ PyModule_AddIntMacro(glpk_module,GLP_MSG_OFF); PyModule_AddIntMacro(glpk_module,GLP_MSG_ERR); PyModule_AddIntMacro(glpk_module,GLP_MSG_ON); PyModule_AddIntMacro(glpk_module,GLP_MSG_ALL); PyModule_AddIntMacro(glpk_module,GLP_MSG_DBG); /* simplex method option: */ PyModule_AddIntMacro(glpk_module,GLP_PRIMAL); PyModule_AddIntMacro(glpk_module,GLP_DUALP); PyModule_AddIntMacro(glpk_module,GLP_DUAL); /* pricing technique: */ PyModule_AddIntMacro(glpk_module,GLP_PT_STD); PyModule_AddIntMacro(glpk_module,GLP_PT_PSE); /* ratio test technique: */ PyModule_AddIntMacro(glpk_module,GLP_RT_STD); PyModule_AddIntMacro(glpk_module,GLP_RT_HAR); /* interior-point solver control parameters */ /* ordering algorithm: */ PyModule_AddIntMacro(glpk_module,GLP_ORD_NONE); PyModule_AddIntMacro(glpk_module,GLP_ORD_QMD); PyModule_AddIntMacro(glpk_module,GLP_ORD_AMD); PyModule_AddIntMacro(glpk_module,GLP_ORD_SYMAMD); } PyMODINIT_FUNC PyInit_glpk(void) { if (!(glpk_module = PyModule_Create(&glpk_module_def))) return NULL; if (PyType_Ready(&iocp_t) < 0 || (PyType_Ready(&smcp_t) < 0)) return NULL; /* Adding macros */ addglpkConstants(); /* Adding option lists as objects */ Py_INCREF(&smcp_t); PyModule_AddObject(glpk_module,"smcp",(PyObject*)&smcp_t); Py_INCREF(&iocp_t); PyModule_AddObject(glpk_module,"iocp",(PyObject*)&iocp_t); if (import_cvxopt() < 0) return NULL; return glpk_module; } #else PyMODINIT_FUNC initglpk(void) { glpk_module = Py_InitModule3("cvxopt.glpk", glpk_functions, glpk__doc__); if (PyType_Ready(&iocp_t) < 0 || (PyType_Ready(&smcp_t) < 0)) return NULL; addglpkConstants(); Py_INCREF(&smcp_t); PyModule_AddObject(glpk_module,"smcp",(PyObject*)&smcp_t); Py_INCREF(&iocp_t); PyModule_AddObject(glpk_module,"iocp",(PyObject*)&iocp_t); if (import_cvxopt() < 0) return; }
PyMODINIT_FUNC initgsl(void) { PyObject *m; m = Py_InitModule3("cvxopt.gsl", gsl_functions, gsl__doc__); if (import_cvxopt() < 0) return; }
static int set_defaults(double *control) { int_t pos=0; int param_id; PyObject *param, *key, *value; #if PY_MAJOR_VERSION < 3 char *keystr; #endif char err_str[100]; amd_defaults(control); if (!(param = PyObject_GetAttrString(amd_module, "options")) || !PyDict_Check(param)){ PyErr_SetString(PyExc_AttributeError, "missing amd.options" "dictionary"); return 0; } while (PyDict_Next(param, &pos, &key, &value)) #if PY_MAJOR_VERSION >= 3 if ((PyUnicode_Check(key)) && get_param_idx(_PyUnicode_AsString(key),¶m_id)) { if (!PyLong_Check(value) && !PyFloat_Check(value)){ sprintf(err_str, "invalid value for AMD parameter: %-.20s", _PyUnicode_AsString(key)); #else if ((keystr = PyString_AsString(key)) && get_param_idx(keystr, ¶m_id)) { if (!PyInt_Check(value) && !PyFloat_Check(value)){ sprintf(err_str, "invalid value for AMD parameter: " "%-.20s", keystr); #endif PyErr_SetString(PyExc_ValueError, err_str); Py_DECREF(param); return 0; } control[param_id] = PyFloat_AsDouble(value); } Py_DECREF(param); return 1; } static char doc_order[] = "Computes the approximate minimum degree ordering of a square " "matrix.\n\n" "p = order(A, uplo='L')\n\n" "PURPOSE\n" "Computes a permutation p that reduces fill-in in the Cholesky\n" "factorization of A[p,p].\n\n" "ARGUMENTS\n" "A square sparse matrix\n\n" "uplo 'L' or 'U'. If uplo is 'L', the lower triangular part\n" " of A is used and the upper triangular is ignored. If\n" " uplo is 'U', the upper triangular part is used and the\n" " lower triangular part is ignored.\n\n" "p 'i' matrix of length equal to the order of A"; static PyObject* order_c(PyObject *self, PyObject *args, PyObject *kwrds) { spmatrix *A; matrix *perm; #if PY_MAJOR_VERSION >= 3 int uplo_ = 'L'; #endif char uplo = 'L'; int j, k, n, nnz, alloc=0, info; int_t *rowind=NULL, *colptr=NULL; double control[AMD_CONTROL]; char *kwlist[] = {"A", "uplo", NULL}; #if PY_MAJOR_VERSION >= 3 if (!PyArg_ParseTupleAndKeywords(args, kwrds, "O|C", kwlist, &A, &uplo_)) return NULL; uplo = (char) uplo_; #else if (!PyArg_ParseTupleAndKeywords(args, kwrds, "O|c", kwlist, &A, &uplo)) return NULL; #endif if (!set_defaults(control)) return NULL; if (!SpMatrix_Check(A) || SP_NROWS(A) != SP_NCOLS(A)){ PyErr_SetString(PyExc_TypeError, "A must be a square sparse " "matrix"); return NULL; } if (uplo != 'U' && uplo != 'L') err_char("uplo", "'L', 'U'"); if (!(perm = (matrix *) Matrix_New((int)SP_NROWS(A),1,INT))) return PyErr_NoMemory(); n = SP_NROWS(A); for (nnz=0, j=0; j<n; j++) { if (uplo == 'L'){ for (k=SP_COL(A)[j]; k<SP_COL(A)[j+1] && SP_ROW(A)[k]<j; k++); nnz += SP_COL(A)[j+1] - k; } else { for (k=SP_COL(A)[j]; k<SP_COL(A)[j+1] && SP_ROW(A)[k] <= j; k++); nnz += k - SP_COL(A)[j]; } } if (nnz == SP_NNZ(A)){ colptr = (int_t *) SP_COL(A); rowind = (int_t *) SP_ROW(A); } else { alloc = 1; colptr = (int_t *) calloc(n+1, sizeof(int_t)); rowind = (int_t *) calloc(nnz, sizeof(int_t)); if (!colptr || !rowind) { Py_XDECREF(perm); free(colptr); free(rowind); return PyErr_NoMemory(); } colptr[0] = 0; for (j=0; j<n; j++) { if (uplo == 'L'){ for (k=SP_COL(A)[j]; k<SP_COL(A)[j+1] && SP_ROW(A)[k] < j; k++); nnz = SP_COL(A)[j+1] - k; colptr[j+1] = colptr[j] + nnz; memcpy(rowind + colptr[j], (int_t *) SP_ROW(A) + k, nnz*sizeof(int_t)); } else { for (k=SP_COL(A)[j]; k<SP_COL(A)[j+1] && SP_ROW(A)[k] <= j; k++); nnz = k - SP_COL(A)[j]; colptr[j+1] = colptr[j] + nnz; memcpy(rowind + colptr[j], (int_t *) (SP_ROW(A) + SP_COL(A)[j]), nnz*sizeof(int_t)); } } } info = amd_order(n, colptr, rowind, MAT_BUFI(perm), control, NULL); if (alloc){ free(colptr); free(rowind); } switch (info) { case AMD_OUT_OF_MEMORY: Py_XDECREF(perm); return PyErr_NoMemory(); case AMD_INVALID: Py_XDECREF(perm); return Py_BuildValue(""); case AMD_OK: return (PyObject *) perm; } return Py_BuildValue(""); } static PyMethodDef amd_functions[] = { {"order", (PyCFunction) order_c, METH_VARARGS|METH_KEYWORDS, doc_order}, {NULL} /* Sentinel */ }; #if PY_MAJOR_VERSION >= 3 static PyModuleDef amd_module_def = { PyModuleDef_HEAD_INIT, "amd", amd__doc__, -1, amd_functions, NULL, NULL, NULL, NULL }; PyMODINIT_FUNC PyInit_amd(void) { if (!(amd_module = PyModule_Create(&amd_module_def))) return NULL; PyModule_AddObject(amd_module, "options", PyDict_New()); if (import_cvxopt() < 0) return NULL; return amd_module; } #else PyMODINIT_FUNC initamd(void) { amd_module = Py_InitModule3("cvxopt.amd", amd_functions, amd__doc__); PyModule_AddObject(amd_module, "options", PyDict_New()); if (import_cvxopt() < 0) return; }