Пример #1
0
int
main (void)
{
   /* Declare variables and arrays where we will store the
      optimization results including the status, objective value,
      and variable values. */

   int       solstat;
   double    objval;

   CPXDIM    colcnt = 0;
   double    *x = NULL;

   CPXENVptr env = NULL;
   CPXLPptr  lp = NULL;
   int       status;
   int       m, p;

   /* Initialize the CPLEX environment */

   env = CPXXopenCPLEX (&status);

   /* If an error occurs, the status value indicates the reason for
      failure.  A call to CPXXgeterrorstring will produce the text of
      the error message.  Note that CPXXopenCPLEX produces no output,
      so the only way to see the cause of the error is to use
      CPXXgeterrorstring.  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");
      CPXXgeterrorstring (env, status, errmsg);
      fprintf (stderr, "%s", errmsg);
      goto TERMINATE;
   }

   /* Turn on output to the screen */

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


   /* Formulate and solve the problem */

   lp = CPXXcreateprob (env, &status, "food manufacturing");

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

   /* Build the model */

   status = buildmodel (env, lp);
   if ( status ) {
      fprintf (stderr, "Failed to build model.\n");
      goto TERMINATE;
   }

   /* Write a copy of the problem to a file. */

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

   /* Optimize the problem and obtain solution. */

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

   solstat = CPXXgetstat (env, lp);

   /* Write solution status, objective and solution vector to the screen. */

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

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

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

   colcnt = NUMVARS*NUMMONTHS*NUMPRODUCTS;
   x = malloc (colcnt * sizeof(*x));
   if ( x == NULL ) {
      status = CPXERR_NO_MEMORY;
      fprintf (stderr, "Could not allocate memory for solution.\n");
      goto TERMINATE;
   }

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

   for (m = 0; m < NUMMONTHS; m++) {
      printf ("Month %d \n", m);

      printf ("  . buy   ");
      for (p = 0; p < NUMPRODUCTS; p++)
         printf ("%f\t", x[varindex(m, p, BUY)]);
      printf ("\n");

      printf ("  . use   ");
      for (p = 0; p < NUMPRODUCTS; p++)
         printf ("%f\t", x[varindex (m, p, USE)]);
      printf ("\n");

      printf ("  . store ");
      for (p = 0; p < NUMPRODUCTS; p++)
         printf ("%f\t", x[varindex (m, p, STORE)]);
      printf ("\n");
   }

   /* Free problem */

   status = CPXXfreeprob (env, &lp);
   if ( status ) {
      fprintf (stderr, "CPXXfreeprob failed, error code %d.\n", status);
      goto TERMINATE;
   }

 TERMINATE:

   free_and_null ((char **) &x);

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

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

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

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

      /* Note that CPXXcloseCPLEX produces no output,
         so the only way to see the cause of the error is to use
         CPXXgeterrorstring.  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");
         CPXXgeterrorstring (env, status, errmsg);
         fprintf (stderr, "%s", errmsg);
      }
   }

   return (status);

}  /* END main */
Пример #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;

   CPXDIM j;
   CPXDIM cur_numcols;
   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 = CPXXopenCPLEX (&status);

   /* If an error occurs, the status value indicates the reason for
      failure.  A call to CPXXgeterrorstring will produce the text of
      the error message.  Note that CPXXopenCPLEX produces no
      output, so the only way to see the cause of the error is to use
      CPXXgeterrorstring.  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");
      CPXXgeterrorstring (env, status, errmsg);
      fprintf (stderr, "%s", errmsg);
      goto TERMINATE;
   }

   /* Turn on output to the screen */

   status = CPXXsetintparam (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 = CPXXcreateprob (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 = CPXXreadcopyprob (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 = CPXXsetnodecallbackfunc (env, userselectnode, NULL)  ||
            CPXXsetbranchcallbackfunc (env, usersetbranch, NULL) ||
            CPXXsetsolvecallbackfunc (env, usersolve, NULL);

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

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

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

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

   /* Set MIP log interval to 1 */

   status = CPXXsetcntparam (env, CPXPARAM_MIP_Interval, 1);
   if ( status )  goto TERMINATE;

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

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



   /* Optimize the problem and obtain solution */

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

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

   status  = CPXXgetobjval (env, lp, &objval);
   if ( status ) {
      fprintf (stderr,"Failed to obtain objective value.\n");
      goto TERMINATE;
   }

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

   cur_numcols = CPXXgetnumcols (env, lp);

   /* Allocate space for solution */

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

   status = CPXXgetx (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 CPXXcreateprob and
      CPXXreadcopyprob, if necessary */

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

   /* Free the CPLEX environment, if necessary */

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

      /* Note that CPXXcloseCPLEX produces no output, so the only 
         way to see the cause of the error is to use
         CPXXgeterrorstring.  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");
         CPXXgeterrorstring (env, status, errmsg);
         fprintf (stderr, "%s", errmsg);
      }
   }
     
   return (status);

} /* END main */
Пример #3
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 */
   
   CPXENVptr env = NULL;
   CPXLPptr  lp = NULL;

   CUTINFO usercutinfo;
   CUTINFO lazyconinfo;

   const char * datadir = argc <= 1 ? "../../../examples/data" : argv[1];
   char *noswot = NULL;

   usercutinfo.x   = NULL;
   usercutinfo.beg = NULL;
   usercutinfo.ind = NULL; 
   usercutinfo.val = NULL;
   usercutinfo.rhs = NULL;

   lazyconinfo.x   = NULL;
   lazyconinfo.beg = NULL;
   lazyconinfo.ind = NULL; 
   lazyconinfo.val = NULL;
   lazyconinfo.rhs = NULL;

   noswot = (char *) malloc (strlen (datadir) + 1 + strlen("noswot.mps") + 1);
   sprintf (noswot, "%s/noswot.mps", datadir);

   /* Initialize the CPLEX environment */

   env = CPXXopenCPLEX (&status);

   /* If an error occurs, the status value indicates the reason for
      failure.  A call to CPXXgeterrorstring will produce the text of
      the error message.  Note that CPXXopenCPLEX produces no
      output, so the only way to see the cause of the error is to use
      CPXXgeterrorstring.  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");
      CPXXgeterrorstring (env, status, errmsg);
      fprintf (stderr, "%s", errmsg);
      goto TERMINATE;
   }

   /* Turn on output to the screen */

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

   /* The problem will be solved several times, so turn off advanced start */
   
   status = CPXXsetintparam (env, CPXPARAM_Advance, CPX_OFF);
   if ( status )  goto TERMINATE;

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

   lp = CPXXcreateprob (env, &status, "noswot");

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

   /* Set parameters */

   /* Assure linear mappings between the presolved and original
      models */

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


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

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

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

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

   /* Create user cuts for noswot problem */

   status = makeusercuts (env, lp, &usercutinfo);
   if ( status )  goto TERMINATE;

   /* Set up to use MIP usercut callback */

   status = CPXXsetusercutcallbackfunc (env, mycutcallback, &usercutinfo);
   if ( status )  goto TERMINATE;

   status = myoptimize (env, lp, -41.0);
   if ( status )  goto TERMINATE;

   /*=======================================================================*/

   /* Create a lazy constraint to alter the optimum */

   status = makelazyconstraint (env, lp, &lazyconinfo);
   if ( status )  goto TERMINATE;

   /* Set up to use MIP lazyconstraint callback. The callback funtion
    * registered is the same, but the data will be different. */

   status = CPXXsetlazyconstraintcallbackfunc (env, mycutcallback, &lazyconinfo);
   if ( status )  goto TERMINATE;

   status = myoptimize (env, lp, -39.0);
   if ( status )  goto TERMINATE;

   /*=======================================================================*/

   /* Now solve the problem without usercut callback */
   
   status = CPXXsetusercutcallbackfunc (env, NULL, NULL);
   if ( status )  goto TERMINATE;

   status = myoptimize (env, lp, -39.0);
   if ( status )  goto TERMINATE;

   /*=======================================================================*/


TERMINATE:

   /* Free the filename */

   free_and_null ((char **) &noswot);

   /* Free the allocated vectors */

   free_and_null ((char **) &usercutinfo.x);
   free_and_null ((char **) &usercutinfo.beg);
   free_and_null ((char **) &usercutinfo.ind);
   free_and_null ((char **) &usercutinfo.val);
   free_and_null ((char **) &usercutinfo.rhs);
   free_and_null ((char **) &lazyconinfo.x);
   free_and_null ((char **) &lazyconinfo.beg);
   free_and_null ((char **) &lazyconinfo.ind);
   free_and_null ((char **) &lazyconinfo.val);
   free_and_null ((char **) &lazyconinfo.rhs);

   /* Free the problem as allocated by CPXXcreateprob and
      CPXXreadcopyprob, if necessary */

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

   /* Free the CPLEX environment, if necessary */

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

      /* Note that CPXXcloseCPLEX produces no output, so the only 
         way to see the cause of the error is to use
         CPXXgeterrorstring.  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");
         CPXXgeterrorstring (env, status, errmsg);
         fprintf (stderr, "%s", errmsg);
      }
   }
     
   return (status);

} /* END main */
Пример #4
0
int
main (int argc, char **argv)
{
   CPXENVptr env = NULL;
   CPXLPptr lp = NULL;
   char errbuf[CPXMESSAGEBUFSIZE];
   int status = 0;
   char const *vmconfig = NULL;
   char const *model = NULL;
   double objval;
   LOGINFO myloginfo;

#ifdef USE_MPI
   MPI_Init (&argc, &argv);
#endif

   if ( argc != 3 ) {
      usage (argv[0]);
      return -1;
   }
   
   vmconfig = argv[1];
   model = argv[2];

   /* Create a new CPLEX environment for each problem to solve.
    */
   env = CPXXopenCPLEX (&status);
   if ( env == NULL || status != 0 ) {
      fprintf (stderr, "Failed to open CPLEX: %s\n",
               CPXXgeterrorstring (NULL, status, errbuf));
      goto TERMINATE;
   }
   /* Load a virtual machine configuration.
    */
   status = CPXXreadcopyvmconfig (env, vmconfig);
   if ( status != 0 ) {
      fprintf (stderr, "Failed to load VMC %s: %s\n",
               vmconfig, CPXXgeterrorstring (env, status, errbuf));
      goto TERMINATE;
   }
   /* Create and input a problem.
    */
   lp = CPXXcreateprob (env, &status, model);
   if ( lp == NULL || status != 0 ) {
      fprintf (stderr, "Failed to create problem: %s\n",
               CPXXgeterrorstring (env, status, errbuf));
      goto TERMINATE;
   }
   status = CPXXreadcopyprob (env, lp, model, NULL);
   if ( status != 0 ) {
      fprintf (stderr, "Failed to read problem %s: %s\n",
               model, CPXXgeterrorstring (env, status, errbuf));
      goto TERMINATE;
   }
   /* Turn off CPLEX logging.
    */
   status = CPXXsetintparam (env, CPXPARAM_MIP_Display, 0);
   if ( status )  goto TERMINATE;
   /* Install an incumbent callback for logging.
    */
   status = CPXXgettime (env, &myloginfo.timestart);
   if ( status ) {
      fprintf (stderr, "Failed to query time.\n");
      goto TERMINATE;
   }
   status = CPXXgetdettime (env, &myloginfo.dettimestart);
   if ( status ) {
      fprintf (stderr, "Failed to query deterministic time.\n");
      goto TERMINATE;
   }
   myloginfo.numcols       = CPXXgetnumcols (env, lp);
   myloginfo.lastincumbent = CPXXgetobjsen (env, lp) * 1e+35;
   myloginfo.lastdettime   = -10000.0;
   status = CPXXsetinfocallbackfunc (env, logcallback, &myloginfo);
   if ( status ) {
      fprintf (stderr, "Failed to set logging callback function.\n");
      goto TERMINATE;
   }
   /* Solve the problem using parallel distributed MIP.
    */
   status = CPXXdistmipopt (env, lp);
   if ( status != 0 ) {
      fprintf (stderr, "Failed to optimize: %s\n",
               CPXXgeterrorstring (env, status, errbuf));
      goto TERMINATE;
   }
   /* Print some solution information.
    */
   status = CPXXgetobjval (env, lp, &objval);
   if ( status == CPXERR_NO_SOLN ) {
      printf ("No solution available.\n");
   }
   else if ( status == 0 ) {
      printf ("Solution value %f\n", objval);
   }
   else {
      fprintf (stderr, "Error %d: %s\n", status,
               CPXXgeterrorstring (env, status, errbuf));
   }
   printf ("Solution status %d\n", CPXXgetstat (env, lp));


 TERMINATE:
   CPXXfreeprob (env, &lp);
   CPXXcloseCPLEX (&env);

#ifdef USE_MPI
   CPXXfinalizeMPIworkers (-1, NULL, 0, NULL, 1);
   MPI_Finalize ();
#endif

   return status;
}
Пример #5
0
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;
   CPXDIM        i, j;
   CPXDIM        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";

   CPXFILEptr fpout  = NULL;


   /* Initialize the CPLEX environment */

   env = CPXXopenCPLEX (&status);

   /* If an error occurs, the status value indicates the reason for
      failure.  A call to CPXXgeterrorstring will produce the text of
      the error message.  Note that CPXXopenCPLEX produces no output,
      so the only way to see the cause of the error is to use
      CPXXgeterrorstring.  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 ("Our Message", "Could not open CPLEX environment.\n");
      goto TERMINATE;
   }

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

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

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

   status = CPXXaddfuncdest (env, cpxerror, errorlabel, ourmsgfunc);
   if ( status ) {
      ourmsgfunc ("Our Message", "Could not set up error message handler.\n");
      CPXXgeterrorstring (env, status, errmsg);
      ourmsgfunc ("Our Message", 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 CPXXgeterrorstring to determine the text of the message.
      We can also use CPXXmsg to do any other printing.  */

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

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

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

   /* Create the problem. */

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

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

   status = populatebycolumn (env, lp);

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


   /* Optimize the problem and obtain solution. */

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

   status = CPXXsolution (env, lp, &solstat, &objval, x, pi, slack, dj);
   if ( status ) {
      CPXXmsg (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 = CPXXaddchannel (env);
   if ( ourchannel == NULL ) {
      CPXXmsg (cpxerror, "Failed to set up our private channel.\n");
      goto TERMINATE;
   }

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

   status = CPXXaddfuncdest (env, ourchannel, ourlabel, ourmsgfunc);
   if ( status ) {
      CPXXmsg (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. */

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

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

   /* First check if ourchannel is open */

   if ( ourchannel != NULL ) {
      int  chanstat;
      chanstat = CPXXdelfuncdest (env, ourchannel, ourlabel, ourmsgfunc);
      if ( chanstat ) {
         strcpy (errmsg, "CPXXdelfuncdest failed.\n");
         ourmsgfunc ("Our Message", errmsg); 
         if (!status)  status = chanstat;
      }
      if ( fpout != NULL ) {
         chanstat = CPXXdelfpdest (env, ourchannel, fpout);
         if ( chanstat ) {
            strcpy (errmsg, "CPXXdelfpdest failed.\n");
            ourmsgfunc ("Our Message", errmsg);
            if (!status)  status = chanstat;
         }
         CPXXfclose (fpout);
      }

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

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

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

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

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

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

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

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

      /* Note that CPXXcloseCPLEX produces no output,
         so the only way to see the cause of the error is to use
         CPXXgeterrorstring.  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 ("Our Message", errmsg);
         CPXXgeterrorstring (env, status, errmsg);
         ourmsgfunc ("Our Message", errmsg);
      }
   }
     
   return (status);

}  /* END main */
Пример #6
0
int
main (int argc, char *argv[])
{
    CPXENVptr env = NULL;
    CPXLPptr  lp = NULL;
    int       status = 0;
    CPXDIM    j;
    CPXDIM    numcols;
    double    totinv;

    int       solstat;
    double    objval;
    double    *x = NULL;

    double    rrhs[1];
    char      rsense[1];
    CPXNNZ    rmatbeg[1];

    CPXDIM    *indices = NULL;
    double    *values = NULL;
    char      *namestore = NULL;
    char      **nameptr = NULL;
    CPXSIZE   surplus, storespace;

    const char * datadir = argc <= 1 ? "../../../examples/data" : argv[1];
    char *prod = NULL;

    prod = (char *) malloc (strlen (datadir) + 1 + strlen("prod.lp") + 1);
    sprintf (prod, "%s/prod.lp", datadir);

    /* Initialize the CPLEX environment */

    env = CPXXopenCPLEX (&status);

    /* If an error occurs, the status value indicates the reason for
       failure.  A call to CPXXgeterrorstring will produce the text of
       the error message.  Note that CPXXopenCPLEX produces no output,
       so the only way to see the cause of the error is to use
       CPXXgeterrorstring.  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");
        CPXXgeterrorstring (env, status, errmsg);
        fprintf (stderr, "%s", errmsg);
        goto TERMINATE;
    }

    /* Turn on output to the screen */

    status = CPXXsetintparam (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 = CPXXcreateprob (env, &status, "prod.lp");

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

    /* Tell presolve to do only primal reductions,
       turn off simplex logging */

    status = CPXXsetintparam (env, CPXPARAM_Preprocessing_Reduce, 1);
    if ( status ) {
        fprintf (stderr, "Failed to set CPXPARAM_Preprocessing_Reduce: %d\n",
                 status);
        goto TERMINATE;
    }
    status = CPXXsetintparam (env, CPXPARAM_Simplex_Display, 0);
    if ( status ) {
        fprintf (stderr, "Failed to set CPXPARAM_Simplex_Display: %d\n", status);
        goto TERMINATE;
    }


    /* Optimize the problem and obtain solution. */

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

    solstat = CPXXgetstat (env, lp);
    status  = CPXXgetobjval (env, lp, &objval);

    if ( status || solstat != CPX_STAT_OPTIMAL ) {
        fprintf (stderr, "Solution failed. Status %d, solstat %d.\n",
                 status, solstat);
        goto TERMINATE;
    }
    printf ("Profit objective value is %g\n", objval);


    /* Allocate space for column names */

    numcols = CPXXgetnumcols (env, lp);
    if ( !numcols ) {
        fprintf (stderr, "No columns in problem\n");
        goto TERMINATE;
    }

    CPXXgetcolname (env, lp, NULL, NULL, 0, &surplus, 0, numcols-1);
    storespace = - surplus;

    namestore = malloc (storespace * sizeof(*namestore));
    nameptr   = malloc (numcols * sizeof(*nameptr));
    if ( namestore == NULL  ||  nameptr == NULL ) {
        fprintf (stderr, "No memory for column names\n");
        goto TERMINATE;
    }

    status = CPXXgetcolname (env, lp, nameptr, namestore, storespace,
                             &surplus, 0, numcols-1);
    if ( status ) {
        fprintf (stderr, "Failed to get column names\n");
        goto TERMINATE;
    }

    /* Allocate space for solution */

    x = malloc (numcols * sizeof(*x));

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

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

    totinv = 0;
    for (j = 0; j < numcols; j++) {
        if ( !strncmp (nameptr[j], "inv", 3) )  totinv += x[j];
    }
    printf ("Inventory level under profit objective is %g\n", totinv);

    /* Allocate space for a constraint */

    indices = malloc (numcols * sizeof (*indices));
    values  = malloc (numcols * sizeof (*values));

    if ( indices == NULL  ||  values == NULL ) {
        fprintf (stderr, "No memory for constraint\n");
        goto TERMINATE;
    }

    /* Get profit objective and add it as a constraint */

    status = CPXXgetobj (env, lp, values, 0, numcols-1);
    if ( status ) {
        fprintf (stderr,
                 "Failed to get profit objective.  Status %d\n", status);
        goto TERMINATE;
    }
    for (j = 0; j < numcols; j++) {
        indices[j] = j;
    }

    rrhs[0]    = objval - fabs (objval) * 1e-6;
    rsense[0]  = 'G';
    rmatbeg[0] = 0;

    status = CPXXpreaddrows (env, lp, 1, numcols, rrhs, rsense,
                             rmatbeg, indices, values, NULL);

    if ( status ) {
        fprintf (stderr,
                 "Failed to add objective as constraint.  Status %d\n",
                 status);
        goto TERMINATE;
    }

    /* Set up objective to maximize negative of sum of inventory */

    totinv = 0;
    for (j = 0; j < numcols; j++) {
        if ( strncmp (nameptr[j], "inv", 3) ) {
            values[j] = 0.0;
        }
        else {
            values[j] = - 1.0;
        }
    }

    status = CPXXprechgobj (env, lp, numcols, indices, values);

    if ( status ) {
        fprintf (stderr,
                 "Failed to change to inventory objective.  Status %d\n",
                 status);
        goto TERMINATE;
    }

    status = CPXXlpopt (env, lp);
    if ( status ) {
        fprintf (stderr, "Optimization on inventory level failed. Status %d.\n",
                 status);
        goto TERMINATE;
    }

    solstat = CPXXgetstat (env, lp);
    status  = CPXXgetobjval (env, lp, &objval);
    if ( status  ||  solstat != CPX_STAT_OPTIMAL ) {
        fprintf (stderr, "Solution failed. Status %d, solstat %d.\n",
                 status, solstat);
        goto TERMINATE;
    }

    printf ("Inventory level after optimization is %g\n", -objval);


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

    /* Write out the solution */
    printf("Solution status: %d", solstat);
    printf ("\n");
    for (j = 0; j < numcols; j++) {
        printf ( "%s:  Value = %17.10g\n", nameptr[j], x[j]);
    }


TERMINATE:

    /* Free the filename */

    free_and_null ((char **) &prod);

    /* Free up the basis and solution */

    free_and_null ((char **) &indices);
    free_and_null ((char **) &values);
    free_and_null ((char **) &nameptr);
    free_and_null ((char **) &namestore);
    free_and_null ((char **) &x);


    /* Free up the problem, if necessary */

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

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

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

        /* Note that CPXXcloseCPLEX produces no output,
           so the only way to see the cause of the error is to use
           CPXXgeterrorstring.  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");
            CPXXgeterrorstring (env, status, errmsg);
            fprintf (stderr, "%s", errmsg);
        }
    }

    return (status);

}  /* END main */
Пример #7
0
int
main (int argc, char **argv)
{
   /* Declare variables and arrays for retrieving problem data and
      solution information later on. */

   int       status = 0;
   CPXENVptr env = NULL;
   CPXNETptr net = NULL;
   CPXLPptr  lp  = NULL;

   /* Check command line */

   if ( argc != 2 ) {
      fprintf (stderr, "Usage: %s <network file>\n", argv[0]);
      fprintf (stderr, "Exiting ...\n");
      goto TERMINATE;
   }

   /* Initialize the CPLEX environment */

   env = CPXXopenCPLEX (&status);

   /* If an error occurs, the status value indicates the reason for
      failure.  A call to CPXXgeterrorstring will produce the text of
      the error message.  Note that CPXXopenCPLEX produces no
      output, so the only way to see the cause of the error is to use
      CPXXgeterrorstring.  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");
      CPXXgeterrorstring (env, status, errmsg);
      fprintf (stderr, "%s", errmsg);
      goto TERMINATE;
   }

   /* Turn on output to the screen */

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

   /* Create the problem. */

   net = CPXXNETcreateprob (env, &status, "netex2");

   /* 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 ( net == NULL ) {
      fprintf (stderr, "Failed to create network object.\n");
      goto TERMINATE;
   }

   /* Read network problem data from file
      with filename given as command line argument. */

   status = CPXXNETreadcopyprob (env, net, argv[1]);

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


   /* Optimize the problem */

   status = CPXXNETprimopt (env, net);
   if ( status ) {
      fprintf (stderr, "Failed to optimize network.\n");
      goto TERMINATE;
   }

   printf ("Solution status = %d\n", CPXXNETgetstat (env, net));


   /* Check network solution status */

   if ( CPXXNETgetstat (env, net) == CPX_STAT_INFEASIBLE ) {

      /* Create LP object used for invoking infeasibility finder */

      lp = CPXXcreateprob (env, &status, "netex2");
      if ( lp == NULL ) {
         fprintf (stderr, "Failed to create LP object.\n");
         goto TERMINATE;
      }

      /* Copy LP representation of network problem to lp object, along
         with the current basis available in the network object. */

      status = CPXXcopynettolp (env, lp, net);
      if ( status ) {
         fprintf (stderr, "Failed to copy network as LP.\n");
         goto TERMINATE;
      }

      /* Optimize the LP with primal to create an LP solution.  This
         optimization will start from the basis previously generated by
         CPXXNETprimopt() as long as the advance indicator is switched
         on (its default).  */

      status = CPXXsetintparam (env, CPXPARAM_LPMethod, CPX_ALG_PRIMAL);
      if ( status ) {
         fprintf (stderr, 
                  "Failure to set LP method, error %d.\n", status);
         goto TERMINATE;
      }
  
      status = CPXXlpopt (env, lp);
      if ( status ) {
         fprintf (stderr, "Failed to optimize LP.\n");
         goto TERMINATE;
      }

      printf ("Solution status = %i\n", CPXXgetstat (env, lp));

      /* Find conflict and write it to a file */

      status = CPXXrefineconflict (env, lp, NULL, NULL);
      if ( status ) {
         fprintf (stderr, "Failed to find conflict\n");
         goto TERMINATE;
      }

      status = CPXXclpwrite (env, lp, "netex2.clp");
      if ( status ) {
         fprintf (stderr, "Failed to write conflict file\n");
         goto TERMINATE;
      }

      printf ("conflict written to file netex2.clp\n");
   }
   else {
      printf ("Network is feasible\n");
   }

   
TERMINATE:

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

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

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

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

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

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

      /* Note that CPXXcloseCPLEX produces no output,
         so the only way to see the cause of the error is to use
         CPXXgeterrorstring.  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");
         CPXXgeterrorstring (env, status, errmsg);
         fprintf (stderr, "%s", errmsg);
      }
   }
     
   return (status);

}  /* END main */
Пример #8
0
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;
   CPXDIM        j;
   CPXDIM        cur_numrows, cur_numcols;
   int           method;

   char          *basismsg;

   /* Check the command line arguments */

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

   /* Initialize the CPLEX environment */

   env = CPXXopenCPLEX (&status);

   /* If an error occurs, the status value indicates the reason for
      failure.  A call to CPXXgeterrorstring will produce the text of
      the error message.  Note that CPXXopenCPLEX produces no output,
      so the only way to see the cause of the error is to use
      CPXXgeterrorstring.  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");
      CPXXgeterrorstring (env, status, errmsg);
      fprintf (stderr, "%s", errmsg);
      goto TERMINATE;
   }

   /* Turn on output to the screen */

   status = CPXXsetintparam (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 = CPXXcreateprob (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 = CPXXreadcopyprob (env, lp, argv[1], NULL);
   if ( status ) {
      fprintf (stderr, "Failed to read and copy the problem data.\n");
      goto TERMINATE;
   }

   if ( CPXXgetprobtype (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 'o':
         method = CPX_ALG_AUTOMATIC;
         break;
      case 'p':
         method = CPX_ALG_PRIMAL;
         break;
      case 'd':
         method = CPX_ALG_DUAL;
         break;
      case 'n':
         method = CPX_ALG_NET;
         break;
      case 'b':
         method = CPX_ALG_BARRIER;
         break;
      default:
         method = CPX_ALG_NONE;
         break;
   }

   status = CPXXsetintparam (env, CPXPARAM_QPMethod, method);
   if ( status ) {
      fprintf (stderr, 
               "Failed to set the optimization method, error %d.\n", status);
      goto TERMINATE;
   }
   

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

   solnstat = CPXXgetstat (env, lp);

   if      ( solnstat == CPX_STAT_UNBOUNDED ) {
      printf ("Model is unbounded\n");
      goto TERMINATE;
   }
   else if ( 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 = CPXXsolninfo (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 = CPXXgetobjval (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 = CPXXgetnumcols (env, lp);
   cur_numrows = CPXXgetnumrows (env, lp);

   /* Retrieve basis, if one is available */

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

      status = CPXXgetbase (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 = malloc (cur_numcols*sizeof(*x));
   if ( x == NULL ) {
      fprintf (stderr, "No memory for solution.\n");
      goto TERMINATE;
   }

   status = CPXXgetx (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 = CPXXgetdblquality (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 = CPXXfreeprob (env, &lp);
      if ( status ) {
         fprintf (stderr, "CPXXfreeprob failed, error code %d.\n", status);
      }
   }

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

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

      /* Note that CPXXcloseCPLEX produces no output,
         so the only way to see the cause of the error is to use
         CPXXgeterrorstring.  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");
         CPXXgeterrorstring (env, status, errmsg);
         fprintf (stderr, "%s", errmsg);
      }
   }
     
   return (status);

}  /* END main */
Пример #9
0
int
main (int argc, char *argv[])
{
   int     uselogcallback = 0;
   LOGINFO myloginfo;

   int         usetimelimcallback = 0;
   TIMELIMINFO mytimeliminfo;

   int          useterminate = 0;
   volatile int terminator;

   CPXENVptr env = NULL;
   CPXLPptr  lp = NULL;
   int       solstat;
   int       status = 0;

   /* Check the command line arguments */

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

   switch (argv[2][0]) {
      case 'l':
         uselogcallback = 1;
         break;
      case 't':
         usetimelimcallback = 1;
         break;
      case 'a':
         useterminate = 1;
         break;
      default:
         break;
   }

   /* Initialize the CPLEX environment */

   env = CPXXopenCPLEX (&status);

   /* If an error occurs, the status value indicates the reason for
      failure.  A call to CPXXgeterrorstring will produce the text of
      the error message.  Note that CPXXopenCPLEX produces no output,
      so the only way to see the cause of the error is to use
      CPXXgeterrorstring.  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");
      CPXXgeterrorstring (env, status, errmsg);
      fprintf (stderr, "%s", errmsg);
      goto TERMINATE;
   }

   /* Turn on output to the screen */

   status = CPXXsetintparam (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 = CPXXcreateprob (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 = CPXXreadcopyprob (env, lp, argv[1], NULL);
   if ( status ) {
      fprintf (stderr, "Failed to read and copy the problem data.\n");
      goto TERMINATE;
   }


   if ( usetimelimcallback ) {
      double t;
      status = CPXXgettime (env, &t);
      if ( status ) {
         fprintf (stderr, "Failed to initialize timer.\n");
         goto TERMINATE;
      }
      mytimeliminfo.acceptablegap = 10.0;
      mytimeliminfo.aborted       = 0;
      mytimeliminfo.timestart     = t;
      mytimeliminfo.timelim       = 1.0;

      status = CPXXsetinfocallbackfunc (env, timelimcallback, &mytimeliminfo);
      if ( status ) {
         fprintf (stderr, "Failed to set time limit callback function.\n");
         goto TERMINATE;
      }
   }
   else if ( uselogcallback ) {
      /* Set overall node limit in case callback conditions are not met */
      status = CPXXsetcntparam (env, CPXPARAM_MIP_Limits_Nodes, 5000);
      if ( status ) goto TERMINATE;

      status = CPXXgettime (env, &myloginfo.timestart);
      if ( status ) {
         fprintf (stderr, "Failed to query time.\n");
         goto TERMINATE;
      }
      status = CPXXgetdettime (env, &myloginfo.dettimestart);
      if ( status ) {
         fprintf (stderr, "Failed to query deterministic time.\n");
         goto TERMINATE;
      }
      myloginfo.numcols       = CPXXgetnumcols (env, lp);
      myloginfo.lastincumbent = CPXXgetobjsen (env, lp) * 1e+35;
      myloginfo.lastlog       = -10000;
      status = CPXXsetinfocallbackfunc (env, logcallback, &myloginfo);
      if ( status ) {
         fprintf (stderr, "Failed to set logging callback function.\n");
         goto TERMINATE;
      }
      /* Turn off CPLEX logging */
      status = CPXXsetintparam (env, CPXPARAM_MIP_Display, 0);
      if ( status )  goto TERMINATE;
   }
   else if ( useterminate) {
      status = CPXXsetterminate (env, &terminator);
      if ( status ) {
         fprintf (stderr, "Failed to set terminator.\n");
         goto TERMINATE;
      }
      /* Typically, you would pass the terminator variable to
         another thread or pass it to an interrupt handler,
         and  monitor for some event to occur.  When it does,
         set terminator to a non-zero value.
 
         To illustrate its use without creating a thread or
         an interrupt handler, terminate immediately by setting
         terminator before the solve.
      */
      terminator = 1;
   }

   /* Optimize the problem and obtain solution. */

   status = CPXXmipopt (env, lp);

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

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

TERMINATE:

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

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

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

   if ( env != NULL ) {
      int xstatus = CPXXcloseCPLEX (&env);

      /* Note that CPXXcloseCPLEX produces no output,
         so the only way to see the cause of the error is to use
         CPXXgeterrorstring.  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");
         CPXXgeterrorstring (env, status, errmsg);
         fprintf (stderr, "%s", errmsg);
         status = xstatus;
      }
   }
 
   return (status);

}  /* END main */
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;
   CPXDIM   numcols;
   CPXDIM   numrows;
   int      objsen;
   double   *obj = NULL;
   double   *rhs = NULL;
   char     *sense = NULL;
   CPXNNZ   *matbeg = NULL;
   CPXDIM   *matcnt = NULL;
   CPXDIM   *matind = NULL;
   double   *matval = NULL;
   double   *lb = NULL;
   double   *ub = NULL;
   CPXNNZ   *qmatbeg = NULL;
   CPXDIM   *qmatcnt = NULL;
   CPXDIM   *qmatind = NULL;
   double   *qmatval = NULL;

   /* Declare pointers for the variables that will contain the data
      for the constraint that cuts off certain local optima. */

   CPXDIM   numrows_extra;
   CPXNNZ   numnnz_extra;
   double   *rhs_extra = NULL;
   char     *sense_extra = NULL;
   CPXNNZ   *rmatbeg = NULL;
   CPXDIM   *rmatind = NULL;
   double   *rmatval = NULL;
   CPXDIM   rowind[1];

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

   CPXENVptr     env = NULL;
   CPXLPptr      lp = NULL;
   int           status;

   /* Initialize the CPLEX environment */

   env = CPXXopenCPLEX (&status);

   /* If an error occurs, the status value indicates the reason for
      failure.  A call to CPXXgeterrorstring will produce the text of
      the error message.  Note that CPXXopenCPLEX produces no output,
      so the only way to see the cause of the error is to use
      CPXXgeterrorstring.  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");
      CPXXgeterrorstring (env, status, errmsg);
      fprintf (stderr, "%s", errmsg);
      goto TERMINATE;
   }

   /* Turn on output to the screen */

   status = CPXXsetintparam (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,
                            &numrows_extra, &numnnz_extra, 
                            &rhs_extra, &sense_extra, 
                            &rmatbeg, &rmatind, &rmatval);
   if ( status ) {
      fprintf (stderr, "Failed to build problem data arrays.\n");
      goto TERMINATE;
   }

   /* Create the problem. */

   lp = CPXXcreateprob (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 = CPXXcopylp (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 = CPXXcopyquad (env, lp, qmatbeg, qmatcnt, qmatind, qmatval);
   if ( status ) {
      fprintf (stderr, "Failed to copy quadratic matrix.\n");
      goto TERMINATE;
   }


   /* When a non-convex objective function is present, CPLEX will
      return error CPXERR_Q_NOT_POS_DEF unless the parameter
      CPXPARAM_SolutionTarget is set to accept first-order optimal
      solutions.  */
   status = CPXXsetintparam (env, CPXPARAM_SolutionTarget, CPX_SOLUTIONTARGET_FIRSTORDER);
   if ( status ) goto TERMINATE;

   /* Optimize the problem and obtain solution. */

   status = optimize_and_report(env, lp, &solstat, &objval);
   if ( status ) goto TERMINATE;


   /* Add a constraint to cut off the solution at (-1, 1) */

   status = CPXXaddrows (env, lp, 0, numrows_extra, numnnz_extra,
                        rhs_extra, sense_extra,
                        rmatbeg, rmatind, rmatval,
                        NULL, NULL);
   if ( status ) goto TERMINATE;

   status = optimize_and_report(env, lp, &solstat, &objval);
   if ( status ) goto TERMINATE;


   /* Reverse the sense of the new constraint to cut off the solution at (1, 1) */

   rowind[0] = CPXXgetnumrows (env, lp) - 1;
   status    = CPXXchgsense (env, lp, 1, rowind, "L");
   if ( status ) goto TERMINATE;

   status = optimize_and_report(env, lp, &solstat, &objval);
   if ( status ) goto TERMINATE;

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

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


TERMINATE:

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

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

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

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

      /* Note that CPXXcloseCPLEX produces no output,
         so the only way to see the cause of the error is to use
         CPXXgeterrorstring.  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");
         CPXXgeterrorstring (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);
   free_and_null ((char **) &rhs_extra);
   free_and_null ((char **) &sense_extra);
   free_and_null ((char **) &rmatbeg);
   free_and_null ((char **) &rmatind);
   free_and_null ((char **) &rmatval);

   return (status);

}  /* END main */
Пример #11
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;

   CPXDIM j;
   CPXDIM cur_numcols;

   const char * datadir = argc <= 1 ? "../../../examples/data" : argv[1];
   char *noswot = NULL;

   noswot = (char *) malloc (strlen (datadir) + 1 + strlen("noswot.mps") + 1);
   sprintf (noswot, "%s/noswot.mps", datadir);

   /* Initialize the CPLEX environment */

   env = CPXXopenCPLEX (&status);

   /* If an error occurs, the status value indicates the reason for
      failure.  A call to CPXXgeterrorstring will produce the text of
      the error message.  Note that CPXXopenCPLEX produces no
      output, so the only way to see the cause of the error is to use
      CPXXgeterrorstring.  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");
      CPXXgeterrorstring (env, status, errmsg);
      fprintf (stderr, "%s", errmsg);
      goto TERMINATE;
   }

   /* Turn on output to the screen */

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

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

   lp = CPXXcreateprob (env, &status, "noswot");

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

   /* Set parameters */

   /* Assure linear mappings between the presolved and original
      models */

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


   /* Create user cuts for noswot problem */

   status = addusercuts (env, lp); 
   if ( status )  goto TERMINATE;

   /* Optimize the problem and obtain solution */

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

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

   status = CPXXgetobjval (env, lp, &objval);
   if ( status ) {
      fprintf (stderr,"Failed to obtain objective value.\n");
      goto TERMINATE;
   }

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

   cur_numcols = CPXXgetnumcols (env, lp);

   /* Allocate space for solution */

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

   status = CPXXgetx (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 filename */

   free_and_null ((char **) &noswot);

   /* Free the solution vector */

   free_and_null ((char **) &x);

   /* Free the problem as allocated by CPXXcreateprob and
      CPXXreadcopyprob, if necessary */

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

   /* Free the CPLEX environment, if necessary */

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

      /* Note that CPXXcloseCPLEX produces no output, so the only 
         way to see the cause of the error is to use
         CPXXgeterrorstring.  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");
         CPXXgeterrorstring (env, status, errmsg);
         fprintf (stderr, "%s", errmsg);
      }
   }
     
   return (status);

} /* END main */
Пример #12
0
int
main (void)
{
   /* Declare variables and arrays where we will store the
      optimization results including the status, objective value,
      and variable values. */

   int      solstat;
   double   objval;
   double   x[2*NUMEDGES]; /* One flow variable and one fixed charge indicator
                              for each edge */

   CPXENVptr env = NULL;
   CPXLPptr  lp = NULL;
   int       status;
   int       j;


   /* Initialize the CPLEX environment */

   env = CPXXopenCPLEX (&status);

   /* If an error occurs, the status value indicates the reason for
      failure.  A call to CPXXgeterrorstring will produce the text of
      the error message.  Note that CPXXopenCPLEX produces no output,
      so the only way to see the cause of the error is to use
      CPXXgeterrorstring.  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");
      CPXXgeterrorstring (env, status, errmsg);
      fprintf (stderr, "%s", errmsg);
      goto TERMINATE;
   }

   /* Turn on output to the screen */

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


   /* Create the problem. */

   lp = CPXXcreateprob (env, &status, "fixnet");

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

   /* Build the fixed-charge network flow model using
      indicator constraints. */

   status = buildnetwork (env, lp);
   if ( status ) {
      fprintf (stderr, "Failed to build network.\n");
      goto TERMINATE;
   }

   /* Optimize the problem and obtain solution. */

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

   solstat = CPXXgetstat (env, lp);


   /* Write solution status and objective to the screen. */

   printf ("\nSolution status = %d\n", solstat);
 
   status = CPXXgetobjval (env, lp, &objval);
   if ( status ) {
      fprintf (stderr, "No MIP objective value available.  Exiting...\n");
      goto TERMINATE;
   }

   printf ("Solution value  = %f\n", objval);
   printf ("Solution vector:\n");
   dumpx (env, lp);

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

   /* Make sure flow satisfies fixed-charge constraints */

   for (j = 0; j < NUMEDGES; j++) {
      if ( x[j] > 0.0001 && x[NUMEDGES+j] < 0.9999 ) {
         printf ("WARNING : Edge from %d to %d has non-zero flow %.3f\n",
                 orig[j], dest[j], x[j]);
         printf ("        : fixed-charge indicator has value %.6f.\n",
                 x[NUMEDGES+j]);
      }
   }
   printf("\n");

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

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

   /* Free problem */

   status = CPXXfreeprob (env, &lp);
   if ( status ) {
      fprintf (stderr, "CPXXfreeprob failed, error code %d.\n", status);
      goto TERMINATE;
   }

TERMINATE:

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

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

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

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

      /* Note that CPXXcloseCPLEX produces no output,
         so the only way to see the cause of the error is to use
         CPXXgeterrorstring.  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");
         CPXXgeterrorstring (env, status, errmsg);
         fprintf (stderr, "%s", errmsg);
      }
   }
     
   return (status);

}  /* END main */
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,
      and variable values. */


   int      solstat;
   double   objval;
   double   *x     = NULL;

   CPXENVptr     env = NULL;
   CPXLPptr      lp = NULL;
   int           status;
   CPXDIM        j;
   CPXDIM        cur_numcols;

   /* Check the command line arguments */

   if ( argc != 2 ) {
      usage (argv[0]);
      goto TERMINATE;
   }

   /* Initialize the CPLEX environment */

   env = CPXXopenCPLEX (&status);

   /* If an error occurs, the status value indicates the reason for
      failure.  A call to CPXXgeterrorstring will produce the text of
      the error message.  Note that CPXXopenCPLEX produces no output,
      so the only way to see the cause of the error is to use
      CPXXgeterrorstring.  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");
      CPXXgeterrorstring (env, status, errmsg);
      fprintf (stderr, "%s", errmsg);
      goto TERMINATE;
   }

   /* Turn on output to the screen */

   status = CPXXsetintparam (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 = CPXXcreateprob (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 = CPXXreadcopyprob (env, lp, argv[1], NULL);
   if ( status ) {
      fprintf (stderr, "Failed to read and copy the problem data.\n");
      goto TERMINATE;
   }


   /* Optimize the problem and obtain solution. */

   status = CPXXmipopt (env, lp);

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

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

   status  = CPXXgetobjval (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_numcols stores the current number 
      of columns. */

   cur_numcols = CPXXgetnumcols (env, lp);

   /* Allocate space for solution */

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

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

   status = CPXXgetx (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++) {
      printf ( "Column %d:  Value = %17.10g\n", j, x[j]);
   }

   
   
TERMINATE:

   /* Free up the solution */

   free_and_null ((char **) &x);

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

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

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

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

      /* Note that CPXXcloseCPLEX produces no output,
         so the only way to see the cause of the error is to use
         CPXXgeterrorstring.  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");
         CPXXgeterrorstring (env, status, errmsg);
         fprintf (stderr, "%s", errmsg);
      }
   }
     
   return (status);

}  /* END main */
Пример #14
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, relobj;
   double *x = NULL;
	 
   MYCB info;

   CPXENVptr env = NULL;
   CPXLPptr  lp  = NULL;
   CPXLPptr  lpclone = NULL;

   CPXDIM j;
   CPXDIM cur_numcols;

   /* Check the command line arguments */

   if ( argc != 2 ) {
      usage (argv[0]);
      goto TERMINATE;
   }

   /* Initialize the CPLEX environment */

   env = CPXXopenCPLEX (&status);

   /* If an error occurs, the status value indicates the reason for
      failure.  A call to CPXXgeterrorstring will produce the text of
      the error message.  Note that CPXXopenCPLEX produces no
      output, so the only way to see the cause of the error is to use
      CPXXgeterrorstring.  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");
      CPXXgeterrorstring (env, status, errmsg);
      fprintf (stderr, "%s", errmsg);
      goto TERMINATE;
   }

   /* Turn on output to the screen */

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


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

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

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

   lp = CPXXcreateprob (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 = CPXXreadcopyprob (env, lp, argv[1], NULL);
   if ( status ) {
      fprintf (stderr,
               "Failed to read and copy the problem data.\n");
      goto TERMINATE;
   }

   /* We transfer a problem with semi-continuous or semi-integer
      variables to a MIP problem by adding variables and  
      constraints. So in MIP callbacks, the size of the problem
      is changed and this example won't work for such problems */

   if ( CPXXgetnumsemicont (env, lp) + CPXXgetnumsemiint (env, lp) ) {
      fprintf (stderr, 
         "Not for problems with semi-continuous or semi-integer variables.\n");
      goto TERMINATE;
   }

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

   cur_numcols = CPXXgetnumcols (env, lp);

   x = malloc (cur_numcols * sizeof (*x));
   if ( x == NULL ) {
      fprintf (stderr, "Memory allocation failed.\n");
      goto TERMINATE;
   }

   /* Solve relaxation of MIP */

   /* Clone original model */

   lpclone = CPXXcloneprob (env, lp, &status);
   if ( status ) {
      fprintf (stderr, "Failed to clone problem.\n");
      goto TERMINATE;
   }

   /* Relax */

   status = CPXXchgprobtype (env, lpclone, CPXPROB_LP);
   if ( status ) {
      fprintf (stderr, "Failed to relax problem.\n");
      goto TERMINATE;
   }

   /* Solve LP relaxation of original model using "default"
      LP solver */

   status = CPXXlpopt (env, lpclone);
   if ( status ) {
      fprintf (stderr, "Failed to solve relaxation.\n");
      goto TERMINATE;
   }

   printf ("Solution status %d.\n", CPXXgetstat(env,lpclone));

   status = CPXXsolution (env, lpclone, NULL, &relobj, x, NULL,
                         NULL, NULL);
   if ( status ) {
      fprintf (stderr, "Failed to extract solution.\n");
      goto TERMINATE;
   }

   printf ("\nLP relaxation objective: %.4e\n\n", relobj);

   /* Set up solve callback */
   
   info.count = 0;
   info.mip   = lp;
   info.relx  = x;

   status = CPXXsetsolvecallbackfunc (env, &solvecallback,
                                     (void *) &info);
   if ( status ) {
      fprintf (stderr, "Failed to set solve callback.\n");
      goto TERMINATE;
   }

   /* Optimize the problem and obtain solution */

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

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

   status = CPXXgetobjval (env, lp, &objval);
   if ( status ) {
      fprintf (stderr,"Failed to obtain objective value.\n");
      goto TERMINATE;
   }

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

   status = CPXXgetx (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 CPXXcreateprob and
      CPXXreadcopyprob, if necessary */

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

   /* Free the cloned lp as allocated by CPXXcloneprob,
      if necessary */

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

   /* Free the CPLEX environment, if necessary */

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

      /* Note that CPXXcloseCPLEX produces no output, so the only 
         way to see the cause of the error is to use
         CPXXgeterrorstring.  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");
         CPXXgeterrorstring (env, status, errmsg);
         fprintf (stderr, "%s", errmsg);
      }
   }
     
   return (status);

} /* END main */
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,
      and variable values. */


   int      solstat;
   double   objval;
   double   incobjval;
   double   meanobjval;
   double   *x     = NULL;
   double   *incx  = NULL;
   int      numsol;
   int      numsolreplaced;
   int      numdiff;

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

   /* Check the command line arguments */

   if ( argc != 2 ) {
      usage (argv[0]);
      goto TERMINATE;
   }

   /* Initialize the CPLEX environment */

   env = CPXXopenCPLEX (&status);

   /* If an error occurs, the status value indicates the reason for
      failure.  A call to CPXXgeterrorstring will produce the text of
      the error message.  Note that CPXXopenCPLEX produces no output,
      so the only way to see the cause of the error is to use
      CPXXgeterrorstring.  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");
      CPXXgeterrorstring (env, status, errmsg);
      fprintf (stderr, "%s", errmsg);
      goto TERMINATE;
   }

   /* Turn on output to the screen */

   status = CPXXsetintparam (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 = CPXXcreateprob (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 = CPXXreadcopyprob (env, lp, argv[1], NULL);
   if ( status ) {
      fprintf (stderr, "Failed to read and copy the problem data.\n");
      goto TERMINATE;
   }

   /* Set the solution pool relative gap parameter to obtain solutions
      of objective value within 10% of the optimal */

   status = CPXXsetdblparam (env, CPXPARAM_MIP_Pool_RelGap, 0.1);
   if ( status ) {
      fprintf (stderr, 
               "Failed to set the solution pool relative gap, error %d.\n", 
               status);
      goto TERMINATE;
   }


   /* Optimize the problem and obtain multiple solutions. */

   status = CPXXpopulate (env, lp);

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

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

   status  = CPXXgetobjval (env, lp, &incobjval);

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

   printf ("Objective value of the incumbent: %.10g\n", incobjval);

   /* The size of the problem should be obtained by asking CPLEX what
      the actual size is. cur_numcols stores the current number 
      of columns. */

   cur_numcols = CPXXgetnumcols (env, lp);

   /* Allocate space for solution */

   incx = malloc (cur_numcols*sizeof(*incx));

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

   status = CPXXgetx (env, lp, incx, 0, cur_numcols-1);
   if ( status ) {
      fprintf (stderr, "Failed to obtain the incumbent.\n");
      goto TERMINATE;
   }

   /* Write out the incumbent */

   for (j = 0; j < cur_numcols; j++) {
      printf ("Incumbent: Column %d:  Value = %17.10g\n", j, incx[j]);
   }
   printf ("\n");

   /* Get the number of solutions in the solution pool */

   numsol = CPXXgetsolnpoolnumsolns (env, lp);   
   printf ("The solution pool contains %d solutions.\n", numsol);

   /* Some solutions are deleted from the pool because of the solution
      pool relative gap parameter */

   numsolreplaced = CPXXgetsolnpoolnumreplaced (env, lp);
   printf (
"%d solutions were removed due to the solution pool relative gap parameter.\n",
          numsolreplaced);

   printf ("In total, %d solutions were generated.\n",
           numsol + numsolreplaced);
   
   /* Get the average objective value of solutions in the solution
      pool */

   status = CPXXgetsolnpoolmeanobjval (env, lp, &meanobjval);
   printf ("The average objective value of the solutions is %.10g.\n\n",
          meanobjval);

   /* Write out the objective value of each solution and its
      difference to the incumbent */

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

   printf ("Solution        Objective   Number of variables\n");
   printf ("                value       that differ compared to\n");
   printf ("                            the incumbent\n");
  

   for (i = 0; i < numsol; i++) {
      char namei[BUFSIZE];
      CPXSIZE surplus;

      /* Write out objective value */

      CPXXgetsolnpoolsolnname (env, lp, namei, BUFSIZE, &surplus, i);
      printf ("%-15s ", namei); 


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

      status = CPXXgetsolnpoolx (env, lp, i, x, 0, cur_numcols-1);
      if ( status ) {
         fprintf (stderr, "Failed to obtain solution %d.\n", i);
         goto TERMINATE;
      }
      
      /* Compute the number of variables that differ in the solution
         and in the incumbent */

      numdiff = 0;
      for (j = 0; j < cur_numcols; j++) {
         if ( fabs (x[j] - incx[j]) > EPSZERO )
            numdiff++;
      }      
      printf ("%d / %d\n", numdiff, cur_numcols);
   }

   
TERMINATE:

   /* Free up the solution */

   free_and_null ((char **) &incx);
   free_and_null ((char **) &x);

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

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

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

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

      /* Note that CPXXcloseCPLEX produces no output,
         so the only way to see the cause of the error is to use
         CPXXgeterrorstring.  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");
         CPXXgeterrorstring (env, status, errmsg);
         fprintf (stderr, "%s", errmsg);
      }
   }
     
   return (status);

}  /* END main */
Пример #16
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;
   CPXDIM   numcols;
   CPXDIM   numrows;
   int      objsen;
   double   *obj = NULL;
   double   *rhs = NULL;
   char     *sense = NULL;
   CPXNNZ   *matbeg = NULL;
   CPXDIM   *matcnt = NULL;
   CPXDIM   *matind = NULL;
   double   *matval = NULL;
   double   *lb = NULL;
   double   *ub = NULL;
   CPXNNZ   *qmatbeg = NULL;
   CPXDIM   *qmatcnt = NULL;
   CPXDIM   *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;
   CPXDIM        i, j;
   CPXDIM        cur_numrows, cur_numcols;

   /* Initialize the CPLEX environment */

   env = CPXXopenCPLEX (&status);

   /* If an error occurs, the status value indicates the reason for
      failure.  A call to CPXXgeterrorstring will produce the text of
      the error message.  Note that CPXXopenCPLEX produces no output,
      so the only way to see the cause of the error is to use
      CPXXgeterrorstring.  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");
      CPXXgeterrorstring (env, status, errmsg);
      fprintf (stderr, "%s", errmsg);
      goto TERMINATE;
   }

   /* Turn on output to the screen */

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

   status = CPXXsolution (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 CPXXcopylp.
      cur_numrows and cur_numcols store the current number of rows and
      columns, respectively.  */

   cur_numrows = CPXXgetnumrows (env, lp);
   cur_numcols = CPXXgetnumcols (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 = CPXXwriteprob (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 CPXXcreateprob, if necessary */

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

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

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

      /* Note that CPXXcloseCPLEX produces no output,
         so the only way to see the cause of the error is to use
         CPXXgeterrorstring.  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");
         CPXXgeterrorstring (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 */
Пример #17
0
static int
steelt_opt_and_soln (void (CPXPUBLIC *errmsgfunc)(void *, const char *), 
                     void *handle, int numprod, int tweeks, double const *rate,
                     double const *inv0, double const *avail,
                     double const *flatmarket, double const *prodcost,
                     double const *invcost, double const *flatrevenue,
                     int *lpstat_p, double *objval_p, double *flatmake, 
                     double *flatinv, double *flatsell, int buildbycolumn)
{
   char const *probname = "steelT";

   CPXENVptr     env = NULL;
   CPXLPptr      lp  = NULL;
   CPXCHANNELptr cpxerror   = NULL;
   CPXCHANNELptr cpxwarning = NULL;
   int    status = 0;
   int    lpstat;
   double objval;


   env = CPXXopenCPLEX (&status);

   /* If an error occurs, the status value indicates the reason for
      failure.  We'll call the errmsgfunc with the error, because
      we can't call CPXXmsg if CPXXopenCPLEX failed.  */

   if ( env == NULL ) {
      char  errmsg[CPXMESSAGEBUFSIZE];
      if ( errmsgfunc != NULL ) {
         (*errmsgfunc) (handle, "Could not open CPLEX environment.\n");
         CPXXgeterrorstring (env, status, errmsg);
         (*errmsgfunc) (handle, errmsg);
      }
      goto TERMINATE;
   }

   status = CPXXgetchannels (env, NULL, &cpxwarning, &cpxerror, NULL);
   if ( status ) {
      if ( errmsgfunc != NULL ) {
         (*errmsgfunc) (handle, "Error in CPXXgetchannels.\n");
      }
      goto TERMINATE;
   }

   if ( errmsgfunc != NULL ) {
      status = CPXXaddfuncdest (env, cpxerror, handle, errmsgfunc);
      if ( !status ) {
	 status = CPXXaddfuncdest (env, cpxwarning, handle, errmsgfunc);
      }
   
      if ( status ) {
	 (*errmsgfunc) (handle, "Error in CPXXaddfuncdest.\n");
	 goto TERMINATE;
      }
   }

   /* Now that the channels have the error message function, we can
      use CPXXmsg for errors. */

   /* Create the problem */

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

   if ( lp == NULL ) {
      status = -2;
      CPXXmsg (cpxerror, "Failed to create LP.\n");
      goto TERMINATE;
   }

   if ( buildbycolumn )
      status = colsteel (numprod, tweeks, rate, inv0, avail, flatmarket,
                         prodcost, invcost, flatrevenue, env, lp);
   else
      status = rowsteel (numprod, tweeks, rate, inv0, avail, flatmarket,
                         prodcost, invcost, flatrevenue, env, lp);

   if ( status )  goto TERMINATE;


   status = CPXXlpopt (env, lp);
   if ( status ) {
      CPXXmsg (cpxerror,"Optimization failed. Status %d.\n", status);
      goto TERMINATE;
   }

   status = CPXXsolution (env, lp, &lpstat, &objval, NULL, NULL, NULL, NULL);
   if ( status ) {
      CPXXmsg (cpxerror, "Solution failed. Status %d.\n", status); 
      goto TERMINATE;
   }

   *objval_p = objval;
   *lpstat_p = lpstat;

   /* Now get the solution.  Use the fact that the variables are
    * added in the order Make, Inv, Sell, and that the slices of
    * the x vector can be used to copy over the solution into
    * the flat... arrays. */

   /* Get the Make variables */
   status = CPXXgetx (env, lp, flatmake, 0, numprod*tweeks-1);
   if ( status ) {
      CPXXmsg (cpxerror, "CPXXgetx failed to get Make solution.  Status %d.\n",
              status);
      goto TERMINATE;
   }

   /* Get the Inv variables */
   status = CPXXgetx (env, lp, flatinv, numprod*tweeks, 2*numprod*tweeks-1);
   if ( status ) {
      CPXXmsg (cpxerror, "CPXXgetx failed to get Inv solution.  Status %d.\n",
              status);
      goto TERMINATE;
   }

   /* Get the Sell variables */
   status = CPXXgetx (env, lp, flatsell, 2*numprod*tweeks, 
                     3*numprod*tweeks-1);
   if ( status ) {
      CPXXmsg (cpxerror, "CPXXgetx failed to get Sell solution.  Status %d.\n",
              status);
      goto TERMINATE;
   }

TERMINATE:
   if ( lp != NULL )   CPXXfreeprob (env, &lp);

   if ( errmsgfunc != NULL && env != NULL ) {
      int  dfstat;
      dfstat = CPXXdelfuncdest (env, cpxerror, handle, errmsgfunc);
      if ( !dfstat ) {
         dfstat = CPXXdelfuncdest (env, cpxwarning, handle, errmsgfunc);
      }
      if ( dfstat ) {
         (*errmsgfunc) (handle, "CPXXdelfuncdest failed.\n");
      }
      if ( dfstat && !status )  status = dfstat;
   }
   
   /* Free up the CPLEX environment, if necessary */

   if ( env != NULL ) {
      int  closestat;
      closestat = CPXXcloseCPLEX (&env);

      if ( closestat && errmsgfunc != NULL) {
      char  errmsg[CPXMESSAGEBUFSIZE];
         (*errmsgfunc) (handle, "Could not close CPLEX environment.\n");
         CPXXgeterrorstring (env, closestat, errmsg);
         (*errmsgfunc) (handle, errmsg);
      }
      if ( closestat && !status ) status = closestat;
   }

   return (status);

} /* END steelt_opt_and_soln */