Example usage for org.apache.commons.math3.linear FieldVector mapMultiply

List of usage examples for org.apache.commons.math3.linear FieldVector mapMultiply

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear FieldVector mapMultiply.

Prototype

FieldVector<T> mapMultiply(T d) throws NullArgumentException;

Source Link

Document

Map a multiplication operation to each entry.

Usage

From source file:model.LP.java

/**
 * Initializes a linear program.//from w ww.ja  v a  2  s  .  c om
 * <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:model.LP.java

/**
 * Do one iteration of the simplex method.
 *
 * @param  entering/*from  w w w. j a va2  s  .  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);
}