Java Code Examples for org.apache.commons.math3.linear.Array2DRowRealMatrix#setRowVector()

The following examples show how to use org.apache.commons.math3.linear.Array2DRowRealMatrix#setRowVector() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Math_33_SimplexTableau_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Create the tableau by itself.
 * @param maximize if true, goal is to maximize the objective function
 * @return created tableau
 */
protected RealMatrix createTableau(final boolean maximize) {

    // create a matrix of the correct size
    int width = numDecisionVariables + numSlackVariables +
    numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
    int height = constraints.size() + getNumObjectiveFunctions();
    Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);

    // initialize the objective function rows
    if (getNumObjectiveFunctions() == 2) {
        matrix.setEntry(0, 0, -1);
    }
    int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
    matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
    RealVector objectiveCoefficients =
        maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
    copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
    matrix.setEntry(zIndex, width - 1,
        maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());

    if (!restrictToNonNegative) {
        matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
            getInvertedCoefficientSum(objectiveCoefficients));
    }

    // initialize the constraint rows
    int slackVar = 0;
    int artificialVar = 0;
    for (int i = 0; i < constraints.size(); i++) {
        LinearConstraint constraint = constraints.get(i);
        int row = getNumObjectiveFunctions() + i;

        // decision variable coefficients
        copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);

        // x-
        if (!restrictToNonNegative) {
            matrix.setEntry(row, getSlackVariableOffset() - 1,
                getInvertedCoefficientSum(constraint.getCoefficients()));
        }

        // RHS
        matrix.setEntry(row, width - 1, constraint.getValue());

        // slack variables
        if (constraint.getRelationship() == Relationship.LEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1);  // slack
        } else if (constraint.getRelationship() == Relationship.GEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
        }

        // artificial variables
        if ((constraint.getRelationship() == Relationship.EQ) ||
                (constraint.getRelationship() == Relationship.GEQ)) {
            matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
            matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
            matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
        }
    }

    return matrix;
}
 
Example 2
Source File: SimplexTableau.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create the tableau by itself.
 * @param maximize if true, goal is to maximize the objective function
 * @return created tableau
 */
protected RealMatrix createTableau(final boolean maximize) {

    // create a matrix of the correct size
    int width = numDecisionVariables + numSlackVariables +
    numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
    int height = constraints.size() + getNumObjectiveFunctions();
    Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);

    // initialize the objective function rows
    if (getNumObjectiveFunctions() == 2) {
        matrix.setEntry(0, 0, -1);
    }
    int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
    matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
    RealVector objectiveCoefficients =
        maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
    copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
    matrix.setEntry(zIndex, width - 1,
        maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());

    if (!restrictToNonNegative) {
        matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
            getInvertedCoefficientSum(objectiveCoefficients));
    }

    // initialize the constraint rows
    int slackVar = 0;
    int artificialVar = 0;
    for (int i = 0; i < constraints.size(); i++) {
        LinearConstraint constraint = constraints.get(i);
        int row = getNumObjectiveFunctions() + i;

        // decision variable coefficients
        copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);

        // x-
        if (!restrictToNonNegative) {
            matrix.setEntry(row, getSlackVariableOffset() - 1,
                getInvertedCoefficientSum(constraint.getCoefficients()));
        }

        // RHS
        matrix.setEntry(row, width - 1, constraint.getValue());

        // slack variables
        if (constraint.getRelationship() == Relationship.LEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1);  // slack
        } else if (constraint.getRelationship() == Relationship.GEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
        }

        // artificial variables
        if ((constraint.getRelationship() == Relationship.EQ) ||
                (constraint.getRelationship() == Relationship.GEQ)) {
            matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
            matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
            matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
        }
    }

    return matrix;
}
 
Example 3
Source File: SimplexTableau.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create the tableau by itself.
 * @param maximize if true, goal is to maximize the objective function
 * @return created tableau
 */
