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. Django 之 ForeignKey、ManyToMany的访问方式

    1.ForeignKey 情况I: from django.db import models class Blog(models.Model): pass class Entry(models.Mod ...

  2. 使用VS2010在Coding.net上进行代码托管

    网上有VS2010和Github结合使用办法,但是Github在国内使用太慢,本文使用相同的配置方法稍作改动让VS2010代码托管在coding.net平台上.由于只是稍做记录让自己不会遗忘,所以叙述 ...

  3. 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 ...

  4. 魅蓝note2在ubuntu14.04下mtp模式无法自动mount的解决方法

    是因为新机型没在列表里的原因. 处理方法如下: As far as I know, MTP works fine in Trusty. You can try this: Uncomment #use ...

  5. PHP命名空间的作用、为什么使用命名空间?

    命名空间一个最明确的目的就是解决重名问题,PHP中不允许两个函数或者类出现相同的名字,否则会产生一个致命的错误.这种情况下只要避免命名重复就可以解决,最常见的一种做法是约定一个前缀. 举个栗子:项目中 ...

  6. oozie调用shell

    <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agree ...

  7. jira的使用

    http://www.cnblogs.com/yangxia-test/p/4448002.html linux下的安装: http://blog.csdn.net/crave_shy/article ...

  8. 定制centos安装iso

    参考 https://gist.github.com/pauljeff/4b9ad551cb6c35870d7c https://www.redhat.com/archives/kickstart-l ...

  9. 实现ApplicationContextAware接口时,获取ApplicationContext为null

    将懒加载关闭,@Lazy(false),默认为true import org.springframework.beans.BeansException; import org.springframew ...

  10. Ubuntu更改鼠标灵敏度

    需要命令:xinput 清自行用 man xinput 查询 xinput 帮助文档 1.插入鼠标,打开终端,输入命令:xinput 查询当前已挂在设备 2.拔出鼠标,打开终端,再输入命令:xinpu ...