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. [ActionScript 3.0] 对代码加密的有效方法

    package { import flash.display.Loader; import flash.display.Sprite; import flash.net.LocalConnection ...

  2. Activiti开启SQL Log

    log4j.logger.org.activiti.engine.impl.persistence.entity=trace

  3. PHP面向对象编程——深入理解方法重载与方法覆盖(多态)

    什么是多态? 多态(Polymorphism)按字面的意思就是“多种状态”.在面向对象语言中,接口的多种不同的实现方式即为多态.引用Charlie Calverts对多态的描述——多态性是允许你将父对 ...

  4. Sorry, but the Android VPN API doesn’t currently allow TAP-based tunnels.

    Sorry, but the Android VPN API doesn’t currently allow TAP-based tunnels. Edit .ovpn configfile “dev ...

  5. javascript实现原生ajax的方法

    <script> var xmlHttp; function createxmlHttpRequest() { if (window.ActiveXObject) { xmlHttp = ...

  6. VS2013环境问题

    1.多字节支持问题,多字节默认(GB2312格式),需要安装一个补丁: https://www.microsoft.com/zh-CN/download/confirmation.aspx?id=40 ...

  7. Esfog_UnityShader教程_UnityShader语法实例浅析

    距离上次首篇前言已经有一段时间了,一直比较忙,今天是周末不可以再拖了,经过我一段时间的考虑,我决定这一系列的教程会避免过于深入细节,一来可以避免一些同学被误导,二来会避免文章过于冗长难读, 三来可以让 ...

  8. flash的读写与擦除

    对于flash的认识,比较肤浅,在网上找了些资料,感谢 http://blog.csdn.net/lin364812726/article/details/18815395  的博主, 将其博文转载过 ...

  9. Window对象方法

    Window对象方法 scrollBy() 按照指定的像素值来滚动内容. scrollTo() 把内容滚动到指定的坐标. setInterval() 按照指定的周期(以毫秒计)来调用函数或计算表达式. ...

  10. .NET牛人应该知道些什么

    任何一个使用.NET的人 1.描述线程与进程的区别? 线程(Thread)与进程(Process)二者都定义了某种边界,不同的是进程定义的是应用程序与应用程序之间的边界,不同的进程之间不能共享代 码和 ...