protected RealMatrix createTableau(final boolean maximize) {

    // create a matrix of the correct size
    int width = numDecisionVariables + numSlackVariables +
    numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
    int height = constraints.size() + getNumObjectiveFunctions();
    Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);

    // initialize the objective function rows
    if (getNumObjectiveFunctions() == 2) {
        matrix.setEntry(0, 0, -1);
    }
    int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
    matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
    RealVector objectiveCoefficients =
        maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
    copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
    matrix.setEntry(zIndex, width - 1,
        maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());

    if (!restrictToNonNegative) {
        matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
            getInvertedCoefficientSum(objectiveCoefficients));
    }

    // initialize the constraint rows
    int slackVar = 0;
    int artificialVar = 0;
    for (int i = 0; i < constraints.size(); i++) {
        LinearConstraint constraint = constraints.get(i);
        int row = getNumObjectiveFunctions() + i;

        // decision variable coefficients
        copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);

        // x-
        if (!restrictToNonNegative) {
            matrix.setEntry(row, getSlackVariableOffset() - 1,
                getInvertedCoefficientSum(constraint.getCoefficients()));
        }

        // RHS
        matrix.setEntry(row, width - 1, constraint.getValue());

        // slack variables
        if (constraint.getRelationship() == Relationship.LEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1);  // slack
        } else if (constraint.getRelationship() == Relationship.GEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
        }

        // artificial variables
        if ((constraint.getRelationship() == Relationship.EQ) ||
                (constraint.getRelationship() == Relationship.GEQ)) {
            matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
            matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
            matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
        }
    }

    return matrix;
}
 
Example 4
Source File: SimplexTableau.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create the tableau by itself.
 * @param maximize if true, goal is to maximize the objective function
 * @return created tableau
 */
protected RealMatrix createTableau(final boolean maximize) {

    // create a matrix of the correct size
    int width = numDecisionVariables + numSlackVariables +
    numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
    int height = constraints.size() + getNumObjectiveFunctions();
    Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);

    // initialize the objective function rows
    if (getNumObjectiveFunctions() == 2) {
        matrix.setEntry(0, 0, -1);
    }
    int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
    matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
    RealVector objectiveCoefficients =
        maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
    copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
    matrix.setEntry(zIndex, width - 1,
        maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());

    if (!restrictToNonNegative) {
        matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
            getInvertedCoefficientSum(objectiveCoefficients));
    }

    // initialize the constraint rows
    int slackVar = 0;
    int artificialVar = 0;
    for (int i = 0; i < constraints.size(); i++) {
        LinearConstraint constraint = constraints.get(i);
        int row = getNumObjectiveFunctions() + i;

        // decision variable coefficients
        copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);

        // x-
        if (!restrictToNonNegative) {
            matrix.setEntry(row, getSlackVariableOffset() - 1,
                getInvertedCoefficientSum(constraint.getCoefficients()));
        }

        // RHS
        matrix.setEntry(row, width - 1, constraint.getValue());

        // slack variables
        if (constraint.getRelationship() == Relationship.LEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1);  // slack
        } else if (constraint.getRelationship() == Relationship.GEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
        }

        // artificial variables
        if ((constraint.getRelationship() == Relationship.EQ) ||
                (constraint.getRelationship() == Relationship.GEQ)) {
            matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
            matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
            matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
        }
    }

    return matrix;
}
 
Example 5
Source File: SimplexTableau.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create the tableau by itself.
 * @param maximize if true, goal is to maximize the objective function
 * @return created tableau
 */
protected RealMatrix createTableau(final boolean maximize) {

    // create a matrix of the correct size
    int width = numDecisionVariables + numSlackVariables +
    numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
    int height = constraints.size() + getNumObjectiveFunctions();
    Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);

    // initialize the objective function rows
    if (getNumObjectiveFunctions() == 2) {
        matrix.setEntry(0, 0, -1);
    }
    int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
    matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
    RealVector objectiveCoefficients =
        maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
    copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
    matrix.setEntry(zIndex, width - 1,
        maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());

    if (!restrictToNonNegative) {
        matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
            getInvertedCoefficientSum(objectiveCoefficients));
    }

    // initialize the constraint rows
    int slackVar = 0;
    int artificialVar = 0;
    for (int i = 0; i < constraints.size(); i++) {
        LinearConstraint constraint = constraints.get(i);
        int row = getNumObjectiveFunctions() + i;

        // decision variable coefficients
        copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);

        // x-
        if (!restrictToNonNegative) {
            matrix.setEntry(row, getSlackVariableOffset() - 1,
                getInvertedCoefficientSum(constraint.getCoefficients()));
        }

        // RHS
        matrix.setEntry(row, width - 1, constraint.getValue());

        // slack variables
        if (constraint.getRelationship() == Relationship.LEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1);  // slack
        } else if (constraint.getRelationship() == Relationship.GEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
        }

        // artificial variables
        if ((constraint.getRelationship() == Relationship.EQ) ||
                (constraint.getRelationship() == Relationship.GEQ)) {
            matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
            matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
            matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
        }
    }

    return matrix;
}
 
