Beispiel #1
0
PyObject *LPX_GetMatrix(LPXObject *self)
{
	int row, numrows, listi, i, nnz, rownz;
	PyObject *retval;

	numrows = glp_get_num_rows(LP);
	nnz = glp_get_num_nz(LP);
	retval = PyList_New(nnz);
	if (nnz == 0 || retval == NULL)
		return retval;

	// We don't really need this much memory, but, eh...
	int *ind = (int*)calloc(nnz, sizeof(int));
	double *val = (double*)calloc(nnz, sizeof(double));

	listi = 0;
	for (row=1; row<=numrows; ++row) {
		rownz = glp_get_mat_row(LP, row, ind-1, val-1);
		if (rownz == 0)
			continue;
		for (i = 0; i < rownz; ++i) {
			PyList_SET_ITEM(retval, listi++, Py_BuildValue("iid", row - 1, ind[i] - 1, val[i]));
		}
		/*
		 * Continue to downscale these vectors, freeing memory in C even
		 * as we use more memory in Python.
		 */
		nnz -= rownz;
		if (nnz) {
			ind = (int*)realloc(ind, nnz*sizeof(int));
			val = (double*)realloc(val, nnz*sizeof(double));
		}
	}
	free(ind);
	free(val);
	if (PyList_Sort(retval)) {
		Py_DECREF(retval);
		return NULL;
	}
		return retval;
}
Beispiel #2
0
static PyObject* LPX_getnumnonzero(LPXObject *self, void *closure) {
  return PyInt_FromLong(glp_get_num_nz(LP));
}
Beispiel #3
0
int lpx_get_num_nz(LPX *lp)
{     /* retrieve number of constraint coefficients */
      return glp_get_num_nz(lp);
}
Beispiel #4
0
// read in all necessary elements for retrieving the LP/MILP
void Rglpk_read_file (char **file, int *type, 
		      int *lp_direction_of_optimization,
		      int *lp_n_constraints, int *lp_n_objective_vars,
		      int *lp_n_values_in_constraint_matrix,
		      int *lp_n_integer_vars, int *lp_n_binary_vars, 
		      char **lp_prob_name,
		      char **lp_obj_name,
		      int *lp_verbosity) {

  int status;
  extern glp_prob *lp;
  glp_tran *tran;
  const char *str; 
  
  // Turn on/off Terminal Output
  if (*lp_verbosity==1)
    glp_term_out(GLP_ON);
  else
    glp_term_out(GLP_OFF);

  // create problem object 
  if (lp)
    glp_delete_prob(lp);
  lp = glp_create_prob();

  // read file -> gets stored as an GLPK problem object 'lp'
  // which file type do we have?
  switch (*type){
  case 1: 
    // Fixed (ancient) MPS Format, param argument currently NULL
    status = glp_read_mps(lp, GLP_MPS_DECK, NULL, *file);
    break;
  case 2:
    // Free (modern) MPS format, param argument currently NULL
    status = glp_read_mps(lp, GLP_MPS_FILE, NULL, *file);
    break;
  case 3:
    // CPLEX LP Format
    status = glp_read_lp(lp, NULL, *file);
    break;
  case 4:
    // MATHPROG Format (based on lpx_read_model function)
    tran = glp_mpl_alloc_wksp();

    status = glp_mpl_read_model(tran, *file, 0);

    if (!status) {
        status = glp_mpl_generate(tran, NULL);
        if (!status) {
            glp_mpl_build_prob(tran, lp);
        }
    }
    glp_mpl_free_wksp(tran);
    break;    
  } 

  // if file read successfully glp_read_* returns zero
  if ( status != 0 ) {
    glp_delete_prob(lp);
    lp = NULL;
    error("Reading file %s failed", *file);
  }

  // retrieve problem name
  str = glp_get_prob_name(lp);
  if (str){
    *lp_prob_name = (char *) str;
  }

  // retrieve name of objective function
  str = glp_get_obj_name(lp);
  if (str){
    *lp_obj_name = (char *) str;
  }
  
  // retrieve optimization direction flag
  *lp_direction_of_optimization = glp_get_obj_dir(lp);  

  // retrieve number of constraints
  *lp_n_constraints = glp_get_num_rows(lp);  

  // retrieve number of objective variables
  *lp_n_objective_vars = glp_get_num_cols(lp);

  // retrieve number of non-zero elements in constraint matrix
  *lp_n_values_in_constraint_matrix = glp_get_num_nz(lp);

  // retrieve number of integer variables
  *lp_n_integer_vars = glp_get_num_int(lp);
  
  // retrieve number of binary variables
  *lp_n_binary_vars = glp_get_num_bin(lp);
}