| 1 | // $Id$cxx . . $ `<file>/Cgl/src/CglKnapsackCover/CglKnapsackCover.hpp`/ |
| 2 | // Copyright (C) 2000, International Business Machines |
| 3 | // Corporation and others. All Rights Reserved. |
| 4 | // This code is licensed under the terms of the Eclipse Public License (EPL). |
| 5 | |
| 6 | #ifndef CglKnapsackCover_H |
| 7 | #define CglKnapsackCover_Hcxx . . $ `Cgl/src/CglKnapsackCover/CglKnapsackCover.hpp:7:9`! |
| 8 | |
| 9 | #include <string> |
| 10 | |
| 11 | #include "CglCutGenerator.hpp"cxx . . $ `<file>/Cgl/src/CglCutGenerator.hpp`/ |
| 12 | #include "CglTreeInfo.hpp"cxx . . $ `<file>/Cgl/src/CglTreeInfo.hpp`/ |
| 13 | |
| 14 | /** Knapsack Cover Cut Generator Class */ |
| 15 | class CglKnapsackCovercxx . . $ CglKnapsackCover# : public CglCutGeneratorcxx . . $ CglCutGenerator# { |
| 16 | friend void CglKnapsackCoverUnitTestcxx . . $ CglKnapsackCoverUnitTest(d8c7ffe7dd7e392).(const OsiSolverInterfacecxx . . $ OsiSolverInterface# * siP, |
| 17 | const stdcxx . . $ std/::stringcxx . . $ std/string# mpdDir ); |
| 18 | |
| 19 | public: |
| 20 | /** A method to set which rows should be tested for knapsack covers */ |
| 21 | void setTestedRowIndicescxx . . $ CglKnapsackCover#setTestedRowIndices(f4da74bb73d50818).(int num, const int* ind); |
| 22 | |
| 23 | /**@name Generate Cuts */ |
| 24 | //@{ |
| 25 | /** Generate knapsack cover cuts for the model of the solver interface, si. |
| 26 | Insert the generated cuts into OsiCut, cs. |
| 27 | */ |
| 28 | virtual void generateCutscxx . . $ CglKnapsackCover#generateCuts(a7cd996c3d1130d3).(const OsiSolverInterfacecxx . . $ OsiSolverInterface# & si, OsiCutscxx . . $ OsiCuts# & cs, |
| 29 | const CglTreeInfocxx . . $ CglTreeInfo# info = CglTreeInfocxx . . $ CglTreeInfo#()); |
| 30 | //@} |
| 31 | |
| 32 | /**@name Constructors and destructors */ |
| 33 | //@{ |
| 34 | /// Default constructor |
| 35 | CglKnapsackCovercxx . . $ CglKnapsackCover#CglKnapsackCover(49f6e7a06ebc5aa8). (); |
| 36 | |
| 37 | /// Copy constructor |
| 38 | CglKnapsackCovercxx . . $ CglKnapsackCover#CglKnapsackCover(add8ea4305bf59c1). ( |
| 39 | const CglKnapsackCovercxx . . $ CglKnapsackCover# &); |
| 40 | |
| 41 | /// Clone |
| 42 | virtual CglCutGeneratorcxx . . $ CglCutGenerator# * clonecxx . . $ CglKnapsackCover#clone(ff686f57cc59f31d).() const; |
| 43 | |
| 44 | /// Assignment operator |
| 45 | CglKnapsackCovercxx . . $ CglKnapsackCover# & |
| 46 | operatorcxx . . $ CglKnapsackCover#`operator=`(4e109743323b7e4f).=( |
| 47 | const CglKnapsackCovercxx . . $ CglKnapsackCover#& rhs); |
| 48 | |
| 49 | /// Destructor |
| 50 | virtual |
| 51 | ~cxx . . $ CglKnapsackCover#`~CglKnapsackCover`(ced63f7c635d850d).CglKnapsackCovercxx . . $ CglKnapsackCover# (); |
| 52 | /// Create C++ lines to get to current state |
| 53 | virtual stdcxx . . $ std/::stringcxx . . $ std/string# generateCppcxx . . $ CglKnapsackCover#generateCpp(d8c45821d4afeee5).( FILE * fp); |
| 54 | /// This can be used to refresh any information |
| 55 | virtual void refreshSolvercxx . . $ CglKnapsackCover#refreshSolver(148ed51524aa7e8b).(OsiSolverInterfacecxx . . $ OsiSolverInterface# * solver); |
| 56 | //@} |
| 57 | |
| 58 | |
| 59 | /**@name Sets and gets */ |
| 60 | //@{ |
| 61 | /// Set limit on number in knapsack |
| 62 | inline void setMaxInKnapsackcxx . . $ CglKnapsackCover#setMaxInKnapsack(d4f767463ce0a6b3).(int value) |
| 63 | { if (value>0) maxInKnapsack_cxx . . $ CglKnapsackCover#maxInKnapsack_. = value;} |
| 64 | /// get limit on number in knapsack |
| 65 | inline int getMaxInKnapsackcxx . . $ CglKnapsackCover#getMaxInKnapsack(455f465bc33b4cdf).() const |
| 66 | {return maxInKnapsack_cxx . . $ CglKnapsackCover#maxInKnapsack_.;} |
| 67 | /// Switch off expensive cuts |
| 68 | inline void switchOffExpensivecxx . . $ CglKnapsackCover#switchOffExpensive(49f6e7a06ebc5aa8).() |
| 69 | { expensiveCuts_cxx . . $ CglKnapsackCover#expensiveCuts_.=false;} |
| 70 | /// Switch on expensive cuts |
| 71 | inline void switchOnExpensivecxx . . $ CglKnapsackCover#switchOnExpensive(49f6e7a06ebc5aa8).() |
| 72 | { expensiveCuts_cxx . . $ CglKnapsackCover#expensiveCuts_.=true;} |
| 73 | private: |
| 74 | |
| 75 | // Private member methods |
| 76 | |
| 77 | |
| 78 | /**@name Private methods */ |
| 79 | //@{ |
| 80 | |
| 81 | /** deriveAKnapsack |
| 82 | returns 1 if it is able to derive |
| 83 | a (canonical) knapsack inequality |
| 84 | in binary variables of the form ax<=b |
| 85 | from the rowIndex-th row in the model, |
| 86 | returns 0 otherwise. |
| 87 | */ |
| 88 | int deriveAKnapsackcxx . . $ CglKnapsackCover#deriveAKnapsack(bcc48ce1960f9e4c).( |
| 89 | const OsiSolverInterfacecxx . . $ OsiSolverInterface# & si, |
| 90 | OsiCutscxx . . $ OsiCuts# & cs, |
| 91 | CoinPackedVectorcxx . . $ CoinPackedVector# & krow, |
| 92 | bool treatAsLRow, |
| 93 | double & b, |
| 94 | int * complement, |
| 95 | double * xstar, |
| 96 | int rowIndex, |
| 97 | int numberElements, |
| 98 | const int * index, |
| 99 | const double * element); |
| 100 | |
| 101 | int deriveAKnapsackcxx . . $ CglKnapsackCover#deriveAKnapsack(dfbbf3ec191b736).( |
| 102 | const OsiSolverInterfacecxx . . $ OsiSolverInterface# & si, |
| 103 | OsiCutscxx . . $ OsiCuts# & cs, |
| 104 | CoinPackedVectorcxx . . $ CoinPackedVector# & krow, |
| 105 | double & b, |
| 106 | int * complement, |
| 107 | double * xstar, |
| 108 | int rowIndex, |
| 109 | const CoinPackedVectorBasecxx . . $ CoinPackedVectorBase# & matrixRow); |
| 110 | |
| 111 | /** Find a violated minimal cover from |
| 112 | a canonical form knapsack inequality by |
| 113 | solving the -most- violated cover problem |
| 114 | and postprocess to ensure minimality |
| 115 | */ |
| 116 | int findExactMostViolatedMinCovercxx . . $ CglKnapsackCover#findExactMostViolatedMinCover(7e27af9ad88535c1).( |
| 117 | int nCols, |
| 118 | int row, |
| 119 | CoinPackedVectorcxx . . $ CoinPackedVector# & krow, |
| 120 | double b, |
| 121 | double * xstar, |
| 122 | CoinPackedVectorcxx . . $ CoinPackedVector# & cover, |
| 123 | CoinPackedVectorcxx . . $ CoinPackedVector# & remainder); |
| 124 | |
| 125 | /** Find the most violate minimum cover by solving the lp-relaxation of the |
| 126 | most-violate-min-cover problem |
| 127 | */ |
| 128 | int findLPMostViolatedMinCovercxx . . $ CglKnapsackCover#findLPMostViolatedMinCover(b7d05aad6f04861f).( |
| 129 | int nCols, |
| 130 | int row, |
| 131 | CoinPackedVectorcxx . . $ CoinPackedVector# & krow, |
| 132 | double & b, |
| 133 | double * xstar, |
| 134 | CoinPackedVectorcxx . . $ CoinPackedVector# & cover, |
| 135 | CoinPackedVectorcxx . . $ CoinPackedVector# & remainder); |
| 136 | |
| 137 | /// find a minimum cover by a simple greedy approach |
| 138 | int findGreedyCovercxx . . $ CglKnapsackCover#findGreedyCover(d21410e04a87a268).( |
| 139 | int row, |
| 140 | CoinPackedVectorcxx . . $ CoinPackedVector# & krow, |
| 141 | double & b, |
| 142 | double * xstar, |
| 143 | CoinPackedVectorcxx . . $ CoinPackedVector# & cover, |
| 144 | CoinPackedVectorcxx . . $ CoinPackedVector# & remainder |
| 145 | ); |
| 146 | |
| 147 | /// lift the cover inequality |
| 148 | int liftCoverCutcxx . . $ CglKnapsackCover#liftCoverCut(c3b097d1701bbc8c).( |
| 149 | double & b, |
| 150 | int nRowElem, |
| 151 | CoinPackedVectorcxx . . $ CoinPackedVector# & cover, |
| 152 | CoinPackedVectorcxx . . $ CoinPackedVector# & remainder, |
| 153 | CoinPackedVectorcxx . . $ CoinPackedVector# & cut ); |
| 154 | |
| 155 | /// sequence-independent lift and uncomplement and add the resulting cut to the cut set |
| 156 | int liftAndUncomplementAndAddcxx . . $ CglKnapsackCover#liftAndUncomplementAndAdd(a747e5ecd2b0d91d).( |
| 157 | double rowub, |
| 158 | CoinPackedVectorcxx . . $ CoinPackedVector# & krow, |
| 159 | double & b, |
| 160 | int * complement, |
| 161 | int row, |
| 162 | CoinPackedVectorcxx . . $ CoinPackedVector# & cover, |
| 163 | CoinPackedVectorcxx . . $ CoinPackedVector# & remainder, |
| 164 | OsiCutscxx . . $ OsiCuts# & cs ); |
| 165 | |
| 166 | /// sequence-dependent lift, uncomplement and add the resulting cut to the cut set |
| 167 | void seqLiftAndUncomplementAndAddcxx . . $ CglKnapsackCover#seqLiftAndUncomplementAndAdd(17c4142c1782aab5).( |
| 168 | int nCols, |
| 169 | double * xstar, |
| 170 | int * complement, |
| 171 | int row, |
| 172 | int nRowElem, |
| 173 | double & b, |
| 174 | CoinPackedVectorcxx . . $ CoinPackedVector# & cover, // need not be violated |
| 175 | CoinPackedVectorcxx . . $ CoinPackedVector# & remainder, |
| 176 | OsiCutscxx . . $ OsiCuts# & cs ); |
| 177 | |
| 178 | /// sequence-dependent lift binary variables either up or down, uncomplement and add to the cut set |
| 179 | void liftUpDownAndUncomplementAndAddcxx . . $ CglKnapsackCover#liftUpDownAndUncomplementAndAdd(9215d3e89d570e9).( |
| 180 | int nCols, |
| 181 | double * xstar, |
| 182 | int * complement, |
| 183 | int row, |
| 184 | int nRowElem, |
| 185 | double & b, |
| 186 | |
| 187 | // the following 3 packed vectors partition the krow: |
| 188 | CoinPackedVectorcxx . . $ CoinPackedVector# & fracCover, // vars have frac soln values in lp relaxation |
| 189 | // and form cover with the vars atOne |
| 190 | CoinPackedVectorcxx . . $ CoinPackedVector# & atOne, // vars have soln value of 1 in lp relaxation |
| 191 | // and together with fracCover form minimal (?) cover. |
| 192 | CoinPackedVectorcxx . . $ CoinPackedVector# & remainder, |
| 193 | OsiCutscxx . . $ OsiCuts# & cs ); |
| 194 | |
| 195 | /// find a cover using a variation of the logic found in OSL (w/o SOS) |
| 196 | int findPseudoJohnAndEllisCovercxx . . $ CglKnapsackCover#findPseudoJohnAndEllisCover(d21410e04a87a268). ( |
| 197 | int row, |
| 198 | CoinPackedVectorcxx . . $ CoinPackedVector# & krow, |
| 199 | double & b, |
| 200 | double * xstar, |
| 201 | CoinPackedVectorcxx . . $ CoinPackedVector# & cover, |
| 202 | CoinPackedVectorcxx . . $ CoinPackedVector# & remainder); |
| 203 | |
| 204 | /// find a cover using the basic logic found in OSL (w/o SOS) |
| 205 | int findJohnAndEllisCovercxx . . $ CglKnapsackCover#findJohnAndEllisCover(c3d90a862a44c29b). ( |
| 206 | int row, |
| 207 | CoinPackedVectorcxx . . $ CoinPackedVector# & krow, |
| 208 | double & b, |
| 209 | double * xstar, |
| 210 | CoinPackedVectorcxx . . $ CoinPackedVector# & fracCover, |
| 211 | CoinPackedVectorcxx . . $ CoinPackedVector# & atOnes, |
| 212 | CoinPackedVectorcxx . . $ CoinPackedVector# & remainder); |
| 213 | |
| 214 | |
| 215 | /** A C-style implementation of the Horowitz-Sahni exact solution |
| 216 | procedure for solving knapsack problem. |
| 217 | |
| 218 | (ToDo: implement the more efficient dynamic programming approach) |
| 219 | |
| 220 | (Reference: Martello and Toth, Knapsack Problems, Wiley, 1990, p30.) |
| 221 | */ |
| 222 | int exactSolveKnapsackcxx . . $ CglKnapsackCover#exactSolveKnapsack(553df3276b6a0834).( |
| 223 | int n, |
| 224 | double c, |
| 225 | double const *pp, |
| 226 | double const *ww, |
| 227 | double & z, |
| 228 | int * x); |
| 229 | /// For testing gub stuff |
| 230 | int gubifyCutcxx . . $ CglKnapsackCover#gubifyCut(d44c843a7e19f000).(CoinPackedVectorcxx . . $ CoinPackedVector# & cut); |
| 231 | public: |
| 232 | /** Creates cliques for use by probing. |
| 233 | Only cliques >= minimumSize and < maximumSize created |
| 234 | Can also try and extend cliques as a result of probing (root node). |
| 235 | Returns number of cliques found. |
| 236 | */ |
| 237 | int createCliquescxx . . $ CglKnapsackCover#createCliques(2866b8ad25985ce7).( OsiSolverInterfacecxx . . $ OsiSolverInterface# & si, |
| 238 | int minimumSize=2, int maximumSize=100, bool extendCliques=false); |
| 239 | private: |
| 240 | /// Delete all clique information |
| 241 | void deleteCliquescxx . . $ CglKnapsackCover#deleteCliques(49f6e7a06ebc5aa8).(); |
| 242 | //@} |
| 243 | |
| 244 | // Private member data |
| 245 | |
| 246 | /**@name Private member data */ |
| 247 | //@{ |
| 248 | /// epsilon |
| 249 | double epsilon_cxx . . $ CglKnapsackCover#epsilon_.; |
| 250 | /// Tolerance to use for violation - bigger than epsilon_ |
| 251 | double epsilon2_cxx . . $ CglKnapsackCover#epsilon2_.; |
| 252 | /// 1-epsilon |
| 253 | double onetol_cxx . . $ CglKnapsackCover#onetol_.; |
| 254 | /// Maximum in knapsack |
| 255 | int maxInKnapsack_cxx . . $ CglKnapsackCover#maxInKnapsack_.; |
| 256 | /** which rows to look at. If specified, only these rows will be considered |
| 257 | for generating knapsack covers. Otherwise all rows will be tried */ |
| 258 | int numRowsToCheck_cxx . . $ CglKnapsackCover#numRowsToCheck_.; |
| 259 | int* rowsToCheck_cxx . . $ CglKnapsackCover#rowsToCheck_.; |
| 260 | /// exactKnapsack can be expensive - this switches off some |
| 261 | bool expensiveCuts_cxx . . $ CglKnapsackCover#expensiveCuts_.; |
| 262 | /// Cliques |
| 263 | /// **** TEMP so can reference from listing |
| 264 | const OsiSolverInterfacecxx . . $ OsiSolverInterface# * solver_cxx . . $ CglKnapsackCover#solver_.; |
| 265 | int whichRow_cxx . . $ CglKnapsackCover#whichRow_.; |
| 266 | int * complement_cxx . . $ CglKnapsackCover#complement_.; |
| 267 | double * elements_cxx . . $ CglKnapsackCover#elements_.; |
| 268 | /// Number of cliques |
| 269 | int numberCliques_cxx . . $ CglKnapsackCover#numberCliques_.; |
| 270 | /// Clique type |
| 271 | typedef structcxx . . $ CglKnapsackCover#$anonymous_type_0# { |
| 272 | unsigned int equalitycxx . . $ CglKnapsackCover#$anonymous_type_0#equality.:1; // nonzero if clique is == |
| 273 | } CliqueTypecxx . . $ CglKnapsackCover#CliqueType#; |
| 274 | CliqueTypecxx . . $ CglKnapsackCover#CliqueType# * cliqueType_cxx . . $ CglKnapsackCover#cliqueType_.; |
| 275 | /// Start of each clique |
| 276 | int * cliqueStart_cxx . . $ CglKnapsackCover#cliqueStart_.; |
| 277 | /// Entries for clique |
| 278 | CliqueEntrycxx . . $ CliqueEntry# * cliqueEntry_cxx . . $ CglKnapsackCover#cliqueEntry_.; |
| 279 | /** Start of oneFixes cliques for a column in matrix or -1 if not |
| 280 | in any clique */ |
| 281 | int * oneFixStart_cxx . . $ CglKnapsackCover#oneFixStart_.; |
| 282 | /** Start of zeroFixes cliques for a column in matrix or -1 if not |
| 283 | in any clique */ |
| 284 | int * zeroFixStart_cxx . . $ CglKnapsackCover#zeroFixStart_.; |
| 285 | /// End of fixes for a column |
| 286 | int * endFixStart_cxx . . $ CglKnapsackCover#endFixStart_.; |
| 287 | /// Clique numbers for one or zero fixes |
| 288 | int * whichClique_cxx . . $ CglKnapsackCover#whichClique_.; |
| 289 | /// Number of columns |
| 290 | int numberColumns_cxx . . $ CglKnapsackCover#numberColumns_.; |
| 291 | /** For each column with nonzero in row copy this gives a clique "number". |
| 292 | So first clique mentioned in row is always 0. If no entries for row |
| 293 | then no cliques. If sequence > numberColumns then not in clique. |
| 294 | */ |
| 295 | //CliqueEntry * cliqueRow_; |
| 296 | /// cliqueRow_ starts for each row |
| 297 | //int * cliqueRowStart_; |
| 298 | //@} |
| 299 | }; |
| 300 | |
| 301 | //############################################################################# |
| 302 | /** A function that tests the methods in the CglKnapsackCover class. The |
| 303 | only reason for it not to be a member method is that this way it doesn't |
| 304 | have to be compiled into the library. And that's a gain, because the |
| 305 | library should be compiled with optimization on, but this method should be |
| 306 | compiled with debugging. */ |
| 307 | void CglKnapsackCoverUnitTestcxx . . $ CglKnapsackCoverUnitTest(d8c7ffe7dd7e392).(const OsiSolverInterfacecxx . . $ OsiSolverInterface# * siP, |
| 308 | const stdcxx . . $ std/::stringcxx . . $ std/string# mpdDir ); |
| 309 | |
| 310 | #endif |