/* Bring in the CPLEX function declarations and the C library header file stdio.h with the following single include. */ #include /* Bring in the declarations for the string functions */ #include #include #include /* Include declaration for functions at end of program */ static int populatebyrow (CPXENVptr env, CPXLPptr lp, double **norm, double **tum, int nnorm, int ntum, int nge, int *active, int numactive, double margin); static void free_and_null (char **ptr), usage (char *progname); #define NR 500 #define NC 20000 #define NT 5000000 #define EPS 0.4 #define DEL 0.01 int NUMROWS; int NUMCOLS; int NUMNZ; int status = 0; double obj[NC]; double lb[NC]; double ub[NC]; char *colname[NC]; int rmatbeg[NR]; int rmatind[NT]; double rmatval[NT]; int qmatbeg[NC]; int qmatind[NC]; int qmatcnt[NC]; double qmatval[NC]; double rhs[NR]; char sense[NR]; char *rowname[NR]; int main (argc, argv) 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, variable values, dual values, row slacks and variable reduced costs. */ int solstat; double objval; double *x = NULL; double *pi = NULL; double *slack = NULL; double *dj = NULL; int nnorm; int ntum; int nge; double **norm; double **tum; double tempfloat; double margin; CPXENVptr env = NULL; CPXLPptr lp = NULL; int status = 0; int i, j; int cur_numrows, cur_numcols; int *activetum; int numactive; FILE *fp1, *fp2; char s1[1024],s2[1024]; double diff; int pos; FILE *fp; /* Check the command line arguments */ scanf("%d%d%d",&nnorm,&ntum,&nge); norm = (double **)calloc(nnorm,sizeof(double *)); for(i = 0; i < nnorm; i++) norm[i] = (double *)calloc(nge,sizeof(double)); tum = (double **)calloc(ntum,sizeof(double *)); for(i = 0; i < ntum; i++) tum[i] = (double *)calloc(nge,sizeof(double)); activetum = (int *)calloc(ntum,sizeof(int)); fp = fopen("normal.txt","r"); for( j = 0; j < nge; j++) for( i = 0; i < nnorm; i++) { fscanf(fp,"%lf",&tempfloat); norm[i][j] = tempfloat; } fclose(fp); fp = fopen("tumor.txt","r"); for( j = 0; j < nge; j++) for( i = 0; i < ntum; i++) { fscanf(fp,"%lf",&tempfloat); tum[i][j] = tempfloat; } fclose(fp); NUMCOLS = nge+1; for(j = 0; j < nge; j++) for(i = 1; i <= nnorm; i++) { rmatval[i*NUMCOLS+j] = norm[i-1][j]; rmatind[i*NUMCOLS+j] = j; } for(i = 0; i < NUMCOLS; i++) { qmatbeg[i] = i; qmatcnt[i] = 1; qmatind[i] = i; qmatval[i] = 1.0; } qmatval[NUMCOLS-1] = 0.0; fp1 = fopen("val.txt","r"); fp2 = fopen("node.txt","r"); srand48(0); while(fgets(s1,1024,fp2) != NULL) { fscanf(fp1,"%lf",&margin); numactive = 0; pos = 0; for(i = 0; i < 1024; i++) { if(s1[i] == '\n') { s2[pos] = '\0'; activetum[numactive++] = atoi(s2); pos = 0; break; } if(s1[i] == ' ') { s2[pos] = '\0'; activetum[numactive++] = atoi(s2); pos = 0; } if(isdigit(s1[i])) s2[pos++] = s1[i]; } /* Create the problem. */ /* 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 data checking */ status = CPXsetintparam (env, CPX_PARAM_DATACHECK, CPX_ON); if ( status ) { fprintf (stderr, "Failure to turn on data checking, error %d.\n", status); goto TERMINATE; } lp = CPXcreateprob (env, &status, "lpex1"); /* 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 populate the problem with the data. For building large problems, consider setting the row, column and nonzero growth parameters before performing this task. */ status = populatebyrow (env, lp, norm,tum, nnorm,ntum,nge, activetum,numactive,margin); if ( status ) { fprintf (stderr, "Failed to populate problem.\n"); goto TERMINATE; } /* Optimize the problem and obtain solution. */ status = CPXbaropt (env, lp); if ( status ) { fprintf (stderr, "Failed to optimize LP.\n"); goto TERMINATE; } /* Free up the solution */ free_and_null ((char **) &x); free_and_null ((char **) &slack); free_and_null ((char **) &dj); free_and_null ((char **) &pi); /* The size of the problem should be obtained by asking CPLEX what the actual size is, rather than using sizes from when the problem was built. cur_numrows and cur_numcols store the current number of rows and columns, respectively. */ cur_numrows = CPXgetnumrows (env, lp); cur_numcols = CPXgetnumcols (env, lp); x = (double *) malloc (cur_numcols * sizeof(double)); slack = (double *) malloc (cur_numrows * sizeof(double)); dj = (double *) malloc (cur_numcols * sizeof(double)); pi = (double *) malloc (cur_numrows * sizeof(double)); if ( x == NULL || slack == NULL || dj == NULL || pi == NULL ) { status = CPXERR_NO_MEMORY; fprintf (stderr, "Could not allocate memory for solution.\n"); goto TERMINATE; } status = CPXsolution (env, lp, &solstat, &objval, x, pi, slack, dj); if ( status ) { fprintf (stderr, "Failed to obtain solution.\n"); goto 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); } } printf("\n=========================\n%s\n============================\n",s1); for(i = 0; i < nge; i++) if(x[i] > 0.001) printf("%d %f\n",i,x[i]); fflush(stdout); } TERMINATE: return (status); } /* END main */ /* This simple routine frees up the pointer *ptr, and sets *ptr to NULL */ static void free_and_null (ptr) char **ptr; { if ( *ptr != NULL ) { free (*ptr); *ptr = NULL; } } /* END free_and_null */ static void usage (progname) char *progname; { fprintf (stderr,"Usage: %s -X\n", progname); fprintf (stderr," where X is one of the following options: \n"); fprintf (stderr," r generate problem by row\n"); fprintf (stderr," c generate problem by column\n"); fprintf (stderr," n generate problem by nonzero\n"); fprintf (stderr," Exiting...\n"); } /* END usage */ /* These functions all populate the problem with data for the following 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 */ /* To populate by row, we first create the columns, and then add the rows. */ static int populatebyrow (env, lp, norm, tum, nnorm, ntum, nge, active, numactive,margin) CPXENVptr env; CPXLPptr lp; double **norm; double **tum; int nnorm; int ntum; int nge; int *active; int numactive; double margin; { int i,j; NUMROWS = nnorm + 1 + numactive; NUMNZ = NUMROWS * NUMCOLS; for(j = 0; j < nge; j++) for(i = 0; i < numactive; i++) { rmatval[(i+nnorm+1)*NUMCOLS+j] = tum[active[i]][j]; rmatind[(i+nnorm+1)*NUMCOLS+j] = j; } for(i = 0; i < NUMROWS; i++) { rhs[i] = 0.0; rowname[i] = ""; rmatbeg[i] = i*NUMCOLS; rmatind[i*NUMCOLS+NUMCOLS-1] = NUMCOLS-1; if(i > 0 && i <= nnorm) { sense[i] = 'L'; rmatval[i*NUMCOLS+NUMCOLS-1] = -1.0; } else if(i > nnorm) { rhs[i] = margin - EPS; sense[i] = 'G'; rmatval[i*NUMCOLS+NUMCOLS-1] = -1.0; } } for(j = 0; j < NUMCOLS; j++) { colname[j] = ""; rmatval[j] = 1.0; rmatind[j] = j; } rmatval[NUMCOLS-1] = 0.0; sense[0] = 'E'; rhs[0] = 1.0; obj[NUMCOLS-1] = 0.0; for(j = 0; j < NUMCOLS-1; j++) { obj[j] = 0.0; lb[j] = 0.0; ub[j] = 1.0; } lb[NUMCOLS-1] = -100.0; ub[NUMCOLS-1] = 100.0; status = CPXnewcols (env, lp, NUMCOLS, obj, lb, ub, NULL, colname); if ( status ) goto TERMINATE; status = CPXaddrows (env, lp, 0, NUMROWS, NUMNZ, rhs, sense, rmatbeg, rmatind, rmatval, NULL, rowname); if ( status ) goto TERMINATE; status = CPXcopyquad(env,lp,qmatbeg,qmatcnt,qmatind,qmatval); if(status) { fprintf(stderr,"Failed to Copy Quadratic Matrix\n"); goto TERMINATE; } TERMINATE: return (status); } /* END populatebyrow */