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

📄 variableconstraint.java

📁 Create a problem solver for linear programming
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package ro.simplex;import java.util.ArrayList;import ro.utils.Fraction;/** * * @author Doan Chien Thang */public class VariableConstraint {    private Variable leftVar;    private ArrayList<Variable> rightVars;    private ArrayList<Fraction> coefs;    private Fraction value;    public VariableConstraint(Variable leftVar, ArrayList<Variable> rightVars,            ArrayList<Fraction> coefs, Fraction value) {        this.leftVar = leftVar;        this.rightVars = rightVars;        this.coefs = coefs;        this.value = value;    }        @Override    public String toString() {        String result = "";        result += leftVar.toString() + " = " + value;        for (int i = 0; i < rightVars.size(); i++) {            if (coefs.get(i).isNegative()) {                result += "  -  ";                if (!coefs.get(i).absolute().equals(new Fraction(1)))                     result += coefs.get(i).absolute().toString();                result += rightVars.get(i).toString();            }            else if (!coefs.get(i).equals(new Fraction(0))) {                result += "  +  ";                if (!coefs.get(i).absolute().equals(new Fraction(1)))                    result += coefs.get(i).absolute().toString();                result += rightVars.get(i).toString();            }        }        return result;    }    public Variable getLeftVar() {        return this.leftVar;    }    public Fraction getValue() {        return this.value;    }    public Fraction getCoef(Variable var) {        System.out.println("@@@@@@@@@@@@ " + var);        for (int i = 0; i < this.rightVars.size(); i++) {            System.out.printf("%20s", this.rightVars.get(i));            if (this.rightVars.get(i).equals(var))                return this.coefs.get(i);        }        System.out.println();        return null;    }    public ArrayList<Fraction> getCoefs() {        return this.coefs;    }    public VariableConstraint getNewVarConstraint(Variable inVar,             Variable outVar, VariableConstraint varConstraint) {                ArrayList<Variable> newRightVars = new ArrayList<Variable>();        ArrayList<Fraction> newCoefs = new ArrayList<Fraction>();        Fraction newValue;        if (this.equals(varConstraint)) {            Variable newLeftVar = outVar;            int index = this.rightVars.indexOf(outVar);            Fraction coef = this.coefs.get(index);            for (int i = 0; i < this.rightVars.size(); i++)                if (!this.rightVars.get(i).equals(outVar) /*&&                !this.coefs.get(i).equals(new Fraction(0))*/) {                    newRightVars.add(this.rightVars.get(i));                    newCoefs.add(Fraction.divide(this.coefs.get(i), coef).getOpposite());                }            newValue = Fraction.divide(this.value, coef).getOpposite();/*            for (int i = 0; i < newRightVars.size(); i++)                newCoefs.add(Fraction.divide(                        this.coefs.get(i), coef).getOpposite());*/            newRightVars.add(inVar);            newCoefs.add(Fraction.divide(new Fraction(1), coef));            /*for (int i = 0; i < this.rightVars.size(); i++)                if (!newRightVars.contains(this.rightVars.get(i)) &&                !this.rightVars.get(i).equals(outVar)) {                    newRightVars.add(this.rightVars.get(i));                    newCoefs.add(new Fraction(0));                }*/           /* for (int i = 0; i < newCoefs.size(); i++)                System.out.printf("**********%8s", newCoefs.get(i));            System.out.println();*/            return new VariableConstraint(newLeftVar, newRightVars,                newCoefs, newValue);        }        Fraction coef;        if (!this.getCoef(outVar).equals(new Fraction(0)))            coef = Fraction.divide(varConstraint.getCoef(outVar),                this.getCoef(outVar));        else            coef = new Fraction(0);        if (!coef.equals(new Fraction(0))) {            newValue = Fraction.substract(this.value,                    Fraction.divide(varConstraint.getValue(), coef));            for (int i = 0; i < this.rightVars.size(); i++) {                if (!this.rightVars.get(i).equals(outVar) && !this.rightVars.get(i).equals(inVar)) {                    //System.out.printf("===%20s", this.rightVars.get(i));                    newRightVars.add(this.rightVars.get(i));                    int index = varConstraint.getRightVars().indexOf(this.rightVars.get(i));                    newCoefs.add(Fraction.substract(this.coefs.get(i),                            Fraction.divide(varConstraint.getCoefs().get(index), coef)));                }            }        } else {            newValue = this.value;            for (int i = 0; i < this.rightVars.size(); i++) {                if (!this.rightVars.get(i).equals(outVar) && !this.rightVars.get(i).equals(inVar)) {                    //System.out.printf("===%20s", this.rightVars.get(i));                    newRightVars.add(this.rightVars.get(i));                    int index = varConstraint.getRightVars().indexOf(this.rightVars.get(i));                    newCoefs.add(Fraction.substract(this.coefs.get(i),                            new Fraction(0)));                }            }        }        newRightVars.add(inVar);        if (!coef.equals(new Fraction(0))) {            newCoefs.add(Fraction.divide(new Fraction(1), coef));        } else            newCoefs.add(new Fraction(0));        newRightVars.add(outVar);        newCoefs.add(new Fraction(0));        return new VariableConstraint(this.leftVar, newRightVars,                newCoefs, newValue);    }    public ArrayList<Variable> getRightVars() {        return this.rightVars;    }}

⌨️ 快捷键说明

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