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

📄 constraint.java

📁 This is my implementation 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 Constraint {    private int type;    private ArrayList<Fraction> coefs;    private ArrayList<Variable> vars;    private Fraction value;    public Constraint() {        this.coefs = new ArrayList<Fraction>();        this.vars = new ArrayList<Variable>();        this.value = null;    }    public Constraint(int type, ArrayList<Fraction> coefficients,            ArrayList<Variable> variables, Fraction value) {        this.type = type;        this.coefs = coefficients;        this.vars = variables;        this.value = value;    }    public ArrayList<Fraction> getCoefs() {        return this.coefs;    }    public void setVars(ArrayList<Variable> vars) {        this.vars = vars;    }    public ArrayList<Variable> getVars() {        return this.vars;    }    public void setValue(Fraction value) {        this.value = value;    }    public Fraction getValue() {        return this.value;    }    public int getType() {        return this.type;    }    public VariableConstraint getVarConstraint(Variable leftVar) {        ArrayList<Variable> rightVars = new ArrayList<Variable>();        ArrayList<Fraction> newCoefs = new ArrayList<Fraction>();        Fraction coef = new Fraction(1);        for (int i = 0; i < coefs.size(); i++)            if (!vars.get(i).equals(leftVar)) {                rightVars.add(vars.get(i));                newCoefs.add(coefs.get(i).getOpposite());            } else                coef = coefs.get(i);        for (int i = 0; i < newCoefs.size(); i++)            Fraction.divide(newCoefs.get(i), coef);        return new VariableConstraint(leftVar, rightVars, newCoefs,                Fraction.divide(value, coef));    }    @Override    public String toString() {        if (coefs.size() == 0)            return "";        String result = "&nbsp;&nbsp;&nbsp;&nbsp;";        if (!coefs.get(0).absolute().equals(new Fraction(1)))            result += coefs.get(0).toString();        if (coefs.get(0).equals(new Fraction(-1)))            result += "-";        result += vars.get(0).toString();        for (int i = 1; i < vars.size(); i++) {            if (coefs.get(i).isNegative()) {                result += "  -  ";                if (!coefs.get(i).absolute().equals(new Fraction(1)))                     result += coefs.get(i).absolute().toString();                result += vars.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 += vars.get(i).toString();            }        }        switch (type) {            case ConstraintTypes.EQUAL_TO:                result += " = " + value.toString();                break;            case ConstraintTypes.GREATER_THAN:                result += " &ge; " + value.toString();                break;            case ConstraintTypes.LESS_THAN:                result += " &le; " + value.toString();                break;            default:                break;        }        return result + "<br />";    }}

⌨️ 快捷键说明

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