1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
| /
#include <cplex.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/* Include declarations for functions in this program */
static void
free_and_null (char **ptr);
int
main (void)
{
CPXENVptr env = NULL;
CPXLPptr lp = NULL;
int status = 0;
int j;
int numcols;
double totinv;
int solstat;
double objval;
double *x = NULL;
double rrhs[1];
char rsense[1];
int rmatbeg[1];
int *indices = NULL;
double *values = NULL;
char *namestore = NULL;
char **nameptr = NULL;
int surplus, storespace;
/* 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;
}
/* Create the problem, using the filename as the problem name */
lp = CPXcreateprob (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 CPX_PARAM_SCRIND causes the error message to
appear on stdout. Note that most CPLEX routines return
an error code to indicate the reason for failure. */
if ( lp == NULL ) {
fprintf (stderr, "Failed to create LP.\n");
goto TERMINATE;
}
/* Now read the file, and copy the data into the created lp */
status = CPXreadcopyprob (env, lp, "../../data/prod.lp", 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 = CPXsetintparam (env, CPX_PARAM_REDUCE, 1) ||
CPXsetintparam (env, CPX_PARAM_SIMDISPLAY, 0);
if ( status ) {
fprintf (stderr, "Failure to set parameters\n");
goto TERMINATE;
}
/* Optimize the problem and obtain solution. */
status = CPXlpopt (env, lp);
if ( status ) {
fprintf (stderr, "Failed to optimize profit LP.\n");
goto TERMINATE;
}
solstat = CPXgetstat (env, lp);
status = CPXgetobjval (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 = CPXgetnumcols (env, lp);
if ( !numcols ) {
fprintf (stderr, "No columns in problem\n");
goto TERMINATE;
}
CPXgetcolname (env, lp, NULL, NULL, 0, &surplus, 0, numcols-1);
storespace = - surplus;
namestore = (char *) malloc (storespace * sizeof(char));
nameptr = (char **) malloc (numcols * sizeof(char *));
if ( namestore == NULL || nameptr == NULL ) {
fprintf (stderr, "No memory for column names\n");
goto TERMINATE;
}
status = CPXgetcolname (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 = (double *) malloc (numcols * sizeof(double));
if ( x == NULL ) {
fprintf (stderr,"No memory for solution.\n");
goto TERMINATE;
}
status = CPXgetx (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 = (int *) malloc (numcols * sizeof (int));
values = (double *) malloc (numcols * sizeof (double));
if ( indices == NULL || values == NULL ) {
fprintf (stderr, "No memory for constraint\n");
goto TERMINATE;
}
/* Get profit objective and add it as a constraint */
status = CPXgetobj (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 = CPXpreaddrows (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 = CPXprechgobj (env, lp, numcols, indices, values);
if ( status ) {
fprintf (stderr,
"Failed to change to inventory objective. Status %d\n",
status);
goto TERMINATE;
}
status = CPXlpopt (env, lp);
if ( status ) {
fprintf (stderr, "Optimization on inventory level failed. Status %d.\n",
status);
goto TERMINATE;
}
solstat = CPXgetstat (env, lp);
status = CPXgetobjval (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 = CPXgetx (env, lp, x, 0, numcols-1);
if ( status ) {
fprintf (stderr, "Failed to obtain primal solution.\n");
goto TERMINATE;
}
/* Write out the solution */
printf ("\n");
for (j = 0; j < numcols; j++) {
printf ( "%s: Value = %17.10g\n", nameptr[j], x[j]);
}
TERMINATE:
/* 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 = 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);
}
}
return (status);
} /* END main */
/* This simple routine frees up the pointer *ptr, and sets *ptr to NULL */
static void
free_and_null (char **ptr)
{
if ( *ptr != NULL ) {
free (*ptr);
*ptr = NULL;
}
getchar();
} /* END free_and_null */ |
Partager