[27e935] | 1 | // $Id: IP.lib,v 1.2 2000-05-11 09:04:05 Singular Exp $ |
---|
[6ba162] | 2 | // |
---|
| 3 | // author : Christine Theis |
---|
| 4 | // |
---|
| 5 | |
---|
[27e935] | 6 | //version="$Id: IP.lib,v 1.2 2000-05-11 09:04:05 Singular Exp $"; |
---|
[6ba162] | 7 | |
---|
| 8 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 9 | |
---|
| 10 | info=" |
---|
[27e935] | 11 | LIBRARY: IP.lib PROCEDURES FOR INTEGER PROGRAMMING USING GROEBNER BASIS METHODS |
---|
[6ba162] | 12 | |
---|
| 13 | |
---|
| 14 | Let A an integral (mxn)-matrix, b a vector with m integral |
---|
| 15 | coefficients and c a vector with n nonnegative integral coefficients. The |
---|
[27e935] | 16 | solution of the IP-problem |
---|
| 17 | |
---|
[6ba162] | 18 | (*) minimize{ cx | Ax=b, all components of x are nonnegative integers } |
---|
[27e935] | 19 | |
---|
[6ba162] | 20 | proceeds in two steps: |
---|
[27e935] | 21 | |
---|
[6ba162] | 22 | 1. We compute the toric ideal of A and its Groebner basis with respect |
---|
| 23 | to a term ordering refining the cost function c (such an ordering |
---|
| 24 | exists because c is nonnegative). |
---|
[27e935] | 25 | |
---|
[6ba162] | 26 | 2. We reduce the right hand vector b or an initial solution of the problem |
---|
| 27 | modulo this ideal. |
---|
[27e935] | 28 | |
---|
[6ba162] | 29 | For these purposes, we can use seven different algorithms: |
---|
| 30 | The algorithm of Conti/Traverso (ct) can compute an optimal solution |
---|
| 31 | of the IP-problem directly from the right hand vector b. The same is |
---|
| 32 | true for its `positive' variant (pct) which can only be applied if A |
---|
[27e935] | 33 | and b have nonnegative entries, but should be faster in that case. |
---|
[6ba162] | 34 | All other algorithms need initial solutions of the IP-problem. Except |
---|
| 35 | from the Conti-Traverso algorithm with elimination (ect), |
---|
| 36 | they should be more efficient than the algorithm mentionned before. |
---|
| 37 | These are the algorithms of Pottier (pt), Bigatti/La Scala/Robbiano |
---|
| 38 | (blr), Hosten/Sturmfels (hs) and Di Biase/Urbanke (du). The last two |
---|
| 39 | seem to be the fastest in the actual implementation. |
---|
| 40 | |
---|
| 41 | |
---|
[27e935] | 42 | solve_IP(intmat A, intvec bx, intvec c, string alg); |
---|
[6ba162] | 43 | solve_IP(intmat A, list bx, intvec c, string alg); |
---|
[27e935] | 44 | solve_IP(intmat A, intvec bx, intvec c, string alg, intvec prsv); |
---|
[6ba162] | 45 | solve_IP(intmat A, list bx, intvec c, string alg, intvec prsv); |
---|
[27e935] | 46 | |
---|
| 47 | procedures for solving the IP-problem (*) |
---|
| 48 | They return the solution(s) of the given problem(s) or the |
---|
[6ba162] | 49 | message `not solvable'. |
---|
| 50 | `alg' may be one of the algorithm abbreviations as above. |
---|
[27e935] | 51 | If `alg' is chosen to be `ct' or `pct', bx is read as the right |
---|
[6ba162] | 52 | hand vector b of the system Ax=b. b should then be an intvec of |
---|
| 53 | size m where m is the number of rows of A. Furthermore, bx and |
---|
[27e935] | 54 | A should be nonnegative if `pct' is used. |
---|
| 55 | If `alg' is chosen to be `ect',`pt',`blr',`hs' or `du', bx is |
---|
| 56 | read as an initial solution x of the system Ax=b. bx should |
---|
| 57 | then be a nonnegative intvec of size n where n is the number |
---|
| 58 | of columns of A. |
---|
[6ba162] | 59 | If `alg' is chosen to be `blr' or `hs', the algorithm needs a |
---|
| 60 | vector with positive coefficcients in the row space of A. If |
---|
| 61 | no row of A contains only positive entries, one must use the |
---|
| 62 | versions of solve_IP which take such a vector prsv as |
---|
[27e935] | 63 | argument. |
---|
| 64 | solve_IP may also be called with a list bx of intvecs instead |
---|
| 65 | of a single intvec. |
---|
[6ba162] | 66 | |
---|
| 67 | |
---|
[27e935] | 68 | "; |
---|
[6ba162] | 69 | |
---|
[27e935] | 70 | /////////////////////////////////////////////////////////////////////////////// |
---|
[6ba162] | 71 | |
---|
| 72 | static proc solve_IP_1(intmat A, intvec bx, intvec c, string alg) |
---|
[27e935] | 73 | { |
---|
[6ba162] | 74 | intvec v; |
---|
| 75 | // to be returned |
---|
| 76 | |
---|
| 77 | // check arguments as far as necessary |
---|
[27e935] | 78 | // other inconsistencies are detected by the external program |
---|
[6ba162] | 79 | if(size(c)!=ncols(A)) |
---|
| 80 | { |
---|
| 81 | "ERROR: number of matrix columns must equal size of cost vector"; |
---|
| 82 | return(v); |
---|
[27e935] | 83 | } |
---|
[6ba162] | 84 | |
---|
| 85 | // create first temporary file with that the external program is |
---|
[27e935] | 86 | // called |
---|
[6ba162] | 87 | |
---|
| 88 | int process=system("pid"); |
---|
[27e935] | 89 | string matrixfile="temp_MATRIX"+string(process); |
---|
[6ba162] | 90 | link MATRIX=":w "+matrixfile; |
---|
| 91 | open(MATRIX); |
---|
| 92 | |
---|
| 93 | write(MATRIX,"MATRIX","columns:",ncols(A),"cost vector:"); |
---|
| 94 | int i,j; |
---|
| 95 | for(j=1;j<=ncols(A);j++) |
---|
| 96 | { |
---|
| 97 | write(MATRIX,c[j]); |
---|
| 98 | } |
---|
| 99 | write(MATRIX,"rows:",nrows(A),"matrix:"); |
---|
| 100 | for(i=1;i<=nrows(A);i++) |
---|
| 101 | { |
---|
| 102 | for(j=1;j<=ncols(A);j++) |
---|
| 103 | { |
---|
| 104 | write(MATRIX,A[i,j]); |
---|
| 105 | } |
---|
| 106 | } |
---|
[27e935] | 107 | |
---|
[6ba162] | 108 | // search for positive row space vector, if required by the |
---|
[27e935] | 109 | // algorithm |
---|
[6ba162] | 110 | int found=0; |
---|
| 111 | if((alg=="blr") || (alg=="hs")) |
---|
[27e935] | 112 | { |
---|
[6ba162] | 113 | for(i=1;i<=nrows(A);i++) |
---|
| 114 | { |
---|
| 115 | found=i; |
---|
| 116 | for(j=1;j<=ncols(A);j++) |
---|
| 117 | { |
---|
| 118 | if(A[i,j]<=0) |
---|
| 119 | { |
---|
| 120 | found=0; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | if(found>0) |
---|
| 124 | { |
---|
| 125 | break; |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | if(found==0) |
---|
| 129 | { |
---|
[27e935] | 130 | "ERROR: algorithm needs positive vector in the row space of the matrix"; |
---|
[6ba162] | 131 | close(MATRIX); |
---|
| 132 | system("sh","rm -f "+matrixfile); |
---|
| 133 | return(v); |
---|
[27e935] | 134 | } |
---|
[6ba162] | 135 | write(MATRIX,"positive row space vector:"); |
---|
| 136 | for(j=1;j<=ncols(A);j++) |
---|
[27e935] | 137 | { |
---|
[6ba162] | 138 | write(MATRIX,A[found,j]); |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | close(MATRIX); |
---|
| 142 | |
---|
| 143 | // create second temporary file for the external program |
---|
| 144 | |
---|
[27e935] | 145 | string problemfile="temp_PROBLEM"+string(process); |
---|
[6ba162] | 146 | link PROBLEM=":w "+problemfile; |
---|
| 147 | open(PROBLEM); |
---|
| 148 | |
---|
[27e935] | 149 | write(PROBLEM,"PROBLEM","vector size:",size(bx),"number of instances:",1,"right hand or initial solution vectors:"); |
---|
[6ba162] | 150 | for(i=1;i<=size(bx);i++) |
---|
| 151 | { |
---|
| 152 | write(PROBLEM,bx[i]); |
---|
| 153 | } |
---|
| 154 | close(PROBLEM); |
---|
| 155 | |
---|
| 156 | // call external program |
---|
| 157 | int dummy=system("sh","solve_IP -alg "+alg+" "+matrixfile+" "+problemfile); |
---|
| 158 | |
---|
| 159 | // read solution from created file |
---|
| 160 | link SOLUTION=":r "+matrixfile+".sol."+alg; |
---|
| 161 | string solution=read(SOLUTION); |
---|
| 162 | int pos; |
---|
| 163 | string s; |
---|
| 164 | if(alg=="ct" || alg=="pct") |
---|
[27e935] | 165 | { |
---|
[6ba162] | 166 | pos=find(solution,"NO"); |
---|
| 167 | if(pos!=0) |
---|
| 168 | { |
---|
| 169 | "not solvable"; |
---|
| 170 | } |
---|
| 171 | else |
---|
| 172 | { |
---|
| 173 | pos=find(solution,"YES"); |
---|
| 174 | pos=find(solution,":",pos); |
---|
| 175 | pos++; |
---|
| 176 | for(j=1;j<=ncols(A);j++) |
---|
| 177 | { |
---|
| 178 | while(solution[pos]==" " || solution[pos]==newline) |
---|
| 179 | { |
---|
| 180 | pos++; |
---|
| 181 | } |
---|
| 182 | s=""; |
---|
| 183 | while(solution[pos]!=" " && solution[pos]!=newline) |
---|
| 184 | { |
---|
| 185 | s=s+solution[pos]; |
---|
| 186 | pos++; |
---|
| 187 | } |
---|
| 188 | execute("v[j]="+s+";"); |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | } |
---|
| 192 | else |
---|
[27e935] | 193 | { |
---|
[6ba162] | 194 | pos=find(solution,"optimal"); |
---|
| 195 | pos=find(solution,":",pos); |
---|
| 196 | pos++; |
---|
| 197 | for(j=1;j<=ncols(A);j++) |
---|
| 198 | { |
---|
| 199 | while(solution[pos]==" " || solution[pos]==newline) |
---|
| 200 | { |
---|
| 201 | pos++; |
---|
| 202 | } |
---|
| 203 | s=""; |
---|
| 204 | while(solution[pos]!=" " && solution[pos]!=newline) |
---|
| 205 | { |
---|
| 206 | s=s+solution[pos]; |
---|
| 207 | pos++; |
---|
| 208 | } |
---|
| 209 | execute("v[j]="+s+";"); |
---|
| 210 | } |
---|
| 211 | } |
---|
[27e935] | 212 | |
---|
[6ba162] | 213 | // delete all created files |
---|
| 214 | dummy=system("sh","rm -f "+matrixfile); |
---|
| 215 | dummy=system("sh","rm -f "+matrixfile+".GB."+alg); |
---|
| 216 | dummy=system("sh","rm -f "+problemfile); |
---|
| 217 | dummy=system("sh","rm -f "+matrixfile+".sol."+alg); |
---|
| 218 | |
---|
| 219 | return(v); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | static proc solve_IP_2(intmat A, list bx, intvec c, string alg) |
---|
[27e935] | 223 | { |
---|
[6ba162] | 224 | list l;; |
---|
| 225 | // to be returned |
---|
| 226 | |
---|
| 227 | // check arguments as far as necessary |
---|
[27e935] | 228 | // other inconsistencies are detected by the external program |
---|
[6ba162] | 229 | if(size(c)!=ncols(A)) |
---|
| 230 | { |
---|
| 231 | "ERROR: number of matrix columns must equal size of cost vector"; |
---|
| 232 | return(l); |
---|
[27e935] | 233 | } |
---|
[6ba162] | 234 | |
---|
| 235 | int k; |
---|
| 236 | for(k=2;k<=size(bx);k++) |
---|
| 237 | { |
---|
| 238 | if(size(bx[k])!=size(bx[1])) |
---|
| 239 | { |
---|
| 240 | "ERROR: size of all right hand vectors must be equal"; |
---|
| 241 | return(l); |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | // create first temporary file with that the external program is |
---|
[27e935] | 246 | // called |
---|
[6ba162] | 247 | |
---|
| 248 | int process=system("pid"); |
---|
[27e935] | 249 | string matrixfile="temp_MATRIX"+string(process); |
---|
[6ba162] | 250 | link MATRIX=":w "+matrixfile; |
---|
| 251 | open(MATRIX); |
---|
| 252 | |
---|
| 253 | write(MATRIX,"MATRIX","columns:",ncols(A),"cost vector:"); |
---|
| 254 | int i,j; |
---|
| 255 | for(j=1;j<=ncols(A);j++) |
---|
| 256 | { |
---|
| 257 | write(MATRIX,c[j]); |
---|
| 258 | } |
---|
| 259 | write(MATRIX,"rows:",nrows(A),"matrix:"); |
---|
| 260 | for(i=1;i<=nrows(A);i++) |
---|
| 261 | { |
---|
| 262 | for(j=1;j<=ncols(A);j++) |
---|
| 263 | { |
---|
| 264 | write(MATRIX,A[i,j]); |
---|
| 265 | } |
---|
| 266 | } |
---|
[27e935] | 267 | |
---|
[6ba162] | 268 | // search for positive row space vector, if required by the |
---|
[27e935] | 269 | // algorithm |
---|
[6ba162] | 270 | int found=0; |
---|
| 271 | if((alg=="blr") || (alg=="hs")) |
---|
[27e935] | 272 | { |
---|
[6ba162] | 273 | for(i=1;i<=nrows(A);i++) |
---|
| 274 | { |
---|
| 275 | found=i; |
---|
| 276 | for(j=1;j<=ncols(A);j++) |
---|
| 277 | { |
---|
| 278 | if(A[i,j]<=0) |
---|
| 279 | { |
---|
| 280 | found=0; |
---|
| 281 | } |
---|
| 282 | } |
---|
| 283 | if(found>0) |
---|
| 284 | { |
---|
| 285 | break; |
---|
| 286 | } |
---|
| 287 | } |
---|
| 288 | if(found==0) |
---|
| 289 | { |
---|
[27e935] | 290 | "ERROR: algorithm needs positive vector in the row space of the matrix"; |
---|
[6ba162] | 291 | close(MATRIX); |
---|
| 292 | system("sh","rm -f "+matrixfile); |
---|
| 293 | return(l); |
---|
[27e935] | 294 | } |
---|
[6ba162] | 295 | write(MATRIX,"positive row space vector:"); |
---|
| 296 | for(j=1;j<=ncols(A);j++) |
---|
[27e935] | 297 | { |
---|
[6ba162] | 298 | write(MATRIX,A[found,j]); |
---|
| 299 | } |
---|
| 300 | } |
---|
| 301 | close(MATRIX); |
---|
| 302 | |
---|
| 303 | // create second temporary file for the external program |
---|
| 304 | |
---|
[27e935] | 305 | string problemfile="temp_PROBLEM"+string(process); |
---|
[6ba162] | 306 | link PROBLEM=":w "+problemfile; |
---|
| 307 | open(PROBLEM); |
---|
| 308 | |
---|
[27e935] | 309 | write(PROBLEM,"PROBLEM","vector size:",size(bx[1]),"number of instances:",size(bx),"right hand or initial solution vectors:"); |
---|
[6ba162] | 310 | for(k=1;k<=size(bx);k++) |
---|
| 311 | { |
---|
| 312 | for(i=1;i<=size(bx[1]);i++) |
---|
| 313 | { |
---|
| 314 | write(PROBLEM,bx[k][i]); |
---|
| 315 | } |
---|
| 316 | } |
---|
| 317 | close(PROBLEM); |
---|
| 318 | |
---|
| 319 | // call external program |
---|
| 320 | int dummy=system("sh","solve_IP -alg "+alg+" "+matrixfile+" "+problemfile); |
---|
| 321 | |
---|
| 322 | // read solution from created file |
---|
| 323 | link SOLUTION=":r "+matrixfile+".sol."+alg; |
---|
| 324 | string solution=read(SOLUTION); |
---|
| 325 | intvec v; |
---|
| 326 | int pos,pos1,pos2; |
---|
| 327 | string s; |
---|
| 328 | if(alg=="ct" || alg=="pct") |
---|
[27e935] | 329 | { |
---|
[6ba162] | 330 | pos=1; |
---|
| 331 | for(k=1;k<=size(bx);k++) |
---|
[27e935] | 332 | { |
---|
[6ba162] | 333 | pos1=find(solution,"NO",pos); |
---|
| 334 | pos2=find(solution,"YES",pos); |
---|
| 335 | if(pos1!=0 && (pos1<pos2 || pos2==0)) |
---|
| 336 | // first problem not solvable |
---|
| 337 | { |
---|
| 338 | pos=find(solution,":",pos1); |
---|
| 339 | l=insert(l,"not solvable",size(l)); |
---|
| 340 | } |
---|
| 341 | else |
---|
| 342 | // first problem solvable |
---|
| 343 | { |
---|
| 344 | pos=find(solution,":",pos2); |
---|
| 345 | pos++; |
---|
| 346 | for(j=1;j<=ncols(A);j++) |
---|
| 347 | { |
---|
| 348 | while(solution[pos]==" " || solution[pos]==newline) |
---|
| 349 | { |
---|
| 350 | pos++; |
---|
| 351 | } |
---|
| 352 | s=""; |
---|
| 353 | while(solution[pos]!=" " && solution[pos]!=newline) |
---|
| 354 | { |
---|
| 355 | s=s+solution[pos]; |
---|
| 356 | pos++; |
---|
[27e935] | 357 | } |
---|
[6ba162] | 358 | execute("v[j]="+s+";"); |
---|
| 359 | } |
---|
| 360 | l=insert(l,v,size(l)); |
---|
| 361 | } |
---|
| 362 | } |
---|
| 363 | } |
---|
| 364 | else |
---|
[27e935] | 365 | { |
---|
[6ba162] | 366 | pos=1; |
---|
| 367 | for(k=1;k<=size(bx);k++) |
---|
| 368 | { |
---|
| 369 | pos=find(solution,"optimal",pos); |
---|
| 370 | pos=find(solution,":",pos); |
---|
| 371 | pos++; |
---|
| 372 | for(j=1;j<=ncols(A);j++) |
---|
| 373 | { |
---|
| 374 | while(solution[pos]==" " || solution[pos]==newline) |
---|
| 375 | { |
---|
| 376 | pos++; |
---|
| 377 | } |
---|
| 378 | s=""; |
---|
| 379 | while(solution[pos]!=" " && solution[pos]!=newline) |
---|
| 380 | { |
---|
| 381 | s=s+solution[pos]; |
---|
| 382 | pos++; |
---|
[27e935] | 383 | } |
---|
[6ba162] | 384 | execute("v[j]="+s+";"); |
---|
| 385 | } |
---|
| 386 | l=insert(l,v,size(l)); |
---|
| 387 | } |
---|
| 388 | } |
---|
[27e935] | 389 | |
---|
[6ba162] | 390 | // delete all created files |
---|
| 391 | dummy=system("sh","rm -f "+matrixfile); |
---|
| 392 | dummy=system("sh","rm -f "+matrixfile+".GB."+alg); |
---|
| 393 | dummy=system("sh","rm -f "+problemfile); |
---|
| 394 | dummy=system("sh","rm -f "+matrixfile+".sol."+alg); |
---|
| 395 | |
---|
| 396 | return(l); |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | static proc solve_IP_3(intmat A, intvec bx, intvec c, string alg, intvec prsv) |
---|
[27e935] | 400 | { |
---|
[6ba162] | 401 | intvec v; |
---|
| 402 | // to be returned |
---|
| 403 | |
---|
| 404 | // check arguments as far as necessary |
---|
[27e935] | 405 | // other inconsistencies are detected by the external program |
---|
[6ba162] | 406 | if(size(c)!=ncols(A)) |
---|
| 407 | { |
---|
| 408 | "ERROR: number of matrix columns must equal size of cost vector"; |
---|
| 409 | return(v); |
---|
[27e935] | 410 | } |
---|
[6ba162] | 411 | |
---|
| 412 | if(size(prsv)!=ncols(A)) |
---|
| 413 | { |
---|
| 414 | "ERROR: number of matrix columns must equal size of positive row space vector"; |
---|
| 415 | return(v); |
---|
[27e935] | 416 | } |
---|
[6ba162] | 417 | |
---|
| 418 | // create first temporary file with that the external program is |
---|
[27e935] | 419 | // called |
---|
[6ba162] | 420 | |
---|
| 421 | int process=system("pid"); |
---|
[27e935] | 422 | string matrixfile="temp_MATRIX"+string(process); |
---|
[6ba162] | 423 | link MATRIX=":w "+matrixfile; |
---|
| 424 | open(MATRIX); |
---|
| 425 | |
---|
| 426 | write(MATRIX,"MATRIX","columns:",ncols(A),"cost vector:"); |
---|
| 427 | int i,j; |
---|
| 428 | for(j=1;j<=ncols(A);j++) |
---|
| 429 | { |
---|
| 430 | write(MATRIX,c[j]); |
---|
| 431 | } |
---|
| 432 | write(MATRIX,"rows:",nrows(A),"matrix:"); |
---|
| 433 | for(i=1;i<=nrows(A);i++) |
---|
| 434 | { |
---|
| 435 | for(j=1;j<=ncols(A);j++) |
---|
| 436 | { |
---|
| 437 | write(MATRIX,A[i,j]); |
---|
| 438 | } |
---|
| 439 | } |
---|
[27e935] | 440 | |
---|
[6ba162] | 441 | // enter positive row space vector, if required by the algorithm |
---|
| 442 | if((alg=="blr") || (alg=="hs")) |
---|
[27e935] | 443 | { |
---|
[6ba162] | 444 | write(MATRIX,"positive row space vector:"); |
---|
| 445 | for(j=1;j<=ncols(A);j++) |
---|
[27e935] | 446 | { |
---|
[6ba162] | 447 | write(MATRIX,prsv[j]); |
---|
| 448 | } |
---|
| 449 | } |
---|
| 450 | close(MATRIX); |
---|
| 451 | |
---|
| 452 | // create second temporary file for the external program |
---|
| 453 | |
---|
[27e935] | 454 | string problemfile="temp_PROBLEM"+string(process); |
---|
[6ba162] | 455 | link PROBLEM=":w "+problemfile; |
---|
| 456 | open(PROBLEM); |
---|
| 457 | |
---|
[27e935] | 458 | write(PROBLEM,"PROBLEM","vector size:",size(bx),"number of instances:",1,"right hand or initial solution vectors:"); |
---|
[6ba162] | 459 | for(i=1;i<=size(bx);i++) |
---|
| 460 | { |
---|
| 461 | write(PROBLEM,bx[i]); |
---|
| 462 | } |
---|
| 463 | close(PROBLEM); |
---|
| 464 | |
---|
| 465 | // call external program |
---|
| 466 | int dummy=system("sh","solve_IP -alg "+alg+" "+matrixfile+" "+problemfile); |
---|
| 467 | |
---|
| 468 | // read solution from created file |
---|
| 469 | link SOLUTION=":r "+matrixfile+".sol."+alg; |
---|
| 470 | string solution=read(SOLUTION); |
---|
| 471 | int pos; |
---|
| 472 | string s; |
---|
| 473 | if(alg=="ct" || alg=="pct") |
---|
[27e935] | 474 | { |
---|
[6ba162] | 475 | pos=find(solution,"NO"); |
---|
| 476 | if(pos!=0) |
---|
| 477 | { |
---|
| 478 | "not solvable"; |
---|
| 479 | } |
---|
| 480 | else |
---|
| 481 | { |
---|
| 482 | pos=find(solution,"YES"); |
---|
| 483 | pos=find(solution,":",pos); |
---|
| 484 | pos++; |
---|
| 485 | for(j=1;j<=ncols(A);j++) |
---|
| 486 | { |
---|
| 487 | while(solution[pos]==" " || solution[pos]==newline) |
---|
| 488 | { |
---|
| 489 | pos++; |
---|
| 490 | } |
---|
| 491 | s=""; |
---|
| 492 | while(solution[pos]!=" " && solution[pos]!=newline) |
---|
| 493 | { |
---|
| 494 | s=s+solution[pos]; |
---|
| 495 | pos++; |
---|
[27e935] | 496 | } |
---|
[6ba162] | 497 | execute("v[j]="+s+";"); |
---|
| 498 | } |
---|
| 499 | } |
---|
| 500 | } |
---|
| 501 | else |
---|
[27e935] | 502 | { |
---|
[6ba162] | 503 | pos=find(solution,"optimal"); |
---|
| 504 | pos=find(solution,":",pos); |
---|
| 505 | pos++; |
---|
| 506 | for(j=1;j<=ncols(A);j++) |
---|
| 507 | { |
---|
| 508 | while(solution[pos]==" " || solution[pos]==newline) |
---|
| 509 | { |
---|
| 510 | pos++; |
---|
| 511 | } |
---|
| 512 | s=""; |
---|
| 513 | while(solution[pos]!=" " && solution[pos]!=newline) |
---|
| 514 | { |
---|
| 515 | s=s+solution[pos]; |
---|
| 516 | pos++; |
---|
[27e935] | 517 | } |
---|
[6ba162] | 518 | execute("v[j]="+s+";"); |
---|
| 519 | } |
---|
| 520 | } |
---|
[27e935] | 521 | |
---|
[6ba162] | 522 | // delete all created files |
---|
| 523 | dummy=system("sh","rm -f "+matrixfile); |
---|
| 524 | dummy=system("sh","rm -f "+matrixfile+".GB."+alg); |
---|
| 525 | dummy=system("sh","rm -f "+problemfile); |
---|
| 526 | dummy=system("sh","rm -f "+matrixfile+".sol."+alg); |
---|
| 527 | |
---|
| 528 | return(v); |
---|
| 529 | } |
---|
| 530 | |
---|
| 531 | static proc solve_IP_4(intmat A, list bx, intvec c, string alg, intvec prsv) |
---|
[27e935] | 532 | { |
---|
[6ba162] | 533 | list l; |
---|
| 534 | // to be returned |
---|
| 535 | |
---|
| 536 | // check arguments as far as necessary |
---|
[27e935] | 537 | // other inconsistencies are detected by the external program |
---|
[6ba162] | 538 | if(size(c)!=ncols(A)) |
---|
| 539 | { |
---|
| 540 | "ERROR: number of matrix columns must equal size of cost vector"; |
---|
| 541 | return(l); |
---|
[27e935] | 542 | } |
---|
[6ba162] | 543 | |
---|
| 544 | if(size(prsv)!=ncols(A)) |
---|
| 545 | { |
---|
| 546 | "ERROR: number of matrix columns must equal size of positive row space vector"; |
---|
| 547 | return(v); |
---|
[27e935] | 548 | } |
---|
[6ba162] | 549 | |
---|
| 550 | int k; |
---|
| 551 | for(k=2;k<=size(bx);k++) |
---|
| 552 | { |
---|
| 553 | if(size(bx[k])!=size(bx[1])) |
---|
| 554 | { |
---|
| 555 | "ERROR: size of all right hand vectors must be equal"; |
---|
| 556 | return(l); |
---|
| 557 | } |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | // create first temporary file with that the external program is |
---|
[27e935] | 561 | // called |
---|
[6ba162] | 562 | |
---|
| 563 | int process=system("pid"); |
---|
[27e935] | 564 | string matrixfile="temp_MATRIX"+string(process); |
---|
[6ba162] | 565 | link MATRIX=":w "+matrixfile; |
---|
| 566 | open(MATRIX); |
---|
| 567 | |
---|
| 568 | write(MATRIX,"MATRIX","columns:",ncols(A),"cost vector:"); |
---|
| 569 | int i,j; |
---|
| 570 | for(j=1;j<=ncols(A);j++) |
---|
| 571 | { |
---|
| 572 | write(MATRIX,c[j]); |
---|
| 573 | } |
---|
| 574 | write(MATRIX,"rows:",nrows(A),"matrix:"); |
---|
| 575 | for(i=1;i<=nrows(A);i++) |
---|
| 576 | { |
---|
| 577 | for(j=1;j<=ncols(A);j++) |
---|
| 578 | { |
---|
| 579 | write(MATRIX,A[i,j]); |
---|
| 580 | } |
---|
| 581 | } |
---|
[27e935] | 582 | |
---|
[6ba162] | 583 | // enter positive row space vector if required by the algorithm |
---|
| 584 | if((alg=="blr") || (alg=="hs")) |
---|
[27e935] | 585 | { |
---|
[6ba162] | 586 | write(MATRIX,"positive row space vector:"); |
---|
| 587 | for(j=1;j<=ncols(A);j++) |
---|
[27e935] | 588 | { |
---|
[6ba162] | 589 | write(MATRIX,prsv[j]); |
---|
| 590 | } |
---|
| 591 | } |
---|
| 592 | close(MATRIX); |
---|
| 593 | |
---|
| 594 | // create second temporary file for the external program |
---|
| 595 | |
---|
[27e935] | 596 | string problemfile="temp_PROBLEM"+string(process); |
---|
[6ba162] | 597 | link PROBLEM=":w "+problemfile; |
---|
| 598 | open(PROBLEM); |
---|
| 599 | |
---|
[27e935] | 600 | write(PROBLEM,"PROBLEM","vector size:",size(bx[1]),"number of instances:",size(bx),"right hand or initial solution vectors:"); |
---|
[6ba162] | 601 | for(k=1;k<=size(bx);k++) |
---|
| 602 | { |
---|
| 603 | for(i=1;i<=size(bx[1]);i++) |
---|
| 604 | { |
---|
| 605 | write(PROBLEM,bx[k][i]); |
---|
| 606 | } |
---|
| 607 | } |
---|
| 608 | close(PROBLEM); |
---|
| 609 | |
---|
| 610 | // call external program |
---|
| 611 | int dummy=system("sh","solve_IP -alg "+alg+" "+matrixfile+" "+problemfile); |
---|
| 612 | |
---|
| 613 | // read solution from created file |
---|
| 614 | link SOLUTION=":r "+matrixfile+".sol."+alg; |
---|
| 615 | string solution=read(SOLUTION); |
---|
| 616 | intvec v; |
---|
| 617 | int pos,pos1,pos2; |
---|
| 618 | string s; |
---|
| 619 | if(alg=="ct" || alg=="pct") |
---|
[27e935] | 620 | { |
---|
[6ba162] | 621 | pos=1; |
---|
| 622 | for(k=1;k<=size(bx);k++) |
---|
[27e935] | 623 | { |
---|
[6ba162] | 624 | pos1=find(solution,"NO",pos); |
---|
| 625 | pos2=find(solution,"YES",pos); |
---|
| 626 | if(pos1!=0 && (pos1<pos2 || pos2==0)) |
---|
| 627 | // first problem not solvable |
---|
| 628 | { |
---|
| 629 | pos=find(solution,":",pos1); |
---|
| 630 | l=insert(l,"not solvable",size(l)); |
---|
| 631 | } |
---|
| 632 | else |
---|
| 633 | // first problem solvable |
---|
| 634 | { |
---|
| 635 | pos=find(solution,":",pos2); |
---|
| 636 | pos++; |
---|
| 637 | for(j=1;j<=ncols(A);j++) |
---|
| 638 | { |
---|
| 639 | while(solution[pos]==" " || solution[pos]==newline) |
---|
| 640 | { |
---|
| 641 | pos++; |
---|
| 642 | } |
---|
| 643 | s=""; |
---|
| 644 | while(solution[pos]!=" " && solution[pos]!=newline) |
---|
| 645 | { |
---|
| 646 | s=s+solution[pos]; |
---|
| 647 | pos++; |
---|
[27e935] | 648 | } |
---|
[6ba162] | 649 | execute("v[j]="+s+";"); |
---|
| 650 | } |
---|
| 651 | l=insert(l,v,size(l)); |
---|
| 652 | } |
---|
| 653 | } |
---|
| 654 | } |
---|
| 655 | else |
---|
[27e935] | 656 | { |
---|
[6ba162] | 657 | pos=1; |
---|
| 658 | for(k=1;k<=size(bx);k++) |
---|
| 659 | { |
---|
| 660 | pos=find(solution,"optimal",pos); |
---|
| 661 | pos=find(solution,":",pos); |
---|
| 662 | pos++; |
---|
| 663 | for(j=1;j<=ncols(A);j++) |
---|
| 664 | { |
---|
| 665 | while(solution[pos]==" " || solution[pos]==newline) |
---|
| 666 | { |
---|
| 667 | pos++; |
---|
| 668 | } |
---|
| 669 | s=""; |
---|
| 670 | while(solution[pos]!=" " && solution[pos]!=newline) |
---|
| 671 | { |
---|
| 672 | s=s+solution[pos]; |
---|
| 673 | pos++; |
---|
| 674 | } |
---|
| 675 | execute("v[j]="+s+";"); |
---|
| 676 | } |
---|
| 677 | l=insert(l,v,size(l)); |
---|
| 678 | } |
---|
| 679 | } |
---|
[27e935] | 680 | |
---|
[6ba162] | 681 | // delete all created files |
---|
| 682 | dummy=system("sh","rm -f "+matrixfile); |
---|
| 683 | dummy=system("sh","rm -f "+matrixfile+".GB."+alg); |
---|
| 684 | dummy=system("sh","rm -f "+problemfile); |
---|
| 685 | dummy=system("sh","rm -f "+matrixfile+".sol."+alg); |
---|
| 686 | |
---|
| 687 | return(l); |
---|
| 688 | } |
---|
| 689 | |
---|
| 690 | proc solve_IP |
---|
[27e935] | 691 | "USAGE: |
---|
[6ba162] | 692 | solve_IP(A,bx,c,alg); A intmat, bx intvec, c intvec, alg string |
---|
[27e935] | 693 | solve_IP(A,bx,c,alg); A intmat, bx list of intvec, c intvec, alg string |
---|
[6ba162] | 694 | solve_IP(A,bx,c,alg,prsv); A intmat, bx intvec, c intvec, alg string, |
---|
[27e935] | 695 | prsv intvec |
---|
[6ba162] | 696 | solve_IP(A,bx,c,alg,prsv); A intmat, bx list of intvec, c intvec, alg string, |
---|
[27e935] | 697 | prsv intvec |
---|
| 698 | RETURN: solution of the associated integer programming problem as explained |
---|
[6ba162] | 699 | in IP.lib |
---|
[27e935] | 700 | return type = type of bx |
---|
[6ba162] | 701 | EXAMPLE: example solve_IP; shows an example" |
---|
| 702 | { |
---|
| 703 | if(size(#)==4) |
---|
| 704 | { |
---|
| 705 | if(typeof(#[2])=="intvec") |
---|
| 706 | { |
---|
| 707 | return(solve_IP_1(#[1],#[2],#[3],#[4])); |
---|
| 708 | } |
---|
| 709 | else |
---|
| 710 | { |
---|
| 711 | return(solve_IP_2(#[1],#[2],#[3],#[4])); |
---|
| 712 | } |
---|
| 713 | } |
---|
| 714 | else |
---|
| 715 | { |
---|
| 716 | if(typeof(#[2])=="intvec") |
---|
| 717 | { |
---|
| 718 | return(solve_IP_3(#[1],#[2],#[3],#[4],#[5])); |
---|
| 719 | } |
---|
| 720 | else |
---|
| 721 | { |
---|
| 722 | return(solve_IP_4(#[1],#[2],#[3],#[4],#[5])); |
---|
| 723 | } |
---|
| 724 | } |
---|
| 725 | } |
---|
| 726 | example |
---|
| 727 | { |
---|
[27e935] | 728 | "EXAMPLE"; echo=2; |
---|
| 729 | |
---|
| 730 | // call with single right hand vector |
---|
| 731 | intmat A[2][3]=1,1,0,0,1,1; |
---|
| 732 | A; |
---|
| 733 | intvec b1=1,1; |
---|
| 734 | b1; |
---|
| 735 | intvec c=2,2,1; |
---|
| 736 | c; |
---|
| 737 | intvec solution_vector=solve_IP(A,b1,c,"pct"); |
---|
| 738 | solution_vector; |
---|
| 739 | |
---|
| 740 | // call with list of right hand vectors |
---|
| 741 | intvec b2=-1,1; |
---|
| 742 | list l=b1,b2; |
---|
| 743 | l; |
---|
| 744 | list solution_list=solve_IP(A,l,c,"ct"); |
---|
| 745 | solution_list; |
---|
| 746 | |
---|
| 747 | // call with single initial solution vector |
---|
| 748 | A=2,1,-1,-1,1,2; |
---|
| 749 | A; |
---|
| 750 | b1=3,4,5; |
---|
| 751 | solution_vector=solve_IP(A,b1,c,"du"); |
---|
| 752 | |
---|
| 753 | // call with single initial solution vector and algorithm needing a positive |
---|
| 754 | // row space vector |
---|
| 755 | solution_vector=solve_IP(A,b1,c,"hs"); |
---|
| 756 | |
---|
| 757 | // call with single initial solution vector and positive row space vector |
---|
| 758 | intvec prsv=1,2,1; |
---|
| 759 | prsv; |
---|
| 760 | solution_vector=solve_IP(A,b1,c,"hs",prsv); |
---|
| 761 | solution_vector; |
---|
| 762 | |
---|
| 763 | // call with list of initial solution vectors and positive row space vector |
---|
| 764 | b2=7,8,0; |
---|
| 765 | l=b1,b2; |
---|
| 766 | l; |
---|
| 767 | solution_list=solve_IP(A,l,c,"blr",prsv); |
---|
| 768 | solution_list; |
---|
[6ba162] | 769 | } |
---|