Example 6
Source File: SimplexTableau.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create the tableau by itself.
 * @param maximize if true, goal is to maximize the objective function
 * @return created tableau
 */
protected Array2DRowRealMatrix createTableau(final boolean maximize) {

    // create a matrix of the correct size
    int width = numDecisionVariables + numSlackVariables +
    numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
    int height = constraints.size() + getNumObjectiveFunctions();
    Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);

    // initialize the objective function rows
    if (getNumObjectiveFunctions() == 2) {
        matrix.setEntry(0, 0, -1);
    }

    int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
    matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
    RealVector objectiveCoefficients = maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
    copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
    matrix.setEntry(zIndex, width - 1, maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());

    if (!restrictToNonNegative) {
        matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
                        getInvertedCoefficientSum(objectiveCoefficients));
    }

    // initialize the constraint rows
    int slackVar = 0;
    int artificialVar = 0;
    for (int i = 0; i < constraints.size(); i++) {
        LinearConstraint constraint = constraints.get(i);
        int row = getNumObjectiveFunctions() + i;

        // decision variable coefficients
        copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);

        // x-
        if (!restrictToNonNegative) {
            matrix.setEntry(row, getSlackVariableOffset() - 1,
                            getInvertedCoefficientSum(constraint.getCoefficients()));
        }

        // RHS
        matrix.setEntry(row, width - 1, constraint.getValue());

        // slack variables
        if (constraint.getRelationship() == Relationship.LEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1);  // slack
        } else if (constraint.getRelationship() == Relationship.GEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
        }

        // artificial variables
        if ((constraint.getRelationship() == Relationship.EQ) ||
            (constraint.getRelationship() == Relationship.GEQ)) {
            matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
            matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
            matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
        }
    }

    return matrix;
}
 
Example 7
Source File: SimplexTableau.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create the tableau by itself.
 * @param maximize if true, goal is to maximize the objective function
 * @return created tableau
 */
protected RealMatrix createTableau(final boolean maximize) {

    // create a matrix of the correct size
    int width = numDecisionVariables + numSlackVariables +
    numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
    int height = constraints.size() + getNumObjectiveFunctions();
    Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);

    // initialize the objective function rows
    if (getNumObjectiveFunctions() == 2) {
        matrix.setEntry(0, 0, -1);
    }
    int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
    matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
    RealVector objectiveCoefficients =
        maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
    copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
    matrix.setEntry(zIndex, width - 1,
        maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());

    if (!restrictToNonNegative) {
        matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
            getInvertedCoefficientSum(objectiveCoefficients));
    }

    // initialize the constraint rows
    int slackVar = 0;
    int artificialVar = 0;
    for (int i = 0; i < constraints.size(); i++) {
        LinearConstraint constraint = constraints.get(i);
        int row = getNumObjectiveFunctions() + i;

        // decision variable coefficients
        copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);

        // x-
        if (!restrictToNonNegative) {
            matrix.setEntry(row, getSlackVariableOffset() - 1,
                getInvertedCoefficientSum(constraint.getCoefficients()));
        }

        // RHS
        matrix.setEntry(row, width - 1, constraint.getValue());

        // slack variables
        if (constraint.getRelationship() == Relationship.LEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1);  // slack
        } else if (constraint.getRelationship() == Relationship.GEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
        }

        // artificial variables
        if ((constraint.getRelationship() == Relationship.EQ) ||
                (constraint.getRelationship() == Relationship.GEQ)) {
            matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
            matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
            matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
        }
    }

    return matrix;
}
 
Example 8
Source File: SimplexTableau.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create the tableau by itself.
 * @param maximize if true, goal is to maximize the objective function
 * @return created tableau
 */
