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