在本程序中默認該現(xiàn)行規(guī)劃問題有最優(yōu)解
創(chuàng)新新互聯(lián),憑借十多年的做網(wǎng)站、成都網(wǎng)站建設經(jīng)驗,本著真心·誠心服務的企業(yè)理念服務于成都中小企業(yè)設計網(wǎng)站有近千家案例。做網(wǎng)站建設,選創(chuàng)新互聯(lián)。
針對此問題:
#includeusing namespace std; int check(float *sigema, int m) { for (int i = 1; i <= m ; i++) { if (sigema[i] > 0) { return 0; } } return 1; } //此程序已經(jīng)化為標準型的線性規(guī)劃問題中,且默認有最優(yōu)解 int main(int argc, char* argv[]) { //數(shù)據(jù)輸入部分 int m, n; cout << "請輸入變量個數(shù):"; cin >> m; cout << "請輸入不等式個數(shù):"; cin >> n; float **matrix = new float*[n + 1]; //系數(shù)矩陣 for (int i = 1; i <= n; i++) { matrix[i] = new float[m + 2]; } float *cj = new float[m + 1]; float *cB = new float[n + 1]; //基變量系數(shù) int *XB = new int[n + 1]; //用來標注基變量x的下標 float *b = new float[n + 1]; float *sigema = new float[n + 1]; float *sita = new float[n + 1]; //初始化 for (int i = 0; i <= m; i++) { cj[i] = 0; } for (int i = 0; i <= n; i++) { cB[i] = 0; XB[i] = 0; b[i] = 0; sigema[i] = 0; sita[i] = 0; } cout << "請輸入目標函數(shù)系數(shù)(用空格間開):" << endl; for (int i = 1; i <= m; i++) { cin >> cj[i]; } cout << "請輸入各不等式的系數(shù)和常量(用空格間開):" << endl; for (int i = 1; i <= n; i++) { cout << "不等式" << i << ": "; for (int j = 1; j <= m + 1; j++) { cin >> matrix[i][j]; } } cout << "請輸入目標函數(shù)中基變量下標:" << endl; for (int i = 1; i <= n; i++) { cin >> XB[i]; cB[i] = cj[XB[i]]; //常量 b[i] = matrix[i][m + 1]; } //計算檢驗數(shù) for (int i = 1; i <= m; i++) { sigema[i] = cj[i]; for (int j = 1; j <= n; j++) { sigema[i] -= cB[j] * matrix[j][i]; } } while (check(sigema, m) == 0) { //尋找入基變量 float maxn = sigema[1]; int sigema_xindex = 0; float sigema_xcoefficient = 0; for (int i = 1; i <= m; i++) { if (maxn <= sigema[i]) { maxn = sigema[i]; sigema_xindex = i; sigema_xcoefficient = cj[i]; } } //計算sita for (int i = 1; i <= n; i++) { if (matrix[i][sigema_xindex] > 0) { sita[i] = b[i] / matrix[i][sigema_xindex]; } else { sita[i] = 9999; //表示sita值為負數(shù) } } //尋找出基變量 float minn = sita[1]; int sita_xindex = 0; for (int i = 1; i <= n; i++) { if (minn >= sita[i] && sita[i] > 0) { minn = sita[i]; sita_xindex = i; } } //入基出基變換,先入基再出基 //入基操作 for (int i = 1; i <= n; i++) { if (i == sita_xindex) { XB[i] = sigema_xindex; cB[i] = sigema_xcoefficient; break; } } //出基計算 //化1 //cout << endl << "此處為化1的結(jié)果------" << endl; float mul1 = matrix[sita_xindex][sigema_xindex]; for (int i = 1; i <= m; i++) { matrix[sita_xindex][i] /= mul1; } b[sita_xindex] /= mul1; //化0 //cout << endl << "此處為化0的結(jié)果------" << endl; for (int i = 1; i <= n; i++) { if (i == sita_xindex) { continue; } float mul2 = matrix[i][sigema_xindex] / matrix[sita_xindex][sigema_xindex]; for (int j = 1; j <= m; j++) { matrix[i][j] -= (matrix[sita_xindex][j] * mul2); } b[i] -= (b[sita_xindex] * mul2); } for (int i = 1; i <= n; i++) { if (i == sita_xindex) { continue; } } for (int i = 1; i <= m; i++) { sigema[i] = cj[i]; for (int j = 1; j <= n; j++) { sigema[i] -= cB[j] * matrix[j][i]; } } } float MaxZ = 0; float *result = new float[m + 1]; for (int i = 0; i <= m; i++) { result[i] = 0; } for (int i = 1; i <= n; i++) { result[XB[i]] = b[i]; } cout << "最優(yōu)解為:X = ("; for (int i = 1; i < m; i++) { cout << result[i] << ","; } cout << result[m] << ")" << endl; for (int i = 1; i <= m; i++) { MaxZ += result[i] * cj[i]; } cout << "最優(yōu)值為:MzxZ = " << MaxZ; return 0; }
程序運行結(jié)果:
總結(jié)
到此這篇關于c++實現(xiàn)單純形法現(xiàn)行規(guī)劃問題的求解的文章就介紹到這了,更多相關c++單純形法內(nèi)容請搜索創(chuàng)新互聯(lián)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持創(chuàng)新互聯(lián)!