List of usage examples for org.apache.commons.math3.linear ArrayFieldVector ArrayFieldVector
public ArrayFieldVector(ArrayFieldVector<T> v) throws NullArgumentException
From source file:controller.Parser.java
static LP parse(File f) throws FileNotFoundException { Scanner s = new Scanner(f); Pattern p = Pattern.compile(dvarreg); HashMap<String, Integer> x = new HashMap<String, Integer>(); HashMap<Integer, String> xReverse = new HashMap<Integer, String>(); int xcol = 0; /* Get input size and names of the decision variables. */ int constraints = -1; // Take the objective function into account. while (s.hasNextLine()) { String line = s.nextLine(); if (line.trim().equals("")) continue; /* //from w ww .jav a2 s . c o m * TODO: Beware, will now accept invalid * files with multiple objective functions. */ /* if (!validConstraint(line) && !validObj(line)) { String e = "Unsupported format in file " + f; throw new IllegalArgumentException(e); } */ Matcher m = p.matcher(line); while (m.find()) { String var = m.group(3); if (validVarName(var) && !x.containsKey(var)) { x.put(var, xcol); xReverse.put(xcol++, var); } } constraints++; } BigFraction[][] Ndata = new BigFraction[constraints][x.size()]; for (int i = 0; i < Ndata.length; i++) { Arrays.fill(Ndata[i], BigFraction.ZERO); } BigFraction[] bdata = new BigFraction[constraints]; BigFraction[] cdata = new BigFraction[x.size()]; Arrays.fill(cdata, BigFraction.ZERO); s = new Scanner(f); String obj = s.nextLine(); Matcher m = p.matcher(obj); while (m.find()) { String var = m.group(3); if (!x.containsKey(var)) continue; String sign = m.group(1); if (sign == null) sign = "+"; String coeffStr = m.group(2); BigFraction coeff; if (coeffStr == null) { coeff = BigFraction.ONE; } else { coeff = new BigFraction(Double.parseDouble(coeffStr)); } if (sign.equals("-")) coeff = coeff.negate(); cdata[x.get(var)] = coeff; } int row = 0; while (s.hasNextLine()) { String line = s.nextLine(); String[] split = line.split("<="); if (line.trim().equals("")) continue; if (split.length != 2) { String e = "Unsupported format in file " + f; throw new IllegalArgumentException(e); } m = p.matcher(line); bdata[row] = new BigFraction(Double.parseDouble(split[1])); while (m.find()) { String var = m.group(3); if (!x.containsKey(var)) continue; String sign = m.group(1); if (sign == null) sign = "+"; String coeffStr = m.group(2); BigFraction coeff; if (coeffStr == null) { coeff = BigFraction.ONE; } else { coeff = new BigFraction(Double.parseDouble(coeffStr)); } if (sign.equals("-")) coeff = coeff.negate(); Ndata[row][x.get(var)] = coeff; } row++; } return new LP(new Array2DRowFieldMatrix<BigFraction>(Ndata), new ArrayFieldVector<BigFraction>(bdata), new ArrayFieldVector<BigFraction>(cdata), xReverse); }
From source file:controller.VisLP.java
private static Point2D[] getFeasibleIntersections(FieldMatrix<BigFraction> cons) { FieldMatrix<BigFraction> N = cons.getSubMatrix(0, cons.getRowDimension() - 1, 0, cons.getColumnDimension() - 2); FieldVector<BigFraction> b = cons.getColumnVector(cons.getColumnDimension() - 1); HashSet<Point2D> points = new HashSet<Point2D>(); unb = new ArrayList<Point2D>(); /* Find all intersections */ for (int i = 0; i < N.getRowDimension(); i++) { for (int j = 0; j < N.getRowDimension(); j++) { if (i == j) continue; FieldMatrix<BigFraction> line1 = N.getRowMatrix(i); FieldMatrix<BigFraction> line2 = N.getRowMatrix(j); BigFraction[] bval = new BigFraction[] { b.getEntry(i), b.getEntry(j) }; FieldVector<BigFraction> bsys = new ArrayFieldVector<BigFraction>(bval); FieldMatrix<BigFraction> sys = LP.addBlock(line1, line2, LP.UNDER); try { FieldVector<BigFraction> point = new FieldLUDecomposition<BigFraction>(sys).getSolver() .getInverse().operate(bsys); double x = point.getEntry(0).doubleValue(); double y = point.getEntry(1).doubleValue(); Point2D p2d = new Point2D.Double(x, y); /* Only add feasible points */ if (feasible(p2d, N, b)) { if (i >= N.getRowDimension() - 1) unb.add(p2d);/*from www.j av a 2s. c om*/ points.add(p2d); } } catch (IllegalArgumentException e) { /* * Two lines that don't intersect forms an invertible * matrix. Skip these points. */ } } } return points.toArray(new Point2D[0]); }
From source file:model.LP.java
/** * Return a new linear program with a new objective function * made of the given coefficients.// www . j a v a 2 s . c o 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); }