- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# /home/coin/SVN-release/Bcps-0.91.0/Cgl/src/CglTwomir/CglTwomir.hpp

Go to the documentation of this file.

    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
    00151
    00152   CglTwomir ();
    00153
    00155   CglTwomir (const CglTwomir &);
    00156
    00158   virtual CglCutGenerator * clone() const;
    00159
    00161   CglTwomir & operator=(const CglTwomir& rhs);
    00162
    00164   virtual  ~CglTwomir ();
    00166   virtual std::string generateCpp( FILE * fp);
    00168
    00169 private:
    00170   // Private member data
    00173
    00174   double away_;
    00176   double awayAtRoot_;
    00177   bool do_mir_;
    00178   bool do_2mir_;
    00179   bool do_tab_;
    00180   bool do_form_;
    00181
    00182   int t_min_;
    00183   int t_max_;
    00184   int q_min_;
    00185   int q_max_;
    00186   int a_max_;
    00187   int max_elements_;
    00188   int max_elements_root_;
    00189   int form_nrows_; //number of rows on which formulation cuts will be generated
    00191 };
    00192
    00193 //#############################################################################
    00194
    00195 /*
    00196 #include <stdlib.h>
    00197 #include <stdio.h>
    00198 #include <stdarg.h>
    00199 #include <math.h>
    00200 #include <float.h>
    00201 #include <cassert>
    00202 #include <iostream.h>
    00203 */
    00204
    00205 /******************** DEBUG DEFINITIONS ***************************************/
    00206
    00207 #define DGG_DEBUG_DGG 1
    00208 #define DGG_TRACE_ERRORS 0
    00209 #define DGG_DISPLAY   0
    00210 #define DGG_AUTO_CHECK_CUT_OFF_OPTIMAL 1
    00211
    00212 /******************** CONFIGURATION DEFAULTS **********************************/
    00213
    00214 #define DGG_DEFAULT_METHOD 2
    00215 #define DGG_DEFAULT_TMIN 1
    00216 #define DGG_DEFAULT_TMAX 1
    00217 #define DGG_DEFAULT_TAUMIN 2
    00218 #define DGG_DEFAULT_TAUMAX 6
    00219 #define DGG_DEFAULT_MAX_CUTS 500
    00220 #define DGG_DEFAULT_IMPROVEMENT_THRESH 0.001
    00221 #define DGG_DEFAULT_NBELOW_THRESH INT_MAX
    00222 #define DGG_DEFAULT_NROOT_ROUNDS 2
    00223 #define DGG_DEFAULT_NEGATIVE_SCALED_TWOSTEPS 0
    00224 #define DGG_DEFAULT_ALPHA_RULE 0
    00225 #define DGG_DEFAULT_CUT_INC 250
    00226 #define DGG_DEFAULT_CUT_FORM 0
    00227 #define DGG_DEFAULT_NICEFY 0
    00228 #define DGG_DEFAULT_ONLY_DELAYED 0
    00229 #define DGG_DEFAULT_DELAYED_FREQ 9999999
    00230 #define DGG_DEFAULT_LPROWS_FREQ 9999999
    00231 #define DGG_DEFAULT_WHICH_FORMULATION_CUTS 2
    00232
    00233 /******************** SOLVER CONFIGURATION DEFINITIONS ************************/
    00234
    00235 #define DGG_OSI 0
    00236 #define DGG_CPX 1
    00237 #define DGG_QSO 2
    00238
    00239 /* determines the solver to be used */
    00240 #define DGG_SOLVER DGG_OSI
    00241
    00242 /* adds checking routines to make sure solver works as expected */
    00243 #define DGG_DEBUG_SOLVER 0
    00244
    00245 /* turn off screen output from solver */
    00246 #define DGG_SOLVER_SCREEN_FLAG 0
    00247
    00248 /******************** CUT DEFINITIONS *****************************************/
    00249
    00250 /* internal names for cut types */
    00251 #define DGG_TMIR_CUT 1
    00252 #define DGG_2STEP_CUT 2
    00253
    00254 /* internal names for alpha-selection rules */
    00255 #define DGG_ALPHA_MIN_SUM 0
    00256 #define DGG_ALPHA_RANDOM_01 1
    00257 #define DGG_ALPHA_RANDOM_COEFF 2
    00258 #define DGG_ALPHA_ALL 3
    00259 #define DGG_ALPHA_MAX_STEEP 5
    00260
    00261 /******************** PRECISION & NUMERICAL ISSUES DEFINITIONS ****************/
    00262
    00263 /* how steep a cut must be before adding it to the lp */
    00264 #define DGG_MIN_STEEPNESS 1.0e-4
    00265 #define DGG_MAX_L2NORM 1.0e7
    00266
    00267 /* 0 = min steepness, 1 = max norm */
    00268 #define DGG_NORM_CRITERIA 1
    00269
    00270 /* internal representations of +infinity and -infinity */
    00271 #define UB_MAX DBL_MAX
    00272 #define LB_MIN DBL_MIN
    00273
    00274 /* used to define how fractional a basic-integer variable must be
    00275    before choosing to use it to generate a TMIR cut on.
    00276    OSI's default is 1.0e-7 */
    00277 #define DGG_GOMORY_THRESH 0.005
    00278
    00279 #define DGG_RHS_THRESH 0.005
    00280
    00281 /* used for comparing variables to their upper bounds.
    00282    OSI's default is 1.0e-7.
    00283    We set it to 1.0e6 because e-7 seems too sensitive.
    00284    In fact, with e-7 the problem dsbmip.mps complains. */
    00285 #define DGG_BOUND_THRESH 1.0e-6
    00286
    00287 /* used for comparing the lhs (activity) value of a tableau row
    00288    with the rhs. This is only used for debugging purposes. */
    00289 #define DGG_EQUALITY_THRESH 1.0e-5
    00290
    00291 /* used for comparing a variable's lower bound to 0.0
    00292    and determining if we need to shift the variable    */
    00293 #define DGG_SHIFT_THRESH 1.0e-6
    00294
    00295 /* used for determing how far from an integer is still an integer.
    00296    This value is used for comparing coefficients to integers.
    00297    OSI's default is 1.0e-10.                               */
    00298 #define DGG_INTEGRALITY_THRESH 1.0e-10
    00299
    00300 /* the min value that a coeff can have in the tableau row
    00301    before being set to zero. */
    00302 #define CBC_CHECK_CUT
    00303 #ifndef CBC_CHECK_CUT
    00304 #define DGG_MIN_TABLEAU_COEFFICIENT 1.0e-8
    00305 #else
    00306 #define DGG_MIN_TABLEAU_COEFFICIENT 1.0e-12
    00307 #endif
    00308
    00309 /* smallest value rho is allowed to have for a simple 2-step MIR
    00310    (ie: not an extended two-step MIR) */
    00311 #define DGG_MIN_RHO 1.0e-7
    00312 #define DGG_MIN_ALPHA 1.0e-7
    00313
    00314 /* when a slack is null: used to check if a cut is satisfied or not. */
    00315 #define DGG_NULL_SLACK 1.0e-5
    00316
    00317 /* nicefy constants */
    00318 #define DGG_NICEFY_MIN_ABSVALUE 1.0e-13
    00319 #define DGG_NICEFY_MIN_FIX 1.0e-7
    00320 #define DGG_NICEFY_MAX_PADDING 1.0e-6
    00321 #define DGG_NICEFY_MAX_RATIO 1.0e9
    00322
    00323
    00324 /******************** ERROR-CATCHING MACROS ***********************************/
    00325 #if DGG_TRACE_ERRORS > 0
    00326
    00327 #define __DGG_PRINT_LOC__(F) fprintf(((F==0)?stdout:F), " in %s (%s:%d)\n", __func__, __FILE__, __LINE__)
    00328
    00329 #define DGG_THROW(A,REST...) {\
    00330  fprintf(stdout, ##REST); \
    00331  __DGG_PRINT_LOC__(stdout); \
    00332  return (A);}
    00333
    00334 #define DGG_IF_EXIT(A,B,REST...) {\
    00335  if(A) {\
    00336  fprintf(stdout, ##REST); \
    00337  __DGG_PRINT_LOC__(stdout); \
    00338  exit(B);}}
    00339
    00340 #define DGG_CHECKRVAL(A,B) {\
    00341  if(A) {\
    00342    __DGG_PRINT_LOC__(stdout); \
    00343    return B; } }
    00344
    00345 #define DGG_CHECKRVAL1(A,B) {\
    00346  if(A) {\
    00347    __DGG_PRINT_LOC__(stdout); \
    00348    rval = B; goto CLEANUP; } }
    00349
    00350 #define DGG_WARNING(A, REST...) {\
    00351   if(A) {\
    00352           fprintf(stdout, ##REST); \
    00353                 __DGG_PRINT_LOC__(stdout); \
    00354                 }}
    00355
    00356 #define DGG_TEST(A,B,REST...) {\
    00357  if(A) DGG_THROW(B,##REST) }
    00358
    00359 #define DGG_TEST2(A,B,C,REST)   {DGG_TEST(A,B,C,REST) }
    00360 #define DGG_TEST3(A,B,C,D,REST) {DGG_TEST(A,B,C,D,REST) }
    00361
    00362 #else
    00363
    00364 #define DGG_IF_EXIT(A,B,REST) {if(A) {fprintf(stdout, REST);exit(B);}}
    00365
    00366 #define DGG_THROW(A,B) return(A)
    00367
    00368 #define DGG_CHECKRVAL(A,B) {  if(A) return(B); }
    00369 #define DGG_CHECKRVAL1(A,B){ if(A) { rval = B; goto CLEANUP; } }
    00370
    00371 #define DGG_TEST(A,B,REST) { if(A) return(B);}
    00372 #define DGG_TEST2(A,B,REST,C) { DGG_TEST(A,B,REST) }
    00373 #define DGG_TEST3(A,B,REST,C,D) { DGG_TEST(A,B,REST) }
    00374
    00375 #endif
    00376
    00377 /******************** SIMPLE MACROS AND FUNCTIONS *****************************/
    00378
    00379 #define DGG_MIN(a,b) ( (a<b)?a:b )
    00380 #define DGG_MAX(a,b) ( (a>b)?a:b )
    00381 #define KREM(vht,alpha,tau)  (DGG_MIN( ceil(vht / alpha), tau ) - 1)
    00382 #define LMIN(vht, d, bht) (DGG_MIN( floor(d*bht/bht), d))
    00383 #define ABOV(v) (v - floor(v))
    00384 #define QINT(vht,bht,tau) ( (int)floor( (vht*(tau-1))/bht ) )
    00385 #define V2I(bht,tau,i) ( ((i+1)*bht / tau) )
    00386
    00387 int DGG_is_even(double vht, double bht, int tau, int q);
    00388 double frac_part(double value);
    00389 int DGG_is_a_multiple_of_b(double a, double b);
    00390
    00391
    00392 /* free function for DGG_data_t. Frees internal arrays and data structure */
    00393 int DGG_freeData( DGG_data_t *data );
    00394
    00395 /******************** CONSTRAINT ADTs *****************************************/
    00396 DGG_constraint_t* DGG_newConstraint(int max_arrays);
    00397 void DGG_freeConstraint(DGG_constraint_t *c);
    00398 DGG_constraint_t *DGG_copyConstraint(DGG_constraint_t *c);
    00399 void DGG_scaleConstraint(DGG_constraint_t *c, int t);
    00400
    00401 /******************** CONFIGURATION *******************************************/
    00402 void DGG_list_init (DGG_list_t *l);
    00403 int DGG_list_addcut (DGG_list_t *l, DGG_constraint_t *cut, int ctype, double alpha);
    00404 void DGG_list_delcut (DGG_list_t *l, int i);
    00405 void DGG_list_free(DGG_list_t *l);
    00406
    00407 /******************* SOLVER SPECIFIC METHODS **********************************/
    00408 DGG_data_t *DGG_getData(const void *solver_ptr);
    00409
    00410 /******************* CONSTRAINT MANIPULATION **********************************/
    00411
    00412 /* DGG_transformConstraint: manipulates a constraint in the following way:
    00413
    00414 packs everything in output
    00415
    00416 1 - variables at their upper bounds are substituted for their
    00417 complements. This is done by adjusting the coefficients and
    00418 the right hand side (simple substitution).
    00419
    00420 2 - variables with non-zero lower bounds are shifted.            */
    00421
    00422 int DGG_transformConstraint( DGG_data_t *data,
    00423                              double **x_out,
    00424                              double **rc_out,
    00425                              char **isint_out,
    00426                              DGG_constraint_t *constraint );
    00427
    00428 /* DGG_unTransformConstraint :
    00429
    00430 1 - Undoes step (1) of DGG_transformConstraint
    00431 2 - Undoes step (2) of DGG_transformConstraint                  */
    00432
    00433 int DGG_unTransformConstraint( DGG_data_t *data,
    00434                                DGG_constraint_t *constraint );
    00435
    00436 /* substitutes each slack variable by the structural variables which
    00437    define it. This function, hence, changes the constraint 'cut'.    */
    00438
    00439 int DGG_substituteSlacks( const void *solver_ptr,
    00440                           DGG_data_t *data,
    00441                           DGG_constraint_t *cut );
    00442
    00443 int DGG_nicefyConstraint( const void *solver_ptr,
    00444                           DGG_data_t *data,
    00445                           DGG_constraint_t *cut);
    00446
    00447 /******************* CUT GENERATION *******************************************/
    00448 int DGG_getFormulaConstraint( int row_idx,
    00449                               const void *solver_ptr,
    00450                               DGG_data_t *data,
    00451                               DGG_constraint_t* row );
    00452
    00453 int DGG_getTableauConstraint( int index,
    00454                               const void *solver_ptr,
    00455                               DGG_data_t *data,
    00456                               DGG_constraint_t* tabrow,
    00457                               const int * colIsBasic,
    00458                               const int * rowIsBasic,
    00459                               CoinFactorization & factorization,
    00460                               int mode );
    00461
    00462 DGG_constraint_t* DGG_getSlackExpression(const void *solver_ptr, DGG_data_t* data, int row_index);
    00463
    00464   int DGG_generateTabRowCuts( DGG_list_t *list,
    00465                               DGG_data_t *data,
    00466                               const void *solver_ptr );
    00467
    00468   int DGG_generateFormulationCuts( DGG_list_t *list,
    00469                                    DGG_data_t *data,
    00470                                    const void *solver_ptr,
    00471                                    int nrows);
    00472
    00473
    00474   int DGG_generateFormulationCutsFromBase( DGG_constraint_t *base,
    00475                                            double slack,
    00476                                            DGG_list_t *list,
    00477                                            DGG_data_t *data,
    00478                                            const void *solver_ptr );
    00479
    00480   int DGG_generateCutsFromBase( DGG_constraint_t *base,
    00481                                 DGG_list_t *list,
    00482                                 DGG_data_t *data,
    00483                                 const void *solver_ptr );
    00484
    00485 int DGG_buildMir( char *isint,
    00486                   DGG_constraint_t *base,
    00487                   DGG_constraint_t **cut_out );
    00488
    00489 int DGG_build2step( double alpha,
    00490                     char *isint,
    00491                     DGG_constraint_t *base,
    00492                     DGG_constraint_t **cut_out );
    00493
    00494   int DGG_addMirToList   ( DGG_constraint_t *base,
    00495                            char *isint,
    00496                            double *x,
    00497                            DGG_list_t *list,
    00498                            DGG_data_t *data,
    00499                            DGG_constraint_t *orig_base );
    00500
    00501   int DGG_add2stepToList ( DGG_constraint_t *base,
    00502                            char *isint,
    00503                            double *x,
    00504                            double *rc,
    00505                            DGG_list_t *list,
    00506                            DGG_data_t *data,
    00507                            DGG_constraint_t *orig_base );
    00508
    00509 /******************* CUT INFORMATION ******************************************/
    00510
    00511 double DGG_cutLHS(DGG_constraint_t *c, double *x);
    00512 int DGG_isCutDesirable(DGG_constraint_t *c, DGG_data_t *d);
    00513
    00514 /******************* TEST / DEBUGGING ROUTINES ********************************/
    00515
    00516 int DGG_isConstraintViolated(DGG_data_t *d, DGG_constraint_t *c);
    00517
    00518 int DGG_isBaseTrivial(DGG_data_t *d, DGG_constraint_t* c);
    00519 int DGG_is2stepValid(double alpha, double bht);
    00520
    00521 int DGG_cutsOffPoint(double *x, DGG_constraint_t *cut);
    00522
    00523 //#############################################################################
    00529 void CglTwomirUnitTest(const OsiSolverInterface * siP,
    00530                        const std::string mpdDir);
    00531
    00532
    00533 #endif
    00534
    00535