?? constraint.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 = " "; 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 += " ≥ " + value.toString(); break; case ConstraintTypes.LESS_THAN: result += " ≤ " + value.toString(); break; default: break; } return result + "<br />"; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -