Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 最小化单纯形法_Java_Algorithm_Math_Simplex - Fatal编程技术网

Java 最小化单纯形法

Java 最小化单纯形法,java,algorithm,math,simplex,Java,Algorithm,Math,Simplex,我在这里找到了关于单纯形法的话题 但答案并没有帮助。当我从 double[] variables = { 13.0, 23.0 }; 到 这个程序不计算(没有例外),它只打印第一步,仅此而已。 有人能帮我把单纯形法从最大化改为最小化吗 代码: 导入java.util.* public class Simplex { private static final double EPSILON = 1.0E-10; private double[][] tableaux; private int

我在这里找到了关于单纯形法的话题 但答案并没有帮助。当我从

double[] variables = {  13.0,  23.0 };

这个程序不计算(没有例外),它只打印第一步,仅此而已。 有人能帮我把单纯形法从最大化改为最小化吗

代码:

导入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;
        }
    }
公共类单纯形
{
专用静态最终双ε=1.0E-10;
私人双[][]表;
私有约束;
私有变量;
私人基础;
/**
*单纯形类对象的构造函数
*/
公共单纯形()
{
双[][]此表格={
{  5.0, 15.0 },
{  4.0,  4.0 },
{ 35.0, 20.0 },
};
双[]约束={480.0,160.0,1190.0};
双[]变量={-13.0,-23.0};
numOfConstraints=constraints.length;
numOfVariables=variables.length;
tableaux=新的双精度[numOfConstraints+1][numOfVariables+numOfConstraints+1];
//将此tableaux中的所有元素添加到tableaux
对于(int i=0;i0){
返回i;
}
}
返回-1;
}
public int findIndexOfLowestNonBasicCol(){
int q=0;
对于(int i=1;itableaux[numOfConstraints][q]){
q=i;
}
}

if(tableaux[numOfConstraints][q]我猜程序什么也没做,因为初始解决方案是最佳解决方案。

您可能需要研究(或)。如果原始问题的标准形式是:

Maximize = 13*X1 + 23*X2;
有限制:

5*X1    +   15*X2   <= 480;
4*X1    +   4*X2    <= 160;
35*X1   +   20*X2   <= 1190;
X1 >= 0;
X2 >= 0;
5*Y1    +   4*Y2    +   35*Y3   >= 13;
15*Y1 +     4*Y2    +   20*Y3   >= 23;
Y1 >= 0;
Y2 >= 0;
Y3 >= 0;
有限制:

5*X1    +   15*X2   <= 480;
4*X1    +   4*X2    <= 160;
35*X1   +   20*X2   <= 1190;
X1 >= 0;
X2 >= 0;
5*Y1    +   4*Y2    +   35*Y3   >= 13;
15*Y1 +     4*Y2    +   20*Y3   >= 23;
Y1 >= 0;
Y2 >= 0;
Y3 >= 0;

我在中测试了这两个问题,得到了相同的答案(Z=800,X1=12,X2=28--Y1=1,Y2=2,Y3=0).

你的代码在哪里?你只想用
-1
将它们叠加起来。欢迎使用StackOverflow。你必须意识到我们无法读懂你的心思。你需要包含足够的信息,以便我们可以重现你遇到的问题,你应该提供你期望的答案。当你说
时,不要计算
,你必须这样做说明它的作用。我还建议您尝试调试您的程序,因为这通常比解释问题的细节要快。调试后,我发现将表单(13,23)更改为(-13,-23)没有帮助。此问题与方法findLowestNonBasicCol()有关,存在时检查正数,如果(tableaux[numOfConstraints][i]>0)我尝试另一个变量,它具有最佳结果…但程序仅显示第一步