protected RealMatrix createTableau(final boolean maximize) {

    // create a matrix of the correct size
    int width = numDecisionVariables + numSlackVariables +
    numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
    int height = constraints.size() + getNumObjectiveFunctions();
    Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);

    // initialize the objective function rows
    if (getNumObjectiveFunctions() == 2) {
        matrix.setEntry(0, 0, -1);
    }
    int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
    matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
    RealVector objectiveCoefficients =
        maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
    copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
    matrix.setEntry(zIndex, width - 1,
        maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());

    if (!restrictToNonNegative) {
        matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
            getInvertedCoefficientSum(objectiveCoefficients));
    }

    // initialize the constraint rows
    int slackVar = 0;
    int artificialVar = 0;
    for (int i = 0; i < constraints.size(); i++) {
        LinearConstraint constraint = constraints.get(i);
        int row = getNumObjectiveFunctions() + i;

        // decision variable coefficients
        copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);

        // x-
        if (!restrictToNonNegative) {
            matrix.setEntry(row, getSlackVariableOffset() - 1,
                getInvertedCoefficientSum(constraint.getCoefficients()));
        }

        // RHS
        matrix.setEntry(row, width - 1, constraint.getValue());

        // slack variables
        if (constraint.getRelationship() == Relationship.LEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1);  // slack
        } else if (constraint.getRelationship() == Relationship.GEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
        }

        // artificial variables
        if ((constraint.getRelationship() == Relationship.EQ) ||
                (constraint.getRelationship() == Relationship.GEQ)) {
            matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
            matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
            matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
        }
    }

    return matrix;
}
 
Example 9
Source File: SimplexTableau.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create the tableau by itself.
 * @param maximize if true, goal is to maximize the objective function
 * @return created tableau
 */
protected Array2DRowRealMatrix createTableau(final boolean maximize) {

    // create a matrix of the correct size
    int width = numDecisionVariables + numSlackVariables +
    numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
    int height = constraints.size() + getNumObjectiveFunctions();
    Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);

    // initialize the objective function rows
    if (getNumObjectiveFunctions() == 2) {
        matrix.setEntry(0, 0, -1);
    }

    int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
    matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
    RealVector objectiveCoefficients = maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
    copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
    matrix.setEntry(zIndex, width - 1, maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());

    if (!restrictToNonNegative) {
        matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
                        getInvertedCoefficientSum(objectiveCoefficients));
    }

    // initialize the constraint rows
    int slackVar = 0;
    int artificialVar = 0;
    for (int i = 0; i < constraints.size(); i++) {
        LinearConstraint constraint = constraints.get(i);
        int row = getNumObjectiveFunctions() + i;

        // decision variable coefficients
        copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);

        // x-
        if (!restrictToNonNegative) {
            matrix.setEntry(row, getSlackVariableOffset() - 1,
                            getInvertedCoefficientSum(constraint.getCoefficients()));
        }

        // RHS
        matrix.setEntry(row, width - 1, constraint.getValue());

        // slack variables
        if (constraint.getRelationship() == Relationship.LEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1);  // slack
        } else if (constraint.getRelationship() == Relationship.GEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
        }

        // artificial variables
        if ((constraint.getRelationship() == Relationship.EQ) ||
            (constraint.getRelationship() == Relationship.GEQ)) {
            matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
            matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
            matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
        }
    }

    return matrix;
}
 
Example 10
Source File: SimplexTableau.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create the tableau by itself.
 * @param maximize if true, goal is to maximize the objective function
 * @return created tableau
 */
protected RealMatrix createTableau(final boolean maximize) {

    // create a matrix of the correct size
    int width = numDecisionVariables + numSlackVariables +
    numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
    int height = constraints.size() + getNumObjectiveFunctions();
    Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);

    // initialize the objective function rows
    if (getNumObjectiveFunctions() == 2) {
        matrix.setEntry(0, 0, -1);
    }
    int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
    matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
    RealVector objectiveCoefficients =
        maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
    copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
    matrix.setEntry(zIndex, width - 1,
        maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());

    if (!restrictToNonNegative) {
        matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
            getInvertedCoefficientSum(objectiveCoefficients));
    }

    // initialize the constraint rows
    int slackVar = 0;
    int artificialVar = 0;
    for (int i = 0; i < constraints.size(); i++) {
        LinearConstraint constraint = constraints.get(i);
        int row = getNumObjectiveFunctions() + i;

        // decision variable coefficients
        copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);

        // x-
        if (!restrictToNonNegative) {
            matrix.setEntry(row, getSlackVariableOffset() - 1,
                getInvertedCoefficientSum(constraint.getCoefficients()));
        }

        // RHS
        matrix.setEntry(row, width - 1, constraint.getValue());

        // slack variables
        if (constraint.getRelationship() == Relationship.LEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1);  // slack
        } else if (constraint.getRelationship() == Relationship.GEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
        }

        // artificial variables
        if ((constraint.getRelationship() == Relationship.EQ) ||
                (constraint.getRelationship() == Relationship.GEQ)) {
            matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
            matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
            matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
        }
    }

    return matrix;
}
 
Example 11
Source File: 1_SimplexTableau.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create the tableau by itself.
 * @param maximize if true, goal is to maximize the objective function
 * @return created tableau
 */
protected RealMatrix createTableau(final boolean maximize) {

    // create a matrix of the correct size
    int width = numDecisionVariables + numSlackVariables +
    numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
    int height = constraints.size() + getNumObjectiveFunctions();
    Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);

    // initialize the objective function rows
    if (getNumObjectiveFunctions() == 2) {
        matrix.setEntry(0, 0, -1);
    }
    int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
    matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
    RealVector objectiveCoefficients =
        maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
    copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
    matrix.setEntry(zIndex, width - 1,
        maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());

    if (!restrictToNonNegative) {
        matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
            getInvertedCoefficientSum(objectiveCoefficients));
    }

    // initialize the constraint rows
    int slackVar = 0;
    int artificialVar = 0;
    for (int i = 0; i < constraints.size(); i++) {
        LinearConstraint constraint = constraints.get(i);
        int row = getNumObjectiveFunctions() + i;

        // decision variable coefficients
        copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);

        // x-
        if (!restrictToNonNegative) {
            matrix.setEntry(row, getSlackVariableOffset() - 1,
                getInvertedCoefficientSum(constraint.getCoefficients()));
        }

        // RHS
        matrix.setEntry(row, width - 1, constraint.getValue());

        // slack variables
        if (constraint.getRelationship() == Relationship.LEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1);  // slack
        } else if (constraint.getRelationship() == Relationship.GEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
        }

        // artificial variables
        if ((constraint.getRelationship() == Relationship.EQ) ||
                (constraint.getRelationship() == Relationship.GEQ)) {
            matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
            matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
            matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
        }
    }

    return matrix;
}
 
Example 12
Source File: Math_33_SimplexTableau_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Create the tableau by itself.
 * @param maximize if true, goal is to maximize the objective function
 * @return created tableau
 */
protected RealMatrix createTableau(final boolean maximize) {

    // create a matrix of the correct size
    int width = numDecisionVariables + numSlackVariables +
    numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
    int height = constraints.size() + getNumObjectiveFunctions();
    Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);

    // initialize the objective function rows
    if (getNumObjectiveFunctions() == 2) {
        matrix.setEntry(0, 0, -1);
    }
    int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
    matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
    RealVector objectiveCoefficients =
        maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
    copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
    matrix.setEntry(zIndex, width - 1,
        maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());

    if (!restrictToNonNegative) {
        matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
            getInvertedCoefficientSum(objectiveCoefficients));
    }

    // initialize the constraint rows
    int slackVar = 0;
    int artificialVar = 0;
    for (int i = 0; i < constraints.size(); i++) {
        LinearConstraint constraint = constraints.get(i);
        int row = getNumObjectiveFunctions() + i;

        // decision variable coefficients
        copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);

        // x-
        if (!restrictToNonNegative) {
            matrix.setEntry(row, getSlackVariableOffset() - 1,
                getInvertedCoefficientSum(constraint.getCoefficients()));
        }

        // RHS
        matrix.setEntry(row, width - 1, constraint.getValue());

        // slack variables
        if (constraint.getRelationship() == Relationship.LEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1);  // slack
        } else if (constraint.getRelationship() == Relationship.GEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
        }

        // artificial variables
        if ((constraint.getRelationship() == Relationship.EQ) ||
                (constraint.getRelationship() == Relationship.GEQ)) {
            matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
            matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
            matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
        }
    }

    return matrix;
}
 
Example 13
Source File: Elixir_0024_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Create the tableau by itself.
 * @param maximize if true, goal is to maximize the objective function
 * @return created tableau
 */
