List of usage examples for org.apache.commons.math3.fraction BigFraction MINUS_ONE
BigFraction MINUS_ONE
To view the source code for org.apache.commons.math3.fraction BigFraction MINUS_ONE.
Click Source Link
From source file:model.LP.java
/** * Initializes a linear program.//from w w w . ja v a 2 s. co m * <p> * n being the number of variables and m being the number of constraints, * this {@code constructor} does the following: * <p><blockquote><pre> * B is set to the identity matrix of dimension m. * * The indices of the basic and non-basic variables are set to * 0..n-1 and n-1..n+m-1, respectively. * * The slack variables are called w1..wm. * </pre></blockquote<p> * * @param N * A {@code Matrix} with the coefficients * of the non-basic variables. * @param b * A {@code Matrix} with the upper bounds on * the constraints in the original program. * @param c * A {@code Matrix} with the coefficients of the * decision variables in the original program. * @param x * A {@code HashMap} mapping the indices of the * basic and non-basic variables to their names. */ public LP(FieldMatrix<BigFraction> N, FieldVector<BigFraction> b, FieldVector<BigFraction> c, HashMap<Integer, String> x) { this(null, N, b, c, null, N.copy(), b.copy(), c.mapMultiply(BigFraction.MINUS_ONE).copy(), x, new int[N.getRowDimension()], new int[N.getColumnDimension()]); /* Create an identity matrix of BigFraction's */ int m = N.getRowDimension(); BigFraction[][] Bd = new BigFraction[m][m]; for (int i = 0; i < m; i++) { Arrays.fill(Bd[i], BigFraction.ZERO); Bd[i][i] = BigFraction.ONE; } FieldMatrix<BigFraction> B = new Array2DRowFieldMatrix<BigFraction>(Bd); this.B = B; this.B_ = B.copy(); for (int i = 0; i < Ni.length; i++) Ni[i] = i; for (int i = 0; i < Bi.length; i++) { Bi[i] = i + Ni.length; x.put(Bi[i], "w" + (i + 1)); } }
From source file:controller.VisLP.java
/** * Dictionary form assumes all x-values >= 0. For a geometric * representation we need these constraints explicitly stated * in order to draw the feasible region. * /*w w w. j ava 2s . c om*/ * This method adds the constraints x >= 0 (-x <= 0) * and y >= 0 (-y <= 0) unless more bounding constraints * on the x and y-values already exists. * * It also always adds a line with a negative slope with * a <<high enough>> positive x- and y-intercept needed * to color unbounded feasible regions. * * @param cons * A constraints-matrix * @return * A constraints matrix guaranteed to have lower bounds. */ static FieldMatrix<BigFraction> checkForBounds(FieldMatrix<BigFraction> cons) { boolean lowerx = false; boolean lowery = false; BigFraction valsum = BigFraction.ZERO; /* Does lower bounds already exist? */ for (int i = 0; i < cons.getRowDimension(); i++) { BigFraction x = cons.getEntry(i, 0); BigFraction y = cons.getEntry(i, 1); if (x.compareTo(BigFraction.ZERO) < 0 && y.equals(BigFraction.ZERO)) { lowerx = true; } else if (x.equals(BigFraction.ZERO) && y.compareTo(BigFraction.ZERO) < 0) { lowery = true; } valsum = valsum.add(cons.getEntry(i, 2).abs()); } FieldMatrix<BigFraction> ncons = cons.copy(); BigFraction[] cxdata = new BigFraction[] { BigFraction.MINUS_ONE, BigFraction.ZERO, BigFraction.ZERO }; BigFraction[] cydata = new BigFraction[] { BigFraction.ZERO, BigFraction.MINUS_ONE, BigFraction.ZERO }; /* Add lower bounds if they do not exist */ if (!lowerx) { FieldMatrix<BigFraction> c = new Array2DRowFieldMatrix<BigFraction>(cxdata).transpose(); ncons = LP.addBlock(ncons, c, LP.UNDER); } if (!lowery) { FieldMatrix<BigFraction> c = new Array2DRowFieldMatrix<BigFraction>(cydata).transpose(); ncons = LP.addBlock(ncons, c, LP.UNDER); } valsum = valsum.add(BigFraction.TWO).multiply(valsum); BigFraction[] uc = new BigFraction[] { BigFraction.ONE, BigFraction.ONE, valsum }; FieldMatrix<BigFraction> c = new Array2DRowFieldMatrix<BigFraction>(uc).transpose(); ncons = LP.addBlock(ncons, c, LP.UNDER); return ncons; }
From source file:model.LP.java
/** * Find a leaving variable index that is the most * bounding on the given entering variable index. * * @param entering//from ww w . j av a 2s . com * an entering variable index. * @param dual * If true, find a leaving variable index for the dual dictionary. * Otherwise, find one for the primal dictionary. * @return * A leaving variable index. */ private int leaving(int entering, boolean dual) { FieldVector<BigFraction> check; FieldVector<BigFraction> sd; FieldMatrix<BigFraction> bin = new FieldLUDecomposition<BigFraction>(B_).getSolver().getInverse() .multiply(N_); if (dual) { check = c_; FieldVector<BigFraction> unit = new ArrayFieldVector<BigFraction>(bin.getRowDimension(), BigFraction.ZERO); unit.setEntry(entering, BigFraction.ONE); sd = bin.transpose().scalarMultiply(BigFraction.MINUS_ONE).operate(unit); } else { check = b_; FieldVector<BigFraction> unit = new ArrayFieldVector<BigFraction>(bin.getColumnDimension(), BigFraction.ZERO); unit.setEntry(entering, BigFraction.ONE); sd = bin.operate(unit); } boolean unbounded = true; int index = -1; /* Check for unboundedness and find first non-zero element in check */ for (int i = 0; i < sd.getDimension(); i++) { if (!check.getEntry(i).equals(BigFraction.ZERO) && index == -1) { index = i; } if (sd.getEntry(i).compareTo(BigFraction.ZERO) > 0) { unbounded = false; } } String e = "Program is unbounded."; if (unbounded) throw new RuntimeException(e); /* Set temporarily max value as ratio of the first divisible pair. */ BigFraction max = sd.getEntry(index).divide(check.getEntry(index)); for (int i = 0; i < sd.getDimension(); i++) { BigFraction num = sd.getEntry(i); BigFraction denom = check.getEntry(i); if (!denom.equals(BigFraction.ZERO)) { BigFraction val = num.divide(denom); if (val.compareTo(max) > 0) { max = val; index = i; } } else { if (num.compareTo(BigFraction.ZERO) > 0) return i; } } return index; }
From source file:model.LP.java
/** * Return a new linear program with a new objective function * made of the given coefficients.//from w w w . j a v a 2 s . co m * * @param coeff * Coefficients of the decision variables. * @return * A linear program with the new objective function. */ public LP replaceObj(BigFraction[] coeff) { // TODO: Is this the behavior we want? FieldVector<BigFraction> nc_ = new ArrayFieldVector<BigFraction>(coeff).mapMultiply(BigFraction.MINUS_ONE); return new LP(B, N, b, c, B_, N_, b_, nc_, x, Bi, Ni); }
From source file:model.LP.java
/** * Do one iteration of the simplex method. * * @param entering/*from w ww . j av a2s . c om*/ * Index of variable to enter the basis. * @param leaving * Index of variable to leave the basis. * @return * A linear program after one iteration. */ public LP pivot(int entering, int leaving) { FieldMatrix<BigFraction> bin = new FieldLUDecomposition<BigFraction>(B_).getSolver().getInverse() .multiply(N_); // Step 1: Check for optimpivality // Step 2: Select entering variable. // Naive method. Does not check for optimality. Assumes feasibility. // Entering variable is given. // Step 3: Compute primal step direction. FieldVector<BigFraction> ej = new ArrayFieldVector<BigFraction>(bin.getColumnDimension(), BigFraction.ZERO); ej.setEntry(entering, BigFraction.ONE); FieldVector<BigFraction> psd = bin.operate(ej); // Step 4: Compute primal step length. // Step 5: Select leaving variable. // Leaving variable is given. BigFraction t = b_.getEntry(leaving).divide(psd.getEntry(leaving)); // Step 6: Compute dual step direction. FieldVector<BigFraction> ei = new ArrayFieldVector<BigFraction>(bin.getRowDimension(), BigFraction.ZERO); ei.setEntry(leaving, BigFraction.ONE); FieldVector<BigFraction> dsd = bin.transpose().scalarMultiply(BigFraction.MINUS_ONE).operate(ei); // Step 7: Compute dual step length. BigFraction s = c_.getEntry(entering).divide(dsd.getEntry(entering)); // Step 8: Update current primal and dual solutions. FieldVector<BigFraction> nb_ = b_.subtract(psd.mapMultiply(t)); nb_.setEntry(leaving, t); FieldVector<BigFraction> nc_ = c_.subtract(dsd.mapMultiply(s)); nc_.setEntry(entering, s); // Step 9: Update basis. FieldVector<BigFraction> temp = B_.getColumnVector(leaving); FieldMatrix<BigFraction> nB_ = B_.copy(); nB_.setColumn(leaving, N_.getColumn(entering)); FieldMatrix<BigFraction> nN_ = N_.copy(); nN_.setColumnVector(entering, temp); int[] nBi = Bi.clone(); int[] nNi = Ni.clone(); nBi[leaving] = Ni[entering]; nNi[entering] = Bi[leaving]; return new LP(B, N, b, c, nB_, nN_, nb_, nc_, x, nBi, nNi); }
From source file:org.matheclipse.commons.parser.client.eval.bigfraction.BigFractionEvaluator.java
/** * /*www . ja v a 2 s . c o m*/ * TODO: add more derivation rules * * @param node * @param var * @return */ public ASTNode derivative(final ASTNode node, SymbolNode var) { if (node.isFree(var)) { return new BigFractionNode(fDfpField.getZero()); } if (node instanceof FunctionNode) { FunctionNode f = (FunctionNode) node; if (f.size() > 1 && f.getNode(0) instanceof SymbolNode) { SymbolNode head = (SymbolNode) f.getNode(0); if (f.size() == 2) { ASTNode arg1Derived = derivative(f.getNode(1), var); if (isSymbol(head, "Exp")) { FunctionNode fun = new FunctionNode(fASTFactory.createSymbol("Exp")); fun.add(f.getNode(1)); return getDerivativeResult(arg1Derived, fun); } if (isSymbol(head, "Cos")) { FunctionNode fun = new FunctionNode(fASTFactory.createSymbol("Times")); fun.add(new BigFractionNode(BigFraction.MINUS_ONE)); fun.add(new FunctionNode(fASTFactory.createSymbol("Cos"), f.getNode(1))); return getDerivativeResult(arg1Derived, fun); } if (isSymbol(head, "Sin")) { FunctionNode fun = new FunctionNode(fASTFactory.createSymbol("Cos")); fun.add(f.getNode(1)); return getDerivativeResult(arg1Derived, fun); } } else if (f.size() == 3 && isSymbol(head, "Power")) { if (f.get(2).isFree(var)) {// derive x^r ASTNode arg1Derived = derivative(f.getNode(1), var); // (r-1) FunctionNode exponent = fASTFactory.createFunction(fASTFactory.createSymbol("Plus"), new BigFractionNode(BigFraction.MINUS_ONE), f.get(2)); // r*x^(r-1) FunctionNode fun = fASTFactory.createFunction(fASTFactory.createSymbol("Times"), f.get(2), fASTFactory.createFunction(fASTFactory.createSymbol("Power"), f.get(1), exponent)); return getDerivativeResult(arg1Derived, fun); } if (f.get(1).isFree(var)) {// derive a^x ASTNode arg2Derived = derivative(f.getNode(2), var); // log(a) * a^x FunctionNode fun = fASTFactory.createFunction(fASTFactory.createSymbol("Times"), fASTFactory.createFunction(fASTFactory.createSymbol("Log"), f.get(1)), f); return getDerivativeResult(arg2Derived, fun); } } else { if (isSymbol(head, "Plus")) { FunctionNode result = new FunctionNode(f.getNode(0)); for (int i = 1; i < f.size(); i++) { ASTNode deriv = derivative(f.getNode(i), var); if (!deriv.equals(fZERO)) { result.add(deriv); } } return result; } if (isSymbol(head, "Times")) { FunctionNode plusResult = new FunctionNode(fASTFactory.createSymbol("Plus")); for (int i = 1; i < f.size(); i++) { FunctionNode timesResult = new FunctionNode(f.getNode(0)); boolean valid = true; for (int j = 1; j < f.size(); j++) { if (j == i) { ASTNode deriv = derivative(f.getNode(j), var); if (deriv.equals(fZERO)) { valid = false; } else { timesResult.add(deriv); } } else { timesResult.add(f.getNode(j)); } } if (valid) { plusResult.add(timesResult); } } return plusResult; } } } return new FunctionNode(new SymbolNode("D"), node, var); // return evaluateFunction((FunctionNode) node); } if (node instanceof SymbolNode) { if (isSymbol((SymbolNode) node, var)) { return new BigFractionNode(fDfpField.getOne()); } FieldElementVariable<BigFraction> v = fVariableMap.get(node.toString()); if (v != null) { return new BigFractionNode(fDfpField.getZero()); } BigFraction dbl = SYMBOL_MAP.get(node.toString()); if (dbl != null) { return new BigFractionNode(fDfpField.getZero()); } return new BigFractionNode(fDfpField.getZero()); } else if (node instanceof NumberNode) { return new BigFractionNode(fDfpField.getZero()); } throw new ArithmeticMathException( "BigFractionEvaluator#derivative(ASTNode, SymbolNode) not possible for: " + node.toString()); }