我在这里找到关于Simplex方法的主题Alter Simplex Algorithm to Minimize on objective function NOT maximize 但回答并没有帮助。当我改变
double[] variables = { 13.0, 23.0 };
到
double[] variables = { -13.0, -23.0 };
程序不计算(没有例外),它打印第一步,这就是全部。 有人可以帮助我改变单纯形法从最大化到最小化吗?
代码:
import java.util。*;
public class Simplex
{
private static final double EPSILON = 1.0E-10;
private double[][] tableaux;
private int numOfConstraints;
private int numOfVariables;
private int[] basis;
/**
* Constructor for objects of class Simplex
*/
public Simplex()
{
double[][] thisTableaux = {
{ 5.0, 15.0 },
{ 4.0, 4.0 },
{ 35.0, 20.0 },
};
double[] constraints = { 480.0, 160.0, 1190.0 };
double[] variables = { -13.0, -23.0 };
numOfConstraints = constraints.length;
numOfVariables = variables.length;
tableaux = new double[numOfConstraints+1][numOfVariables+numOfConstraints+1];
//adds all elements from thisTableaux to tableaux
for(int i=0; i < numOfConstraints; i++)
{
for(int j=0; j < numOfVariables; j++)
{
tableaux[i][j] = thisTableaux[i][j];
}
}
//adds a slack variable for each variable there is and sets it to 1.0
for(int i=0; i < numOfConstraints; i++)
{
tableaux[i][numOfVariables+i] = 1.0;
}
//adds variables into the second [] of tableux
for(int j=0; j < numOfVariables; j++)
{
tableaux[numOfConstraints][j] = variables[j];
}
//adds constraints to first [] of tableaux
for(int k=0; k < numOfConstraints; k++)
{
tableaux[k][numOfConstraints+numOfVariables] = constraints[k];
}
basis = new int[numOfConstraints];
for(int i=0; i < numOfConstraints; i++)
{
basis[i] = numOfVariables + i;
}
show();
optimise();
assert check(thisTableaux, constraints, variables);
}
public void optimise() {
while(true) {
int q = findLowestNonBasicCol();
if(q == -1) {
break;
}
int p = getPivotRow(q);
if(p == -1) throw new ArithmeticException("Linear Program Unbounded");
pivot(p, q);
basis[p] = q;
}
}
public int findLowestNonBasicCol() {
for(int i=0; i < numOfConstraints + numOfVariables; i++)
{
if(tableaux[numOfConstraints][i] > 0) {
return i;
}
}
return -1;
}
public int findIndexOfLowestNonBasicCol() {
int q = 0;
for(int i=1; i < numOfConstraints + numOfVariables; i++)
{
if(tableaux[numOfConstraints][i] > tableaux[numOfConstraints][q]) {
q = i;
}
}
if(tableaux[numOfConstraints][q] <= 0) {
return -1;
}
else {
return q;
}
}
/**
* Finds row p which will be the pivot row using the minimum ratio rule.
* -1 if there is no pivot row
*/
public int getPivotRow(int q) {
int p = -1;
for(int i=0; i < numOfConstraints; i++) {
if (tableaux[i][q] <=0) {
continue;
}
else if (p == -1) {
p = i;
}
else if((tableaux[i][numOfConstraints+numOfVariables] / tableaux[i][q] < tableaux[p][numOfConstraints+numOfVariables] / tableaux[p][q])) {
p = i;
}
}
答案 0 :(得分:1)
您可能需要查看Dual Simplex Method(或Duality Theory)。如果原始问题的标准形式是:
Maximize = 13*X1 + 23*X2;
有约束:
5*X1 + 15*X2 <= 480;
4*X1 + 4*X2 <= 160;
35*X1 + 20*X2 <= 1190;
X1 >= 0;
X2 >= 0;
然后双重问题是:
Minimize = 480*Y1 + 160*Y2 + 1190*Y3;
有约束:
5*Y1 + 4*Y2 + 35*Y3 >= 13;
15*Y1 + 4*Y2 + 20*Y3 >= 23;
Y1 >= 0;
Y2 >= 0;
Y3 >= 0;
我在LINGO中测试了这两个问题并得到了相同的答案(Z = 800,X1 = 12,X2 = 28 - Y1 = 1,Y2 = 2,Y3 = 0)。 / p>
答案 1 :(得分:-1)
我想程序什么也没做,因为最初的解决方案是最佳解决方案。