⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 probleminstandardform.java

📁 This is my implementation for linear programming
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        this.dict = getDictionary();        result += dict.toString();        this.table = new Table(dict, this);        if (!this.objective.isAuxiliary()) {            outVar = table.getOutVar();            inVar = inVar = table.getInVar(outVar);        } else {            inVar = table.getInvar();            outVar = table.getOutVar(inVar);        }        int count = 0;        while (outVar != null) {            count++;            dict = dict.getNewDictionaryByChangeVariableRoles(inVar, outVar);            this.updateSolution(inVar, outVar, dict);            result += "<p><b>" + outVar.toString() +                " sortie et " + inVar.toString() + " entre</b>" +                "</p></br />" + dict.toString();            result += "<p>" + this.getSolution() + "</p><br />";            table = new Table(dict, this);            if (!this.objective.isAuxiliary()) {                outVar = table.getOutVar();                inVar = inVar = table.getInVar(outVar);            } else {                inVar = table.getInvar();                outVar = table.getOutVar(inVar);            }        }        switch (this.answer) {            case Problem.HAS_INFINITVE_SOLUTIONS:                result += "<p>Infinitf solution</p>";                break;            case Problem.HAS_ONE_SOLUTION:                result += "<p>Has one solution</p>";                break;            case Problem.IS_UNBOUND:                result += "<p>Is unbound</p>";                break;            case Problem.NO_SOLUTION:                result += "<p>No solution</p>";                break;            default:                break;        }        return result;    } */    public void getAuxiliaryProblem() {        ArrayList<Variable> newVars = new ArrayList<Variable>();        ArrayList<Variable> newBaseVars = new ArrayList<Variable>();        ArrayList<Variable> newNonbaseVars = new ArrayList<Variable>();        ArrayList<Constraint> newConstraints = new ArrayList<Constraint>();        Objective newObjective = new Objective();        Fraction newObjValue = new Fraction(0);        for (int i = 0; i < this.vars.size(); i++)            newObjective.getCoefs().add(new Fraction(0));        for (int i = 0; i < this.vars.size(); i++) {            newVars.add(this.vars.get(i));//            newBaseVars.add(this.vars.get(i));        }                String prefix = "t";        int numOfNegativeConstraints = 0;        for (int i = 0; i < this.constraints.size(); i++)            if (this.nonbaseVars.get(i).getValue().isNegative()) {                for (int j = 0; j < this.constraints.get(i).getCoefs().size(); j++) {                    newObjective.getCoefs().set(j, Fraction.add(newObjective.getCoefs().get(j),                            this.constraints.get(i).getCoefs().get(j)));                }                newObjValue = Fraction.add(newObjValue, this.constraints.get(i).getValue());                numOfNegativeConstraints++;                Variable var = new Variable(new Fraction(0),                        numOfNegativeConstraints, prefix,                    ConstraintTypes.GREATER_THAN);                if (this.constraints.get(i).getValue().isNegative())                    var.setValue(                            this.constraints.get(i).getValue().getOpposite());                else                    var.setValue(                            this.constraints.get(i).getValue());            newVars.add(var);            newNonbaseVars.add(var);            }        int index = 0;        for (int i = 0; i < this.constraints.size(); i++) {            Constraint newConstraint = new Constraint();            Constraint constraint = this.constraints.get(i);            newConstraint.setVars(newVars);            for (int j = 0; j < constraint.getCoefs().size(); j++) {                newConstraint.getCoefs().add(constraint.getCoefs().get(j));            }            if (this.nonbaseVars.get(i).getValue().isNegative()) {                index++;                for (int j = 1; j < index; j++)                    newConstraint.getCoefs().add(new Fraction(0));                if (constraint.getValue().isNegative())                    newConstraint.getCoefs().add(new Fraction(-1));                else                    newConstraint.getCoefs().add(new Fraction(1));                for (int j = index + 1; j <= numOfNegativeConstraints; j++)                    newConstraint.getCoefs().add(new Fraction(0));            } else {                newNonbaseVars.add(this.nonbaseVars.get(i));                for (int j = 1; j <= numOfNegativeConstraints; j++)                    newConstraint.getCoefs().add(new Fraction(0));            }            newConstraint.setValue(constraint.getValue());            newConstraints.add(newConstraint);        }        newObjective.setVars(newVars);/*        for (int i = 0; i < vars.size(); i++)            newObjective.getCoefs().add(new Fraction(0)); */        for (int i = 1; i <= numOfNegativeConstraints; i++)            newObjective.getCoefs().add(new Fraction(0));        for (int i = 0; i < newObjective.getCoefs().size(); i++)            newObjective.getCoefs().set(i, newObjective.getCoefs().get(i).getOpposite());        newObjective.setType(Objective.MAXIMIZE);        newObjective.setValue(newObjValue);        newObjective.setIsAuxiliary(true);        /*for (int i = 0; i < newVars.size(); i++)            if (newVars.get(i).getPrefix().compareTo("t") == 0)                newVars.get(i).setValue(new Fraction(0));*/        for (int i = 0; i < newConstraints.size(); i++) {            if (newConstraints.get(i).getValue().isNegative()) {                for (int j = 0; j < newConstraints.get(i).getCoefs().size(); j++) {                    newConstraints.get(i).getCoefs().set(j,                            newConstraints.get(i).getCoefs().get(j).getOpposite());                }                newConstraints.get(i).setValue(newConstraints.get(i).getValue().getOpposite());            }        }        this.auxiliaryProblem =                new ProblemInStandardForm(newVars, newConstraints, newObjective);        for (int i = 0; i < this.vars.size(); i++)            if (!newNonbaseVars.contains(newVars.get(i))) {                newVars.get(i).setValue(new Fraction(0));                newBaseVars.add(newVars.get(i));            }        this.auxiliaryProblem.baseVars = newBaseVars;        this.auxiliaryProblem.nonbaseVars = newNonbaseVars;        this.auxiliaryProblem.dict = this.auxiliaryProblem.getDictionary();    }    public ProblemInStandardForm auxiliaryProb() {        return this.auxiliaryProblem;    }    public void updateAuxiliaryProblem() {        Objective newObjective = new Objective();        ArrayList<Fraction> newCoefs = new ArrayList<Fraction>();        newObjective.setType(Objective.MINIMIZE);        newObjective.setVars(this.vars);        for (int i = 0; i < this.vars.size(); i++)            newCoefs.add(new Fraction(0));        for (int i = 0; i < this.constraints.size(); i++) {                    }    }    public Dictionary getDict() {        return this.dict;    }    public void updateDict(ProblemInStandardForm auxiliaryProblem) {        Dictionary AuxDict = auxiliaryProblem.getDict();        Dictionary newDict = new Dictionary();        newDict.setObjective(this.objective);        for (int i = 0; i < AuxDict.getVarConstraints().size(); i++) {            VariableConstraint varConstraint = AuxDict.getVarConstraints().get(i);            Variable newLeftVar = varConstraint.getLeftVar();            ArrayList<Variable> newRightVars = new ArrayList<Variable>();            ArrayList<Fraction> newCoefs = new ArrayList<Fraction>();            Fraction newValue = varConstraint.getValue();            for (int j = 0; j < varConstraint.getRightVars().size(); j++) {                if (varConstraint.getRightVars().get(j).getPrefix().compareTo("t") != 0) {                    newCoefs.add(varConstraint.getCoefs().get(j));                    newRightVars.add(varConstraint.getRightVars().get(j));                }            }            newDict.getVarConstraints().add(new VariableConstraint(newLeftVar,                    newRightVars, newCoefs, newValue));        }        this.dict = newDict;   //     System.out.println(this.dict.toString());        this.nonbaseVars = auxiliaryProblem.nonbaseVars;        this.baseVars = new ArrayList<Variable>();        for (int i = 0; i < this.vars.size(); i++)            if (!this.nonbaseVars.contains(this.vars.get(i)))                this.baseVars.add(this.vars.get(i));        ArrayList<Fraction> newObjCoefs = new ArrayList<Fraction>();        ArrayList<Variable> newObjVars = new ArrayList<Variable>();        for (int i = 0; i < this.vars.size(); i++)            newObjCoefs.add(new Fraction(0));        ArrayList<Variable> tmpVars = new ArrayList<Variable>();        Fraction newObjValue = new Fraction(0);        for (int i = 0; i < this.objective.getCoefs().size(); i++) {            if (!this.objective.getCoefs().get(i).equals(new Fraction(0)))                tmpVars.add(this.objective.getVars().get(i));        }        for (int i = 0; i < newDict.getVarConstraints().size(); i++) {            if (tmpVars.contains(newDict.getVarConstraints().get(i).getLeftVar())) {                VariableConstraint varConstraint = newDict.getVarConstraints().get(i);                Fraction coef = objective.getCoef(                    newDict.getVarConstraints().get(i).getLeftVar());                newObjValue = Fraction.add(newObjValue,                        Fraction.multiple(                        newDict.getVarConstraints().get(i).getValue(), coef));                for (int j = 0; j < varConstraint.getCoefs().size(); j++) {                    newObjCoefs.set(j, Fraction.add(newObjCoefs.get(j),                            Fraction.multiple(varConstraint.getCoefs().get(j), coef)));                    if (!newObjVars.contains(varConstraint.getRightVars().get(j)))                    newObjVars.add(varConstraint.getRightVars().get(j));                }            }        }        System.out.println("-----------");        for (int i = 0; i < newObjVars.size(); i++)            System.out.printf("%20s", newObjVars.get(i));        System.out.println();        for (int i = 0; i < newObjCoefs.size(); i++)            System.out.printf("%20s", newObjCoefs.get(i));        System.out.println("-----------");        System.out.println("!!!!!!!!!" + newObjValue);        this.objective = new Objective(Objective.MAXIMIZE, newObjCoefs, newObjVars, newObjValue);        this.dict.setObjective(this.objective);        System.out.println(this.objective.toString());    }    public void setDict() {        this.dict = getDictionary();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -