Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
可行方法被限制在无限循环中 我已经运行了C++中的以下单纯形方法,代码如下: #include<bits/stdc++.h> #include<cstdio> using namespace std; #define maxm 500 #define maxn 500 double inf = 1e100; double eps = 1e-13; int row,col; double A[maxm][maxn]; double B[maxn]; /////////////////////////////////////////////////////////////////////////////////////////// // Simon Lo's // Simplex algorithm on augmented matrix a of dimension (m+1)x(n+1) // returns 1 if feasible, 0 if not feasible, -1 if unbounded // returns solution in b[] in original var order, max(f) in ret // form: maximize sum_j(a_mj*x_j)-a_mn s.t. sum_j(a_ij*x_j)<=a_in // in standard form. // To convert into standard form: // 1. if exists equality constraint, then replace by both >= and <= // 2. if variable x doesn't have nonnegativity constraint, then replace by // difference of 2 variables like x1-x2, where x1>=0, x2>=0 // 3. for a>=b constraints, convert to -a<=-b // note: watch out for -0.0 in the solution, algorithm may cycle // eps = 1e-7 may give wrong answer, 1e-10 is better void pivot(int m, int n, double a[maxm][maxn], int B[maxm], int N[maxn], int r, int c) { int i, j; swap(N[c], B[r]); a[r][c]=1/a[r][c]; for (j=0; j<=n; j++)if (j!=c) a[r][j]*=a[r][c]; for (i=0; i<=m; i++)if (i!=r) { for (j=0; j<=n; j++)if (j!=c) a[i][j]-=a[i][c]*a[r][j]; a[i][c] = -a[i][c]*a[r][c]; } } int feasible(int m, int n, double a[maxm][maxn], int B[maxm], int N[maxn]) { int r, c, i; double p, v; while (1) { for (p=inf, i=0; i<m; i++) if (a[i][n]<p) p=a[r=i][n]; if (p>-eps) return 1; for (p=0, i=0; i<n; i++) if (a[r][i]<p) p=a[r][c=i]; if (p>-eps) return 0; cout<<"Sultan"<<endl; p = a[r][n]/a[r][c]; for (i=r+1; i<m; i++) if (a[i][c]>eps) { v = a[i][n]/a[i][c]; if (v<p) r=i, p=v; } pivot(m, n, a, B, N, r, c); } } int simplex(int m, int n, double a[maxm][maxn], double b[maxn], double& ret) { int B[maxm], N[maxn], r, c, i; double p, v; for (i=0; i<n; i++) N[i]=i; for (i=0; i<m; i++) B[i]=n+i; if (!feasible(m, n, a, B, N)) return 0; while (1) { for (p=0, i=0; i<n; i++) if (a[m][i]>p) p=a[m][c=i]; if (p<eps) { for (i=0; i<n; i++) if (N[i]<n) b[N[i]]=0; for (i=0; i<m; i++) if (B[i]<n) b[B[i]]=a[i][n]; ret = -a[m][n]; return 1; } for (p=inf, i=0; i<m; i++) if (a[i][c]>eps) { v = a[i][n]/a[i][c]; if (v<p) p=v, r=i; } if (p==inf) return -1; pivot(m, n, a, B, N, r, c); } } ////////////////////////////////////////////////////////////////////////////////////////////// void read_file() { freopen("Dimen.txt","r",stdin); scanf("%d",&row); scanf("%d",&col); row = 30 ; col = 100; cout << row << " "<<col<<endl; freopen("A1.txt","r",stdin); for(int i=0;i<row;i++) for(int j=0;j<col;j++) scanf("%lf",&A[i][j]); cout<"Completed A"; freopen("B1.txt","r",stdin); for(int i=0;i<row;i++){ scanf("%lf",&A[i][col]); } cout<"Completed B"; freopen("F1.txt","r",stdin); for(int j=0;j<col;j++){ scanf("%lf",&A[row][j]); //B[i]=-B[i]; } cout<"Completed F"; } int main() { read_file(); double value,opt=0; int flag = simplex(row,col, A,B, value); freopen("opt.txt","r",stdin); scanf("%lf",&opt); if(flag != -1) { cout<<"The result is "<<(value+opt); } else { cout<<"The result is infeasible"; } return 0; }_C++_Simplex - Fatal编程技术网

可行方法被限制在无限循环中 我已经运行了C++中的以下单纯形方法,代码如下: #include<bits/stdc++.h> #include<cstdio> using namespace std; #define maxm 500 #define maxn 500 double inf = 1e100; double eps = 1e-13; int row,col; double A[maxm][maxn]; double B[maxn]; /////////////////////////////////////////////////////////////////////////////////////////// // Simon Lo's // Simplex algorithm on augmented matrix a of dimension (m+1)x(n+1) // returns 1 if feasible, 0 if not feasible, -1 if unbounded // returns solution in b[] in original var order, max(f) in ret // form: maximize sum_j(a_mj*x_j)-a_mn s.t. sum_j(a_ij*x_j)<=a_in // in standard form. // To convert into standard form: // 1. if exists equality constraint, then replace by both >= and <= // 2. if variable x doesn't have nonnegativity constraint, then replace by // difference of 2 variables like x1-x2, where x1>=0, x2>=0 // 3. for a>=b constraints, convert to -a<=-b // note: watch out for -0.0 in the solution, algorithm may cycle // eps = 1e-7 may give wrong answer, 1e-10 is better void pivot(int m, int n, double a[maxm][maxn], int B[maxm], int N[maxn], int r, int c) { int i, j; swap(N[c], B[r]); a[r][c]=1/a[r][c]; for (j=0; j<=n; j++)if (j!=c) a[r][j]*=a[r][c]; for (i=0; i<=m; i++)if (i!=r) { for (j=0; j<=n; j++)if (j!=c) a[i][j]-=a[i][c]*a[r][j]; a[i][c] = -a[i][c]*a[r][c]; } } int feasible(int m, int n, double a[maxm][maxn], int B[maxm], int N[maxn]) { int r, c, i; double p, v; while (1) { for (p=inf, i=0; i<m; i++) if (a[i][n]<p) p=a[r=i][n]; if (p>-eps) return 1; for (p=0, i=0; i<n; i++) if (a[r][i]<p) p=a[r][c=i]; if (p>-eps) return 0; cout<<"Sultan"<<endl; p = a[r][n]/a[r][c]; for (i=r+1; i<m; i++) if (a[i][c]>eps) { v = a[i][n]/a[i][c]; if (v<p) r=i, p=v; } pivot(m, n, a, B, N, r, c); } } int simplex(int m, int n, double a[maxm][maxn], double b[maxn], double& ret) { int B[maxm], N[maxn], r, c, i; double p, v; for (i=0; i<n; i++) N[i]=i; for (i=0; i<m; i++) B[i]=n+i; if (!feasible(m, n, a, B, N)) return 0; while (1) { for (p=0, i=0; i<n; i++) if (a[m][i]>p) p=a[m][c=i]; if (p<eps) { for (i=0; i<n; i++) if (N[i]<n) b[N[i]]=0; for (i=0; i<m; i++) if (B[i]<n) b[B[i]]=a[i][n]; ret = -a[m][n]; return 1; } for (p=inf, i=0; i<m; i++) if (a[i][c]>eps) { v = a[i][n]/a[i][c]; if (v<p) p=v, r=i; } if (p==inf) return -1; pivot(m, n, a, B, N, r, c); } } ////////////////////////////////////////////////////////////////////////////////////////////// void read_file() { freopen("Dimen.txt","r",stdin); scanf("%d",&row); scanf("%d",&col); row = 30 ; col = 100; cout << row << " "<<col<<endl; freopen("A1.txt","r",stdin); for(int i=0;i<row;i++) for(int j=0;j<col;j++) scanf("%lf",&A[i][j]); cout<"Completed A"; freopen("B1.txt","r",stdin); for(int i=0;i<row;i++){ scanf("%lf",&A[i][col]); } cout<"Completed B"; freopen("F1.txt","r",stdin); for(int j=0;j<col;j++){ scanf("%lf",&A[row][j]); //B[i]=-B[i]; } cout<"Completed F"; } int main() { read_file(); double value,opt=0; int flag = simplex(row,col, A,B, value); freopen("opt.txt","r",stdin); scanf("%lf",&opt); if(flag != -1) { cout<<"The result is "<<(value+opt); } else { cout<<"The result is infeasible"; } return 0; }

