/* -------------------------------------------------------------------------- * File: sub_gradient_cplex.c * Version 1.0 * -------------------------------------------------------------------------- */ /* mip.c - Entering and optimizing a problem */ /* Bring in the CPLEX function declarations and the C library header file stdio.h with the include of cplex.h. */ /* Pour compiler: gcc mip.c -o mip -L /opt/lipn/software/cplex101/lib/x86_rhel4.0_3.4/static_pic/ -lcplex -lm -lnsl -lpthread */ #include #include #include /* Bring in the declarations for the string functions */ #include /* Include declaration for function at end of program */ #define CardR 10 #define T 5 #define CardRi 2 #define CardRc 2 int NUMNucl; int NUMTps; int NUMAff; int NUMCliq; double cl[CardR], ct[T], dlt[CardR][T], Dt[T], Mint[T], Maxt[T]; int Affect[CardR][CardRi], Clique[CardR][CardRc], bc[CardRc]; void init (void); static int setproblemdata (char **probname_p, int *numcols_p, int *numrows_p, int *objsen_p, double **obj_p, double **rhs_p, char **sense_p, int **matbeg_p, int **matcnt_p, int **matind_p, double **matval_p, double **lb_p, double **ub_p, char **ctype_p); static void free_and_null (char **ptr); // #define NUMROWS 2 // #define NUMCOLS 3 // #define NUMNZ 6 static int NUMROWS; static int NUMCOLS; static int NUMNZ; int main (void) { /* Declare pointers for the variables and arrays that will contain the data which define the LP problem. The setproblemdata() routine allocates space for the problem data. */ char *probname = NULL; int numcols; int numrows; int objsen; double *obj = NULL; double *rhs = NULL; char *sense = NULL; int *matbeg = NULL; int *matcnt = NULL; int *matind = NULL; double *matval = NULL; double *lb = NULL; double *ub = NULL; char *ctype = NULL; /* 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. */ init(); NUMNucl = CardR; NUMTps = T; NUMAff = CardRi; NUMCliq = CardRc; NUMCOLS = NUMNucl+NUMTps; NUMROWS = NUMTps+NUMAff+NUMCliq; NUMNZ = NUMROWS * NUMCOLS; int solstat; double objval; double x[NUMCOLS]; double pi[NUMROWS]; double slack[NUMROWS]; double dj[NUMCOLS]; CPXENVptr env = NULL; CPXLPptr lp = NULL; int status; int i, j; int cur_numrows, cur_numcols; /* Initialize the CPLEX environment */ env = CPXopenCPLEX (&status); /* If an error occurs, the status value indicates the reason for failure. A call to CPXgeterrorstring will produce the text of the error message. Note that CPXopenCPLEX produces no output, so the only way to see the cause of the error is to use CPXgeterrorstring. For other CPLEX routines, the errors will be seen if the CPX_PARAM_SCRIND indicator is set to CPX_ON. */ if ( env == NULL ) { char errmsg[1024]; fprintf (stderr, "Could not open CPLEX environment.\n"); CPXgeterrorstring (env, status, errmsg); fprintf (stderr, "%s", errmsg); goto TERMINATE; } /* Turn on output to the screen */ status = CPXsetintparam (env, CPX_PARAM_SCRIND, CPX_ON); if ( status ) { fprintf (stderr, "Failure to turn on screen indicator, error %d.\n", status); goto TERMINATE; } /* Allocate memory and fill in the data for the problem. */ status = setproblemdata (&probname, &numcols, &numrows, &objsen, &obj, &rhs, &sense, &matbeg, &matcnt, &matind, &matval, &lb, &ub, &ctype); if ( status ) { fprintf (stderr, "Failed to build problem data arrays.\n"); goto TERMINATE; } /* Create the problem. */ lp = CPXcreateprob (env, &status, probname); /* A returned pointer of NULL may mean that not enough memory was available or there was some other problem. In the case of failure, an error message will have been written to the error channel from inside CPLEX. In this example, the setting of the parameter CPX_PARAM_SCRIND causes the error message to appear on stdout. */ if ( lp == NULL ) { fprintf (stderr, "Failed to create LP.\n"); goto TERMINATE; } /* Now copy the problem data into the lp */ status = CPXcopylp (env, lp, numcols, numrows, objsen, obj, rhs, sense, matbeg, matcnt, matind, matval, lb, ub, NULL); if ( status ) { fprintf (stderr, "Failed to copy problem data.\n"); goto TERMINATE; } /* Now copy the ctype array */ status = CPXcopyctype (env, lp, ctype); if ( status ) { fprintf (stderr, "Failed to copy ctype\n"); goto TERMINATE; } /* Finally, write a copy of the problem to a file. */ status = CPXwriteprob (env, lp, "mip.lp", NULL); if ( status ) { fprintf (stderr, "Failed to write LP to disk.\n"); goto TERMINATE; } /* Optimize the problem and obtain solution. */ status = CPXmipopt (env, lp); if ( status ) { fprintf (stderr, "Failed to optimize LP.\n"); goto TERMINATE; } solstat = CPXgetstat (env, lp); /* Write the output to the screen. */ printf ("\nSolution status = %d\n", solstat); status = CPXgetmipobjval (env, lp, &objval); if ( status ) { fprintf (stderr,"No MIP objective value available. Exiting...\n"); goto TERMINATE; } printf ("Solution value = %f\n\n", objval); /* The size of the problem should be obtained by asking CPLEX what the actual size is, rather than using what was passed to CPXcopylp. cur_numrows and cur_numcols store the current number of rows and columns, respectively. */ cur_numrows = CPXgetnumrows (env, lp); cur_numcols = CPXgetnumcols (env, lp); status = CPXgetmipx (env, lp, x, 0, cur_numcols-1); if ( status ) { fprintf (stderr, "Failed to get optimal integer x_cplex.\n"); goto TERMINATE; } status = CPXgetmipslack (env, lp, slack, 0, cur_numrows-1); if ( status ) { fprintf (stderr, "Failed to get optimal slack values.\n"); goto TERMINATE; } for (i = 0; i < cur_numrows; i++) { printf ("Row %d: Slack = %10f\n", i, slack[i]); } for (j = 0; j < cur_numcols; j++) { printf ("Column %d: Value = %10f\n", j, x[j]); } // status = CPXsolution (env, lp, &solstat, &objval, x, pi, slack, dj); // if ( status ) { // fprintf (stderr, "Failed to obtain solution.\n"); // goto TERMINATE; // } // // // /* Write the output to the screen. */ // // printf ("\nSolution status = %d\n", solstat); // printf ("Solution value = %f\n\n", objval); // // /* The size of the problem should be obtained by asking CPLEX what // the actual size is, rather than using what was passed to CPXcopylp. // cur_numrows and cur_numcols store the current number of rows and // columns, respectively. */ // // cur_numrows = CPXgetnumrows (env, lp); // cur_numcols = CPXgetnumcols (env, lp); // for (i = 0; i < cur_numrows; i++) { // printf ("Row %d: Slack = %10f Pi = %10f\n", i, slack[i], pi[i]); // } // // for (j = 0; j < cur_numcols; j++) { // printf ("Column %d: Value = %10f Reduced cost = %10f\n", // j, x[j], dj[j]); // } TERMINATE: /* Free up the problem as allocated by CPXcreateprob, if necessary */ if ( lp != NULL ) { status = CPXfreeprob (env, &lp); if ( status ) { fprintf (stderr, "CPXfreeprob failed, error code %d.\n", status); } } /* Free up the CPLEX environment, if necessary */ if ( env != NULL ) { status = CPXcloseCPLEX (&env); /* Note that CPXcloseCPLEX produces no output, so the only way to see the cause of the error is to use CPXgeterrorstring. For other CPLEX routines, the errors will be seen if the CPX_PARAM_SCRIND indicator is set to CPX_ON. */ if ( status ) { char errmsg[1024]; fprintf (stderr, "Could not close CPLEX environment.\n"); CPXgeterrorstring (env, status, errmsg); fprintf (stderr, "%s", errmsg); } } /* Free up the problem data arrays, if necessary. */ free_and_null ((char **) &probname); free_and_null ((char **) &obj); free_and_null ((char **) &rhs); free_and_null ((char **) &sense); free_and_null ((char **) &matbeg); free_and_null ((char **) &matcnt); free_and_null ((char **) &matind); free_and_null ((char **) &matval); free_and_null ((char **) &lb); free_and_null ((char **) &ub); free_and_null ((char **) &ctype); return (status); } /* END main */ static int setproblemdata (char **probname_p, int *numcols_p, int *numrows_p, int *objsen_p, double **obj_p, double **rhs_p, char **sense_p, int **matbeg_p, int **matcnt_p, int **matind_p, double **matval_p, double **lb_p, double **ub_p, char **ctype_p) { char *zprobname = NULL; /* Problem name <= 16 characters */ double *zobj = NULL; double *zrhs = NULL; char *zsense = NULL; int *zmatbeg = NULL; int *zmatcnt = NULL; int *zmatind = NULL; double *zmatval = NULL; double *zlb = NULL; double *zub = NULL; char *zctype = NULL; int status = 0; int i,j; zprobname = (char *) malloc (16 * sizeof(char)); zobj = (double *) malloc (NUMCOLS * sizeof(double)); zrhs = (double *) malloc (NUMROWS * sizeof(double)); zsense = (char *) malloc (NUMROWS * sizeof(char)); zmatbeg = (int *) malloc (NUMCOLS * sizeof(int)); zmatcnt = (int *) malloc (NUMCOLS * sizeof(int)); zmatind = (int *) malloc (NUMNZ * sizeof(int)); zmatval = (double *) malloc (NUMNZ * sizeof(double)); zlb = (double *) malloc (NUMCOLS * sizeof(double)); zub = (double *) malloc (NUMCOLS * sizeof(double)); zctype = (char *) malloc (NUMCOLS * sizeof(char)); if ( zprobname == NULL || zobj == NULL || zrhs == NULL || zsense == NULL || zmatbeg == NULL || zmatcnt == NULL || zmatind == NULL || zmatval == NULL || zctype == NULL || zlb == NULL || zub == NULL ) { status = 1; goto TERMINATE; } strcpy (zprobname, "mip"); /* The code is formatted to make a visual correspondence between the mathematical linear program and the specific data items. */ /* This function fills in the data structures for the linear program: Maximize obj: x1 + 2 x2 + 3 x3 Subject To c1: - x1 + x2 + x3 <= 20 c2: x1 - 3 x2 + x3 <= 30 Bounds 0 <= x1 <= 40 End */ for(i=0;i= zrhs[i] = Dt[i]; } else if(i>=NUMTps && i<(NUMTps+NUMAff)) { zsense[i] = 'E'; zrhs[i] = 1.0; } else { zsense[i] = 'L'; zrhs[i] = (double)bc[i-(NUMTps+NUMAff)]; } } for(i=0;i=NUMTps) { if(j=NUMNucl && j