?? objective.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 Objective { public static final int MINIMIZE = 0; public static final int MAXIMIZE = 1; private int type; private ArrayList<Fraction> coefs; private ArrayList<Variable> vars; private Fraction value; private Fraction objectiveValue; private boolean isAuxiliary = false; public Objective() { coefs = new ArrayList<Fraction>(); vars = new ArrayList<Variable>(); this.value = new Fraction(0); } public Objective(int type, ArrayList<Fraction> coefficients, ArrayList<Variable> variables, Fraction value) { this.type = type; this.coefs = coefficients; this.vars = variables; this.value = value; this.objectiveValue = null; } @Override public String toString() { if (this.coefs.size() == 0) return ""; String result = "<b>"; switch (type) { case Objective.MAXIMIZE: result += "Maximizer (" + this.value; break; case Objective.MINIMIZE: result += "Minimizer (" + this.value; break; default: break; } /*if (!coefs.get(0).absolute().equals(new Fraction(0))) { 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 = 0; 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, 1))) { result += " + "; if (!coefs.get(i).absolute().equals(new Fraction(1))) result += coefs.get(i).absolute().toString(); result += vars.get(i).toString(); } } return result + ")</b><br />"; } public Fraction getObjectiveValue() { Fraction result = new Fraction(0); for (int i = 0; i < vars.size(); i++) { result = Fraction.add(result, Fraction.multiple(vars.get(i).getValue(), coefs.get(i))); } return Fraction.add(this.value, result); } public Fraction getCoef(Variable outVar) { for (int i = 0; i < this.vars.size(); i++) if (this.vars.get(i).equals(outVar)) return this.coefs.get(i); return null; } public Objective getNewObjective(Variable inVar, Variable outVar, VariableConstraint varConstraint) { ArrayList<Variable> newVars = new ArrayList<Variable>(); ArrayList<Fraction> newCoefs = new ArrayList<Fraction>(); Fraction newValue; /* for (int i = 0; i < this.getVars().size(); i++) { // System.out.printf("%8s" + varConstraint.getCoefs().get(i).toString()); System.out.print(" " + this.getCoefs().get(i)); } */ Fraction coef = Fraction.divide(varConstraint.getCoef(outVar), this.getCoef(outVar)); newValue = Fraction.substract(this.value, Fraction.divide(varConstraint.getValue(), coef)); ArrayList<Fraction> nonZeroCoefs = new ArrayList<Fraction>(); ArrayList<Variable> baseVars = new ArrayList<Variable>(); int index = 0; for (int i = 0; i < this.coefs.size(); i++) if (!this.coefs.get(i).equals(new Fraction(0))) { nonZeroCoefs.add(this.coefs.get(i)); baseVars.add(this.vars.get(i)); index++; } for (int i = 0; i < nonZeroCoefs.size(); i++) { if (!this.vars.get(i).equals(outVar)) { newVars.add(baseVars.get(i)); System.out.println(baseVars.get(i)); index = varConstraint.getRightVars().indexOf(baseVars.get(i)); newCoefs.add(Fraction.substract(nonZeroCoefs.get(i), Fraction.divide(varConstraint.getCoefs().get(index), coef))); } } for (int i = 0; i < this.vars.size(); i++) { if (!baseVars.contains(this.vars.get(i))) { newVars.add(this.vars.get(i)); newCoefs.add(new Fraction(0)); } } newVars.add(outVar); newCoefs.add(Fraction.divide(new Fraction(0), coef)); index = newVars.indexOf(inVar); newCoefs.set(index, Fraction.divide(new Fraction(1), coef)); return new Objective(this.type, newCoefs, newVars, newValue); } public ArrayList<Fraction> getCoefs() { return this.coefs; } public int getType() { return type; } public void setType(int type) { this.type = type; } public void setVars(ArrayList<Variable> newVars) { this.vars = newVars; } public ArrayList<Variable> getVars() { return this.vars; } public void setValue(Fraction value) { this.value = value; } public Variable getVariableWithMaximalPositiveCoef() { Variable var = null; Fraction maxCoef = new Fraction(0); for (int i = 0; i < this.vars.size(); i++) { if (this.coefs.get(i).isGreaterThan(maxCoef)) { var = this.vars.get(i); maxCoef = this.coefs.get(i); } } if (maxCoef.equals(new Fraction(0))) return null; return var; } public boolean hasPositiveCoefs() { for (int i = 0; i < this.coefs.size(); i++) if (this.coefs.get(i).getOpposite().isNegative()) return true; return false; } public void setIsAuxiliary(boolean flag) { this.isAuxiliary = flag; } public boolean isAuxiliary() { return isAuxiliary; } public Fraction getValue() { return this.value; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -