/////////////////////////////////////////////////////////////////////////////// version="$Id$"; 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