model:
max=13*A+ 23*B;

5*A + 15*B <480 ;
4*A + 4 *B <160 ;
35* A + 20 *B <1190 ;

end

Variable Value Reduced Cost
A 12.00000 0.000000
B 28.00000 0.000000

Row Slack or Surplus Dual Price
1 800.0000 1.000000
2 0.000000 1.000000
3 0.000000 2.000000
4 210.0000 0.000000

public static void test0() {
double[][] A = {
{ 5, 15 },
{ 4, 4 },
{ 35, 20 } };
double[] c = { 13, 23 };
double[] b = { 480,160,1190};
test(A, b, c);
}

value = 800.0
x[0] = 11.999999999999998
x[1] = 28.0
y[0] = 1.0
y[1] = 2.0
y[2] = -0.0

package 线性规划;

/**
* Created by han on 2016/5/5.
*/ import java.io.PrintStream; /*************************************************************************
* 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; // tableaux
private int M; // number of constraints
private int N; // number of original variables private int[] basis; // basis[i] = basic variable corresponding to row i
private static PrintStream StdOut=System.out;
// only needed to print out solution, not book // sets up the simplex tableaux
public 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 conditions
assert check(A, b, c);
} // run simplex algorithm starting from initial BFS
private void solve() {
while (true) { // find entering column q
int q = bland();
if (q == -1) break; // optimal // find leaving row p
int p = minRatioRule(q);
if (p == -1) throw new RuntimeException("Linear program is unbounded"); // pivot
pivot(p, q); // update basis
basis[p] = q;
}
} // lowest index of a non-basic column with a positive cost
private 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 cost
private 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; // optimal
else 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 q
for (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 q
for (int i = 0; i <= M; i++)
if (i != p) a[i][q] = 0.0; // scale row p
for (int j = 0; j <= M + N; j++)
if (j != q) a[p][j] /= a[p][q];
a[p][q] = 1.0;
} // return optimal objective value
public double value() {
return -a[M][M+N];
} // return primal solution vector
public 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 vector
public 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 >= 0
for (int j = 0; j < x.length; j++) {
if (x[j] < 0.0) {
StdOut.println("x[" + j + "] = " + x[j] + " is negative");
return false;
}
} // check that Ax <= b
for (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) {
StdOut.println("not primal feasible");
StdOut.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 >= 0
for (int i = 0; i < y.length; i++) {
if (y[i] < 0.0) {
StdOut.println("y[" + i + "] = " + y[i] + " is negative");
return false;
}
} // check that yA >= c
for (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) {
StdOut.println("not dual feasible");
StdOut.println("c[" + j + "] = " + c[j] + ", sum = " + sum);
return false;
}
}
return true;
} // check that optimal value = cx = yb
private boolean isOptimal(double[] b, double[] c) {
double[] x = primal();
double[] y = dual();
double value = value(); // check that value = cx = yb
double 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) {
StdOut.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 tableaux
public void show() {
StdOut.println("M = " + M);
StdOut.println("N = " + N);
for (int i = 0; i <= M; i++) {
for (int j = 0; j <= M + N; j++) {
StdOut.printf("%7.2f ", a[i][j]);
}
StdOut.println();
}
StdOut.println("value = " + value());
for (int i = 0; i < M; i++)
if (basis[i] < N) StdOut.println("x_" + basis[i] + " = " + a[i][M+N]);
StdOut.println();
} public static void test(double[][] A, double[] b, double[] c) {
Simplex lp = new Simplex(A, b, c);
StdOut.println("value = " + lp.value());
double[] x = lp.primal();
for (int i = 0; i < x.length; i++)
StdOut.println("x[" + i + "] = " + x[i]);
double[] y = lp.dual();
for (int j = 0; j < y.length; j++)
StdOut.println("y[" + j + "] = " + y[j]);
}
public static void test0() {
double[][] A = {
{ 5, 15 },
{ 4, 4 },
{ 35, 20 } };
double[] c = { 13, 23 };
double[] b = { 480,160,1190};
test(A, b, c);
}
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 = 800
public 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);
} // unbounded
public 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 coefficient
public 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 client
public static void main(String[] args) {
try { test0(); }
catch (Exception e) { e.printStackTrace(); }
StdOut.println("--------------------------------"); try { test1(); }
catch (Exception e) { e.printStackTrace(); }
StdOut.println("--------------------------------"); try { test2(); }
catch (Exception e) { e.printStackTrace(); }
StdOut.println("--------------------------------"); try { test3(); }
catch (Exception e) { e.printStackTrace(); }
StdOut.println("--------------------------------"); try { test4(); }
catch (Exception e) { e.printStackTrace(); }
StdOut.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] = StdRandom.uniform(1000);
for (int i = 0; i < M; i++)
b[i] = StdRandom.uniform(1000);
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
A[i][j] = StdRandom.uniform(100);
Simplex lp = new Simplex(A, b, c);
StdOut.println(lp.value());
} private static class StdRandom {
public static double uniform(int i) {
return Math.random()*i;
}
}
}

java 线性规划 和lingo 比较的更多相关文章

  1. Asynchronous calls and remote callbacks using Lingo Spring Remoting

    http://www.jroller.com/sjivan/entry/asynchronous_calls_and_callbacks_using Asynchronous calls and re ...

  2. Lingo (Spring Remoting) : Passing client credentials to the server

    http://www.jroller.com/sjivan/entry/lingo_spring_remoting_passing_client Lingo (Spring Remoting) : P ...

  3. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  4. 使用python scipy.optimize linprog和lingo线性规划求解最大值,最小值(运筹学学习笔记)

    1.线性规划模型: 2.使用python scipy.optimize linprog求解模型最优解: 在这里我们用到scipy中的linprog进行求解,linprog的用法见https://doc ...

  5. Lingo求解线性规划案例4——下料问题

    凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 造纸厂接到定单,所需卷纸的宽度和长度如表 卷纸的宽度 长度 5 7 9 10000 30000 20000 工 ...

  6. Lingo求解线性规划案例1——生产计划问题

    凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 说明: Lingo版本:                            某工厂明年根据合同,每个季度末 ...

  7. Lingo简单入门,以及对线性规划做敏感性分析设置

    Lingo中用!表示注释,注释结束用;表示,lingo不区分大小写,运行时会自动统一装换成大写 编程步骤: 1.推算出正确的模型 2.确定描述集,定义集合 3.确定变量 4.正确写出每个式子 常用函数 ...

  8. LinGo:疏散问题——线性规划,0-1规划

    个部门(A.B.C.D.E)组成.现要将它的几个部门迁出甲市,迁至乙市或丙市. (每个城市最多接纳三个部门) 除去因政府鼓励这样做以外,还有用房便宜,招工方便等好处.对这些好处已作出数量估计,其值如下 ...

  9. LinGo:装货问题——线性规划,整数规划,1988年美国数模B题

    7种规格的包装箱要装有两辆铁路平板车上去,包装箱的宽和高相同,但厚度(t,以cm计)和重量(以kg计)不同, 表A-1给出了每包装箱的厚度.重量和数量,每辆车有10.2m长的地方用来装包装箱(像面包片 ...

随机推荐

  1. HTTP 协议详解

    相关文章:HTTP 协议之压缩 当今web程序的开发技术真是百家争鸣,ASP.NET, PHP, JSP,Perl, AJAX 等等. 无论Web技术在未来如何发展,理解Web程序之间通信的基本协议相 ...

  2. 转 Dynamics CRM Alert and Notification JavaScript Methods

    http://www.powerobjects.com/2015/09/23/dynamics-crm-alert-and-notification-javascript-methods/ Befor ...

  3. PSP(11.16~11.23)

    18号 类别c 内容c 开始时间s 结束e 中断I 净时间T 看书 构建之法 9:00 10:00 0 60m 看书 查资料 10:00 11:15 5 70m 个人 写博客 13:30 14:55 ...

  4. 利用Continuous Testing实现Eclipse环境自动单元测试

    当你Eclipse环境中修改项目中的某个方法时,你可能由于各种原因没有运行单元测试,结果代码提交,悲剧就可能随之而来. 所幸infinitest(http://infinitest.github.io ...

  5. LeetCode 168. Excel Sheet Column Title

    Given a positive integer, return its corresponding column title as appear in an Excel sheet. -> A ...

  6. Sublime Text

    今天在网上找了个Sublime Text 3 Build 3065 的 license key .在最新的Build 3083 下可以使用,记录下. ----- BEGIN LICENSE ----- ...

  7. JQuery -- this 和 $(this) 的区别

    this和$(this)都是指的调用它的东西,但this是一个Html 元素,例如给img标签设置src属性可以这样写 this.src= "test.jpg"; 但是如果将thi ...

  8. Python格式化字符串和转义字符

    地址:http://blog.chinaunix.net/uid-20794157-id-3038417.html Python格式化字符串的替代符以及含义     符   号     说     明 ...

  9. JS-为金额添加千分位逗号分割符

    前言:这个功能在前端页面中使用的还是比较多的,正好我们的项目中也有使用此功能,不过YY同学写的代码不像个方法的样子,一个入口中间又插了几道子,所             以,我写了下面这个方法,经过测 ...

  10. PHP操作MongoDB学习笔记

    <?php/*** PHP操作MongoDB学习笔记*///*************************//**   连接MongoDB数据库  **////*************** ...