Exemplo n.º 1
0
void mexFunction(
    int nlhs, mxArray *plhs[],
    int nrhs, const mxArray *prhs[]
)
{
  int i, j;
  double *c=NULL, *b=NULL, *A=NULL, *H=NULL,
    *l=NULL, *u=NULL, *x=NULL, *lambda=NULL ;
  int *nzA=NULL, *nzH=NULL ;
  int *iA=NULL, *kA=NULL ;
  int *iH=NULL, *kH=NULL ;
#ifndef MX_COMPAT_32
  long *iA_=NULL, *kA_=NULL ;
  long *iH_=NULL, *kH_=NULL ;
#endif 
  int neq=0, m=0, n=0, display=0;
  long *cpenv=NULL, *p_qp=NULL;
  char *Sense=NULL ;
  CPXENVptr     env = NULL;
  CPXLPptr      qp = NULL;
  int           status, qpstat;
  double        objval;
  double * p_qpstat ;
  char          opt_method[128]="auto" ;
  
  if (nrhs > 10 || nrhs < 1) {
    mexErrMsgTxt("Usage: [x,lambda,how,p_qp] "
		 "= qp_solve(cpenv,Q,c,A,b,l,u,neq,disp,method)");
    return;
  }
  switch (nrhs) {
  case 10:
	  if (mxGetM(prhs[9]) != 0 || mxGetN(prhs[9]) != 0) {
		  if (mxIsNumeric(prhs[9]) || mxIsComplex(prhs[9]) || !mxIsChar(prhs[9]) 
			  ||  mxIsSparse(prhs[9])
			  || !(mxGetM(prhs[9])==1 && mxGetN(prhs[9])>=1)) {
			  mexErrMsgTxt("10th argument (method) must be "
						   "a string.");
			  return;
		  }
		  mxGetString(prhs[9], opt_method, 128) ;
	  }
  case 9:
    if (mxGetM(prhs[8]) != 0 || mxGetN(prhs[8]) != 0) {
      if (!mxIsNumeric(prhs[8]) || mxIsComplex(prhs[8]) 
	  ||  mxIsSparse(prhs[8])
	  || !(mxGetM(prhs[8])==1 && mxGetN(prhs[8])==1)) {
	mexErrMsgTxt("9th argument (display) must be "
		     "an integer scalar.");
	return;
      }
      display = *mxGetPr(prhs[8]);
    }
  case 8:
    if (mxGetM(prhs[7]) != 0 || mxGetN(prhs[7]) != 0) {
      if (!mxIsNumeric(prhs[7]) || mxIsComplex(prhs[7]) 
	  ||  mxIsSparse(prhs[7])
	  || !(mxGetM(prhs[7])==1 && mxGetN(prhs[7])==1)) {
	mexErrMsgTxt("8th argument (neqcstr) must be "
		     "an integer scalar.");
	return;
      }
      neq = *mxGetPr(prhs[7]);
    }
  case 7:
    if (mxGetM(prhs[6]) != 0 || mxGetN(prhs[6]) != 0) {
      if (!mxIsNumeric(prhs[6]) || mxIsComplex(prhs[6]) 
	  ||  mxIsSparse(prhs[6])
	  || !mxIsDouble(prhs[6]) 
	  ||  mxGetN(prhs[6])!=1 ) {
	mexErrMsgTxt("7th argument (u) must be "
		     "a column vector.");
	return;
      }
      u = mxGetPr(prhs[6]);
      n = mxGetM(prhs[6]);
    }
  case 6:
    if (mxGetM(prhs[5]) != 0 || mxGetN(prhs[5]) != 0) {
      if (!mxIsNumeric(prhs[5]) || mxIsComplex(prhs[5]) 
	  ||  mxIsSparse(prhs[5])
	  || !mxIsDouble(prhs[5]) 
	  ||  mxGetN(prhs[5])!=1 ) {
	mexErrMsgTxt("6th argument (l) must be "
		     "a column vector.");
	return;
      }
      if (n != 0 && n != mxGetM(prhs[5])) {
	mexErrMsgTxt("Dimension error (arg 6 and later).");
	return;
      }
      l = mxGetPr(prhs[5]);
      n = mxGetM(prhs[5]);
    }
  case 5:
    if (mxGetM(prhs[4]) != 0 || mxGetN(prhs[4]) != 0) {
      if (!mxIsNumeric(prhs[4]) || mxIsComplex(prhs[4]) 
	  ||  mxIsSparse(prhs[4])
	  || !mxIsDouble(prhs[4]) 
	  ||  mxGetN(prhs[4])!=1 ) {
	mexErrMsgTxt("5th argument (b) must be "
		     "a column vector.");
	return;
      }
      if (m != 0 && m != mxGetM(prhs[4])) {
	mexErrMsgTxt("Dimension error (arg 5 and later).");
	return;
      }
      b = mxGetPr(prhs[4]);
      m = mxGetM(prhs[4]);
    }
  case 4:
    if (mxGetM(prhs[3]) != 0 || mxGetN(prhs[3]) != 0) {
      if (!mxIsNumeric(prhs[3]) || mxIsComplex(prhs[3]) 
	  || !mxIsSparse(prhs[3]) ) {
	mexErrMsgTxt("4th argument (A) must be "
		     "a sparse matrix.");
	return;
      }
      if (m != 0 && m != mxGetM(prhs[3])) {
	mexErrMsgTxt("Dimension error (arg 4 and later).");
	return;
      }
      if (n != 0 && n != mxGetN(prhs[3])) {
	mexErrMsgTxt("Dimension error (arg 4 and later).");
	return;
      }
      m = mxGetM(prhs[3]);
      n = mxGetN(prhs[3]);
      
      A = mxGetPr(prhs[3]);
#ifdef MX_COMPAT_32
      iA = mxGetIr(prhs[3]);
      kA = mxGetJc(prhs[3]);
#else
      iA_ = mxGetIr(prhs[3]);
      kA_ = mxGetJc(prhs[3]);

	  iA = (int*)malloc(mxGetNzmax(prhs[3])*sizeof(int)) ;
	  for (i=0; i<mxGetNzmax(prhs[3]); i++)
		  iA[i]=iA_[i] ;

	  kA = (int*)malloc((n+1)*sizeof(int)) ;
	  for (i=0; i<n+1; i++)
		  kA[i]=kA_[i] ;
#endif
	  
      nzA=myMalloc(n*sizeof(int)) ;
      for (i=0; i<n; i++)
		  nzA[i]=kA[i+1]-kA[i] ;
	  
      Sense=myMalloc((m+1)*sizeof(char)) ;
      for (i=0; i<m; i++)
		  if (i<neq) Sense[i]='E' ;
		  else Sense[i]='L' ;
      Sense[m]=0 ;
    }
  case 3:
    if (mxGetM(prhs[2]) != 0 || mxGetN(prhs[2]) != 0) {
      if (!mxIsNumeric(prhs[2]) || mxIsComplex(prhs[2]) 
	  ||  mxIsSparse(prhs[2])
	  || !mxIsDouble(prhs[2]) 
	  ||  mxGetN(prhs[2])!=1 ) {
	mexErrMsgTxt("3rd argument (c) must be "
		     "a column vector.");
	return;
      }
      if (n != 0 && n != mxGetM(prhs[2])) {
	mexErrMsgTxt("Dimension error (arg 3 and later).");
	return;
      }
      c = mxGetPr(prhs[2]);
      n = mxGetM(prhs[2]);
    }
  case 2:
    if (mxGetM(prhs[1]) != 0 || mxGetN(prhs[1]) != 0) {
      if (!mxIsNumeric(prhs[1]) || mxIsComplex(prhs[1]) 
	  || !mxIsSparse(prhs[1]) ) {
	mexErrMsgTxt("2nd argument (H) must be "
		     "a sparse matrix.");
	return;
      }
      if (n != 0 && n != mxGetM(prhs[1])) {
	mexErrMsgTxt("Dimension error (arg 2 and later).");
	return;
      }
      if (n != 0 && n != mxGetN(prhs[1])) {
		  mexErrMsgTxt("Dimension error (arg 2 and later).");
		  return;
      }
      n = mxGetN(prhs[1]);
      
      H = mxGetPr(prhs[1]);

#ifdef MX_COMPAT_32
      iH = mxGetIr(prhs[1]);
      kH = mxGetJc(prhs[1]);
#else
      iH_ = mxGetIr(prhs[1]);
      kH_ = mxGetJc(prhs[1]);

	  iH = (int*)malloc(mxGetNzmax(prhs[1])*sizeof(int)) ;
	  for (i=0; i<mxGetNzmax(prhs[1]); i++)
		  iH[i]=iH_[i] ;

	  kH = (int*)malloc((n+1)*sizeof(int)) ;
	  for (i=0; i<n+1; i++)
		  kH[i]=kH_[i] ;
#endif
	  
      nzH=myMalloc(n*sizeof(int)) ;
      for (i=0; i<n; i++)
		  nzH[i]=kH[i+1]-kH[i] ;
    }
  case 1:
	  if (mxGetM(prhs[0]) != 0 || mxGetN(prhs[0]) != 0) {
		  if (!mxIsNumeric(prhs[0]) || mxIsComplex(prhs[0]) 
			  ||  mxIsSparse(prhs[0])
	  || !mxIsDouble(prhs[0]) 
			  ||  mxGetN(prhs[0])!=1 ) {
			  mexErrMsgTxt("1st argument (cpenv) must be "
						   "a column vector.");
			  return;
      }
		  if (1 != mxGetM(prhs[0])) {
			  mexErrMsgTxt("Dimension error (arg 1).");
			  return;
		  }
		  cpenv = (long*) mxGetPr(prhs[0]);
	  }
  }
  /*if (display>3) */
	  fprintf(STD_OUT,"argument processing finished") ;
  
  /* Initialize the CPLEX environment */
  env = (CPXENVptr) cpenv[0] ;
  
  /* Turn on output to the screen */
  if (display>0)
	  status = CPXsetintparam (env, CPX_PARAM_SCRIND, CPX_ON);
  else
	  status = CPXsetintparam (env, CPX_PARAM_SCRIND, CPX_OFF);
  if ( status ) {
	  fprintf (STD_OUT, 
			   "Failure to turn on screen indicator, error %d.\n", status);
	  goto TERMINATE;
  }
  status = CPXsetintparam (env, CPX_PARAM_SIMDISPLAY, display);
  if ( status ) {
	  fprintf (STD_OUT,"Failed to turn up simplex display level.\n");
	  goto TERMINATE;
  }
  
  if (nlhs > 4 || nlhs < 1) {
	  mexErrMsgTxt("Usage: [x,lambda,how,p_qp] "
				   "= qp_solve(cpenv,H,c,A,b,l,u,neqcstr)");
	  return;
  }
  if (display>3) fprintf(STD_OUT, "(m=%i, n=%i, neq=%i) \n", m, n, neq) ;
  
  switch (nlhs) {
  case 4:
	  plhs[3] = mxCreateDoubleMatrix(1, 1, mxREAL);
	  p_qp = (long*) mxGetPr(plhs[3]);
  case 3:
	  /*    plhs[2] = mxCreateDoubleMatrix(1, 1, mxREAL);
			p_qpstat = mxGetPr(plhs[2]);*/
  case 2:
	  plhs[1] = mxCreateDoubleMatrix(m, 1, mxREAL);
	  lambda = mxGetPr(plhs[1]);
  case 1:
	  plhs[0] = mxCreateDoubleMatrix(n, 1, mxREAL);
	  x = mxGetPr(plhs[0]);
	  break;
  }
  if (display>2) fprintf(STD_OUT, "argument processing finished\n") ;
  
  if (strcmp(opt_method, "primal") &&  
	  strcmp(opt_method, "dual") &&
      strcmp(opt_method, "net") && 
	  strcmp(opt_method, "bar") &&
	  strcmp(opt_method, "sift") &&
	  strcmp(opt_method, "con") &&
	  strcmp(opt_method, "auto"))
	  mexErrMsgTxt("method \\in " 
				  "{'auto','primal','dual','bar','net','sift','con'}\n") ;

  if (strcmp(opt_method, "primal")==0)
	  status = CPXsetintparam (env, CPX_PARAM_QPMETHOD, 1);
  else if (strcmp(opt_method, "dual")==0)
	  status = CPXsetintparam (env, CPX_PARAM_QPMETHOD, 2);
  else if (strcmp(opt_method, "net")==0)
	  status = CPXsetintparam (env, CPX_PARAM_QPMETHOD, 3);
  else if (strcmp(opt_method, "bar")==0)
	  status = CPXsetintparam (env, CPX_PARAM_QPMETHOD, 4);
  else if (strcmp(opt_method, "sift")==0)
	  status = CPXsetintparam (env, CPX_PARAM_QPMETHOD, 5);
  else if (strcmp(opt_method, "con")==0)
	  status = CPXsetintparam (env, CPX_PARAM_QPMETHOD, 6);
  else if (strcmp(opt_method, "auto")==0)
	  status = CPXsetintparam (env, CPX_PARAM_QPMETHOD, 0);
  else 
	  status = 1 ;
  
  if ( status ) {
    fprintf (STD_OUT,"Failed to set QP method.\n");
    goto TERMINATE;
  }

  /* Create the problem */    
  if (display>2) fprintf(STD_OUT, "calling CPXcreateprob \n") ;
  qp = CPXcreateprob (env, &status, "xxx");
  if ( qp == NULL ) {
    fprintf (STD_OUT,"Failed to create subproblem\n");
    status = 1;
    goto TERMINATE;
  } 
  if (p_qp) *p_qp=(long) qp ;
  
  /* Copy network part of problem.  */    
  /*if (display>2) */
    fprintf(STD_OUT, "calling CPXcopylp (m=%i, n=%i) \n", m, n) ;
  status = CPXcopylp(env, qp, n, m, CPX_MIN, c, b, 
		     Sense, kA, nzA, iA, A, 
		     l, u, NULL);
  if ( status ) {
    fprintf (STD_OUT, "CPXcopylp failed.\n");
    goto TERMINATE;
  }
  
  /*if (display>2) */
    fprintf(STD_OUT, "calling CPXcopyquad \n") ;
  status = CPXcopyquad (env, qp, kH, nzH, iH, H);    
  
  if ( status ) {
    fprintf (STD_OUT, "CPXcopyquad failed.\n");
    goto TERMINATE;
  }
  
  /*if (display>2) */
    fprintf(STD_OUT, "calling optimizer 'bar'\n") ;
  status = CPXqpopt (env, qp);
  if (display>3)
    fprintf(STD_OUT, "CPXbaropt=%i\n", status) ;
  if ( status ) {
    fprintf (STD_OUT,"CPXbaropt failed.\n");
    goto TERMINATE;
  }
  
  if (display>2)
    fprintf(STD_OUT, "calling CPXsolution\n") ;
  status = CPXsolution (env, qp, &qpstat, &objval, x, lambda, NULL, NULL);
  if ( status ) {
    fprintf (STD_OUT,"CPXsolution failed.\n");
    goto TERMINATE;
  }
  
  if (display>1)
    fprintf (STD_OUT, "Solution status: %i,%s\n", qpstat, err_str[qpstat]);
  if (display>2)
    fprintf (STD_OUT, "Objective value %g\n", objval);
  
  if (nlhs >= 3) 
    if (qpstat==1)
      plhs[2] = mxCreateString(err_str[0]) ;
    else
      plhs[2] = mxCreateString(err_str[qpstat]) ;

  /*  if (nlhs >= 3) 
    if (qpstat==1)
      *p_qpstat = 0 ;
    else
    *p_qpstat = qpstat ;*/
  
 TERMINATE:
  if (status) {
    char  errmsg[1024];
    CPXgeterrorstring (env, status, errmsg);
    fprintf (STD_OUT, "%s", errmsg);
    if (nlhs >= 3) 
      plhs[2] = mxCreateString(errmsg) ;
    } ;
  if (nzA) myFree(nzA) ;
  if (nzH) myFree(nzH) ;
  if (Sense) myFree(Sense) ;

#ifndef MX_COMPAT_32
  if (iA) myFree(iA) ;
  if (kA) myFree(kA) ;
  if (iH) myFree(iH) ;
  if (kH) myFree(kH) ;
#endif
  
  if (!p_qp)
      {
	if ( qp != NULL ) {
	  if (display>2)
	    fprintf(STD_OUT, "calling CPXfreeprob\n") ;
	  status = CPXfreeprob (env, &qp);
	  if ( status ) {
	    fprintf (STD_OUT, "CPXfreeprob failed, error code %d.\n", status);
	  }
	}
      }
  return ;
}     
Exemplo n.º 2
0
long GenModelCplex::CreateModel()
{
    if(!binit)
        return ThrowError("CreateModel() not available : Problem not initialized yet");
	CplexData* d = (CplexData*)solverdata;
	int status = 0;
	d->nc = nc;
	d->nc = nc;
	d->onc = nc;
	d->onr = nr;

	if(boolParam.count("maximize") > 0 && boolParam["maximize"])
		CPXchgobjsen (d->env, d->lp, CPX_MAX);
	else
		CPXchgobjsen (d->env, d->lp, CPX_MIN);
	d->lrhs = new double[nr];
	d->urhs = new double[nr];
	d->sense = new char[nr];
	d->ub = new double[nc];
	d->lb = new double[nc];
	d->obj = new double[nc];
	d->type = new char[nc];
	d->mat_r = new int[nz];
	d->mat_c = new int[nz];
	d->mat_v = new double[nz];
	d->cname = new char*[nc];
	d->rname = new char*[nr];


	nz=0;
	for(unsigned long i = 0; i < nr; i++)
	{
		d->rname[i] = new char[consts[i].name.length()+1];
		snprintf(d->rname[i], consts[i].name.length()+1, "%s", consts[i].name.c_str());
		//printf("%ld %s: ", i, consts[i].name.c_str());
		for(unsigned long j = 0; j < consts[i].nz; j++)
		{
			d->mat_r[nz] = i;
			d->mat_c[nz] = consts[i].cols[j];
			d->mat_v[nz] = consts[i].coefs[j];
			//if(i >= 198)
				//printf("(%ld,%ld(%s),%f) ", d->mat_r[nz], d->mat_c[nz], vars.name[d->mat_c[nz]].c_str(), d->mat_v[nz]);
			nz++;
		}

		if(consts[i].lrhs == numeric_limits<double>::infinity())
			d->lrhs[i] = CPX_INFBOUND;
		else if(consts[i].lrhs == -numeric_limits<double>::infinity())
			d->lrhs[i] = -CPX_INFBOUND;
		else
			d->lrhs[i] = consts[i].lrhs;
		if(consts[i].urhs == numeric_limits<double>::infinity())
			d->urhs[i] = CPX_INFBOUND;
		else if(consts[i].urhs == -numeric_limits<double>::infinity())
			d->urhs[i] = -CPX_INFBOUND;
		else
			d->urhs[i] = consts[i].urhs-consts[i].lrhs;
		d->sense[i] = consts[i].sense;
	//	printf("%ld/%ld -> %c\n", i, nr, d->sense[i]);
	}
	for(unsigned long i = 0; i < nc; i++)
	{
		d->cname[i] = new char[vars.name[i].length()+1];
		snprintf(d->cname[i], vars.name[i].length()+1, "%s", vars.name[i].c_str());
		d->obj[i] = vars.obj[i];
		if(vars.ub[i] == numeric_limits<double>::infinity())
			d->ub[i] = CPX_INFBOUND;
		else if(vars.ub[i] == -numeric_limits<double>::infinity())
			d->ub[i] = -CPX_INFBOUND;
		else
			d->ub[i] = vars.ub[i];
		if(vars.lb[i] == numeric_limits<double>::infinity())
			d->lb[i] = CPX_INFBOUND;
		else if(vars.lb[i] == -numeric_limits<double>::infinity())
			d->lb[i] = -CPX_INFBOUND;
		else
			d->lb[i] = vars.lb[i];
		d->type[i] = vars.type[i];

		//printf("%ld (%s) -> %f %f %f %c\n", i, vars.name[i].c_str(), d->obj[i], d->lb[i], d->ub[i], d->type[i]);
	}
	status = CPXnewrows (d->env, d->lp, nr, d->lrhs, d->sense, d->urhs, d->rname);
	if ( status )
	{
		char  errmsg[1024];
		fprintf (stderr, "Could not create new rows.\n");
		CPXgeterrorstring (d->env, status, errmsg);
		fprintf (stderr, "%s", errmsg);
		return 1;
	}
	//else
		//printf("Row added!\n");

	if(boolParam.count("mip") > 0 && boolParam["mip"])
		status = CPXnewcols (d->env, d->lp, nc, d->obj, d->lb, d->ub, d->type, d->cname);
	else
		status = CPXnewcols (d->env, d->lp, nc, d->obj, d->lb, d->ub, NULL, NULL);
	if ( status )
	{
		char  errmsg[1024];
		fprintf (stderr, "Could not create new cols.\n");
		CPXgeterrorstring (d->env, status, errmsg);
		fprintf (stderr, "%s", errmsg);
		return 1;
	}
	//status = CPXnewcols (env, lp, nc, obj, lb, ub, NULL, colname);
	if ( status )
		return 1;
	//else
		//printf("Col added!\n");
	status = CPXchgcoeflist (d->env, d->lp, nz, d->mat_r, d->mat_c, d->mat_v);
	if ( status )
		return 1;

	vector<long>::iterator iti;
	vector<long>::iterator itj = vars.qj.begin();
	vector<double>::iterator itv = vars.qobj.begin();

	vector<vector<pair<int,double> > > qptemp;
	qptemp.resize(nc);
	int* qpbeg = NULL;
	int* qpnum = NULL;
	int* qpind = NULL;
	double* qpv = NULL;
	int qpnz = 0;

	if(!vars.qi.empty())
	{
        boolParam["qp"] = true;
		qpbeg = new int[nc];
		qpnum = new int[nc];
	}
    if(boolParam.count("qp_mat") == 0 || boolParam["qp_mat"])
    {
        for(iti = vars.qi.begin(); iti != vars.qi.end(); iti++, itj++, itv++)
        {
            qptemp[*iti].push_back(pair<int, double>(*itj,*itv));
            qpnz++;
            if(*iti != *itj)
            {
                qptemp[*itj].push_back(pair<int, double>(*iti,*itv));
                qpnz++;
            }
        }
        if(!vars.qi.empty())
        {
            qpv = new double[qpnz];
            qpind = new int[qpnz];
            
            qpnz=0;
            for(int i = 0; i < int(nc); i++)
            {
                qpbeg[i] = qpnz;
                qpnum[i] = int(qptemp[i].size());
                for(int j = 0; j < int(qptemp[i].size()); j++)
                {
                    qpind[qpnz] = qptemp[i][j].first;
                    qpv[qpnz] = 2.0*qptemp[i][j].second;
                    qpnz++;
                }
            }
            status = CPXcopyquad(d->env, d->lp, qpbeg, qpnum, qpind, qpv);
            delete[] qpbeg;
            delete[] qpnum;
            delete[] qpind;
            delete[] qpv;
        }
        if ( status )
        {
            printf("QP problem!\n");
            return 1;
        }
	}
	//else
		//printf("Coefs added!\n");
    bcreated = true;

	return 0;
}
Exemplo n.º 3
0
int
main (void)
{
/* Declare pointers for the variables and arrays that will contain
   the data which define the LP problem.  The setproblemdata() routine
   allocates space for the problem data.  */

   char     *probname = NULL;
   int      numcols;
   int      numrows;
   int      objsen;
   double   *obj = NULL;
   double   *rhs = NULL;
   char     *sense = NULL;
   int      *matbeg = NULL;
   int      *matcnt = NULL;
   int      *matind = NULL;
   double   *matval = NULL;
   double   *lb = NULL;
   double   *ub = NULL;
   char     *ctype = NULL;
   int      *qmatbeg = NULL;
   int      *qmatcnt = NULL;
   int      *qmatind = NULL;
   double   *qmatval = NULL;

   /* Declare and allocate space for the variables and arrays where we will
      store the optimization results including the status, objective value,
      variable values, and row slacks. */

   int      solstat;
   double   objval;
   double   x[NUMCOLS];
   double   slack[NUMROWS];


   CPXENVptr     env = NULL;
   CPXLPptr      lp = NULL;
   int           status;
   int           i, j;
   int           cur_numrows, cur_numcols;

   /* Initialize the CPLEX environment */

   env = CPXopenCPLEX (&status);

   /* If an error occurs, the status value indicates the reason for
      failure.  A call to CPXgeterrorstring will produce the text of
      the error message.  Note that CPXopenCPLEX produces no output,
      so the only way to see the cause of the error is to use
      CPXgeterrorstring.  For other CPLEX routines, the errors will
      be seen if the CPXPARAM_ScreenOutput indicator is set to CPX_ON.  */

   if ( env == NULL ) {
      char  errmsg[CPXMESSAGEBUFSIZE];
      fprintf (stderr, "Could not open CPLEX environment.\n");
      CPXgeterrorstring (env, status, errmsg);
      fprintf (stderr, "%s", errmsg);
      goto TERMINATE;
   }

   /* Turn on output to the screen */

   status = CPXsetintparam (env, CPXPARAM_ScreenOutput, CPX_ON);
   if ( status ) {
      fprintf (stderr,
               "Failure to turn on screen indicator, error %d.\n", status);
      goto TERMINATE;
   }

   /* Fill in the data for the problem.  */

   status = setproblemdata (&probname, &numcols, &numrows, &objsen, &obj,
                            &rhs, &sense, &matbeg, &matcnt, &matind, &matval,
                            &lb, &ub, &ctype, &qmatbeg, &qmatcnt, &qmatind,
                            &qmatval);
   if ( status ) {
      fprintf (stderr, "Failed to build problem data arrays.\n");
      goto TERMINATE;
   }

   /* Create the problem. */

   lp = CPXcreateprob (env, &status, probname);

   /* A returned pointer of NULL may mean that not enough memory
      was available or there was some other problem.  In the case of
      failure, an error message will have been written to the error
      channel from inside CPLEX.  In this example, the setting of
      the parameter CPXPARAM_ScreenOutput causes the error message to
      appear on stdout.  */

   if ( lp == NULL ) {
      fprintf (stderr, "Failed to create LP.\n");
      goto TERMINATE;
   }

   /* Now copy the problem data into the lp */

   status = CPXcopylp (env, lp, numcols, numrows, objsen, obj, rhs,
                       sense, matbeg, matcnt, matind, matval,
                       lb, ub, NULL);

   if ( status ) {
      fprintf (stderr, "Failed to copy problem data.\n");
      goto TERMINATE;
   }

   /* Now copy the ctype array */

   status = CPXcopyctype (env, lp, ctype);
   if ( status ) {
      fprintf (stderr, "Failed to copy ctype\n");
      goto TERMINATE;
   }

   status = CPXcopyquad (env, lp, qmatbeg, qmatcnt, qmatind, qmatval);
   if ( status ) {
      fprintf (stderr, "Failed to copy quadratic matrix.\n");
      goto TERMINATE;
   }


   /* Optimize the problem and obtain solution. */

   status = CPXmipopt (env, lp);
   if ( status ) {
      fprintf (stderr, "Failed to optimize MIQP.\n");
      goto TERMINATE;
   }

   solstat = CPXgetstat (env, lp);

   /* Write the output to the screen. */

   printf ("\nSolution status = %d\n", solstat);

   status = CPXgetobjval (env, lp, &objval);
   if ( status ) {
      fprintf (stderr,"No MIQP objective value available.  Exiting...\n");
      goto TERMINATE;
   }

   printf ("Solution value  = %f\n\n", objval);

   /* The size of the problem should be obtained by asking CPLEX what
      the actual size is, rather than using what was passed to CPXcopylp.
      cur_numrows and cur_numcols store the current number of rows and
      columns, respectively.  */

   cur_numrows = CPXgetnumrows (env, lp);
   cur_numcols = CPXgetnumcols (env, lp);

   status = CPXgetx (env, lp, x, 0, cur_numcols-1);
   if ( status ) {
      fprintf (stderr, "Failed to get optimal integer x.\n");
      goto TERMINATE;
   }

   status = CPXgetslack (env, lp, slack, 0, cur_numrows-1);
   if ( status ) {
      fprintf (stderr, "Failed to get optimal slack values.\n");
      goto TERMINATE;
   }

   for (i = 0; i < cur_numrows; i++) {
      printf ("Row %d:  Slack = %10f\n", i, slack[i]);
   }

   for (j = 0; j < cur_numcols; j++) {
      printf ("Column %d:  Value = %10f\n", j, x[j]);
   }

   /* Finally, write a copy of the problem to a file. */

   status = CPXwriteprob (env, lp, "miqpex1.lp", NULL);
   if ( status ) {
      fprintf (stderr, "Failed to write LP to disk.\n");
      goto TERMINATE;
   }


TERMINATE:

   /* Free up the problem as allocated by CPXcreateprob, if necessary */

   if ( lp != NULL ) {
      status = CPXfreeprob (env, &lp);
      if ( status ) {
         fprintf (stderr, "CPXfreeprob failed, error code %d.\n", status);
      }
   }

   /* Free up the CPLEX environment, if necessary */

   if ( env != NULL ) {
      status = CPXcloseCPLEX (&env);

      /* Note that CPXcloseCPLEX produces no output,
         so the only way to see the cause of the error is to use
         CPXgeterrorstring.  For other CPLEX routines, the errors will
         be seen if the CPXPARAM_ScreenOutput indicator is set to CPX_ON. */

      if ( status ) {
         char  errmsg[CPXMESSAGEBUFSIZE];
         fprintf (stderr, "Could not close CPLEX environment.\n");
         CPXgeterrorstring (env, status, errmsg);
         fprintf (stderr, "%s", errmsg);
      }
   }

   /* Free up the problem data arrays, if necessary. */

   free_and_null ((char **) &probname);
   free_and_null ((char **) &obj);
   free_and_null ((char **) &rhs);
   free_and_null ((char **) &sense);
   free_and_null ((char **) &matbeg);
   free_and_null ((char **) &matcnt);
   free_and_null ((char **) &matind);
   free_and_null ((char **) &matval);
   free_and_null ((char **) &lb);
   free_and_null ((char **) &ub);
   free_and_null ((char **) &ctype);
   free_and_null ((char **) &qmatbeg);
   free_and_null ((char **) &qmatcnt);
   free_and_null ((char **) &qmatind);
   free_and_null ((char **) &qmatval);

   return (status);

}  /* END main */