int main(int argc) { IloEnv env; try { IloModel model(env); NumVarMatrix varOutput(env, J + current); NumVar3Matrix varHelp(env, J + current); Range3Matrix cons(env, J + current); for (int j = 0; j <J + current; j++){ varOutput[j] = IloNumVarArray(env, K); varHelp[j] = NumVarMatrix(env, K); cons[j] = RangeMatrix(env, K); for (int k = 0; k < K; k++){ varOutput[j][k] = IloNumVar(env, 0.0, IloInfinity); varHelp[j][k] = IloNumVarArray(env, L); cons[j][k] = IloRangeArray(env, C); for (int l = 0; l < L; l++){ varHelp[j][k][l] = IloNumVar(env, 0.0, IloInfinity); } if (j > current){ cons[j][k][0] = IloRange(env, 0.0, 0.0);//will be used to express equality of varOutput, constraint (0) cons[j][k][1] = IloRange(env, 0.0, IloInfinity);// constraint (1) cons[j][k][2] = IloRange(env, -IloInfinity, T[j] - Tdc - Tblow[j] - Tslack);// constraint (2) cons[j][k][3] = IloRange(env, Tfd[k], Tfd[k]);// constraint (3) cons[j][k][4] = IloRange(env, 0.0, IloInfinity);// constraint (4) cons[j][k][5] = IloRange(env, Tdf[k], IloInfinity);// constraint (5) cons[j][k][6] = IloRange(env, T[j - a[k]] + Tcd, T[j - a[k]] + Tcd);// constraint (6) cons[j][k][7] = IloRange(env, TlossD[k], IloInfinity);// constraint (7) cons[j][k][8] = IloRange(env, TlossF[k], IloInfinity);// constraint (8) } } } populatebynonzero(model, varOutput, varHelp, cons); IloCplex cplex(model); // Optimize the problem and obtain solution. if (!cplex.solve()) { env.error() << "Failed to optimize LP" << endl; throw(-1); } IloNumArray vals(env); IloNumVar val(env); //vars to save output double TimeAvailable[J][K]; double TimeInstances[J][K][L]; double LK103[J][2]; env.out() << "Solution status = " << cplex.getStatus() << endl; env.out() << "Solution value = " << cplex.getObjValue() << endl; for (int j = current; j < current + J; ++j) { cplex.getValues(vals, varOutput[j]); env.out() << "Seconds for load "<<j<<" = " << vals << endl; /*for (int k = 0; k < K; k++){ TimeAvailable[j][k] = cplex.getValue(varOutput[j][k]); }*/ } for (int j = current; j < current + J; j++){ for (int k = 0; k < K; k++){ cplex.getValues(vals, varHelp[j][k]); env.out() << "Time instances for spoon "<<k<<" in load "<<j<<" = " << vals << endl; /*for (int l = 0; l < L; l++){ TimeInstances[j][k][l] = cplex.getValue(varHelp[j][k][l]); }*/ } } for (int j = current + 2; j < J + current; j++){ LK103[j][0] = TimeInstances[j - 2][0][0]; LK103[j][1] = TimeInstances[j][0][5]; env.out() << "LK103, load " << j << " : " << LK103[j][1]-LK103[j][0] << endl; } /*cplex.getSlacks(vals, cons); env.out() << "Slacks = " << vals << endl; cplex.getDuals(vals, cons); env.out() << "Duals = " << vals << endl; cplex.getReducedCosts(vals, varOutput); env.out() << "Reduced Costs = " << vals << endl;*/ cplex.exportModel("lpex1.lp"); } catch (IloException& e) { cerr << "Concert exception caught: " << e << endl; } catch (...) { cerr << "Unknown exception caught" << endl; } env.end(); cin.get(); return 0; } // END main
extern int solve_allocation(int nodeSize, int windowSize, int timeout, sched_nodeinfo_t *node_array, solver_job_list_t *job_array) { solver_job_list_t *solver_job_ptr; int solstat; int n = windowSize, m = nodeSize; double objval; double *x = NULL; double *pi = NULL; double *slack = NULL; double *dj = NULL; double *obj = NULL; int NUMCOLS = n * (2 * m + 2); CPXENVptr env = NULL; CPXLPptr lp = NULL; int status = 0; int i, j, k; int cur_numrows, cur_numcols; char envstr[256] = "ILOG_LICENSE_FILE=/home/seren/ILOG/CPLEX_Studio_AcademicResearch122/licenses/access.ilm"; if ( envstr != NULL ) { CPXputenv (envstr); } env = CPXopenCPLEX (&status); if ( env == NULL ) { char errmsg[1024]; CPXgeterrorstring (env, status, errmsg); fprintf (stderr, "%s", errmsg); goto TERMINATE; } status = CPXsetintparam (env, CPX_PARAM_SCRIND, CPX_ON); if ( status ) { goto TERMINATE; } status = CPXsetintparam (env, CPX_PARAM_DATACHECK, CPX_ON); if ( status ) { goto TERMINATE; } lp = CPXcreateprob (env, &status, "lpex1"); if ( lp == NULL ) { goto TERMINATE; } obj = (double*)malloc(NUMCOLS * sizeof(double)); status = CPXsetdblparam(env,CPX_PARAM_TILIM,5); status = populatebynonzero (env, lp, nodeSize, windowSize, timeout, node_array, job_array); if ( status ) { fprintf (stderr, "Failed to populate problem."); goto TERMINATE; } status = CPXlpopt (env, lp); if ( status ) { fprintf (stderr, "Failed to optimize LP."); goto TERMINATE; } 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; goto TERMINATE; } status = CPXsolution (env, lp, &solstat, &objval, x, pi, slack, dj); if ( status ) { goto TERMINATE; } /*debug3("\nSolution status = %d\n", solstat);*/ printf("Solution value = %f\n\n", objval); /* 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]); } */ /*debug3("sending solution results to slurm");*/ /* for (j = 0; j < windowSize; j++) { if (x[j] > 0) { solver_job_ptr = &job_array[j]; solver_job_ptr->node_bitmap = (bitstr_t *) bit_alloc (node_record_count); solver_job_ptr->job_ptr->details->req_node_bitmap = (bitstr_t *) bit_alloc (node_record_count); solver_job_ptr->onnodes = (int *) xmalloc (sizeof(int)*node_record_count); solver_job_ptr->job_ptr->details->req_node_layout = (int *)xmalloc(sizeof(int) * node_record_count); solver_job_ptr->job_ptr->details->req_node_bitmap = (bitstr_t *) bit_alloc (node_record_count); for (i = 0; i < nodeSize; i++) { k = (1 + i) * windowSize + j; if (x[k] > 0) { bit_set (solver_job_ptr->node_bitmap, (bitoff_t) (i)); bit_set (solver_job_ptr->job_ptr->details->req_node_bitmap, (bitoff_t) (i)); node_array[i].rem_cpus -= x[k]; node_array[i].rem_gpus -= solver_job_ptr->gpu; solver_job_ptr->onnodes[i] = x[k]; solver_job_ptr->job_ptr->details->req_node_layout[i] = solver_job_ptr->onnodes[i]; solver_job_ptr->alloc_total += x[k]; } } } else job_array[j].alloc_total = 0; } */ /* status = CPXwriteprob (env, lp, "lpex1.lp", NULL); if ( status ) { fprintf (stderr, "Failed to write LP to disk."); goto TERMINATE; } */ TERMINATE: free_and_null ((char **) &x); free_and_null ((char **) &slack); free_and_null ((char **) &dj); free_and_null ((char **) &pi); if ( lp != NULL ) { status = CPXfreeprob (env, &lp); if ( status ) { fprintf (stderr, "CPXfreeprob failed, error code %d.", status); } } if ( env != NULL ) { status = CPXcloseCPLEX (&env); if ( status ) { char errmsg[1024]; fprintf (stderr, "Could not close CPLEX environment."); CPXgeterrorstring (env, status, errmsg); fprintf (stderr, "%s", errmsg); } } return (status); }
extern int solve_allocation(int nodeSize, int windowSize, int timeout, sched_nodeinfo_t *node_array, solver_job_list_t *job_array) { solver_job_list_t *sjob_ptr; struct job_details *job_det_ptr; int solstat; int n = windowSize, m = nodeSize; double objval; double *x = NULL; double *pi = NULL; double *slack = NULL; double *dj = NULL; CPXENVptr env = NULL; CPXLPptr lp = NULL; int status = 0; int i, j, k; int cur_numrows, cur_numcols; char envstr[256]; sprintf(envstr,"ILOG_LICENSE_FILE=%s",get_cplex_license_address()); if ( envstr != NULL ) { CPXputenv (envstr); } env = CPXopenCPLEX (&status); if ( env == NULL ) { char errmsg[1024]; fatal ("Could not open CPLEX environment.\n"); CPXgeterrorstring (env, status, errmsg); fprintf (stderr, "%s", errmsg); goto TERMINATE; } status = CPXsetintparam (env, CPX_PARAM_SCRIND, CPX_ON); if (status) { fatal("Failure to turn on screen indicator, error %d.",status); goto TERMINATE; } status = CPXsetintparam (env, CPX_PARAM_DATACHECK, CPX_ON); if (status) { fatal("Failure to turn on data checking, error %d.", status); goto TERMINATE; } lp = CPXcreateprob (env, &status, "lpex1"); if (lp == NULL) { fatal("Failed to create LP."); goto TERMINATE; } status = CPXsetdblparam(env,CPX_PARAM_TILIM,timeout); status = populatebynonzero (env, lp, m, n, timeout, node_array, job_array); if ( status ) { fprintf (stderr, "Failed to populate problem."); goto TERMINATE; } status = CPXlpopt (env, lp); if ( status ) { fprintf (stderr, "Failed to optimize LP."); goto TERMINATE; } 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; fatal ("Could not allocate memory for solution."); goto TERMINATE; } status = CPXsolution (env, lp, &solstat, &objval, x, pi, slack, dj); if ( status ) { fatal ("Failed to obtain solution."); goto TERMINATE; } for (j = 0; j < windowSize; j++) { if (x[j] > 0) { sjob_ptr = &job_array[j]; job_det_ptr = sjob_ptr->job_ptr->details; sjob_ptr->node_bitmap = (bitstr_t *) bit_alloc (node_record_count); job_det_ptr->req_node_bitmap = (bitstr_t *) bit_alloc (node_record_count); sjob_ptr->onnodes = (uint32_t *) xmalloc (sizeof(uint32_t) * node_record_count); job_det_ptr->req_node_layout = (uint16_t *) xmalloc (sizeof(uint16_t) * node_record_count); job_det_ptr->req_node_bitmap = (bitstr_t *) bit_alloc (node_record_count); for (i = 0; i < nodeSize; i++) { k = (1 + i) * windowSize + j; if (x[k] > 0) { bit_set (sjob_ptr->node_bitmap, (bitoff_t) (i)); bit_set (job_det_ptr->req_node_bitmap, (bitoff_t) (i)); node_array[i].rem_cpus -= x[k]; node_array[i].rem_gpus -= sjob_ptr->gpu; sjob_ptr->onnodes[i] = x[k]; job_det_ptr->req_node_layout[i] = sjob_ptr->onnodes[i]; sjob_ptr->alloc_total += x[k]; } } } else job_array[j].alloc_total = 0; } TERMINATE: free_and_null ((char **) &x); free_and_null ((char **) &slack); free_and_null ((char **) &dj); free_and_null ((char **) &pi); if (lp != NULL) { status = CPXfreeprob (env, &lp); if (status) { fatal("CPXfreeprob failed, error code %d.", status); } } if (env != NULL) { status = CPXcloseCPLEX (&env); if (status) { char errmsg[1024]; fatal("Could not close CPLEX environment."); CPXgeterrorstring (env, status, errmsg); fatal("%s", errmsg); } } return (status); }
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, 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; CPXENVptr env = NULL; CPXLPptr lp = NULL; int status = 0; int i, j; int cur_numrows, cur_numcols; /* Check the command line arguments */ if (( argc != 2 ) || ( argv[1][0] != '-' ) || ( strchr ("rcn", argv[1][1]) == NULL ) ) { usage (argv[0]); goto TERMINATE; } /* 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 CPXPARAM_ScreenOutput indicator is set to CPX_ON. */ if ( env == NULL ) { char errmsg[CPXMESSAGEBUFSIZE]; 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, CPXPARAM_ScreenOutput, CPX_ON); if ( status ) { fprintf (stderr, "Failure to turn on screen indicator, error %d.\n", status); goto TERMINATE; } /* Turn on data checking */ status = CPXsetintparam (env, CPXPARAM_Read_DataCheck, CPX_ON); if ( status ) { fprintf (stderr, "Failure to turn on data checking, error %d.\n", status); goto TERMINATE; } /* Create the problem. */ 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 CPXPARAM_ScreenOutput 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. */ switch (argv[1][1]) { case 'r': status = populatebyrow (env, lp); break; case 'c': status = populatebycolumn (env, lp); break; case 'n': status = populatebynonzero (env, lp); break; } if ( status ) { fprintf (stderr, "Failed to populate problem.\n"); goto TERMINATE; } /* Optimize the problem and obtain solution. */ status = CPXlpopt (env, lp); if ( status ) { fprintf (stderr, "Failed to optimize LP.\n"); goto TERMINATE; } /* 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; } /* Write the output to the screen. */ printf ("\nSolution status = %d\n", solstat); printf ("Solution value = %f\n\n", objval); 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 = CPXwriteprob (env, lp, "lpex1.lp", NULL); if ( status ) { fprintf (stderr, "Failed to write LP to disk.\n"); goto TERMINATE; } 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); /* 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 CPXPARAM_ScreenOutput indicator is set to CPX_ON. */ if ( status ) { char errmsg[CPXMESSAGEBUFSIZE]; fprintf (stderr, "Could not close CPLEX environment.\n"); CPXgeterrorstring (env, status, errmsg); fprintf (stderr, "%s", errmsg); } } return (status); } /* END main */
int main (int argc, char **argv) { IloEnv env; try { IloModel model(env); if (( argc != 2 ) || ( argv[1][0] != '-' ) || ( strchr ("rcn", argv[1][1]) == NULL ) ) { usage (argv[0]); throw(-1); } IloIntVarArray var(env); IloRangeArray con(env); switch (argv[1][1]) { case 'r': populatebyrow (model, var, con); break; case 'c': populatebycolumn (model, var, con); break; case 'n': populatebynonzero (model, var, con); break; } IloCplex cplex(model); // Optimize the problem and obtain solution. if ( !cplex.solve() ) { env.error() << "Failed to optimize LP" << endl; throw(-1); } IloNumArray vals(env); env.out() << "Solution status = " << cplex.getStatus() << endl; env.out() << "Solution value = " << cplex.getObjValue() << endl; cplex.getValues(vals, var); env.out() << "Values = " << vals << endl; cplex.getSlacks(vals, con); env.out() << "Slacks = " << vals << endl; //cplex.getDuals(vals, con); env.out() << "Duals = " << vals << endl; //cplex.getReducedCosts(vals, var); env.out() << "Reduced Costs = " << vals << endl; cplex.exportModel("lpex1.lp"); } catch (IloException& e) { cerr << "Concert exception caught: " << e << endl; } catch (...) { cerr << "Unknown exception caught" << endl; } env.end(); return 0; } // END main