List of usage examples for org.apache.commons.math3.linear FieldVector setEntry
void setEntry(int index, T value) throws OutOfRangeException;
From source file:model.LP.java
/** * Do one iteration of the simplex method. * * @param entering//ww w .j a v a 2s .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:model.LP.java
/** * Find a leaving variable index that is the most * bounding on the given entering variable index. * * @param entering//from w w w .j a v a2s . c o m * 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
public LP reinstate() { FieldVector<BigFraction> nc_ = new ArrayFieldVector<BigFraction>(c_.getDimension(), BigFraction.ZERO); FieldMatrix<BigFraction> bin = new FieldLUDecomposition<BigFraction>(B_).getSolver().getInverse() .multiply(N_);//from w w w . ja v a2s. c o m for (int i = 0; i < Bi.length; i++) { int k = Bi[i]; if (k < Ni.length) { for (int j = 0; j < Ni.length; j++) { BigFraction bf = c.getEntry(k).multiply(bin.getEntry(i, j)); nc_.setEntry(j, nc_.getEntry(j).add(bf)); } } } for (int i = 0; i < Ni.length; i++) { int k = Ni[i]; if (k < Ni.length) { nc_.setEntry(i, nc_.getEntry(i).subtract(c.getEntry(i))); } } return new LP(B, N, b, c, B_, N_, b_, nc_, x, Bi, Ni); }