coin-Cgl
|
00001 // Copyright (C) 2002, International Business Machines 00002 // Corporation and others. All Rights Reserved. 00003 #ifndef CglTwomir_H 00004 #define CglTwomir_H 00005 #include <string> 00006 00007 #include "CglCutGenerator.hpp" 00008 #include "CoinFactorization.hpp" 00009 00010 typedef struct 00011 { 00012 00013 int nz; /* current length of arrays index[] and coeff[] */ 00014 int max_nz; /* max length of arrays index[] and coeff[] */ 00015 double *coeff; /* coefficient of each variable in the constraint */ 00016 int *index; /* index of the variable (value in 0 ... nrow+ncol) */ 00017 double rhs; /* rhs of the constraint */ 00018 char sense; /* ?? is it necessary */ 00019 00020 } DGG_constraint_t; 00021 00022 typedef struct{ 00023 int n; 00024 DGG_constraint_t **c; 00025 int *ctype; 00026 double *alpha; 00027 } DGG_list_t; 00028 00029 /******************** BASIS INFORMATION ADTs **********************************/ 00030 typedef struct{ 00031 int q_min; 00032 int q_max; 00033 int t_min; 00034 int t_max; 00035 int a_max; 00036 int max_elements; 00037 } cutParams; 00038 00039 typedef struct 00040 { 00041 double gomory_threshold; /* factional variable must be this away from int */ 00042 int ncol, /* number of columns in LP */ 00043 nrow, /* number of constaints in LP */ 00044 ninteger; /* number of integer variables in LP */ 00045 00046 int nbasic_col, /* number of basic columns in the LP */ 00047 nbasic_row; /* number of basic rows in the LP */ 00048 00049 /* the following arrays are all of size (ncol+nrow) */ 00050 int *info; /* description of each variable (see below) */ 00051 double *lb; /* specifies the lower bound (if any) of each variable */ 00052 double *ub; /* specifies the upper bound (if any) of each variable */ 00053 double *x; /* current solution */ 00054 double *rc; /* current reduced cost */ 00055 double *opt_x; 00056 00057 cutParams cparams; 00058 } DGG_data_t; 00059 00060 /* the following macros allow us to decode the info of the DGG_data 00061 type. The encoding is as follows, 00062 bit 1 : if the variable is basic or not (non-basic). 00063 bit 2 : if the variable is integer or or not (rational). 00064 bit 3 : if the variable is structural or not (artifical). 00065 bit 4 : if the variable is non-basic and at its upper bound 00066 (else if non-basic at lower bound). */ 00067 00068 #define DGG_isBasic(data,idx) ((data->info[idx])&1) 00069 #define DGG_isInteger(data,idx) ((data->info[idx] >> 1)&1) 00070 #define DGG_isStructural(data,idx) ((data->info[idx] >> 2)&1) 00071 #define DGG_isEqualityConstraint(data,idx) ((data->info[idx] >> 3)&1) 00072 #define DGG_isNonBasicAtUB(data,idx) ((data->info[idx] >> 4)&1) 00073 #define DGG_isNonBasicAtLB(data,idx) ((data->info[idx] >> 5)&1) 00074 #define DGG_isConstraintBoundedAbove(data,idx) ((data->info[idx] >> 6)&1) 00075 #define DGG_isConstraintBoundedBelow(data,idx) ((data->info[idx] >> 7)&1) 00076 00077 #define DGG_setIsBasic(data,idx) ((data->info[idx]) |= 1) 00078 #define DGG_setIsInteger(data,idx) ((data->info[idx]) |= (1<<1)) 00079 #define DGG_setIsStructural(data,idx) ((data->info[idx]) |= (1<<2)) 00080 #define DGG_setEqualityConstraint(data,idx) ((data->info[idx]) |= (1<<3)) 00081 #define DGG_setIsNonBasicAtUB(data,idx) ((data->info[idx]) |= (1<<4)) 00082 #define DGG_setIsNonBasicAtLB(data,idx) ((data->info[idx]) |= (1<<5)) 00083 #define DGG_setIsConstraintBoundedAbove(data,idx) ((data->info[idx]) |= (1<<6)) 00084 #define DGG_setIsConstraintBoundedBelow(data,idx) ((data->info[idx]) |= (1<<7)) 00085 00086 class CoinWarmStartBasis; 00088 class CglTwomir : public CglCutGenerator { 00089 00090 friend void CglTwomirUnitTest(const OsiSolverInterface * siP, 00091 const std::string mpdDir ); 00092 00093 00094 public: 00095 00097 mutable std::string probname_; 00098 00104 virtual void generateCuts( const OsiSolverInterface & si, OsiCuts & cs, 00105 const CglTreeInfo info = CglTreeInfo()) const; 00107 virtual bool needsOptimalBasis() const; 00108 00111 00112 void setMirScale (int tmin, int tmax) {t_min_ = tmin; t_max_ = tmax;} 00113 void setTwomirScale (int qmin, int qmax) {q_min_ = qmin; q_max_ = qmax;} 00114 void setAMax (int a) {a_max_ = a;} 00115 void setMaxElements (int n) {max_elements_ = n;} 00116 void setMaxElementsRoot (int n) {max_elements_root_ = n;} 00117 void setCutTypes (bool mir, bool twomir, bool tab, bool form) 00118 { do_mir_ = mir; do_2mir_ = twomir; do_tab_ = tab; do_form_ = form;} 00119 void setFormulationRows (int n) {form_nrows_ = n;} 00120 00122 int getTmin() const {return t_min_;} 00123 int getTmax() const {return t_max_;} 00124 int getQmin() const {return q_min_;} 00125 int getQmax() const {return q_max_;} 00126 int getAmax() const {return a_max_;} 00127 int getMaxElements() const {return max_elements_;} 00128 int getMaxElementsRoot() const {return max_elements_root_;} 00129 int getIfMir() const { return do_mir_;} 00130 int getIfTwomir() const { return do_2mir_;} 00131 int getIfTableau() const { return do_tab_;} 00132 int getIfFormulation() const { return do_form_;} 00134 00139 00140 void setAway(double value); 00142 double getAway() const; 00144 void setAwayAtRoot(double value); 00146 double getAwayAtRoot() const; 00148 virtual int maximumLengthOfCutInTree() const 00149 { return max_elements_;} 00151 00154 00155 CglTwomir (); 00156 00158 CglTwomir (const CglTwomir &); 00159 00161 virtual CglCutGenerator * clone() const; 00162 00164 CglTwomir & operator=(const CglTwomir& rhs); 00165 00167 virtual ~CglTwomir (); 00169 virtual std::string generateCpp( FILE * fp); 00171 00172 private: 00173 // Private member data 00176 00177 mutable CoinThreadRandom randomNumberGenerator_; 00179 double away_; 00181 double awayAtRoot_; 00182 bool do_mir_; 00183 bool do_2mir_; 00184 bool do_tab_; 00185 bool do_form_; 00186 00187 int t_min_; 00188 int t_max_; 00189 int q_min_; 00190 int q_max_; 00191 int a_max_; 00192 int max_elements_; 00193 int max_elements_root_; 00194 int form_nrows_; //number of rows on which formulation cuts will be generated 00196 }; 00197 00198 //############################################################################# 00199 00200 /* 00201 #include <stdlib.h> 00202 #include <stdio.h> 00203 #include <stdarg.h> 00204 #include <math.h> 00205 #include <float.h> 00206 #include <cassert> 00207 #include <iostream.h> 00208 */ 00209 00210 /******************** DEBUG DEFINITIONS ***************************************/ 00211 00212 #define DGG_DEBUG_DGG 1 00213 #define DGG_TRACE_ERRORS 0 00214 #define DGG_DISPLAY 0 00215 #define DGG_AUTO_CHECK_CUT_OFF_OPTIMAL 1 00216 00217 /******************** CONFIGURATION DEFAULTS **********************************/ 00218 00219 #define DGG_DEFAULT_METHOD 2 00220 #define DGG_DEFAULT_TMIN 1 00221 #define DGG_DEFAULT_TMAX 1 00222 #define DGG_DEFAULT_TAUMIN 2 00223 #define DGG_DEFAULT_TAUMAX 6 00224 #define DGG_DEFAULT_MAX_CUTS 500 00225 #define DGG_DEFAULT_IMPROVEMENT_THRESH 0.001 00226 #define DGG_DEFAULT_NBELOW_THRESH INT_MAX 00227 #define DGG_DEFAULT_NROOT_ROUNDS 2 00228 #define DGG_DEFAULT_NEGATIVE_SCALED_TWOSTEPS 0 00229 #define DGG_DEFAULT_ALPHA_RULE 0 00230 #define DGG_DEFAULT_CUT_INC 250 00231 #define DGG_DEFAULT_CUT_FORM 0 00232 #define DGG_DEFAULT_NICEFY 0 00233 #define DGG_DEFAULT_ONLY_DELAYED 0 00234 #define DGG_DEFAULT_DELAYED_FREQ 9999999 00235 #define DGG_DEFAULT_LPROWS_FREQ 9999999 00236 #define DGG_DEFAULT_WHICH_FORMULATION_CUTS 2 00237 00238 /******************** SOLVER CONFIGURATION DEFINITIONS ************************/ 00239 00240 #define DGG_OSI 0 00241 #define DGG_CPX 1 00242 #define DGG_QSO 2 00243 00244 /* determines the solver to be used */ 00245 #define DGG_SOLVER DGG_OSI 00246 00247 /* adds checking routines to make sure solver works as expected */ 00248 #define DGG_DEBUG_SOLVER 0 00249 00250 /* turn off screen output from solver */ 00251 #define DGG_SOLVER_SCREEN_FLAG 0 00252 00253 /******************** CUT DEFINITIONS *****************************************/ 00254 00255 /* internal names for cut types */ 00256 #define DGG_TMIR_CUT 1 00257 #define DGG_2STEP_CUT 2 00258 00259 /* internal names for alpha-selection rules */ 00260 #define DGG_ALPHA_MIN_SUM 0 00261 #define DGG_ALPHA_RANDOM_01 1 00262 #define DGG_ALPHA_RANDOM_COEFF 2 00263 #define DGG_ALPHA_ALL 3 00264 #define DGG_ALPHA_MAX_STEEP 5 00265 00266 /******************** PRECISION & NUMERICAL ISSUES DEFINITIONS ****************/ 00267 00268 /* how steep a cut must be before adding it to the lp */ 00269 #define DGG_MIN_STEEPNESS 1.0e-4 00270 #define DGG_MAX_L2NORM 1.0e7 00271 00272 /* 0 = min steepness, 1 = max norm */ 00273 #define DGG_NORM_CRITERIA 1 00274 00275 /* internal representations of +infinity and -infinity */ 00276 #define UB_MAX DBL_MAX 00277 #define LB_MIN DBL_MIN 00278 00279 /* used to define how fractional a basic-integer variable must be 00280 before choosing to use it to generate a TMIR cut on. 00281 OSI's default is 1.0e-7 */ 00282 #define DGG_GOMORY_THRESH 0.005 00283 00284 #define DGG_RHS_THRESH 0.005 00285 00286 /* used for comparing variables to their upper bounds. 00287 OSI's default is 1.0e-7. 00288 We set it to 1.0e6 because e-7 seems too sensitive. 00289 In fact, with e-7 the problem dsbmip.mps complains. */ 00290 #define DGG_BOUND_THRESH 1.0e-6 00291 00292 /* used for comparing the lhs (activity) value of a tableau row 00293 with the rhs. This is only used for debugging purposes. */ 00294 #define DGG_EQUALITY_THRESH 1.0e-5 00295 00296 /* used for comparing a variable's lower bound to 0.0 00297 and determining if we need to shift the variable */ 00298 #define DGG_SHIFT_THRESH 1.0e-6 00299 00300 /* used for determing how far from an integer is still an integer. 00301 This value is used for comparing coefficients to integers. 00302 OSI's default is 1.0e-10. */ 00303 #define DGG_INTEGRALITY_THRESH 1.0e-10 00304 00305 /* the min value that a coeff can have in the tableau row 00306 before being set to zero. */ 00307 #define CBC_CHECK_CUT 00308 #ifndef CBC_CHECK_CUT 00309 #define DGG_MIN_TABLEAU_COEFFICIENT 1.0e-8 00310 #else 00311 #define DGG_MIN_TABLEAU_COEFFICIENT 1.0e-12 00312 #endif 00313 00314 /* smallest value rho is allowed to have for a simple 2-step MIR 00315 (ie: not an extended two-step MIR) */ 00316 #define DGG_MIN_RHO 1.0e-7 00317 #define DGG_MIN_ALPHA 1.0e-7 00318 00319 /* when a slack is null: used to check if a cut is satisfied or not. */ 00320 #define DGG_NULL_SLACK 1.0e-5 00321 00322 /* nicefy constants */ 00323 #define DGG_NICEFY_MIN_ABSVALUE 1.0e-13 00324 #define DGG_NICEFY_MIN_FIX 1.0e-7 00325 #define DGG_NICEFY_MAX_PADDING 1.0e-6 00326 #define DGG_NICEFY_MAX_RATIO 1.0e9 00327 00328 00329 /******************** ERROR-CATCHING MACROS ***********************************/ 00330 #if DGG_TRACE_ERRORS > 0 00331 00332 #define __DGG_PRINT_LOC__(F) fprintf(((F==0)?stdout:F), " in %s (%s:%d)\n", __func__, __FILE__, __LINE__) 00333 00334 #define DGG_THROW(A,REST...) {\ 00335 fprintf(stdout, ##REST); \ 00336 __DGG_PRINT_LOC__(stdout); \ 00337 return (A);} 00338 00339 #define DGG_IF_EXIT(A,B,REST...) {\ 00340 if(A) {\ 00341 fprintf(stdout, ##REST); \ 00342 __DGG_PRINT_LOC__(stdout); \ 00343 exit(B);}} 00344 00345 #define DGG_CHECKRVAL(A,B) {\ 00346 if(A) {\ 00347 __DGG_PRINT_LOC__(stdout); \ 00348 return B; } } 00349 00350 #define DGG_CHECKRVAL1(A,B) {\ 00351 if(A) {\ 00352 __DGG_PRINT_LOC__(stdout); \ 00353 rval = B; goto CLEANUP; } } 00354 00355 #define DGG_WARNING(A, REST...) {\ 00356 if(A) {\ 00357 fprintf(stdout, ##REST); \ 00358 __DGG_PRINT_LOC__(stdout); \ 00359 }} 00360 00361 #define DGG_TEST(A,B,REST...) {\ 00362 if(A) DGG_THROW(B,##REST) } 00363 00364 #define DGG_TEST2(A,B,C,REST) {DGG_TEST(A,B,C,REST) } 00365 #define DGG_TEST3(A,B,C,D,REST) {DGG_TEST(A,B,C,D,REST) } 00366 00367 #else 00368 00369 #define DGG_IF_EXIT(A,B,REST) {if(A) {fprintf(stdout, REST);exit(B);}} 00370 00371 #define DGG_THROW(A,B) return(A) 00372 00373 #define DGG_CHECKRVAL(A,B) { if(A) return(B); } 00374 #define DGG_CHECKRVAL1(A,B){ if(A) { rval = B; goto CLEANUP; } } 00375 00376 #define DGG_TEST(A,B,REST) { if(A) return(B);} 00377 #define DGG_TEST2(A,B,REST,C) { DGG_TEST(A,B,REST) } 00378 #define DGG_TEST3(A,B,REST,C,D) { DGG_TEST(A,B,REST) } 00379 00380 #endif 00381 00382 /******************** SIMPLE MACROS AND FUNCTIONS *****************************/ 00383 00384 #define DGG_MIN(a,b) ( (a<b)?a:b ) 00385 #define DGG_MAX(a,b) ( (a>b)?a:b ) 00386 #define KREM(vht,alpha,tau) (DGG_MIN( ceil(vht / alpha), tau ) - 1) 00387 #define LMIN(vht, d, bht) (DGG_MIN( floor(d*bht/bht), d)) 00388 #define ABOV(v) (v - floor(v)) 00389 #define QINT(vht,bht,tau) ( (int)floor( (vht*(tau-1))/bht ) ) 00390 #define V2I(bht,tau,i) ( ((i+1)*bht / tau) ) 00391 00392 int DGG_is_even(double vht, double bht, int tau, int q); 00393 double frac_part(double value); 00394 int DGG_is_a_multiple_of_b(double a, double b); 00395 00396 00397 /* free function for DGG_data_t. Frees internal arrays and data structure */ 00398 int DGG_freeData( DGG_data_t *data ); 00399 00400 /******************** CONSTRAINT ADTs *****************************************/ 00401 DGG_constraint_t* DGG_newConstraint(int max_arrays); 00402 void DGG_freeConstraint(DGG_constraint_t *c); 00403 DGG_constraint_t *DGG_copyConstraint(DGG_constraint_t *c); 00404 void DGG_scaleConstraint(DGG_constraint_t *c, int t); 00405 00406 /******************** CONFIGURATION *******************************************/ 00407 void DGG_list_init (DGG_list_t *l); 00408 int DGG_list_addcut (DGG_list_t *l, DGG_constraint_t *cut, int ctype, double alpha); 00409 void DGG_list_delcut (DGG_list_t *l, int i); 00410 void DGG_list_free(DGG_list_t *l); 00411 00412 /******************* SOLVER SPECIFIC METHODS **********************************/ 00413 DGG_data_t *DGG_getData(const void *solver_ptr); 00414 00415 /******************* CONSTRAINT MANIPULATION **********************************/ 00416 00417 /* DGG_transformConstraint: manipulates a constraint in the following way: 00418 00419 packs everything in output 00420 00421 1 - variables at their upper bounds are substituted for their 00422 complements. This is done by adjusting the coefficients and 00423 the right hand side (simple substitution). 00424 00425 2 - variables with non-zero lower bounds are shifted. */ 00426 00427 int DGG_transformConstraint( DGG_data_t *data, 00428 double **x_out, 00429 double **rc_out, 00430 char **isint_out, 00431 DGG_constraint_t *constraint ); 00432 00433 /* DGG_unTransformConstraint : 00434 00435 1 - Undoes step (1) of DGG_transformConstraint 00436 2 - Undoes step (2) of DGG_transformConstraint */ 00437 00438 int DGG_unTransformConstraint( DGG_data_t *data, 00439 DGG_constraint_t *constraint ); 00440 00441 /* substitutes each slack variable by the structural variables which 00442 define it. This function, hence, changes the constraint 'cut'. */ 00443 00444 int DGG_substituteSlacks( const void *solver_ptr, 00445 DGG_data_t *data, 00446 DGG_constraint_t *cut ); 00447 00448 int DGG_nicefyConstraint( const void *solver_ptr, 00449 DGG_data_t *data, 00450 DGG_constraint_t *cut); 00451 00452 /******************* CUT GENERATION *******************************************/ 00453 int DGG_getFormulaConstraint( int row_idx, 00454 const void *solver_ptr, 00455 DGG_data_t *data, 00456 DGG_constraint_t* row ); 00457 00458 int DGG_getTableauConstraint( int index, 00459 const void *solver_ptr, 00460 DGG_data_t *data, 00461 DGG_constraint_t* tabrow, 00462 const int * colIsBasic, 00463 const int * rowIsBasic, 00464 CoinFactorization & factorization, 00465 int mode ); 00466 00467 DGG_constraint_t* DGG_getSlackExpression(const void *solver_ptr, DGG_data_t* data, int row_index); 00468 00469 int DGG_generateTabRowCuts( DGG_list_t *list, 00470 DGG_data_t *data, 00471 const void *solver_ptr ); 00472 00473 int DGG_generateFormulationCuts( DGG_list_t *list, 00474 DGG_data_t *data, 00475 const void *solver_ptr, 00476 int nrows, 00477 CoinThreadRandom & generator); 00478 00479 00480 int DGG_generateFormulationCutsFromBase( DGG_constraint_t *base, 00481 double slack, 00482 DGG_list_t *list, 00483 DGG_data_t *data, 00484 const void *solver_ptr, 00485 CoinThreadRandom & generator); 00486 00487 int DGG_generateCutsFromBase( DGG_constraint_t *base, 00488 DGG_list_t *list, 00489 DGG_data_t *data, 00490 const void *solver_ptr ); 00491 00492 int DGG_buildMir( char *isint, 00493 DGG_constraint_t *base, 00494 DGG_constraint_t **cut_out ); 00495 00496 int DGG_build2step( double alpha, 00497 char *isint, 00498 DGG_constraint_t *base, 00499 DGG_constraint_t **cut_out ); 00500 00501 int DGG_addMirToList ( DGG_constraint_t *base, 00502 char *isint, 00503 double *x, 00504 DGG_list_t *list, 00505 DGG_data_t *data, 00506 DGG_constraint_t *orig_base ); 00507 00508 int DGG_add2stepToList ( DGG_constraint_t *base, 00509 char *isint, 00510 double *x, 00511 double *rc, 00512 DGG_list_t *list, 00513 DGG_data_t *data, 00514 DGG_constraint_t *orig_base ); 00515 00516 /******************* CUT INFORMATION ******************************************/ 00517 00518 double DGG_cutLHS(DGG_constraint_t *c, double *x); 00519 int DGG_isCutDesirable(DGG_constraint_t *c, DGG_data_t *d); 00520 00521 /******************* TEST / DEBUGGING ROUTINES ********************************/ 00522 00523 int DGG_isConstraintViolated(DGG_data_t *d, DGG_constraint_t *c); 00524 00525 int DGG_isBaseTrivial(DGG_data_t *d, DGG_constraint_t* c); 00526 int DGG_is2stepValid(double alpha, double bht); 00527 00528 int DGG_cutsOffPoint(double *x, DGG_constraint_t *cut); 00529 00530 //############################################################################# 00536 void CglTwomirUnitTest(const OsiSolverInterface * siP, 00537 const std::string mpdDir); 00538 00539 00540 #endif 00541 00542