protected RealMatrix createTableau(final boolean maximize) {

    // create a matrix of the correct size
    int width = numDecisionVariables + numSlackVariables +
    numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
    int height = constraints.size() + getNumObjectiveFunctions();
    Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);

    // initialize the objective function rows
    if (getNumObjectiveFunctions() == 2) {
        matrix.setEntry(0, 0, -1);
    }
    int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
    matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
    RealVector objectiveCoefficients =
        maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
    copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
    matrix.setEntry(zIndex, width - 1,
        maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());

    if (!restrictToNonNegative) {
        matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
            getInvertedCoefficientSum(objectiveCoefficients));
    }

    // initialize the constraint rows
    int slackVar = 0;
    int artificialVar = 0;
    for (int i = 0; i < constraints.size(); i++) {
        LinearConstraint constraint = constraints.get(i);
        int row = getNumObjectiveFunctions() + i;

        // decision variable coefficients
        copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);

        // x-
        if (!restrictToNonNegative) {
            matrix.setEntry(row, getSlackVariableOffset() - 1,
                getInvertedCoefficientSum(constraint.getCoefficients()));
        }

        // RHS
        matrix.setEntry(row, width - 1, constraint.getValue());

        // slack variables
        if (constraint.getRelationship() == Relationship.LEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1);  // slack
        } else if (constraint.getRelationship() == Relationship.GEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
        }

        // artificial variables
        if ((constraint.getRelationship() == Relationship.EQ) ||
                (constraint.getRelationship() == Relationship.GEQ)) {
            matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
            matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
            matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
        }
    }

    return matrix;
}
 
Example 14
Source File: Elixir_0024_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Create the tableau by itself.
 * @param maximize if true, goal is to maximize the objective function
 * @return created tableau
 */
protected RealMatrix createTableau(final boolean maximize) {

    // create a matrix of the correct size
    int width = numDecisionVariables + numSlackVariables +
    numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
    int height = constraints.size() + getNumObjectiveFunctions();
    Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);

    // initialize the objective function rows
    if (getNumObjectiveFunctions() == 2) {
        matrix.setEntry(0, 0, -1);
    }
    int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
    matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
    RealVector objectiveCoefficients =
        maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
    copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
    matrix.setEntry(zIndex, width - 1,
        maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());

    if (!restrictToNonNegative) {
        matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
            getInvertedCoefficientSum(objectiveCoefficients));
    }

    // initialize the constraint rows
    int slackVar = 0;
    int artificialVar = 0;
    for (int i = 0; i < constraints.size(); i++) {
        LinearConstraint constraint = constraints.get(i);
        int row = getNumObjectiveFunctions() + i;

        // decision variable coefficients
        copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);

        // x-
        if (!restrictToNonNegative) {
            matrix.setEntry(row, getSlackVariableOffset() - 1,
                getInvertedCoefficientSum(constraint.getCoefficients()));
        }

        // RHS
        matrix.setEntry(row, width - 1, constraint.getValue());

        // slack variables
        if (constraint.getRelationship() == Relationship.LEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1);  // slack
        } else if (constraint.getRelationship() == Relationship.GEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
        }

        // artificial variables
        if ((constraint.getRelationship() == Relationship.EQ) ||
                (constraint.getRelationship() == Relationship.GEQ)) {
            matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
            matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
            matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
        }
    }

    return matrix;
}
 
Example 15
Source File: Nopol2017_0066_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Create the tableau by itself.
 * @param maximize if true, goal is to maximize the objective function
 * @return created tableau
 */
protected RealMatrix createTableau(final boolean maximize) {

    // create a matrix of the correct size
    int width = numDecisionVariables + numSlackVariables +
    numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
    int height = constraints.size() + getNumObjectiveFunctions();
    Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);

    // initialize the objective function rows
    if (getNumObjectiveFunctions() == 2) {
        matrix.setEntry(0, 0, -1);
    }
    int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
    matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
    RealVector objectiveCoefficients =
        maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
    copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
    matrix.setEntry(zIndex, width - 1,
        maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());

    if (!restrictToNonNegative) {
        matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
            getInvertedCoefficientSum(objectiveCoefficients));
    }

    // initialize the constraint rows
    int slackVar = 0;
    int artificialVar = 0;
    for (int i = 0; i < constraints.size(); i++) {
        LinearConstraint constraint = constraints.get(i);
        int row = getNumObjectiveFunctions() + i;

        // decision variable coefficients
        copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);

        // x-
        if (!restrictToNonNegative) {
            matrix.setEntry(row, getSlackVariableOffset() - 1,
                getInvertedCoefficientSum(constraint.getCoefficients()));
        }

        // RHS
        matrix.setEntry(row, width - 1, constraint.getValue());

        // slack variables
        if (constraint.getRelationship() == Relationship.LEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1);  // slack
        } else if (constraint.getRelationship() == Relationship.GEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
        }

        // artificial variables
        if ((constraint.getRelationship() == Relationship.EQ) ||
                (constraint.getRelationship() == Relationship.GEQ)) {
            matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
            matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
            matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
        }
    }

    return matrix;
}
 
Example 16
Source File: Nopol2017_0066_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Create the tableau by itself.
 * @param maximize if true, goal is to maximize the objective function
 * @return created tableau
 */
protected RealMatrix createTableau(final boolean maximize) {

    // create a matrix of the correct size
    int width = numDecisionVariables + numSlackVariables +
    numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
    int height = constraints.size() + getNumObjectiveFunctions();
    Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);

    // initialize the objective function rows
    if (getNumObjectiveFunctions() == 2) {
        matrix.setEntry(0, 0, -1);
    }
    int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
    matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
    RealVector objectiveCoefficients =
        maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
    copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
    matrix.setEntry(zIndex, width - 1,
        maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());

    if (!restrictToNonNegative) {
        matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
            getInvertedCoefficientSum(objectiveCoefficients));
    }

    // initialize the constraint rows
    int slackVar = 0;
    int artificialVar = 0;
    for (int i = 0; i < constraints.size(); i++) {
        LinearConstraint constraint = constraints.get(i);
        int row = getNumObjectiveFunctions() + i;

        // decision variable coefficients
        copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);

        // x-
        if (!restrictToNonNegative) {
            matrix.setEntry(row, getSlackVariableOffset() - 1,
                getInvertedCoefficientSum(constraint.getCoefficients()));
        }

        // RHS
        matrix.setEntry(row, width - 1, constraint.getValue());

        // slack variables
        if (constraint.getRelationship() == Relationship.LEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1);  // slack
        } else if (constraint.getRelationship() == Relationship.GEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
        }

        // artificial variables
        if ((constraint.getRelationship() == Relationship.EQ) ||
                (constraint.getRelationship() == Relationship.GEQ)) {
            matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
            matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
            matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
        }
    }

    return matrix;
}
 
Example 17
Source File: Cardumen_0041_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Create the tableau by itself.
 * @param maximize if true, goal is to maximize the objective function
 * @return created tableau
 */
protected RealMatrix createTableau(final boolean maximize) {

    // create a matrix of the correct size
    int width = numDecisionVariables + numSlackVariables +
    numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
    int height = constraints.size() + getNumObjectiveFunctions();
    Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);

    // initialize the objective function rows
    if (getNumObjectiveFunctions() == 2) {
        matrix.setEntry(0, 0, -1);
    }
    int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
    matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
    RealVector objectiveCoefficients =
        maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
    copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
    matrix.setEntry(zIndex, width - 1,
        maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());

    if (!restrictToNonNegative) {
        matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
            getInvertedCoefficientSum(objectiveCoefficients));
    }

    // initialize the constraint rows
    int slackVar = 0;
    int artificialVar = 0;
    for (int i = 0; i < constraints.size(); i++) {
        LinearConstraint constraint = constraints.get(i);
        int row = getNumObjectiveFunctions() + i;

        // decision variable coefficients
        copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);

        // x-
        if (!restrictToNonNegative) {
            matrix.setEntry(row, getSlackVariableOffset() - 1,
                getInvertedCoefficientSum(constraint.getCoefficients()));
        }

        // RHS
        matrix.setEntry(row, width - 1, constraint.getValue());

        // slack variables
        if (constraint.getRelationship() == Relationship.LEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1);  // slack
        } else if (constraint.getRelationship() == Relationship.GEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
        }

        // artificial variables
        if ((constraint.getRelationship() == Relationship.EQ) ||
                (constraint.getRelationship() == Relationship.GEQ)) {
            matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
            matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
            matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
        }
    }

    return matrix;
}
 
Example 18
Source File: Cardumen_0041_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Create the tableau by itself.
 * @param maximize if true, goal is to maximize the objective function
 * @return created tableau
 */
