#include #include #include "cplexcall.h" #include using namespace std; #ifdef DEBUG_CPLEX #define DEBUG_CPLEX_PRINT(a) std::cout<, double>::iterator it = m_cliques.begin() ; it != m_cliques.end() ; it ++ ) { for ( int j = 0 ; j < nbColonnes ; j++ ) { clique[j][numCliqueTraitee] = it->first[j]; } bc[numCliqueTraitee] = it->second; numCliqueTraitee++; } } } void Cplex::ajoutContrainte ( vector & coefs, double rhs ) { map, double>::iterator it = m_cliques.find ( coefs ); if ( it == m_cliques.end() ) { //Si la contraint n'existe pas déjà, on l'ajoute m_cliques[coefs] = rhs; } else { //Sinon, on la met à jour m_cliques[coefs] = min ( m_cliques[coefs], rhs ); } } void Cplex::ajoute_contraintes ( constraint_t type1, constraint_t type2 ) { for ( constraint_t type = type1; type <= type2; type = ( constraint_t ) ( type + 1 ) ) { if ( type != C18 ) { // contraintes 14 à 21, sauf la 18 // pour chaque contrainte du type courant for ( size_t m = 0 ; m < CInstance::M ( type ) ; m++ ) { const vector & centralesDuSubset = CInstance::constraint ( type, m )->C(); size_t nbCentralesDuSubset = centralesDuSubset.size(); // une clique possible par semaine for ( week_t h = ( week_t ) 0; h < H(); ++h ) { typedef map facteur_t ; facteur_t colonnes_concernees ; // pour chaque colonne de la contrainte for ( size_t idx_i = 0 ; idx_i < nbCentralesDuSubset ; idx_i++ ) { powerplants2_t i = centralesDuSubset[idx_i]; for ( size_t idx_c = 0 ; idx_c < Clique::annuaire[i].size() ; idx_c++ ) { int c = Clique::annuaire[i][idx_c]; // ajout de la colonne dans la clique, si elle est concernée double val = CInstance::constraint ( type, m )->concerne ( h, Clique::s_plans[c] ); if ( val != 0 ) { colonnes_concernees[c] = val ; } } } double somme = 0 ; for ( facteur_t::iterator it = colonnes_concernees.begin(); it != colonnes_concernees.end(); it++ ) somme += it->second ; double borne = CInstance::constraint ( type, m )->borne ( h ) ; if ( somme <= borne ) continue ; vector contrainte ( nbColonnes, 0 ); for ( facteur_t::iterator it = colonnes_concernees.begin(); it != colonnes_concernees.end(); it++ ) contrainte[it->first] = it->second ; ajoutContrainte ( contrainte, borne ); } } } else { // contraintes 18 for ( size_t m = 0 ; m < CInstance::M ( type ) ; m++ ) { const vector & centralesDuSubset = CInstance::constraint ( type, m )->C(); size_t nbCentralesDuSubset = centralesDuSubset.size(); // une clique possible par colonne concernée et par semaine de début d'outage // pour chaque colonne de la contrainte for ( size_t idx_i = 0 ; idx_i < nbCentralesDuSubset ; idx_i++ ) { powerplants2_t i = centralesDuSubset[idx_i]; for ( size_t idx_c = 0 ; idx_c < Clique::annuaire[i].size() ; idx_c++ ) { int c = Clique::annuaire[i][idx_c]; // pour chaque début d'outage possible for ( campaign_t k = ( campaign_t ) 0; ( unsigned ) k < Clique::s_plans[c]->m_ha.size(); ++k ) { week_t ha1 = Clique::s_plans[c]->ha ( k ); typedef map facteur_t ; facteur_t colonnes_concernees ; for ( size_t idx_i2 = 0 ; idx_i2 < nbCentralesDuSubset ; idx_i2++ ) { if ( idx_i2 == idx_i ) continue ; powerplants2_t i2 = centralesDuSubset[idx_i2]; for ( size_t idx_c2 = 0 ; idx_c2 < Clique::annuaire[i2].size() ; idx_c2++ ) { // ajout de la colonne dans la clique, si elle est concernée int c2 = Clique::annuaire[i2][idx_c2]; double val = CInstance::constraint ( type, m )->concerne ( ha1, Clique::s_plans[c2] ); if ( val != 0 ) { colonnes_concernees[c2] = val ; } } } // ajout d'une contrainte <= somme, avec contrainte[c1]=somme et somme sur c2 des contraintes[c2]=somme double somme ; if ( ( somme = colonnes_concernees.size() ) == 0 ) continue ; vector contrainte ( nbColonnes, 0 ); for ( facteur_t::iterator it = colonnes_concernees.begin(); it != colonnes_concernees.end(); it++ ) contrainte[it->first] = 1 ; contrainte[c] = somme ; ajoutContrainte ( contrainte, somme ); } } } } } } } /* This simple routine frees up the pointer *ptr, and sets *ptr to NULL */ void Cplex::free_and_null ( char **ptr ) { if ( *ptr != NULL ) { free ( *ptr ); *ptr = NULL; } } /* END free_and_null */ int Cplex::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: */ for ( i = 0;i < NUMNucl;i++ ) zobj[i] = cl[i]; for ( i = NUMNucl;i < NUMCOLS;i++ ) zobj[i] = ct[i-NUMNucl]; // zobj[0] = 1.0; zobj[1] = 2.0; zobj[2] = 3.0; for ( i = 0;i < NUMROWS;i++ ) { if ( i < NUMTps ) { zsense[i] = 'E'; //mettre 'G' si contrainte >= zrhs[i] = dt[i]; } else if ( i < ( NUMTps + NUMAff ) ) { zsense[i] = 'E'; zrhs[i] = 1.0; } else { zsense[i] = 'L'; zrhs[i] = bc[i- ( NUMTps+NUMAff ) ]; } } for ( i = 0;i < NUMCOLS;i++ ) { if ( i == 0 ) { zmatbeg[i] = 0; // printf("1)zmatbeg[%d]=%d\n",i,zmatbeg[i]); } else { zmatbeg[i] = zmatbeg[i-1] + zmatcnt[i-1]; // printf("2)zmatcnt[%d]=%d et zmatbeg[%d]=%d\n",i-1,zmatcnt[i-1],i,zmatbeg[i]); } if ( i < NUMNucl ) { zmatcnt[i] = NUMROWS; } else { zmatcnt[i] = NUMROWS; } for ( j = 0;j < NUMROWS;j++ ) { zmatind[zmatbeg[i] + j] = j; // printf("j=%d, zmatbeg[%d]=%d et zmatind[%d]=%d\n",j,i,zmatbeg[i],zmatbeg[i]+j,zmatind[zmatbeg[i]+j]); if ( i < NUMNucl && j < NUMTps ) { zmatval[zmatbeg[i] + j] = dlt[i][j]; // printf("0)i=%d j=%d dlt=%lf zmatbeg=%d zmatval=%lf\n", i, j, dlt[i][j], zmatbeg[i], zmatval[zmatbeg[i]+j]); } else if ( i < NUMNucl && j >= NUMTps ) { if ( j < NUMTps + NUMAff ) { if ( affect[i][j-NUMTps] == 1 ) zmatval[zmatbeg[i] + j] = 1.0; else zmatval[zmatbeg[i] + j] = 0.0; } else { // printf("Pb: i=%d j=%d NUMTps=%d NUMAFF=%d \n", i, j, NUMTps, NUMAff); // modif antoine : suppression du test avec EPSILON. A tester. // if(clique[i][j-(NUMTps+NUMAff)]>EPSILON)//MODIF pour accepter la contrainte 21 // { zmatval[zmatbeg[i] + j] = clique[i][j- ( NUMTps+NUMAff ) ];// A revoir contrainte 21cl // } // else // { // zmatval[zmatbeg[i]+j] = 0.0; // } } // printf("1)i=%d j=%d zmatbeg=%d zmatval=%lf\n", i, j, zmatbeg[i], zmatval[zmatbeg[i]+j]); } else if ( i >= NUMNucl && j < NUMTps ) { if ( ( i - NUMNucl ) == j ) { zmatval[zmatbeg[i] + j] = 1.0; //zmatval[zmatbeg[i] + j] = dureeSemaine; // printf("2)i=%d j=%d zmatbeg=%d zmatval=%lf\n", i, j, zmatbeg[i], zmatval[zmatbeg[i]+j]); } else { zmatval[zmatbeg[i] + j] = 0.0; // printf("3)i=%d j=%d zmatbeg=%d zmatval=%lf\n", i, j, zmatbeg[i], zmatval[zmatbeg[i]+j]); } // printf("2)i=%d j=%d zmatbeg=%d zmatval=%lf\n", i, j, zmatbeg[i], zmatval[zmatbeg[i]+j]); } else { zmatval[zmatbeg[i] + j] = 0.0; // printf("3)i=%d j=%d zmatbeg=%d zmatval=%lf\n", i, j, zmatbeg[i], zmatval[zmatbeg[i]+j]); } } } /* zmatbeg[0] = 0; zmatbeg[1] = 2; zmatbeg[2] = 4; zmatcnt[0] = 2; zmatcnt[1] = 2; zmatcnt[2] = 2; zmatind[0] = 0; zmatind[2] = 0; zmatind[4] = 0; zsense[0] = 'L'; zmatval[0] = -1.0; zmatval[2] = 1.0; zmatval[4] = 1.0; zrhs[0] = 20.0; zmatind[1] = 1; zmatind[3] = 1; zmatind[5] = 1; zsense[1] = 'L'; zmatval[1] = 1.0; zmatval[3] = -3.0; zmatval[5] = 1.0; zrhs[1] = 30.0;*/ for ( i = 0;i < NUMNucl;i++ ) { zlb[i] = 0.0; zub[i] = 1.0; zctype[i] = 'B'; } for ( i = NUMNucl;i < NUMCOLS;i++ ) { zlb[i] = Mint[i-NUMNucl]; zub[i] = Maxt[i-NUMNucl]; zctype[i] = 'C'; } /* zlb[0] = 0.0; zlb[1] = 0.0; zlb[2] = 0.0; zub[0] = 40.0; zub[1] = CPX_INFBOUND; zub[2] = CPX_INFBOUND;*/ TERMINATE: if ( status ) { free_and_null ( ( char ** ) &zprobname ); free_and_null ( ( char ** ) &zobj ); free_and_null ( ( char ** ) &zrhs ); free_and_null ( ( char ** ) &zsense ); free_and_null ( ( char ** ) &zmatbeg ); free_and_null ( ( char ** ) &zmatcnt ); free_and_null ( ( char ** ) &zmatind ); free_and_null ( ( char ** ) &zmatval ); free_and_null ( ( char ** ) &zlb ); free_and_null ( ( char ** ) &zub ); free_and_null ( ( char ** ) &zctype ); } else { *numcols_p = NUMCOLS; *numrows_p = NUMROWS; #ifndef NO_CPLEX *objsen_p = CPX_MIN; /* The problem is minimization */ #endif *probname_p = zprobname; *obj_p = zobj; *rhs_p = zrhs; *sense_p = zsense; *matbeg_p = zmatbeg; *matcnt_p = zmatcnt; *matind_p = zmatind; *matval_p = zmatval; *lb_p = zlb; *ub_p = zub; *ctype_p = zctype; } return ( status ); } /* END setproblemdata */ vector Cplex::resolution() { /* 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. */ vector result; #ifndef NO_CPLEX 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 = nbColonnes; NUMTps = H(); NUMAff = I(); NUMCliq = nbClique; 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] ); } for ( int i = 0 ; i < cur_numcols ; i++ ) { result.push_back ( x[i] ); } 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 ); #endif // ifndef NO_CPLEX //Récupération des colonnes sélectionnées vector retour ; for( size_t c =0 ; c1-EPSILON ) retour.push_back(c); } return retour; } Cplex::~Cplex() { #ifndef TEST_CC14 #ifndef TEST_CC19 #ifndef TEST_CC20 #ifndef TEST_CC21 DEBUG_CPLEX_PRINT ( "deletage de cl" ); delete[] cl; DEBUG_CPLEX_PRINT ( "deletage de ct" ); delete[] ct; DEBUG_CPLEX_PRINT ( "deletage du contenu de dlt, affect et clique" ); for ( int i = 0 ; i < nbColonnes ; i++ ) { delete[] dlt[i]; delete[] affect[i]; delete[] clique[i]; } DEBUG_CPLEX_PRINT ( "deletage de dlt" ); delete[] dlt; DEBUG_CPLEX_PRINT ( "deletage de affect" ); delete[] affect; DEBUG_CPLEX_PRINT ( "deletage de clique" ); delete[] clique; DEBUG_CPLEX_PRINT ( "deletage de dt" ); delete[] dt; DEBUG_CPLEX_PRINT ( "deletage de Mint" ); delete[] Mint; DEBUG_CPLEX_PRINT ( "deletage de Maxt" ); delete[] Maxt; DEBUG_CPLEX_PRINT ( "deletage de bc" ); delete[] bc; #endif #endif #endif #endif }