Ejemplo n.º 1
0
/** MIP info callback that is registered with CPLEX.
 *  This callback picks up primal and dual bounds as well as the current
 *  deterministic time. If bounds changed then updated bounds are send
 *  to the master. Deterministic time stamps are always send to the master.
 */
static int CPXPUBLIC
infocallback (CPXCENVptr cbenv, void *cbdata, int wherefrom, void *cbhandle)
{
   CPXCENVptr env = static_cast<CPXCENVptr>(cbhandle);
   double dual, primal, ts;

   // Test if we have improved the primal bound and report if so.
   if ( CPXXgetcallbackinfo (cbenv, cbdata, wherefrom, CPX_CALLBACK_INFO_BEST_INTEGER, &primal) == 0 ) {
      if ( !best.havePrimal || fabs (best.primal - primal) > best.objdiff ) {
         best.havePrimal = true;
         best.primal = primal;
         (void)CPXXsendinfodouble (env, INFO_NEWPRIMAL, 1, &primal);
      }
   }

   // Test if we have improved the dual bound and report if so.
   if ( CPXXgetcallbackinfo (cbenv, cbdata, wherefrom, CPX_CALLBACK_INFO_BEST_REMAINING, &dual) == 0 ) {
      if ( !best.haveDual || fabs (best.dual - dual) > best.objdiff ) {
         best.haveDual = true;
         best.dual = dual;
         (void)CPXXsendinfodouble (env, INFO_NEWDUAL, 1, &dual);
      }
   }

   // Always report the current deterministic time.
   if ( CPXXgetdettime (cbenv, &ts) == 0 ) {
      (void)CPXXsendinfodouble (env, INFO_DETTIME, 1, &ts);
   }

   return 0;
}
Ejemplo n.º 2
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;
}
Ejemplo n.º 3
0
/* Log new incumbents if they are at better than the old by a
 * relative tolerance of 1e-5; also log progress info every
 * 100 nodes.
 */
static int CPXPUBLIC
logcallback (CPXCENVptr env, void *cbdata, int wherefrom, void *cbhandle)
{
   int status = 0;

   LOGINFOptr info = (LOGINFOptr) cbhandle;
   int        hasincumbent = 0;
   int        newincumbent = 0;
   double     dettime;
   double     objval;
   double     bound;
   double     *x = NULL;

   status = CPXXgetcallbackinfo (env, cbdata, wherefrom,
                                CPX_CALLBACK_INFO_MIP_FEAS, &hasincumbent);
   if ( status )  goto TERMINATE;

   if ( hasincumbent ) {
      status = CPXXgetcallbackinfo (env, cbdata, wherefrom,
                                   CPX_CALLBACK_INFO_BEST_INTEGER, &objval);
      if ( status )  goto TERMINATE;

      if ( fabs(info->lastincumbent - objval) > 1e-5*(1.0 + fabs(objval)) ) {
         newincumbent = 1;
         info->lastincumbent = objval;
      }
   }

   status = CPXXgetdettime (env, &dettime);
   if ( status )  goto TERMINATE;

   if ( dettime >= info->lastdettime + 1000.0  ||  newincumbent ) {
      double walltime;

      status = CPXXgetcallbackinfo (env, cbdata, wherefrom,
                                   CPX_CALLBACK_INFO_BEST_REMAINING, &bound);
      if ( status )  goto TERMINATE;

      if ( !newincumbent )  info->lastdettime = dettime;

      status = CPXXgettime (env, &walltime);
      if ( status )  goto TERMINATE;

      printf ("Time = %.2f  Dettime = %.2f  Best objective = %g",
              walltime - info->timestart, dettime - info->dettimestart, bound);
      if ( hasincumbent )  printf ("  Incumbent objective = %g\n", objval);
      else                 printf ("\n");
   }

   if ( newincumbent ) {
      int j;
      CPXDIM numcols = info->numcols;

      x = malloc (numcols*sizeof(*x));
      if ( x == NULL ) {
         status = CPXERR_NO_MEMORY;
         goto TERMINATE;
      }
      status = CPXXgetcallbackincumbent (env, cbdata, wherefrom,
                                        x, 0, numcols-1);
      if ( status )  goto TERMINATE;

      printf ("New incumbent values:\n");
      for (j = 0; j < numcols; j++) {
         if ( fabs(x[j]) > 1e-6 ) {
            printf ("  Column %d:  %g\n", j, x[j]);
         }
      }
   }

TERMINATE:

   free_and_null ((char **) &x);
   return (status);

} /* END logcallback */
Ejemplo n.º 4
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 */