protected RealMatrix createTableau(final boolean maximize) {

    // create a matrix of the correct size
    int width = numDecisionVariables + numSlackVariables +
    numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
    int height = constraints.size() + getNumObjectiveFunctions();
    Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);

    // initialize the objective function rows
    if (getNumObjectiveFunctions() == 2) {
        matrix.setEntry(0, 0, -1);
    }
    int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
    matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
    RealVector objectiveCoefficients =
        maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
    copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
    matrix.setEntry(zIndex, width - 1,
        maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());

    if (!restrictToNonNegative) {
        matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
            getInvertedCoefficientSum(objectiveCoefficients));
    }

    // initialize the constraint rows
    int slackVar = 0;
    int artificialVar = 0;
    for (int i = 0; i < constraints.size(); i++) {
        LinearConstraint constraint = constraints.get(i);
        int row = getNumObjectiveFunctions() + i;

        // decision variable coefficients
        copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);

        // x-
        if (!restrictToNonNegative) {
            matrix.setEntry(row, getSlackVariableOffset() - 1,
                getInvertedCoefficientSum(constraint.getCoefficients()));
        }

        // RHS
        matrix.setEntry(row, width - 1, constraint.getValue());

        // slack variables
        if (constraint.getRelationship() == Relationship.LEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1);  // slack
        } else if (constraint.getRelationship() == Relationship.GEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
        }

        // artificial variables
        if ((constraint.getRelationship() == Relationship.EQ) ||
                (constraint.getRelationship() == Relationship.GEQ)) {
            matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
            matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
            matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
        }
    }

    return matrix;
}
 
Example 19
Source File: 1_SimplexTableau.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create the tableau by itself.
 * @param maximize if true, goal is to maximize the objective function
 * @return created tableau
 */
protected RealMatrix createTableau(final boolean maximize) {

    // create a matrix of the correct size
    int width = numDecisionVariables + numSlackVariables +
    numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
    int height = constraints.size() + getNumObjectiveFunctions();
    Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);

    // initialize the objective function rows
    if (getNumObjectiveFunctions() == 2) {
        matrix.setEntry(0, 0, -1);
    }
    int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
    matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
    RealVector objectiveCoefficients =
        maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
    copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
    matrix.setEntry(zIndex, width - 1,
        maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());

    if (!restrictToNonNegative) {
        matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
            getInvertedCoefficientSum(objectiveCoefficients));
    }

    // initialize the constraint rows
    int slackVar = 0;
    int artificialVar = 0;
    for (int i = 0; i < constraints.size(); i++) {
        LinearConstraint constraint = constraints.get(i);
        int row = getNumObjectiveFunctions() + i;

        // decision variable coefficients
        copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);

        // x-
        if (!restrictToNonNegative) {
            matrix.setEntry(row, getSlackVariableOffset() - 1,
                getInvertedCoefficientSum(constraint.getCoefficients()));
        }

        // RHS
        matrix.setEntry(row, width - 1, constraint.getValue());

        // slack variables
        if (constraint.getRelationship() == Relationship.LEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1);  // slack
        } else if (constraint.getRelationship() == Relationship.GEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
        }

        // artificial variables
        if ((constraint.getRelationship() == Relationship.EQ) ||
                (constraint.getRelationship() == Relationship.GEQ)) {
            matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
            matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
            matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
        }
    }

    return matrix;
}
 
Example 20
Source File: SimplexTableau.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create the tableau by itself.
 * @param maximize if true, goal is to maximize the objective function
 * @return created tableau
 */
protected RealMatrix createTableau(final boolean maximize) {

    // create a matrix of the correct size
    int width = numDecisionVariables + numSlackVariables +
    numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
    int height = constraints.size() + getNumObjectiveFunctions();
    Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);

    // initialize the objective function rows
    if (getNumObjectiveFunctions() == 2) {
        matrix.setEntry(0, 0, -1);
    }
    int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
    matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
    RealVector objectiveCoefficients =
        maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
    copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
    matrix.setEntry(zIndex, width - 1,
        maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());

    if (!restrictToNonNegative) {
        matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
            getInvertedCoefficientSum(objectiveCoefficients));
    }

    // initialize the constraint rows
    int slackVar = 0;
    int artificialVar = 0;
    for (int i = 0; i < constraints.size(); i++) {
        LinearConstraint constraint = constraints.get(i);
        int row = getNumObjectiveFunctions() + i;

        // decision variable coefficients
        copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);

        // x-
        if (!restrictToNonNegative) {
            matrix.setEntry(row, getSlackVariableOffset() - 1,
                getInvertedCoefficientSum(constraint.getCoefficients()));
        }

        // RHS
        matrix.setEntry(row, width - 1, constraint.getValue());

        // slack variables
        if (constraint.getRelationship() == Relationship.LEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1);  // slack
        } else if (constraint.getRelationship() == Relationship.GEQ) {
            matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
        }

        // artificial variables
        if ((constraint.getRelationship() == Relationship.EQ) ||
                (constraint.getRelationship() == Relationship.GEQ)) {
            matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
            matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
            matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
        }
    }

    return matrix;
}