List of usage examples for org.apache.commons.math3.linear FieldMatrix copy
FieldMatrix<T> copy();
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 .ja v a 2 s .co m * 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
/** * Initializes a linear program.//from www . j a v a 2 s . c o 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)); } }