给高分,线性规划含参问题问题用Java怎么编

java 线性规划 和lingo 比较 - cndavy - 博客园
model:max=13*A+ 23*B;
5*A + 15*B &480 ;
4*A + 4 *B &160 ;
35* A + 20 *B &1190
Reduced Cost
Slack or Surplus
Dual Price
public static void test0() {
double[][] A = {
double[] c = { 13, 23 };
double[] b = { 480,160,1190};
test(A, b, c);}
value = 800.0x[0] = 11.998x[1] = 28.0y[0] = 1.0y[1] = 2.0y[2] = -0.0
package 线性规划;
* Created by han on .
import java.io.PrintS
/*************************************************************************
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[][]
// tableaux
private int M;
// number of constraints
private int N;
// number of original variables
private int[]
// basis[i] = basic variable corresponding to row i
private static PrintStream StdOut=System.
// only needed to print out solution, not book
// sets up the simplex tableaux
public Simplex(double[][] A, double[] b, double[] c) {
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]
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 +
// 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(p, q);
// update basis
basis[p] =
// 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
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 =
if (a[M][q] &= 0) return -1;
// optimal
else return
// 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 =
else if ((a[i][M+N] / a[i][q]) & (a[p][M+N] / a[p][q])) 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 dual solution vector
public double[] dual() {
double[] y = new double[M];
for (int i = 0; i & M; i++)
y[i] = -a[M][N+i];
// 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. 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. 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. j++)
value1 += c[j] * x[j];
double value2 = 0.0;
for (int i = 0; i & y. 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. i++)
StdOut.println("x[" + i + "] = " + x[i]);
double[] y = lp.dual();
for (int j = 0; j & y. j++)
StdOut.println("y[" + j + "] = " + y[j]);
public static void test0() {
double[][] A = {
double[] c = { 13, 23 };
double[] b = { 480,160,1190};
test(A, b, c);
public static void test1() {
double[][] A = {
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 = {
double[] b = { 480.0, 160.0, 1190.0 };
double[][] A = {
5.0, 15.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 = {
double[][] A = {
{ -2.0, -9.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 = {
double[][] A = {
{ 0.5, -5.5, -2.5, 9.0 },
{ 0.5, -1.5, -0.5, 1.0 },
0.0, 0.0 },
test(A, b, c);
// test client
public static void main(String[] args) {
{ test0();
catch (Exception e) { e.printStackTrace(); }
StdOut.println("--------------------------------");
{ test1();
catch (Exception e) { e.printStackTrace(); }
StdOut.println("--------------------------------");
{ test2();
catch (Exception e) { e.printStackTrace(); }
StdOut.println("--------------------------------");
{ test3();
catch (Exception e) { e.printStackTrace(); }
StdOut.println("--------------------------------");
{ 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求线性规划_百度知道
用JAVA求线性规划
求JAVA解线性规划
∑xy=387.5
要求输出以下结果
b=3.2429, a=4.3999
The regression equation for this data is therefore:
我有更好的答案
太深奥了,不理解什么意思,无从下手
为您推荐:
其他类似问题
您可能关注的内容
线性规划的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。电子笔记吧~
线性约束规划问题java求解
Linear programming.Linear programming solvers. is a bare-bones versionof the simplex algorithm.is a Java linear program solver created by David Applegate, William Cook,Sanjeeb Dash, and Monika Mevenkamp.It can be used at no cost for research or education purposes. solves a linear programin LP format, such as .contains a linear programming solver in the optimization toolbox.[wayne:tombstone] ~& matlab
& M A T L A B (R) &
The MathWorks, Inc.
Version 7.9.0.529 (R2009b) 64-bit (glnxa64)
August 12, 2009
&& A = [5 15; 4 4; 35 20];
&& b = [480; 160; 1190];
&& c = [13; 23];
&& lb = [0; 0];
&& ub = [ inf];
&& x = linprog(-c, A, b, [], [], lb, ub)
is a high-performance mathematical programming solverfor linear programming, mixed integer programming, and quadratic programming.It supports interfaces to C, C++, Java, Python, Matlab, and Microsoft Excel.It is also accessible via the modeling systems includingAIMMS, AMPL, GAMS, and MPL. is a modeling language for mathematical programming.The files
and specify the model and data for the brewery problem.[wayne:tombstone] ~& ampl
ILOG AMPL 9.100
AMPL Version
(SunOS 5.8)
ampl: model beer.
ampl: data beer.
ILOG CPLEX 9.100
CPLEX 9.1.0: objective 800
2 dual simplex iterations (1 in phase I)
ampl: display constraints.
Microsoft Excel has a primitive solver add-in for Windows, but it isno longer available for Mac.Below is the syntax highlighted version of from ./*************************************************************************
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[][]
// tableaux
private int M;
// number of constraints
private int N;
// number of original variables
private int[]
// basis[i] = basic variable corresponding to row i
// only needed to print out solution, not book
// sets up the simplex tableaux
public Simplex(double[][] A, double[] b, double[] c) {
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]
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 +
// 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)
// optimal
// find leaving row p
int p = minRatioRule(q);
if (p == -1) throw new RuntimeException("Linear program is unbounded");
pivot(p, q);
// update basis
basis[p] =
// 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 -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 =
if (a[M][q] &= 0) return -1;
// optimal
// 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)
else if (p == -1) p =
else if ((a[i][M+N] / a[i][q]) & (a[p][M+N] / a[p][q])) 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 dual solution vector
public double[] dual() {
double[] y = new double[M];
for (int i = 0; i & M; i++)
y[i] = -a[M][N+i];
// 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. j++) {
if (x[j] & 0.0) {
StdOut.println("x[" + j + "] = " + x[j] + " is negative");
// 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);
// 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. i++) {
if (y[i] & 0.0) {
StdOut.println("y[" + i + "] = " + y[i] + " is negative");
// 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);
// 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. j++)
value1 += c[j] * x[j];
double value2 = 0.0;
for (int i = 0; i & y. 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);
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. i++)
StdOut.println("x[" + i + "] = " + x[i]);
double[] y = lp.dual();
for (int j = 0; j & y. j++)
StdOut.println("y[" + j + "] = " + y[j]);
public static void test1() {
double[][] A = {
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 = {
double[] b = { 480.0, 160.0, 1190.0 };
double[][] A = {
5.0, 15.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 = {
double[][] A = {
{ -2.0, -9.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 = {
double[][] A = {
{ 0.5, -5.5, -2.5, 9.0 },
{ 0.5, -1.5, -0.5, 1.0 },
0.0, 0.0 },
test(A, b, c);
// test client
public static void main(String[] args) {
{ test1();
catch (Exception e) { e.printStackTrace(); }
StdOut.println("--------------------------------");
{ test2();
catch (Exception e) { e.printStackTrace(); }
StdOut.println("--------------------------------");
{ test3();
catch (Exception e) { e.printStackTrace(); }
StdOut.println("--------------------------------");
{ 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());
没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!> 问题详情
写出下列线性规划问题的对偶问题:
(1)max z=2x1+x2+x3-x4
(2)max z=4x1+5x2+3x3+6x4
悬赏:0&答案豆
提问人:匿名网友
发布时间:
写出下列线性规划问题的对偶问题:&&(1)max z=2x1+x2+x3-x4&&&&(2)max z=4x1+5x2+3x3+6x4&&&&(3)&&&&(4)&&
我有更好的答案
请先输入下方的验证码查看最佳答案
图形验证:
验证码提交中……
每天只需0.4元
选择支付方式
支付宝付款
郑重提醒:支付后,系统自动为您完成注册
请使用微信扫码支付(元)
支付后,系统自动为您完成注册
遇到问题请联系在线客服QQ:
恭喜你被选中为
扫一扫-免费查看答案!
请您不要关闭此页面,支付完成后点击支付完成按钮
遇到问题请联系在线客服QQ:
恭喜您!升级VIP会员成功
提示:请截图保存您的账号信息,以方便日后登录使用。
常用邮箱:
用于找回密码
确认密码:java如何调用cplex
java如何调用cplex
  Cplex是IBM公司的一个优化软件,可以用来求解线性规划(Linear Programming,LP),二次规划(Quadratic Programming,QP)、混合整数规划(Mixed Integer Programming,MIP)问题。下面就让学习啦小编给大家说说java如何调用cplex吧。
  java调用cplex的方法
  首先需要安装Cplex软件,我安装的版本是cplex_studio122.win-x86-32.exe
  下图安装后打开的Cplex自带的IDE,看上去跟Eclipse差不多。
  在Cplex的安装目录下有许多值得我们学习的东西,还有一些examples,可供我们参考。
  我是在Eclipse中使用Java调用Cplex,所以先把一些Cplex依赖加上。
  运行依赖:cplex.jar(在..\cplex\lib目录下找到)和cplex122.dll(在..\cplex\bin目录下找到)。将cplex.jar加到工程的Build Path中。
  cplex122.dll可以设置到运行时的环境中(VM arguments),或者添加到项目的Native library location。
  接下来我们求解一个具体的线性规划问题。
  例如,我们求解下面这样一个线性规划问题:
  Maximize x1 + 2x2 + 3x3
  subject to
  -x1 + x2 + x3 ≦20
  x1 - 3x2 + x3 ≦30
  with these bounds
  0 ≦x1 ≦40
  0 ≦x2 ≦+&
  0≦ x3≦ +&
  先创建一个IloCplex对象,它是用来创建所有建模对象所需要的模型。此时会抛出一个异常:IloException,需要try\catch。
  代码如下:static public class Application {
  static public main(String[] args) {
  IloCplex cplex = new IloCplex();
  // create model and solve it
  } catch (IloException e) {
  System.err.println(&Concert exception caught: & + e);
  定义决策变量:double[] lb = {0.0, 0.0, 0.0};
  double[] ub = {40.0, Double.MAX_VALUE, Double.MAX_VALUE};
  IloNumVar[] x = cplex.numVarArray(3, lb, ub);
  定义目标函数:
  IloNumExpr expr = cplex.sum(x[0], cplex.prod(2.0, x[1]),cplex.prod(3.0, x[2]));
  cplex.addMaximize(expr);
  其中这个地方有许多写法,大家在使用的时候可以注意一下。
  定义决策的约束条件:cplex.addLe(cplex.sum(cplex.negative(x[0]), x[1], x[2]), 20);cplex.addLe(cplex.sum(cplex.prod(1, x[0]), cplex.prod(-3, x[1]),cplex.prod(1, x[2])), 30);
  最后解决模型问题:if(cplex.solve()){....}
  如果solve()返回true的话,我们可以获取一些信息,例如问题的解决状态、获取方案的目标值、获取数组中的所有决策变量的解值。
  cplex.getStatus()返回值类型:Error、Unknown、Feasible、Bounded、Optimal、Infeasible、Unbouded、InfeasibleorUnbounded。
  获取方案的目标值:double objval = cplex.getObjValue();获取数组中的所有决策变量的解值:double[] xval = cplex.getValues(x);
  运行程序最后控制台的输出结果如下所示:
猜你感兴趣的:
【猜您感兴趣】
学习DOS命令就需要用到命令提示符,命令提示符对我们操作电脑带来了很多便利,想要成为电脑高手不会使用命令提示符怎么行呢,下面就让学习啦小编教大家怎么在最...
当我们的电脑误装了有毒的软件或是很多自己不需要的垃圾软件又卸载不掉时,我们就可以选择用系统还原的方式将它们清除掉,防止那些无用的软件影响我们正常使用电...
我们都知道ubuntu是一款linux系统,它不像WINDOWS系统,它是一个开源的系统,它随时都在更新它系统,所以人们都说Linux系统要比WINDOWS系统安全。下面就让学习啦...
有时候一些程序员,或者网络人士在进行电脑维护和调试时需要查看电脑目前的进程详细信息,下面就让学习啦小编给大家说说如何使用DOS命令查看电脑进程信息吧。 使...
之前隐藏了系统文件夹,时间久了就忘了,该怎么办呢?那么win10怎么显示隐藏的系统文件夹呢?下面是学习啦小编收集整理的win10怎么显示隐藏的系统文件夹,希望对大家...
创建新用户是件很容易的事情,那如果使用命令创建新用户呢,该怎么办呢,下面是学习啦小编收集整理的如何使用命令创建新用户,希望对大家有帮助~~ 使用命令创建...

我要回帖

更多关于 线性规划对偶问题 的文章

 

随机推荐