日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java实现算法导论中线性规划单纯形算法

發布時間:2025/4/16 java 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java实现算法导论中线性规划单纯形算法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

需在理解算法導論中線性規劃單純性算法基礎上理解Java實現的代碼,結合http://blog.csdn.net/fjssharpsword/article/details/53195556理解。

具體代碼如下:

package sk.mlib;import java.util.Random;/************************************************************************** Compilation: javac Simplex.java* Execution: java Simplex** Given an M-by-N matrix A, an M-length vector b, and an* N-length vector c, solve the LP { max cx : Ax <= b, x >= 0 }.* Assumes that b >= 0 so that x = 0 is a basic feasible solution.** Creates an (M+1)-by-(N+M+1) simplex tableaux with the * RHS in column M+N, the objective function in row M, and* slack variables in columns M through M+N-1.**************************************************************************/public class Simplex {private static final double EPSILON = 1.0E-10;private double[][] a; // tableauxprivate int M; // number of constraintsprivate int N; // number of original variablesprivate int[] basis; // basis[i] = basic variable corresponding to row i// only needed to print out solution, not book// sets up the simplex tableauxpublic Simplex(double[][] A, double[] b, double[] c) {M = b.length;N = c.length;a = new double[M+1][N+M+1];for (int i = 0; i < M; i++)for (int j = 0; j < N; j++)a[i][j] = A[i][j];for (int i = 0; i < M; i++) a[i][N+i] = 1.0;for (int j = 0; j < N; j++) a[M][j] = c[j];for (int i = 0; i < M; i++) a[i][M+N] = b[i];basis = new int[M];for (int i = 0; i < M; i++) basis[i] = N + i;solve();// check optimality conditionsassert check(A, b, c);//初始化判斷輸入的線性規劃是否存在初始基本可行解、對偶最優解判斷、無限解}// run simplex algorithm starting from initial BFSprivate void solve() {while (true) {// find entering column qint q = bland();if (q == -1) break; // optimal// find leaving row pint p = minRatioRule(q);if (p == -1) throw new RuntimeException("Linear program is unbounded");// pivotpivot(p, q);// update basisbasis[p] = q;}}// lowest index of a non-basic column with a positive costprivate int bland() {for (int j = 0; j < M + N; j++)if (a[M][j] > 0) return j;return -1; // optimal}// index of a non-basic column with most positive costprivate int dantzig() {int q = 0;for (int j = 1; j < M + N; j++)if (a[M][j] > a[M][q]) q = j;if (a[M][q] <= 0) return -1; // optimalelse return q;}// find row p using min ratio rule (-1 if no such row)private int minRatioRule(int q) {int p = -1;for (int i = 0; i < M; i++) {if (a[i][q] <= 0) continue;else if (p == -1) p = i;else if ((a[i][M+N] / a[i][q]) < (a[p][M+N] / a[p][q])) p = i;}return p;}// pivot on entry (p, q) using Gauss-Jordan elimination,主元操作,交換變量private void pivot(int p, int q) {// everything but row p and column qfor (int i = 0; i <= M; i++)for (int j = 0; j <= M + N; j++)if (i != p && j != q) a[i][j] -= a[p][j] * a[i][q] / a[p][q];// zero out column qfor (int i = 0; i <= M; i++)if (i != p) a[i][q] = 0.0;// scale row pfor (int j = 0; j <= M + N; j++)if (j != q) a[p][j] /= a[p][q];a[p][q] = 1.0;}// return optimal objective valuepublic double value() {return -a[M][M+N];}// return primal solution vectorpublic double[] primal() {double[] x = new double[N];for (int i = 0; i < M; i++)if (basis[i] < N) x[basis[i]] = a[i][M+N];return x;}// return dual solution vectorpublic double[] dual() {double[] y = new double[M];for (int i = 0; i < M; i++)y[i] = -a[M][N+i];return y;}// is the solution primal feasible?構造輔助線性規劃private boolean isPrimalFeasible(double[][] A, double[] b) {double[] x = primal();// check that x >= 0for (int j = 0; j < x.length; j++) {if (x[j] < 0.0) {System.out.println("x[" + j + "] = " + x[j] + " is negative");return false;}}// check that Ax <= bfor (int i = 0; i < M; i++) {double sum = 0.0;for (int j = 0; j < N; j++) {sum += A[i][j] * x[j];}if (sum > b[i] + EPSILON) {System.out.println("not primal feasible");System.out.println("b[" + i + "] = " + b[i] + ", sum = " + sum);return false;}}return true;}// is the solution dual feasible?構造對偶線性規劃private boolean isDualFeasible(double[][] A, double[] c) {double[] y = dual();// check that y >= 0for (int i = 0; i < y.length; i++) {if (y[i] < 0.0) {System.out.println("y[" + i + "] = " + y[i] + " is negative");return false;}}// check that yA >= cfor (int j = 0; j < N; j++) {double sum = 0.0;for (int i = 0; i < M; i++) {sum += A[i][j] * y[i];}if (sum < c[j] - EPSILON) {System.out.println("not dual feasible");System.out.println("c[" + j + "] = " + c[j] + ", sum = " + sum);return false;}}return true;}// check that optimal value = cx = ybprivate boolean isOptimal(double[] b, double[] c) {double[] x = primal();double[] y = dual();double value = value();// check that value = cx = ybdouble value1 = 0.0;for (int j = 0; j < x.length; j++)value1 += c[j] * x[j];double value2 = 0.0;for (int i = 0; i < y.length; i++)value2 += y[i] * b[i];if (Math.abs(value - value1) > EPSILON || Math.abs(value - value2) > EPSILON) {System.out.println("value = " + value + ", cx = " + value1 + ", yb = " + value2);return false;}return true;}private boolean check(double[][]A, double[] b, double[] c) {return isPrimalFeasible(A, b) && isDualFeasible(A, c) && isOptimal(b, c);}// print tableauxpublic void show() {System.out.println("M = " + M);System.out.println("N = " + N);for (int i = 0; i <= M; i++) {for (int j = 0; j <= M + N; j++) {System.out.printf("%7.2f ", a[i][j]);}System.out.println();}System.out.println("value = " + value());for (int i = 0; i < M; i++)if (basis[i] < N) System.out.println("x_" + basis[i] + " = " + a[i][M+N]);System.out.println();}public static void test(double[][] A, double[] b, double[] c) {Simplex lp = new Simplex(A, b, c);System.out.println("value = " + lp.value());double[] x = lp.primal();for (int i = 0; i < x.length; i++)System.out.println("x[" + i + "] = " + x[i]);double[] y = lp.dual();for (int j = 0; j < y.length; j++)System.out.println("y[" + j + "] = " + y[j]);}public static void test1() {double[][] A = {{ -1, 1, 0 },{ 1, 4, 0 },{ 2, 1, 0 },{ 3, -4, 0 },{ 0, 0, 1 },};double[] c = { 1, 1, 1 };double[] b = { 5, 45, 27, 24, 4 };test(A, b, c);}// x0 = 12, x1 = 28, opt = 800public static void test2() {double[] c = { 13.0, 23.0 };double[] b = { 480.0, 160.0, 1190.0 };double[][] A = {{ 5.0, 15.0 },{ 4.0, 4.0 },{ 35.0, 20.0 },};test(A, b, c);}// unboundedpublic static void test3() {double[] c = { 2.0, 3.0, -1.0, -12.0 };double[] b = { 3.0, 2.0 };double[][] A = {{ -2.0, -9.0, 1.0, 9.0 },{ 1.0, 1.0, -1.0, -2.0 },};test(A, b, c);}// degenerate - cycles if you choose most positive objective function coefficientpublic static void test4() {double[] c = { 10.0, -57.0, -9.0, -24.0 };double[] b = { 0.0, 0.0, 1.0 };double[][] A = {{ 0.5, -5.5, -2.5, 9.0 },{ 0.5, -1.5, -0.5, 1.0 },{ 1.0, 0.0, 0.0, 0.0 },};test(A, b, c);}// test clientpublic static void main(String[] args) {try { test1(); }catch (Exception e) { e.printStackTrace(); }System.out.println("--------------------------------");/*try { test2(); }catch (Exception e) { e.printStackTrace(); }System.out.println("--------------------------------");try { test3(); }catch (Exception e) { e.printStackTrace(); }System.out.println("--------------------------------");try { test4(); }catch (Exception e) { e.printStackTrace(); }System.out.println("--------------------------------");int M = Integer.parseInt(args[0]);int N = Integer.parseInt(args[1]);double[] c = new double[N];double[] b = new double[M];double[][] A = new double[M][N];for (int j = 0; j < N; j++)c[j] = new Random().nextInt(1000);for (int i = 0; i < M; i++)b[i] = new Random().nextInt(1000);for (int i = 0; i < M; i++)for (int j = 0; j < N; j++)A[i][j] = new Random().nextInt(100);Simplex lp = new Simplex(A, b, c);System.out.println(lp.value());*/}}
test1測試數據執行結果如下:

value = 22.0 x[0] = 9.000000000000002 x[1] = 9.0 x[2] = 4.0 y[0] = -0.0 y[1] = 0.1428571428571428 y[2] = 0.4285714285714286 y[3] = -0.0 y[4] = 1.0 --------------------------------

總結

以上是生活随笔為你收集整理的Java实现算法导论中线性规划单纯形算法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产精品毛片久久久久久久av | 日韩免费高清视频网站 | 精品久久久无码中文字幕 | 麻豆www.| av在线资源站 | 99久久精品免费 | 欧美性教育视频 | 美女又爽又黄又免费 | 日韩欧美视频网站 | 影音先锋成人资源 | 无码国产精品一区二区高潮 | 久久久www成人免费精品 | 日本亚洲最大的色成网站www | 国产一区a | 成人在线观看h | 亚洲 欧美 自拍偷拍 | 蜜桃久久久 | 亚洲自拍激情 | 黄色av网址大全 | 伊人成年综合网 | 91亚洲精品久久久蜜桃 | 懂色av一区二区三区免费观看 | 自拍偷拍亚洲天堂 | 国产精品日韩一区二区 | 国产一二在线观看 | 韩国三级中文字幕hd浴缸戏 | 国产看黄网站 | 久热国产精品 | 成人高清视频在线观看 | 国产日韩欧美在线观看视频 | 日日夜夜爱爱 | 国产成人啪一区二区 | 亚洲作爱网 | 国产露脸国语对白在线 | 高清视频在线播放 | 毛片在线网站 | 波多野结衣激情视频 | 无码成人精品区在线观看 | 精品一区二区免费视频 | 成人在线国产视频 | 操极品美女| 久草五月天 | 黑人精品无码一区二区三区AV | 亚洲综合二区 | 亚洲经典一区二区三区四区 | 色综合天天| 一区二区三区在线观看免费视频 | 久久精品黄色 | a级无遮挡超级高清-在线观看 | 爱爱视频日本 | 经典三级视频 | 天天透天天操 | 最新中文字幕免费 | 日本边添边摸边做边爱 | 青青青草国产 | 久久久久久视 | av官网| 欧美激情欧美激情在线五月 | 黄色大片免费在线观看 | 国产亚洲制服欧洲高清一区 | 少妇免费毛片久久久久久久久 | 波多野结衣视频网站 | 美女被捅个不停 | 日本中文在线 | 亚洲国产视频在线观看 | 久久久国产一区二区三区 | 青青久久国产 | 欧美第二区| 免费观看黄色一级视频 | av在线视 | av香蕉 | 欧美成人久久 | 亚洲欧美国产一区二区三区 | 苍井空浴缸大战猛男120分钟 | 天天插天天操 | 国产午夜精品一区二区三区 | 久久久久久国产精品一区 | a√天堂在线 | 亚洲欧美日本一区二区 | 亚洲久久一区 | 久久2018| 国产一区亚洲二区 | 影音av在线 | 免费av在线电影 | xxxxx色| 丰满人妻一区二区三区53 | 亚洲欧美一区二区三区在线观看 | 免费国产成人 | www.中文字幕在线观看 | 热久久精| 超碰98在线观看 | 超污视频软件 | 午夜视频福利网站 | 久久久亚洲 | 亚洲精品www久久久 一级aaa毛片 | 精品少妇人妻av一区二区三区 | 黄色肉肉视频 | 亚洲大胆视频 | 成人亚洲综合 |