可行方法被限制在无限循环中 我已经运行了C++中的以下单纯形方法,代码如下: #include<bits/stdc++.h> #include<cstdio> using namespace std; #define maxm 500 #define maxn 500 double inf = 1e100; double eps = 1e-13; int row,col; double A[maxm][maxn]; double B[maxn]; /////////////////////////////////////////////////////////////////////////////////////////// // Simon Lo's // Simplex algorithm on augmented matrix a of dimension (m+1)x(n+1) // returns 1 if feasible, 0 if not feasible, -1 if unbounded // returns solution in b[] in original var order, max(f) in ret // form: maximize sum_j(a_mj*x_j)-a_mn s.t. sum_j(a_ij*x_j)<=a_in // in standard form. // To convert into standard form: // 1. if exists equality constraint, then replace by both >= and <= // 2. if variable x doesn't have nonnegativity constraint, then replace by // difference of 2 variables like x1-x2, where x1>=0, x2>=0 // 3. for a>=b constraints, convert to -a<=-b // note: watch out for -0.0 in the solution, algorithm may cycle // eps = 1e-7 may give wrong answer, 1e-10 is better void pivot(int m, int n, double a[maxm][maxn], int B[maxm], int N[maxn], int r, int c) { int i, j; swap(N[c], B[r]); a[r][c]=1/a[r][c]; for (j=0; j<=n; j++)if (j!=c) a[r][j]*=a[r][c]; for (i=0; i<=m; i++)if (i!=r) { for (j=0; j<=n; j++)if (j!=c) a[i][j]-=a[i][c]*a[r][j]; a[i][c] = -a[i][c]*a[r][c]; } } int feasible(int m, int n, double a[maxm][maxn], int B[maxm], int N[maxn]) { int r, c, i; double p, v; while (1) { for (p=inf, i=0; i<m; i++) if (a[i][n]<p) p=a[r=i][n]; if (p>-eps) return 1; for (p=0, i=0; i<n; i++) if (a[r][i]<p) p=a[r][c=i]; if (p>-eps) return 0; cout<<"Sultan"<<endl; p = a[r][n]/a[r][c]; for (i=r+1; i<m; i++) if (a[i][c]>eps) { v = a[i][n]/a[i][c]; if (v<p) r=i, p=v; } pivot(m, n, a, B, N, r, c); } } int simplex(int m, int n, double a[maxm][maxn], double b[maxn], double& ret) { int B[maxm], N[maxn], r, c, i; double p, v; for (i=0; i<n; i++) N[i]=i; for (i=0; i<m; i++) B[i]=n+i; if (!feasible(m, n, a, B, N)) return 0; while (1) { for (p=0, i=0; i<n; i++) if (a[m][i]>p) p=a[m][c=i]; if (p<eps) { for (i=0; i<n; i++) if (N[i]<n) b[N[i]]=0; for (i=0; i<m; i++) if (B[i]<n) b[B[i]]=a[i][n]; ret = -a[m][n]; return 1; } for (p=inf, i=0; i<m; i++) if (a[i][c]>eps) { v = a[i][n]/a[i][c]; if (v<p) p=v, r=i; } if (p==inf) return -1; pivot(m, n, a, B, N, r, c); } } ////////////////////////////////////////////////////////////////////////////////////////////// void read_file() { freopen("Dimen.txt","r",stdin); scanf("%d",&row); scanf("%d",&col); row = 30 ; col = 100; cout << row << " "<<col<<endl; freopen("A1.txt","r",stdin); for(int i=0;i<row;i++) for(int j=0;j<col;j++) scanf("%lf",&A[i][j]); cout<"Completed A"; freopen("B1.txt","r",stdin); for(int i=0;i<row;i++){ scanf("%lf",&A[i][col]); } cout<"Completed B"; freopen("F1.txt","r",stdin); for(int j=0;j<col;j++){ scanf("%lf",&A[row][j]); //B[i]=-B[i]; } cout<"Completed F"; } int main() { read_file(); double value,opt=0; int flag = simplex(row,col, A,B, value); freopen("opt.txt","r",stdin); scanf("%lf",&opt); if(flag != -1) { cout<<"The result is "<<(value+opt); } else { cout<<"The result is infeasible"; } return 0; },c++,simplex,C++,Simplex,但这段代码在无限循环中运行。可行方法是无限循环的。如何解决此错误?请帮帮我 让我们从修复代码中不清楚的部分开始: #include<bits/stdc++.h> 使用std是一种糟糕的做法,它可能会在大型项目中混淆编译器,并选择一个函数/模板/任何您不想选择的东西 #define maxm 500 #define maxn 500 至少将这些更改为 #define maxm (500) #define maxn (500) 因此,宏替换不会混淆。更好地使用c++11和 co

但这段代码在无限循环中运行。可行方法是无限循环的。如何解决此错误?请帮帮我

让我们从修复代码中不清楚的部分开始:

#include<bits/stdc++.h>  
使用std是一种糟糕的做法,它可能会在大型项目中混淆编译器,并选择一个函数/模板/任何您不想选择的东西

#define maxm 500
#define maxn 500
至少将这些更改为

#define maxm (500)
#define maxn (500)
因此,宏替换不会混淆。更好地使用c++11和

constexpr int maxm = 500; 
constexpr int maxn = 500; 

double inf = 1e100;
double eps = 1e-13;
有找到inf和eps正确值的标准方法


解决此类问题的正确工具是调试器。在询问堆栈溢出之前,应该逐行检查代码。如需更多帮助,请阅读。至少,您应该[编辑]您的问题,以包含一个重现您的问题的示例,以及您在调试器中所做的观察。我已经调试了代码。代码在可行的方法中被卡住了。然后你必须学习如何更有效地使用调试器。@SagorAhmed:嗯,调试器肯定会告诉你更多关于代码被卡住的细节。显然,条件p>-eps从未满足过,因此没有达到任何回报。问题是:为什么会这样?为什么条件总是false?您没有到达任何返回语句。我发现扔掉有缺陷的代码并从头重写它比尝试修补难以理解的代码更快。你现在更了解这个问题了,困难的部分结束了。现在,找到一个更简单、更不容易出错的解决方案。
constexpr int maxm = 500; 
constexpr int maxn = 500; 

double inf = 1e100;
double eps = 1e-13;
double inf = std::numeric_limits<double>::max();
double eps = std::numeric_limits<double>::epsilon();
double eps = std::numeric_limits<double>::round_error();