/* bool LPSolver::Simplex(int phase) { int x = phase == 1 ? m+1 : m; while (true) { int s = -1; for (int j = 0; j <= n; j++) { if (phase == 2 && N[j] == -1) continue; if (s == -1 || D[x][j] < D[x][s] || D[x][j] == D[x][s] && N[j] < N[s]) s = j; } if (D[x][s] >= -EPS) return true; // fertig int r = -1; for (int i = 0; i < m; i++) { if (D[i][s] <= 0) continue; if (r == -1 || D[i][n+1] / D[i][s] < D[r][n+1] / D[r][s] || D[i][n+1] / D[i][s] == D[r][n+1] / D[r][s] && B[i] < B[r]) r = i; } if (r == -1) return false; // alle eintraege in der spalte negativ -> unbeschraenkt! Pivot(r, s); } }*/ double LPSolver::Solve(RealVector &x) { int r = 0; for (int i = 1; i < m; i++) if (D(i,n+1) < D(r,n+1)) r = i; // D[r][n+1] smalles element // if < 0 // phase 1 if (D(r,n+1) <= -eps) { Pivot(r, n); if (!Simplex(1) || D(m+1,n+1) < -eps) return -std::numeric_limits<double>::infinity(); // infeasible! for (int i = 0; i < m; i++) if (B[i] == -1) { int s = -1; for (int j = 0; j <= n; j++) if (s == -1 || D(i,j) < D(i,s) || D(i,j) == D(i,s) && N[j] < N[s]) s = j; Pivot(i, s); } } // otherwise phase 2... if (!Simplex(2)) return std::numeric_limits<double>::infinity(); // unbounded x = RealVector(n); for (int i = 0; i < m; i++) if (B[i] < n) x(B[i]) = D(i,n+1); return D(m,n+1); }
static void qsort_nonaligned(void *base, size_t nmemb, size_t size, int (*compare)(const void *, const void *)) { stack_entry stack[STACK_SIZE]; int stacktop=0; char *first,*last; char *pivot=malloc(size); size_t trunc=TRUNC_nonaligned*size; assert(pivot!=0); first=(char*)base; last=first+(nmemb-1)*size; if (last-first>=trunc) { char *ffirst=first, *llast=last; while (1) { /* Select pivot */ { char * mid=first+size*((last-first)/size >> 1); Pivot(SWAP_nonaligned,size); memcpy(pivot,mid,size); } /* Partition. */ Partition(SWAP_nonaligned,size); /* Prepare to recurse/iterate. */ Recurse(trunc) } }
bool LPSolver::Simplex(int phase) { int x = phase == 1 ? m+1 : m; while (true) { int s = -1; for (int j = 0; j <= n; j++) { if (phase == 2 && N[j] == -1) continue; if (s == -1 || D(x,j) < D(x,s) || D(x,j) == D(x,s) && N[j] < N[s]) s = j; } if (D(x,s) >= -eps) return true; // fertig int r = -1; for (int i = 0; i < m; i++) { if (D(i,s) <= 0) continue; if (r == -1 || D(i,n+1) / D(i,s) < D(r,n+1) / D(r,s) || D(i,n+1) / D(i,s) == D(r,n+1) / D(r,s) && B[i] < B[r]) r = i; } if (r == -1) return false; // alle eintraege in der spalte negativ -> unbeschraenkt! Pivot(r, s); } }
bool initSimplex() { nCnt=bCnt=0; for(int i=1; i<=n; i++) N[++nCnt]=i; for(int i=1; i<=m; i++) B[++bCnt]=i+n,A[i][n+i]=1.0; R=bCnt,C=bCnt+nCnt; double minV=INF; int p=-1; for(int i=1; i<=m; i++) if(fcmp(minV,b[i])==1) minV=b[i],p=i; if(fcmp(minV,0.0)>=0) return true; N[++nCnt]=n+m+1; R++,C++; for(int i=0; i<=C; i++) A[R][i]=0.0; for(int i=1; i<=R; i++) A[i][n+m+1]=-1.0; Pivot(p,n+m+1); if(!Process(A[R])) return false; if(fcmp(b[R],0.0)!=0) return false; p=-1; for(int i=1; i<=bCnt&&p==-1; i++) if(B[i]==n+m+1) p=i; if(p!=-1) { for(int i=1; i<=nCnt; i++) { if(fcmp(A[p][N[i]],0.0)!=0) { Pivot(p,N[i]); break; } } } bool f=false; for(int i=1; i<=nCnt; i++) { if(N[i]==n+m+1) f=true; if(f&&i+1<=nCnt) N[i]=N[i+1]; } nCnt--; R--,C--; return true; }
DOUBLE Solve(VD &x) { int r = 0; for (int i = 1; i < m; i++) if (D[i][n + 1] < D[r][n + 1]) r = i; if (D[r][n + 1] < -EPS) { Pivot(r, n); if (!Simplex(1) || D[m + 1][n + 1] < -EPS) return -numeric_limits<DOUBLE>::infinity(); for (int i = 0; i < m; i++) if (B[i] == -1) { int s = -1; for (int j = 0; j <= n; j++) if (s == -1 || D[i][j] < D[i][s] || D[i][j] == D[i][s] && N[j] < N[s]) s = j; Pivot(i, s); } } if (!Simplex(2)) return numeric_limits<DOUBLE>::infinity(); x = VD(n); for (int i = 0; i < m; i++) if (B[i] < n) x[B[i]] = D[i][n + 1]; return D[m][n + 1]; } };
// FinishTransaction Command* TransformBox::FinishTransaction() { Command* command = fCurrentCommand; if (fCurrentCommand) { fCurrentCommand->SetNewTransformation(Pivot(), Translation(), LocalRotation(), LocalXScale(), LocalYScale()); fCurrentCommand = NULL; } return command; }
bool Simplex(int phase) { int x = phase == 1 ? m + 1 : m; while (true) { int s = -1; for (int j = 0; j <= n; j++) { if (phase == 2 && N[j] == -1) continue; if (s == -1 || D[x][j] < D[x][s] || D[x][j] == D[x][s] && N[j] < N[s]) s = j; } if (D[x][s] > -EPS) return true; int r = -1; for (int i = 0; i < m; i++) { if (D[i][s] < EPS) continue; if (r == -1 || D[i][n + 1] / D[i][s] < D[r][n + 1] / D[r][s] || (D[i][n + 1] / D[i][s]) == (D[r][n + 1] / D[r][s]) && B[i] < B[r]) r = i; } if (r == -1) return false; Pivot(r, s); } }
int PhaseII(int n, int m, double *c, double a[maxn][maxn], double *rhs, double &ans, int PivotIndex) { int i,j,k,l; double tmp; while(k=Pivot(n,m,c,a,rhs,i,j),k==PIVOT_OK || PivotIndex) { if( PivotIndex ) { j=0; i=PivotIndex; PivotIndex=0; } basic[row[i]]=0; col[row[i]]=0; basic[j]=1; col[j]=i; row[i]=j; tmp=a[i][j]; for(k=0;k<=n;k++) a[i][k]/=tmp; rhs[i]/=tmp; for(k=1;k<=m;k++) if(k!=i && dcmp(a[k][j])) { tmp = -a[k][j]; for(l=0;l<=n;l++) a[k][l]+=tmp*a[i][l]; rhs[k] += tmp*rhs[i]; } tmp=-c[j]; for(l=0;l<=n;l++) c[l]+=a[i][l]*tmp; ans-=tmp*rhs[i]; } return k; }
bool Process(double P[]) { while(true) { int e=-1; double mV=-INF; for(int i=1; i<=nCnt; i++) if(fcmp(P[N[i]],mV)==1) mV=P[N[i]],e=N[i]; if(fcmp(mV,0.0)<=0) break; int l=-1; mV=INF; for(int i=1; i<=bCnt; i++) { if(fcmp(A[i][e],0.0)==1) { double t=b[i]/A[i][e]; if(fcmp(mV,t)==1||(fcmp(mV,t)==0&&(l==-1||B[l]>B[i]))) mV=t,l=i; } } if(l==-1) return false; Pivot(l,e); } return true; }
Pivot Pivot::createReversePivot() const { return Pivot(property_index_, bound_, !is_upper_bound_, !is_inclusive_); }
void Simplex() { e10: Pivot(); Formula(); Optimize(); if (NOPTIMAL == 1) goto e10; }