Example #1
0
//*******************************************************************
int CSolver:: Create(int estRows, int estCols, int estNonZeros,
					 int probsense,  char* probname, int namesize,
					 int nSerialnum,  char* licenvstring)
{
     int  i;
     char buff[100]; 

     setLogFile( (char*)probname); 

     memset( m_error, sizeof(m_error), 0 );
     m_debugLog = 0;

     m_probType    = PROBLEM_TYPE_LP;
     m_estRows     = estRows;
     m_estCols     = estCols;
     m_estNonZeros = estNonZeros;
     m_nObjSense   = probsense;
     m_nNameSize   = namesize;

	 m_nColCount   = 0;
	 m_nRowCount   = 0;

     // create arrays given problem size estimates
     // matrix
     m_pRowNdx    = new int[estNonZeros];
     m_pColNdx    = new int[estNonZeros];
     m_pCoef      = new double[estNonZeros];

     m_nRowItems  = 0;
     m_nColItems  = 0;
     m_nCoefItems = 0;

     // rim
     m_pObj       = new double[estCols];
     m_pRhs       = new double[estRows];
     m_pRhsSense  = new char[estRows];

     m_pCtype     = new char[estCols];  // column type
     m_pRstat     = new int[estRows];
     m_pCstat     = new int[estCols];

     m_nObjItems  = 0;
     m_nRhsItems  = 0;
     m_nRhsSense  = 0;

     // column bounds
     m_pBdl      = new double[estCols];
     m_pBdu      = new double[estCols];

     m_nBdItems   = 0;

     if (m_pBdl == NULL || m_pBdu == NULL ) return -1;

     for( i=0; i< estCols; i++ ) {
	      m_pBdl[i] = 0.0;
	      m_pBdu[i] = INFBOUND;
     }

     //---misc
     // build problem name string

     if( probname != NULL ) {
	 // user supplied a problem name on construction, so use that name
	 m_pszProbname = new char[strlen(probname)+1];

	 strcpy(m_pszProbname,probname);
     }
     else {
	     // no name given for problem
	     m_pszProbname = new char[strlen("NONAME")+1];
	     strcpy(m_pszProbname,"NONAME");
     }


     m_lp      = NULL;
     m_env     = NULL;
     m_obj     = 0;
     m_x       = NULL;
     m_pi      = NULL;
     m_slack   = NULL;
     m_dj      = NULL;

     m_nPosInCstore =0;
     m_cstore=NULL;
     m_nPosInCname  =0;
     m_cname=NULL;

     m_nPosInRstore =0;
     m_rstore=NULL;
     m_nPosInRname  =0;
     m_rname=NULL;

     m_RowMap.Create(estRows);
	 m_ColMap.Create(estCols);


     CreateNameSpace();

	 // check for memory allocation error
     if( m_pRowNdx == NULL  ||
	 m_pColNdx == NULL  ||
	 m_pCoef   == NULL  ||
	 m_pObj    == NULL  ||
	 m_pRhs    == NULL  ||
	 m_pRhsSense ==NULL ||
	 m_pBdl    == NULL  ||
	 m_pBdu    == NULL  ||
	 m_cstore  == NULL  ||
	 m_cname   == NULL  ||
	 m_rstore  == NULL  ||
	 m_rname   == NULL  ) {
	 Message("Not enough memory!");
		 return -1;  // failure error
	 }
	 sprintf(buff,"CPLEX Serial Number = %d", nSerialnum);
     
	 Message(buff);

//!     if ( nSerialnum == 0  )  
//!        // no serial number assume development license
//!    else
//!        // serial number provide assume runtime license
     m_env = CPXopenCPLEXruntime(&m_status, nSerialnum, licenvstring);

     if (m_env == NULL){
		  CPXgeterrorstring( m_env, m_status, m_error );
		  if (nSerialnum == 0 )
	          Message( "CPXopenCPLEXdevelop returned NULL !");
		  else
			  Message( "CPXopenCPLEXruntime returned NULL");
		  SetError("Failed to get a CPLEX environment");
          return -1;
     }

     CPXgetchannels(m_env, &m_cpxresults, &m_cpxwarning, &m_cpxerror, &m_cpxlog);

     char cpxbuff[80];

	 sprintf(cpxbuff, "%s.%s", probname, "cpx");

	 m_cpxFile = CPXfopen(cpxbuff,"w");

	 if (m_cpxFile == NULL ) {
         m_log.write("opening cplexlog.log failed");
	 }

	 CPXaddfuncdest(m_env, m_cpxresults, &m_cbData, channelsfunc);
	 CPXaddfuncdest(m_env, m_cpxwarning, &m_cbData, channelsfunc);
	 CPXaddfuncdest(m_env, m_cpxerror,   &m_cbData, channelsfunc);

     if ( CPXsetlogfile(m_env, m_cpxFile) != 0 ) {
          m_log.write("failed to set channels file");
      }

	 m_log.getTimeStamp(buff);

     m_bCreated    = 1;

     CPXmsg(m_cpxlog, "%s : %s\n\n","*** Begin Log ***", buff );

     return 0;
}
Example #2
0
int
main (int  argc,
      char *argv[])
{
   int status = 0;

   /* Declare and allocate space for the variables and arrays where
      we will store the optimization results, including the status, 
      objective value, and variable values */
   
   int    solstat;
   double objval;
   double *x = NULL;
   
   CPXENVptr env = NULL;
   CPXLPptr  lp = NULL;
   
   int        j;
   int        cur_numcols;
   CPXFILEptr logfile = NULL;
   int        wantorig = 1;
   int        nameind = 1;

   /* Check the command line arguments */

   if ( argc != 2 ) {
      if ( argc != 3         ||
           argv[1][0] != '-' ||
           argv[1][1] != 'r'   ) {
         usage (argv[0]);
         goto TERMINATE;
      }
      wantorig = 0;
      nameind = 2;
   }

   /* 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 parameter 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 != 0 ) {
      fprintf (stderr, 
               "Failure to turn on screen indicator, error %d.\n",
               status);
      goto TERMINATE;
   }

   /* Set MIP parameters */

   status = CPXsetintparam (env, CPXPARAM_MIP_Limits_Nodes, 10000);
   if ( status )  goto TERMINATE;

   /* Open a logfile */

   logfile = CPXfopen ("admipex3.log", "a");
   if ( logfile == NULL )  goto TERMINATE;
   status = CPXsetlogfile (env, logfile);
   if ( status )  goto TERMINATE;

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

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

   /* 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[nameind], NULL);
   if ( status ) {
      fprintf (stderr,
               "Failed to read and copy the problem data.\n");
      goto TERMINATE;
   }

   /* Set up to use MIP callbacks */

   status = CPXsetbranchcallbackfunc (env, usersetbranch, NULL);
   if ( status )  goto TERMINATE;

   if ( wantorig ) {
      /* Assure linear mappings between the presolved and original
         models */

      status = CPXsetintparam (env, CPXPARAM_Preprocessing_Linear, 0);
      if ( status )  goto TERMINATE;

      /* Let MIP callbacks work on the original model */

      status = CPXsetintparam (env, CPXPARAM_MIP_Strategy_CallbackReducedLP, CPX_OFF);
      if ( status )  goto TERMINATE;
   }


   /* Turn on traditional search for use with control callbacks */

   status = CPXsetintparam (env, CPXPARAM_MIP_Strategy_Search, CPX_MIPSEARCH_TRADITIONAL);
   if ( status )  goto TERMINATE;

   /* Optimize the problem and obtain solution */

   status = CPXmipopt (env, lp);

   if ( status ) {
      fprintf (stderr, "Failed to optimize MIP.\n");
      goto TERMINATE;
   }

   solstat = CPXgetstat (env, lp);
   printf ("Solution status %d.\n", solstat);

   status = CPXgetobjval (env, lp, &objval);

   if ( status ) {
      fprintf (stderr,"Failed to obtain objective value.\n");
      goto TERMINATE;
   }

   printf ("Objective value %.10g\n", objval);

   cur_numcols = CPXgetnumcols (env, lp);

   /* Allocate space for solution */

   x = (double *) malloc (cur_numcols * sizeof (double));

   if ( x == NULL ) {
      fprintf (stderr, "No memory for solution values.\n");
      goto TERMINATE;
   }

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

   /* Write out the solution */

   for (j = 0; j < cur_numcols; j++) {
      if ( fabs (x[j]) > 1e-10 ) {
         printf ( "Column %d:  Value = %17.10g\n", j, x[j]);
      }
   }
   

TERMINATE:

   /* Free the solution vector */

   free_and_null ((char **) &x);

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

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


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

   if ( logfile != NULL )  CPXfclose (logfile);
     
   return (status);

} /* END main */
Example #3
0
long GenModelCplex::Init(string name)
{
    
    //strParam.count("log_file")
    //dblParam.count("relative_mip_gap_tolerance")
    //dblParam.count("absolute_mip_gap_tolerance")
    //dblParam.count("time_limit")
    //dblParam.count("bounds_feasibility_tolerance")
    //dblParam.count("optimality_tolerance")
    //dblParam.count("markowitz_tolerance"))
    //longParam.count("threads")
    //longParam.count("cutpass")
    //longParam.count("pumplevel")
    //longParam.count("mipemphasis")
	//longParam.count("probinglevel")
    //longParam.count("max_iteration_limit");
	//boolParam.count("preprocoff") : turn on/off preprocessing
	//boolParam.count("datacheckoff")
    //boolParam.count("screen_output")
    //boolParam.count("usecutcb")
    
	if(solverdata == NULL)
		solverdata = new CplexData();
	else
	{
		static_cast<CplexData*>(solverdata)->Delete();
		static_cast<CplexData*>(solverdata)->Reset();
	}

	CplexData* d = static_cast<CplexData*>(solverdata);
	int status = 0;

	d->env = CPXopenCPLEX (&status);

	// If an error occurs
	if ( d->env == NULL )
        return ThrowError(getcplexerror(d->env, status)+string(". ")+string("Could not open CPLEX environment"));
    
	
	hassolution = false;
    
    // Log file
    if(strParam.count("log_file") > 0)
    {
        d->cpxfileptr =  CPXfopen(strParam["log_file"].c_str(), "w");
        status = CPXsetlogfile(d->env, d->cpxfileptr);
        if ( status )
            return ThrowError(getcplexerror(d->env, status)+string(". ")+string("Failure to set the log file"));
    }
    
    // General settings
    boolParam["log_output_stdout"] = true;
    SetParam("log_output_stdout", CPX_PARAM_SCRIND, "bool", "Failure to turn on/off log output to stdout");
    SetParam("log_level", 0, "long", "Failure to set log level", false);
    SetParam("use_data_checking", CPX_PARAM_DATACHECK, "bool", "Failure to turn on/off data checking");
    SetParam("nb_threads", CPX_PARAM_THREADS, "long", "Failure to set the number of threads");
    if(boolParam.count("use_preprocessor") > 0 && !boolParam["use_preprocessor"])
	{
        SetDirectParam(CPX_PARAM_AGGFILL, long2param(0), "long", "Failure to use preprocessor (CPX_PARAM_AGGFILL)");
        SetDirectParam(CPX_PARAM_PREPASS, long2param(0), "long", "Failure to use preprocessor (CPX_PARAM_PREPASS)");
        SetDirectParam(CPX_PARAM_AGGIND, long2param(CPX_OFF), "long", "Failure to use preprocessor (CPX_PARAM_AGGIND)");
        SetDirectParam(CPX_PARAM_DEPIND, long2param(0), "long", "Failure to use preprocessor (CPX_PARAM_DEPIND)");
        SetDirectParam(CPX_PARAM_PRELINEAR, long2param(0), "long", "Failure to use preprocessor (CPX_PARAM_PRELINEAR)");
        SetDirectParam(CPX_PARAM_PREDUAL, long2param(-1), "long", "Failure to use preprocessor (CPX_PARAM_PREDUAL)");
        SetDirectParam(CPX_PARAM_REDUCE, long2param(0), "long", "Failure to use preprocessor (CPX_PARAM_REDUCE)");
        SetDirectParam(CPX_PARAM_PREIND, long2param(CPX_OFF), "long", "Failure to use preprocessor (CPX_PARAM_PREIND)");
	}
    
    // MIP settings
    SetParam("nb_cut_pass", CPX_PARAM_CUTPASS, "long", "Failure to set the number of cut pass");
    SetParam("feasibility_pump_level", CPX_PARAM_FPHEUR, "long", "Failure to set the feasibility pump level");
    SetParam("probing_level", CPX_PARAM_PROBE, "long", "Failure to set the probing level");
    SetParam("mip_emphasis", CPX_PARAM_MIPEMPHASIS, "long", "Failure to set the MIP emphasis");
    if(boolParam.count("use_cut_callback") > 0 && boolParam["use_cut_callback"])
    {
        SetDirectParam(CPX_PARAM_PRELINEAR, long2param(0), "long", "Failure to use cut callback (CPX_PARAM_PRELINEAR)");
        SetDirectParam(CPX_PARAM_MIPCBREDLP, long2param(0), "long", "Failure to use cut callback (CPX_PARAM_MIPCBREDLP)");
    }
    
    // Tolerance and limits
    SetParam("time_limit", CPX_PARAM_TILIM, "dbl", "Failure to set time limit");
    SetParam("max_iteration_limit", CPX_PARAM_ITLIM, "long", "Failure to set the maximal number of simplex iterations");
    SetParam("bounds_feasibility_tolerance", CPX_PARAM_EPRHS, "dbl", "Failure to set bounds feasibility tolerance");
    SetParam("optimality_tolerance", CPX_PARAM_EPOPT, "dbl", "Failure to set optimality tolerance");
    SetParam("markowitz_tolerance", CPX_PARAM_EPMRK, "dbl", "Failure to set Markowitz tolerance");
    SetParam("absolute_mip_gap_tolerance", CPX_PARAM_EPAGAP, "dbl", "Failure to set absolute gap tolerance");
    SetParam("relative_mip_gap_tolerance", CPX_PARAM_EPGAP, "dbl", "Failure to set relative gap tolerance");
    if(boolParam.count("maximize") > 0 && boolParam["maximize"])
        SetParam("lp_objective_limit", CPX_PARAM_OBJULIM, "dbl", "Failure to set lp objective limit");
    else
        SetParam("lp_objective_limit", CPX_PARAM_OBJLLIM, "dbl", "Failure to set lp objective limit");
    

	// Create the problem
	d->lp = CPXcreateprob (d->env, &status, name.c_str());
	if ( d->lp == NULL )
        return ThrowError(getcplexerror(d->env, status)+string(". ")+string("Failure to create Cplex optimization problem"));

    binit = true;
    
	return 0;
}
Example #4
0
long GenModelCplex::Init(string name)
{
    //strParam.count("log_file")
    //dblParam.count("relative_mip_gap_tolerance")
    //dblParam.count("absolute_mip_gap_tolerance")
    //dblParam.count("time_limit")
    //dblParam.count("bounds_feasibility_tolerance")
    //dblParam.count("optimality_tolerance")
    //dblParam.count("markowitz_tolerance"))
    //longParam.count("threads")
    //longParam.count("cutpass")
    //longParam.count("pumplevel")
    //longParam.count("mipemphasis")
    //longParam.count("probinglevel")
    //longParam.count("max_iteration_limit");
    //boolParam.count("preprocoff") : turn on/off preprocessing
    //boolParam.count("datacheckoff")
    //boolParam.count("screen_output")
    //boolParam.count("usecutcb")
    
    if(solverdata == NULL)
        solverdata = new CplexData();
    else
    {
        static_cast<CplexData*>(solverdata)->Delete();
        static_cast<CplexData*>(solverdata)->Reset();
    }

    CplexData* d = static_cast<CplexData*>(solverdata);
    int status = 0;

    d->env = CPXopenCPLEX (&status);

    // If an error occurs
    if ( d->env == NULL )
        return ThrowError(getcplexerror(d->env, status)+string(". ")+string("Could not open CPLEX environment"));
    
    
    hassolution = false;
    
    // Log file
    if(strParam.count("log_file") > 0)
    {
        d->cpxfileptr =  CPXfopen(strParam["log_file"].c_str(), "w");
        status = CPXsetlogfile(d->env, d->cpxfileptr);
        if ( status )
            return ThrowError(getcplexerror(d->env, status)+string(". ")+string("Failure to set the log file"));
    }
    
    // General settings
    boolParam["log_output_stdout"] = true;
    SetParam("log_output_stdout", CPX_PARAM_SCRIND, "bool", "Failure to turn on/off log output to stdout");
	SetParam("log_level", CPX_PARAM_MIPDISPLAY, "long", "Failure to set log level");
    SetParam("use_data_checking", CPX_PARAM_DATACHECK, "bool", "Failure to turn on/off data checking");
    SetParam("nb_threads", CPX_PARAM_THREADS, "long", "Failure to set the number of threads");
    if(boolParam.count("use_preprocessor") > 0 && !boolParam["use_preprocessor"])
    {
        SetDirectParam(CPX_PARAM_AGGFILL, long2param(0), "long", "Failure to use preprocessor (CPX_PARAM_AGGFILL)");
        SetDirectParam(CPX_PARAM_PREPASS, long2param(0), "long", "Failure to use preprocessor (CPX_PARAM_PREPASS)");
        SetDirectParam(CPX_PARAM_AGGIND, long2param(CPX_OFF), "long", "Failure to use preprocessor (CPX_PARAM_AGGIND)");
        SetDirectParam(CPX_PARAM_DEPIND, long2param(0), "long", "Failure to use preprocessor (CPX_PARAM_DEPIND)");
        SetDirectParam(CPX_PARAM_PRELINEAR, long2param(0), "long", "Failure to use preprocessor (CPX_PARAM_PRELINEAR)");
        SetDirectParam(CPX_PARAM_PREDUAL, long2param(-1), "long", "Failure to use preprocessor (CPX_PARAM_PREDUAL)");
        SetDirectParam(CPX_PARAM_REDUCE, long2param(0), "long", "Failure to use preprocessor (CPX_PARAM_REDUCE)");
        SetDirectParam(CPX_PARAM_PREIND, long2param(CPX_OFF), "long", "Failure to use preprocessor (CPX_PARAM_PREIND)");
    }
    
    // MIP settings
    SetParam("nb_cut_pass", CPX_PARAM_CUTPASS, "long", "Failure to set the number of cut pass");
    SetParam("feasibility_pump_level", CPX_PARAM_FPHEUR, "long", "Failure to set the feasibility pump level");
    SetParam("probing_level", CPX_PARAM_PROBE, "long", "Failure to set the probing level");
	SetParam("mip_emphasis", CPX_PARAM_MIPEMPHASIS, "long", "Failure to set the MIP emphasis");
	SetParam("mip_search", CPX_PARAM_MIPSEARCH, "long", "Failure to set the MIP search strategy");
	SetParam("starting_algo", CPX_PARAM_STARTALG, "long", "Failure to set the starting algo parameter");
	SetParam("node_algo", CPX_PARAM_SUBALG, "long", "Failure to set the node algo parameter");
	
	if(boolParam.count("use_cut_callback") > 0 && boolParam["use_cut_callback"])
    {
        SetDirectParam(CPX_PARAM_PRELINEAR, long2param(0), "long", "Failure to use cut callback (CPX_PARAM_PRELINEAR)");
        SetDirectParam(CPX_PARAM_MIPCBREDLP, long2param(0), "long", "Failure to use cut callback (CPX_PARAM_MIPCBREDLP)");
    }
    
    // Tolerance and limits
    SetParam("time_limit", CPX_PARAM_TILIM, "dbl", "Failure to set time limit");
    SetParam("max_iteration_limit", CPX_PARAM_ITLIM, "long", "Failure to set the maximal number of simplex iterations");
    SetParam("bounds_feasibility_tolerance", CPX_PARAM_EPRHS, "dbl", "Failure to set bounds feasibility tolerance");
    SetParam("optimality_tolerance", CPX_PARAM_EPOPT, "dbl", "Failure to set optimality tolerance");
    SetParam("markowitz_tolerance", CPX_PARAM_EPMRK, "dbl", "Failure to set Markowitz tolerance");
    SetParam("absolute_mip_gap_tolerance", CPX_PARAM_EPAGAP, "dbl", "Failure to set absolute gap tolerance");
    SetParam("relative_mip_gap_tolerance", CPX_PARAM_EPGAP, "dbl", "Failure to set relative gap tolerance");
    if(boolParam.count("maximize") > 0 && boolParam["maximize"])
        SetParam("lp_objective_limit", CPX_PARAM_OBJULIM, "dbl", "Failure to set lp objective limit");
    else
        SetParam("lp_objective_limit", CPX_PARAM_OBJLLIM, "dbl", "Failure to set lp objective limit");
    
    // MIP Emphasis
    if(longParam.count("mipemphasis"))
    {
        status = CPXsetintparam (d->env, CPX_PARAM_MIPEMPHASIS, longParam["mipemphasis"]);
    }

    // Probing level
    if(longParam.count("probinglevel"))
    {
        status = CPXsetintparam (d->env, CPX_PARAM_PROBE, longParam["probinglevel"]);
    }

    if ( status )
    {
        fprintf (stderr, "Failure to set cut callback parameters, error %d->\n", status);
        return 1;
    }
    binit = true;
    
    if(dblParam.count("feastol"))
        status = CPXsetdblparam (d->env, CPX_PARAM_EPRHS, dblParam["feastol"]);
    if ( status )
    {
        fprintf (stderr, "Failure to change feasibility tolerance, error %d->\n", status);
        return 1;
    }
    if(dblParam.count("opttol"))
        status = CPXsetdblparam (d->env, CPX_PARAM_EPOPT, dblParam["opttol"]);
    if ( status )
    {
        fprintf (stderr, "Failure to change optimality tolerance, error %d->\n", status);
        return 1;
    }
    if(dblParam.count("marktol"))
        status = CPXsetdblparam (d->env, CPX_PARAM_EPMRK, dblParam["marktol"]);
    if ( status )
    {
        fprintf (stderr, "Failure to change Markowitz tolerance, error %d->\n", status);
        return 1;
    }
    
    if(longParam.count("threads"))
    {
        printf("Threads: %ld\n", longParam["threads"]);
        status = CPXsetintparam (d->env, CPX_PARAM_THREADS, longParam["threads"]);
        if ( status )
        {
            fprintf (stderr, "Failure to change the number of threads, error %d->\n", status);
            return 1;
        } 
    }

    // Turn off preprocessing
    if(boolParam.count("preprocoff") > 0 && boolParam["preprocoff"])
    {
        status = CPXsetintparam (d->env, CPX_PARAM_AGGFILL, 0);
        status = status && CPXsetintparam (d->env, CPX_PARAM_PREPASS, 0);
        status = status && CPXsetintparam (d->env, CPX_PARAM_AGGIND, CPX_OFF);
        status = status && CPXsetintparam (d->env, CPX_PARAM_DEPIND, 0);
        status = status && CPXsetintparam (d->env, CPX_PARAM_PRELINEAR, 0);
        status = status && CPXsetintparam (d->env, CPX_PARAM_PREDUAL, -1);
        status = status && CPXsetintparam (d->env, CPX_PARAM_REDUCE, 0);
        status = status && CPXsetintparam (d->env, CPX_PARAM_PREIND, CPX_OFF);
    }
    if ( status )
    {
        fprintf (stderr, "Failure to turn off preprocessing, error %d->\n", status);
        return 1;
    }

    // Turn on data checking
    if(boolParam.count("datacheckoff") > 0 && boolParam["datacheckoff"])
        status = CPXsetintparam (d->env, CPX_PARAM_DATACHECK, CPX_OFF);
    else
        status = CPXsetintparam (d->env, CPX_PARAM_DATACHECK, CPX_ON);

    if ( status )
    {
        fprintf (stderr, "Failure to turn on data checking, error %d->\n", status);
        return 1;
    }

    // Sets a relative tolerance on the gap between the best integer objective and the objective of the best node remaining (between 0.0 and 1.0)
    if(dblParam.count("epgap"))
    {
        //printf("setting epgap\n");
        status = CPXsetdblparam (d->env, CPX_PARAM_EPGAP, dblParam["epgap"]);
        if ( status )
        {
            fprintf (stderr, "Failure to set relative gap tolerance, error %d->\n", status);
            return 1;
        }
    }


    // Create the problem
    d->lp = CPXcreateprob (d->env, &status, name.c_str());
    if ( d->lp == NULL )
    {
        fprintf (stderr, "Failed to create LP.\n");
        return 1;
    }

	fflush(stdout);

    return 0;
}
Example #5
0
File: lpex5.c Project: renvieir/ioc
int
main (void)
{
   char     probname[16];  /* Problem name is max 16 characters */

   /* 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;
   char          errmsg[CPXMESSAGEBUFSIZE];

   CPXCHANNELptr  cpxerror   = NULL;
   CPXCHANNELptr  cpxwarning = NULL;
   CPXCHANNELptr  cpxresults = NULL;
   CPXCHANNELptr  ourchannel = NULL;

   char errorlabel[] = "cpxerror";
   char warnlabel[]  = "cpxwarning";
   char reslabel[]   = "cpxresults";
   char ourlabel[]   = "Our Channel";
   char ourmessage[] = "Our Message";

   CPXFILEptr fpout  = NULL;


   /* 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.  */

   /* Since the message handler is yet to be set up, we'll call our
      messaging function directly to print out any errors  */

   if ( env == NULL ) {
      ourmsgfunc (ourmessage, "Could not open CPLEX environment.\n");
      goto TERMINATE;
   }

   /* Now get the standard channels.  If an error, just call our
      message function directly. */

   status = CPXgetchannels (env, &cpxresults, &cpxwarning, &cpxerror, NULL);
   if ( status ) {
      ourmsgfunc (ourmessage, "Could not get standard channels.\n");
      CPXgeterrorstring (env, status, errmsg);
      ourmsgfunc (ourmessage, errmsg);
      goto TERMINATE;
   }

   /* Now set up the error channel first.  The label will be "cpxerror" */

   status = CPXaddfuncdest (env, cpxerror, errorlabel, ourmsgfunc);
   if ( status ) {
      ourmsgfunc (ourmessage, "Could not set up error message handler.\n");
      CPXgeterrorstring (env, status, errmsg);
      ourmsgfunc (ourmessage, errmsg);
   }

   /* Now that we have the error message handler set up, all CPLEX
      generated errors will go through ourmsgfunc.  So we don't have
      to use CPXgeterrorstring to determine the text of the message.
      We can also use CPXmsg to do any other printing.  */

   status = CPXaddfuncdest (env, cpxwarning, warnlabel, ourmsgfunc);
   if ( status ) {
      CPXmsg (cpxerror, "Failed to set up handler for cpxwarning.\n");
      goto TERMINATE;
   }

   status = CPXaddfuncdest (env, cpxresults, reslabel, ourmsgfunc);
   if ( status ) {
      CPXmsg (cpxerror, "Failed to set up handler for cpxresults.\n");
      goto TERMINATE;
   }
   
   /* Now turn on the iteration display. */

   status = CPXsetintparam (env, CPXPARAM_Simplex_Display, 2);
   if ( status ) {
      CPXmsg (cpxerror, "Failed to turn on simplex display level.\n");
      goto TERMINATE;
   }

   /* Create the problem. */

   strcpy (probname, "example");
   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 ) {
      CPXmsg (cpxerror, "Failed to create LP.\n");
      goto TERMINATE;
   }

   /* Now populate the problem with the data. */

   status = populatebycolumn (env, lp);

   if ( status ) {
      CPXmsg (cpxerror, "Failed to populate problem data.\n");
      goto TERMINATE;
   }


   /* Optimize the problem and obtain solution. */

   status = CPXlpopt (env, lp);
   if ( status ) {
      CPXmsg (cpxerror, "Failed to optimize LP.\n");
      goto TERMINATE;
   }

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


   /* Write the output to the screen.  We will also write it to a
      file as well by setting up a file destination and a function
      destination. */

   ourchannel = CPXaddchannel (env);
   if ( ourchannel == NULL ) {
      CPXmsg (cpxerror, "Failed to set up our private channel.\n");
      goto TERMINATE;
   }

   fpout = CPXfopen ("lpex5.msg", "w");
   if ( fpout == NULL ) {
      CPXmsg (cpxerror, "Failed to open lpex5.msg file for output.\n");
      goto TERMINATE;
   }
   status = CPXaddfpdest (env, ourchannel, fpout);
   if ( status ) {
      CPXmsg (cpxerror, "Failed to set up output file destination.\n");
      goto TERMINATE;
   }

   status = CPXaddfuncdest (env, ourchannel, ourlabel, ourmsgfunc);
   if ( status ) {
      CPXmsg (cpxerror, "Failed to set up our output function.\n");
      goto TERMINATE;
   }

   /* Now any message to channel ourchannel will go into the file 
      and into the file opened above. */

   CPXmsg (ourchannel, "\nSolution status = %d\n", solstat);
   CPXmsg (ourchannel, "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 sizes from when the problem
      was built.  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++) {
      CPXmsg (ourchannel, "Row %d:  Slack = %10f  Pi = %10f\n", 
              i, slack[i], pi[i]);
   }

   for (j = 0; j < cur_numcols; j++) {
      CPXmsg (ourchannel, "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, "lpex5.lp", NULL);
   if ( status ) {
      CPXmsg (cpxerror, "Failed to write LP to disk.\n");
      goto TERMINATE;
   }
   
   
TERMINATE:

   /* First check if ourchannel is open */

   if ( ourchannel != NULL ) {
      int  chanstat;
      chanstat = CPXdelfuncdest (env, ourchannel, ourlabel, ourmsgfunc);
      if ( chanstat ) {
         strcpy (errmsg, "CPXdelfuncdest failed.\n");
         ourmsgfunc (ourmessage, errmsg); 
         if (!status)  status = chanstat;
      }
      if ( fpout != NULL ) {
         chanstat = CPXdelfpdest (env, ourchannel, fpout);
         if ( chanstat ) {
            strcpy (errmsg, "CPXdelfpdest failed.\n");
            ourmsgfunc (ourmessage, errmsg);
            if (!status)  status = chanstat;
         }
         CPXfclose (fpout);
      }

      chanstat = CPXdelchannel (env, &ourchannel);
      if ( chanstat ) {
         strcpy (errmsg, "CPXdelchannel failed.\n");
         ourmsgfunc (ourmessage, errmsg); 
         if (!status)  status = chanstat;
      }
   }

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

   if ( lp != NULL ) {
      status = CPXfreeprob (env, &lp);
      if ( status ) {
         strcpy (errmsg, "CPXfreeprob failed.\n");
         ourmsgfunc (ourmessage, errmsg);
      }
   }

   /* Now delete our function destinations from the 3 CPLEX channels. */
   if ( cpxresults != NULL ) {
      int  chanstat;
      chanstat = CPXdelfuncdest (env, cpxresults, reslabel, ourmsgfunc);
      if ( chanstat && !status ) {
         status = chanstat;
         strcpy (errmsg, "Failed to delete cpxresults function.\n");
         ourmsgfunc (ourmessage, errmsg);
      }
   }

   if ( cpxwarning != NULL ) {
      int  chanstat;
      chanstat = CPXdelfuncdest (env, cpxwarning, warnlabel, ourmsgfunc);
      if ( chanstat && !status ) {
         status = chanstat;
         strcpy (errmsg, "Failed to delete cpxwarning function.\n");
         ourmsgfunc (ourmessage, errmsg);
      }
   }

   if ( cpxerror != NULL ) {
      int  chanstat;
      chanstat = CPXdelfuncdest (env, cpxerror, errorlabel, ourmsgfunc);
      if ( chanstat && !status ) {
         status = chanstat;
         strcpy (errmsg, "Failed to delete cpxerror function.\n");
         ourmsgfunc (ourmessage, errmsg);
      }
   }

   /* 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 ) {
         strcpy (errmsg, "Could not close CPLEX environment.\n");
         ourmsgfunc (ourmessage, errmsg);
         CPXgeterrorstring (env, status, errmsg);
         ourmsgfunc (ourmessage, errmsg);
      }
   }
     
   return (status);

}  /* END main */