コード例 #1
0
ファイル: GenModelCplex.cpp プロジェクト: StebQC/GenModel
long GenModelCplex::Solve()
{
    if(!bcreated)
        throw string("Solve() not available : Problem not created yet");
    CplexData* d = static_cast<CplexData*>(solverdata);
    int status = 0;
    if(boolParam.count("qp") > 0 && boolParam["qp"])
        status = CPXqpopt(d->env, d->lp);
    else if(boolParam.count("mip") > 0 && boolParam["mip"])
        status = CPXmipopt(d->env, d->lp);
    else if(strParam.count("algo") > 0 && strParam["algo"] == "interior")
        status = CPXbaropt(d->env, d->lp);
    else if(strParam.count("algo") > 0 && strParam["algo"] == "dual")
        status = CPXdualopt(d->env, d->lp);
    else if(strParam.count("algo") > 0 && strParam["algo"] == "primal")
        status = CPXprimopt(d->env, d->lp);
    else if(strParam.count("algo") > 0 && strParam["algo"] == "concurrent")
    {
        //printf("choosing concurrent algo\n");
        CPXsetintparam (d->env, CPX_PARAM_LPMETHOD, CPX_ALG_CONCURRENT);
        status = CPXlpopt(d->env, d->lp);
    }
    else if(strParam.count("algo") > 0 && strParam["algo"] == "sifting")
    {
        CPXsetintparam (d->env, CPX_PARAM_LPMETHOD, CPX_ALG_SIFTING);
        status = CPXlpopt(d->env, d->lp);
    }
    else
        status = CPXlpopt(d->env, d->lp);

    return 0;
}
コード例 #2
0
ファイル: indefqpex1.c プロジェクト: andreasmattas/testcode
static int
optimize_and_report (CPXENVptr env, CPXLPptr lp,
                     int *solstat_p, double *objval_p)
{
    int status = 0;

    double   x[NUMCOLS];
    double   pi[TOTROWS];
    double   slack[TOTROWS];
    double   dj[NUMCOLS];

    int      i, j;
    int      cur_numrows, cur_numcols;

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

    status = CPXsolution (env, lp, solstat_p, objval_p, x, pi, slack, dj);
    if ( status ) {
        fprintf (stderr, "Failed to obtain solution.\n");
        goto TERMINATE;
    }


    /* Write the output to the screen. */

    printf ("\nSolution status = %d\n", *solstat_p);
    printf ("Solution value  = %f\n\n", *objval_p);

    /* 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);
    for (i = 0; i < cur_numrows; i++) {
        printf ("Row %d:  Slack = %10f  Pi = %10f\n", i, slack[i], pi[i]);
    }

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

TERMINATE:

    return (status);

}  /* END optimize_and_report */
コード例 #3
0
ファイル: globalqpex1.c プロジェクト: annaPolytech/PRD
int
main (int argc, char *argv[])
{
   /* Declare and allocate space for the variables and arrays where we will
      store the optimization results including the status, objective value,
      maximum bound violation, variable values, and basis. */

   int      solnstat, solnmethod, solntype;
   double   objval, maxviol;
   double   *x     = NULL;
   int      *cstat = NULL;
   int      *rstat = NULL;

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

   char          *basismsg;

   /* Check the command line arguments */

   if (( argc != 3 )                         ||
       ( strchr ("cfg", argv[2][0]) == NULL )  ) {
      usage (argv[0]);
      goto TERMINATE;
   }

   /* 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;
   }

   /* Create the problem, using the filename as the problem name */

   lp = CPXcreateprob (env, &status, argv[1]);

   /* 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.  Note that most CPLEX routines return
      an error code to indicate the reason for failure.   */

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

   /* Now read the file, and copy the data into the created lp */

   status = CPXreadcopyprob (env, lp, argv[1], NULL);
   if ( status ) {
      fprintf (stderr, "Failed to read and copy the problem data.\n");
      goto TERMINATE;
   }

   if ( CPXgetprobtype (env, lp) != CPXPROB_QP ) {
      fprintf (stderr, "Input file is not a QP.  Exiting.\n");
      goto TERMINATE;
   }

   /* Optimize the problem and obtain solution. */

   switch (argv[2][0]) {
      case 'c':
         status = CPXsetintparam (env, CPXPARAM_SolutionTarget,
                                  CPX_SOLUTIONTARGET_OPTIMALCONVEX);
         if ( status ) goto TERMINATE;

         status = CPXqpopt (env, lp);
         if ( status ) {
            if ( status == CPXERR_Q_NOT_POS_DEF )
               printf ("Problem is not convex. Use argument f to get local optimum "
                       "or g to get global optimum.\n");
            else
               fprintf (stderr, "Failed to optimize QP.\n");
            goto TERMINATE;
         }
            
         break;
      case 'f':
         status = CPXsetintparam (env, CPXPARAM_SolutionTarget,
                                  CPX_SOLUTIONTARGET_FIRSTORDER);
         if ( status ) goto TERMINATE;

         status = CPXqpopt (env, lp);
         if ( status ) {
            fprintf (stderr, "Failed to optimize QP.\n");
            goto TERMINATE;
         }
         break;
      case 'g':
         status = CPXsetintparam (env, CPXPARAM_SolutionTarget,
                                  CPX_SOLUTIONTARGET_OPTIMALGLOBAL);
         if ( status ) goto TERMINATE;

         status = CPXqpopt (env, lp);
         if ( status ) {
            fprintf (stderr, "Failed to optimize noncvonex QP.\n");
            goto TERMINATE;
         }
         break;
      default:
         break;
   }

   solnstat = CPXgetstat (env, lp);

   if      ( solnstat == CPXMIP_UNBOUNDED  ||
             solnstat == CPX_STAT_UNBOUNDED  ) {
      printf ("Model is unbounded\n");
      goto TERMINATE;
   }
   else if ( solnstat == CPXMIP_INFEASIBLE  ||
             solnstat == CPX_STAT_INFEASIBLE  ) {
      printf ("Model is infeasible\n");
      goto TERMINATE;
   }
   else if ( solnstat == CPX_STAT_INForUNBD ) {
      printf ("Model is infeasible or unbounded\n");
      goto TERMINATE;
   }

   status = CPXsolninfo (env, lp, &solnmethod, &solntype, NULL, NULL);
   if ( status ) {
      fprintf (stderr, "Failed to obtain solution info.\n");
      goto TERMINATE;
   }
   printf ("Solution status %d, solution method %d\n", solnstat, solnmethod);

   if ( solntype == CPX_NO_SOLN ) {
      fprintf (stderr, "Solution not available.\n");
      goto TERMINATE;
   }

   status = CPXgetobjval (env, lp, &objval);
   if ( status ) {
      fprintf (stderr, "Failed to obtain objective value.\n");
      goto TERMINATE;
   }
   printf ("Objective value %.10g.\n", objval);


   /* The size of the problem should be obtained by asking CPLEX what
      the actual size is.  cur_numrows and cur_numcols store the
      current number of rows and columns, respectively.  */

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

   /* Retrieve basis, if one is available */

   if ( solntype == CPX_BASIC_SOLN ) {
      cstat = (int *) malloc (cur_numcols*sizeof(int));
      rstat = (int *) malloc (cur_numrows*sizeof(int));
      if ( cstat == NULL || rstat == NULL ) {
         fprintf (stderr, "No memory for basis statuses.\n");
         goto TERMINATE;
      }

      status = CPXgetbase (env, lp, cstat, rstat);
      if ( status ) {
         fprintf (stderr, "Failed to get basis; error %d.\n", status);
         goto TERMINATE;
      }
   }
   else {
      printf ("No basis available\n");
   }

   /* Retrieve solution vector */

   x = (double *) malloc (cur_numcols*sizeof(double));
   if ( x == NULL ) {
      fprintf (stderr, "No memory for solution.\n");
      goto TERMINATE;
   }

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

   /* Write out the solution */

   for (j = 0; j < cur_numcols; j++) {
      printf ( "Column %d:  Value = %17.10g", j, x[j]);
      if ( cstat != NULL ) {
         switch (cstat[j]) {
            case CPX_AT_LOWER:
               basismsg = "Nonbasic at lower bound";
               break;
            case CPX_BASIC:
               basismsg = "Basic";
               break;
            case CPX_AT_UPPER:
               basismsg = "Nonbasic at upper bound";
               break;
            case CPX_FREE_SUPER:
               basismsg = "Superbasic, or free variable at zero";
               break;
            default:
               basismsg = "Bad basis status";
               break;
         }
         printf ("  %s",basismsg);
      }
      printf ("\n");
   }

   /* Display the maximum bound violation. */

   status = CPXgetdblquality (env, lp, &maxviol, CPX_MAX_PRIMAL_INFEAS);
   if ( status ) {
      fprintf (stderr, "Failed to obtain bound violation.\n");
      goto TERMINATE;
   }
   printf ("Maximum bound violation = %17.10g\n", maxviol);

TERMINATE:

   /* Free up the basis and solution */

   free_and_null ((char **) &cstat);
   free_and_null ((char **) &rstat);
   free_and_null ((char **) &x);

   /* Free up the problem, 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);
      }
   }

   return (status);

}  /* END main */
コード例 #4
0
ファイル: CPLEXapi.cpp プロジェクト: aabest/Model-SEED-core
OptSolutionData* CPLEXRunSolver(int ProbType) {
	OptSolutionData* NewSolution = NULL;
	int Status = 0;
	if (ProbType == LP) {
		Status = CPXsetintparam (CPLEXenv, CPX_PARAM_LPMETHOD, CPX_ALG_AUTOMATIC);
		if (Status) {
			FErrorFile() << "Failed to set the optimization method." << endl;
			FlushErrorFile();
			return NULL;
		}
		Status = CPXsetintparam (CPLEXenv, CPX_PARAM_SIMDISPLAY, 0);
		if (Status) {
			FErrorFile() << "Failed to set the optimization method." << endl;
			FlushErrorFile();
			return NULL;
		}
		Status = CPXchgprobtype(CPLEXenv, CPLEXModel, CPXPROB_LP);
		Status = CPXlpopt(CPLEXenv, CPLEXModel);
	} else if(ProbType == MILP || ProbType == MIQP) {
		//Setting the bound tightening on high
		Status = CPXsetintparam (CPLEXenv, CPX_PARAM_BNDSTRENIND, 1);
		if (Status) {
			FErrorFile() << "Failed to set the optimization method." << endl;
			FlushErrorFile();
			return NULL;
		}
		//Setting tolerance to 1e-9 instead of 1e-6
		double tolerance = atof(GetParameter("Solver tolerance").data());
		Status = CPXsetdblparam(CPLEXenv,CPX_PARAM_EPRHS, tolerance);
		if (Status) {
			FErrorFile() << "Failed to set the optimization method." << endl;
			FlushErrorFile();
			return NULL;
		}
		Status = CPXsetdblparam(CPLEXenv,CPX_PARAM_EPINT, tolerance);
		if (Status) {
			FErrorFile() << "Failed to set the optimization method." << endl;
			FlushErrorFile();
			return NULL;
		}
		//Deactivates all messages from MIP solver
		Status = CPXchgprobtype(CPLEXenv, CPLEXModel, CPXPROB_MILP);
		Status = CPXmipopt (CPLEXenv, CPLEXModel);
	} else if(ProbType == QP) {
		Status = CPXqpopt (CPLEXenv, CPLEXModel);
	}
	if (Status ) {
		cout << "Failed to optimize LP." << endl;
		return NULL;
	}
	int Temp = CPXgetstat (CPLEXenv, CPLEXModel);
	NewSolution = new OptSolutionData;
	if (Temp == CPX_STAT_UNBOUNDED) {
		cout << "Model is unbounded" << endl;
		FErrorFile() << "Model is unbounded" << endl;
		FlushErrorFile();
		NewSolution->Status = UNBOUNDED;
		return NewSolution;
	} else if (Temp == CPX_STAT_INFEASIBLE) {
		cout << "Model is infeasible" << endl;
		FErrorFile() << "Model is infeasible" << endl;
		FlushErrorFile();
		NewSolution->Status = INFEASIBLE;
		return NewSolution;
	} else if (Temp == CPX_STAT_INForUNBD ) {
		cout << "Model is infeasible or unbounded" << endl;
		FErrorFile() << "Model is infeasible or unbounded" << endl;
		FlushErrorFile();
		NewSolution->Status = INFEASIBLE;
		return NewSolution;
	} else {
		NewSolution->Status = SUCCESS;
	}

	int NumberColumns = CPXgetnumcols (CPLEXenv, CPLEXModel);
	int NumberRows = CPXgetnumrows (CPLEXenv, CPLEXModel);
	NewSolution->NumVariables = NumberColumns;
	NewSolution->SolutionData.resize(NumberColumns);

	double* x = new double[NumberColumns];
	
	if (ProbType == MILP || ProbType == MIQP) {
		Status = CPXgetmipobjval (CPLEXenv, CPLEXModel, &(NewSolution->Objective));
		Status = CPXgetmipx (CPLEXenv, CPLEXModel, x, 0, NumberColumns-1);
	} else {
		Status = CPXsolution(CPLEXenv,CPLEXModel,NULL,&(NewSolution->Objective),x,NULL,NULL,NULL);
	}
	
	if ( Status ) {
		cout << "Failed to obtain objective value." << endl;
		delete [] x;
		NewSolution->Status = INFEASIBLE;
		return NewSolution;
	}

	cout << "Objective value: " << NewSolution->Objective << endl;
	/*
	string* StrNames = new string[NumberColumns];
	char** Names = new char*[NumberColumns];
	char* NameStore = new char[7*NumberColumns];
	int Surplus = 0;

	Status = CPXgetcolname(CPLEXenv, CPLEXModel, Names, NameStore, 7*NumberColumns, &Surplus, 0, NumberColumns-1);
	if (Status) {
		FErrorFile() << "Failed to get column names." << endl;
		FlushErrorFile();
		delete [] StrNames;
		delete [] Names;
		delete [] NameStore;
		delete [] x;
		delete NewSolution;
		return NULL;
	}
	*/
	for (int i=0; i < NumberColumns; i++) {
		//StrNames[i].assign(Names[i]);
		//StrNames[i] = StrNames[i].substr(1, StrNames[i].length()-1);
		//NewSolution->SolutionData[atoi(StrNames[i].data())-1] = x[i];
		NewSolution->SolutionData[i] = x[i];
	}
	/*
	delete [] StrNames;
	delete [] Names;
	delete [] NameStore;
	*/
	delete [] x;

	return NewSolution;
}
コード例 #5
0
ファイル: qpex1.c プロジェクト: annaPolytech/PRD
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;
   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, dual values, row slacks and variable
      reduced costs. */

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


   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, &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 problem.\n");
      goto TERMINATE;
   }

   /* Now copy the LP part of 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;
   }

   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 = CPXqpopt (env, lp);
   if ( status ) {
      fprintf (stderr, "Failed to optimize QP.\n");
      goto TERMINATE;
   }

   status = CPXsolution (env, lp, &solstat, &objval, x, pi, slack, dj);
   if ( status ) {
      fprintf (stderr, "Failed to obtain solution.\n");
      goto TERMINATE;
   }


   /* Write the output to the screen. */

   printf ("\nSolution status = %d\n", solstat);
   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);
   for (i = 0; i < cur_numrows; i++) {
      printf ("Row %d:  Slack = %10f  Pi = %10f\n", i, slack[i], pi[i]);
   }

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

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

   status = CPXwriteprob (env, lp, "qpex1.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 **) &qmatbeg);
   free_and_null ((char **) &qmatcnt);
   free_and_null ((char **) &qmatind);
   free_and_null ((char **) &qmatval);

   return (status);

}  /* END main */
コード例 #6
0
ファイル: qp_solve.c プロジェクト: ratschlab/HMSVMToolbox
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 ;
}     
コード例 #7
0
EXPORT int fit(const double * X_p, const double * Yl_p, double* w, int postags, int numSamples, int numFeatures, double C, double epsilon,
        int numBoxConstraints, const double * boxValues, const int64_t * boxIndices, const double * boxMatrix)
{
  int i,j,k;
  CPXENVptr     env = NULL;
  CPXLPptr      lp = NULL;
  int status;
  char probname[] = "Testproblem";

  int numrows = postags + numSamples;
  int numcols = numFeatures + 1 + numrows;
  int nnzcol = numFeatures + 2;
  int numEntries = nnzcol * numrows;

  char *sense = (char*) malloc((numrows) * sizeof(char));
  double *lb = (double*) malloc(numcols * sizeof(double));
  double *ub = (double*) malloc(numcols * sizeof(double));
  double *obj = (double*) malloc(numcols * sizeof(double));
  double *rhs = (double*) malloc(numrows * sizeof(double));
  double *tagarray = (double*) malloc(numrows * sizeof(double));

  int      *matbeg = (int*) malloc(numcols * sizeof(int));
  int      *matcnt = (int*) malloc(numcols * sizeof(int));
  int      *matind = (int*) malloc(numEntries * sizeof(int));
  double   *matval = (double* ) malloc(numEntries * sizeof(double));
  double   *qsepvec = (double*) malloc((numcols + 2 * numBoxConstraints) * sizeof(double));

  int       numBoxSamples = 0;
  double   *dens = NULL;
  double   *boxConstraints = NULL;
  int      *boxrmatbeg = NULL;
  int      *boxrmatind = NULL;
  char     *boxSense = NULL;
  int      *hmatbeg = NULL;
  int      *hmatind = NULL;
  double   *hmatval = NULL;
  char     *hSense = NULL;
  env = CPXopenCPLEX (&status);
  lp = CPXcreateprob (env, &status, probname);
  status = CPXsetintparam (env, CPX_PARAM_SCRIND, CPX_OFF);
  status = CPXsetintparam (env, CPX_PARAM_BARCOLNZ, 2);
  if ( status ) {
    fprintf (stderr,
             "Failure to create CPLEX environment, error %d.\n", status);
    goto TERMINATE;
  }

  if (sense == NULL || lb == NULL || ub == NULL || obj == NULL
      || rhs == NULL || tagarray == NULL || qsepvec == NULL) {
    status = 1;
    goto TERMINATE;
  }


  for (i = 0; i < postags; ++i) {
    tagarray[i] = 1;
    sense[i] = 'G';
  }
  for (i = postags; i < numrows; ++i) {
    tagarray[i] = -1;
    sense[i] = 'L';
  }
  for (i = 0; i < postags; ++i) {
    rhs[i] = Yl_p[i] - tagarray[i] * epsilon ;
  }
  for (i = postags; i < numrows; ++i) {
    rhs[i] = Yl_p[i - postags] - tagarray[i] * epsilon ;
  }
  for (i = 0; i < numFeatures + 1; ++i) {
    lb[i] = -CPX_INFBOUND;
    ub[i] = CPX_INFBOUND;
    matbeg[i] = i * (numrows);
    matcnt[i] = numrows;
  }
  for (i = numFeatures + 1; i < numcols; ++i) {
    lb[i] = 0;
    ub[i] = CPX_INFBOUND;
    matbeg[i] = (numFeatures + 1) * numrows + (i - numFeatures - 1);
    matcnt[i] = 1;
  }

  for (j = 0; j < numFeatures; ++j) {
    for (i = 0; i < postags; ++i) {
      matind[j * numrows + i] = i;
      matval[j * (numrows) + i] = X_p[i * numFeatures + j];
    }
    for (i = postags; i < numrows; ++i) {
      matind[j * numrows + i] = i;
      matval[j * (numrows) + i] = X_p[(i - postags) * numFeatures + j];
    }
  }

  /* printf("Status ok\n");*/
  for (i = 0; i < numrows; ++i) {
    matind[numFeatures * numrows + i] = i;
    matval[numFeatures * numrows + i] = 1;
  }

  for (i = 0; i < numrows; ++i) {
    matind[(numFeatures + 1) * numrows + i] = i;
    matval[(numFeatures + 1) * numrows + i] = tagarray[i];
  }

  for (i = 0; i < numFeatures; ++i){
    qsepvec[i] = 1;
    obj[i] = 0;
  }
  obj[numFeatures] = 0;
  qsepvec[numFeatures] = 0;
  for (i = numFeatures + 1; i < numcols; ++i){
    qsepvec[i] = 2 * C;
    obj[i] = 0;
  }

  /*printf("Status ok\n");*/
  status = CPXcopylp (env, lp, numcols, numrows, 1, obj, rhs,
                      sense, matbeg, matcnt, matind, matval,
                      lb, ub, NULL);

  status = CPXcopyqpsep (env, lp, qsepvec);
  status = CPXwriteprob (env, lp, "qpex1.lp", NULL);
  status = CPXqpopt (env, lp);
  status = CPXgetx (env, lp, w, 0, numFeatures);

  if (numBoxConstraints > 0) {
    numBoxSamples = (int) boxIndices[numBoxConstraints];

    dens = (double*) malloc(numBoxSamples * sizeof(double));
    boxConstraints = (double*) calloc(numBoxConstraints * (numFeatures + 2), sizeof(double));
    boxrmatbeg = (int*) malloc(numBoxConstraints * sizeof(int));
    boxrmatind = (int*) malloc(numBoxConstraints * (numFeatures + 2) * sizeof(int));
    boxSense = (char*) malloc(numBoxConstraints * sizeof(char));

    hmatbeg = (int*) malloc(numBoxSamples * sizeof(int));
    hmatind = (int*) malloc(numBoxSamples * (numFeatures + 1) * sizeof(int));
    hmatval = (double* ) malloc(numBoxSamples * (numFeatures + 1) * sizeof(double));
    hSense = (char* ) malloc(numBoxSamples * sizeof(char));

    if (dens == NULL || boxConstraints == NULL || boxrmatbeg == NULL || boxrmatind == NULL
        || boxSense == NULL) {
      status = 1;
      goto TERMINATE;
    }
    if (hmatbeg == NULL || hmatind == NULL || hmatval == NULL || hSense == NULL) {
      status = 1;
      goto TERMINATE;
    }

    /*for every entry in the box features, check if it's background or
    foreground
    double   *boxrmatval = (double* ) malloc( * sizeof(double));*/
    
    for (i = 0; i < numBoxSamples; ++i) {
      dens[i] = w[numFeatures];
      for (j = 0; j < numFeatures; ++j) {
        dens[i] += boxMatrix[i * numFeatures + j] * w[j];
      }
    }
    for (i = 0; i < numBoxSamples; ++i) {
      if (dens[i] > 0){
        dens[i] = 1;
      }
      else {
        dens[i] = 0;
      }
      /* printf("Density: %f\n", dens[i]); */
    }

    /*printfarray(boxConstraints, numBoxConstraints, numFeatures + 2, "boxConstraints"); */

    for (k = 0; k < numBoxConstraints; ++k) {
      boxrmatbeg[k] = k * (numFeatures + 2);
      for (i = (int) boxIndices[k]; i < boxIndices[k + 1]; ++i){
        for (j = 0; j < numFeatures; ++j){
          boxConstraints[k * (numFeatures + 2) + j]  += dens[i] * boxMatrix[i * numFeatures + j];
        }
        boxConstraints[k * (numFeatures + 2) + numFeatures] += dens[i];
      }
    }
    for (i = 0; i < numBoxConstraints; ++i) {
      for (j = 0; j < numFeatures + 1; ++j) {
        boxrmatind[i * (numFeatures + 2) + j] = j;
      }
      boxrmatind[i * (numFeatures + 2) + numFeatures + 1] = numcols + i;
    }

    for (i = 0; i < numBoxConstraints; ++i) {
      boxSense[i] = 'L';
    }
    for (i = 0; i < numBoxConstraints; ++i) {
      boxConstraints[i * (numFeatures + 2) + numFeatures+1] = - 1;
    }
    status = CPXaddrows(env, lp, numBoxConstraints, numBoxConstraints, numBoxConstraints * (numFeatures + 2), boxValues,
                        boxSense, boxrmatbeg, boxrmatind, boxConstraints, NULL, NULL);
    for (i = 0; i < numBoxConstraints; ++i) {
      boxrmatind[i * (numFeatures + 2) + numFeatures + 1] = numcols + numBoxConstraints + i;
    }

    for (i = 0; i < numBoxConstraints; ++i) {
      boxSense[i] = 'G';
    }
    for (i = 0; i < numBoxConstraints; ++i) {
      boxConstraints[i * (numFeatures + 2) + numFeatures+1] = + 1;
    }
    status = CPXaddrows(env, lp, numBoxConstraints, numBoxConstraints, numBoxConstraints * (numFeatures + 2), boxValues,
                        boxSense, boxrmatbeg, boxrmatind, boxConstraints, NULL, NULL);

    for (i = 0; i < numBoxConstraints; ++i) {
      qsepvec[numcols + i] = 2 * C / (boxIndices[i + 1] - boxIndices[i]);
      qsepvec[numcols + i + numBoxConstraints] = 2 * C / (boxIndices[i + 1] - boxIndices[i]);
    /*  printf("%d, %d\n",boxIndices[i], boxIndices[i + 1]);
      printf("%f, %f\n", qsepvec[numcols+i], qsepvec[numcols + i + numBoxConstraints]);
      */
    }

    /*adding hard constraints:*/



    for (i = 0; i < numBoxSamples; ++i) {
      hmatbeg[i] = i * (numFeatures + 1);
      for (j = 0; j < numFeatures; ++j) {
        hmatind[i * (numFeatures + 1) + j] = j;
        hmatval[i * (numFeatures + 1) + j] = boxMatrix[i * numFeatures + j];
      }
      hmatind[i * (numFeatures + 1) + numFeatures] = numFeatures;
      hmatval[i * (numFeatures + 1) + numFeatures] = 1;

      if (dens[i] == 0){
        hSense[i]  = 'L';
      }
      else {
        hSense[i] = 'G';
      }
    }
    /* printf("Density: %f\n", dens[i]);
        printf("Close, but no cigar\n");
    printiarray(hmatind, backgroundcount, numFeatures + 1, "");
        printf("Close, but no cigar\n"); */

    status = CPXaddrows(env, lp, 0, numBoxSamples, numBoxSamples* (numFeatures + 1), NULL,
                        hSense, hmatbeg, hmatind, hmatval, NULL, NULL);
    /*    printf("WHY IS NOTHING HAPPENING\n") */
    printf ("Number of Columns in Problem: %d\n", CPXgetnumcols(env, lp));
    printf("%d\n", numcols + (2 * numBoxConstraints));
    status = CPXcopyqpsep (env, lp, qsepvec);
    status = CPXwriteprob (env, lp, "qpex1.lp", NULL);
    status = CPXqpopt (env, lp);
    status = CPXgetx (env, lp, w, 0, numFeatures);

    /*for (i = 0; i < numBoxSamples; ++i) {
      density[i] = w[numFeatures];
      for (j = 0; j < numFeatures; ++j) {
      density[i] += boxMatrix[i * numFeatures + j] * w[j];
      }
      } */
  }

  /*printf("Objective value: %f\n", sol);
    double * slack = malloc((numcols + 2 * numBoxConstraints) * sizeof(double));
    status = CPXgetx (env, lp, slack, 0, numcols + 2 * numBoxConstraints - 1);
    printfarray(slack, numcols + 2 * numBoxConstraints, 1, "Slack");
    */
TERMINATE:;

  free_and_null ((char **) &obj);
  free_and_null ((char **) &rhs);
  free_and_null ((char **) &sense);
  free_and_null ((char **) &tagarray);
  free_and_null ((char **) &lb);
  free_and_null ((char **) &ub);
  free_and_null ((char **) &matbeg);
  free_and_null ((char **) &matcnt);
  free_and_null ((char **) &matind);
  free_and_null ((char **) &matval);
  free_and_null ((char **) &qsepvec);

  free_and_null ((char **) &dens);
  free_and_null ((char **) &boxConstraints);
  free_and_null ((char **) &boxrmatbeg);
  free_and_null ((char **) &boxrmatind);

  free_and_null ((char **) &hmatbeg);
  free_and_null ((char **) &hmatind);
  free_and_null ((char **) &hmatval);
  free_and_null ((char **) &hSense);
  /*free_and_null ((char **) &slack); */
  return (status);

}