當前位置: 首頁>>代碼示例>>Java>>正文


Java Array2DRowRealMatrix.setRowVector方法代碼示例

本文整理匯總了Java中org.apache.commons.math3.linear.Array2DRowRealMatrix.setRowVector方法的典型用法代碼示例。如果您正苦於以下問題:Java Array2DRowRealMatrix.setRowVector方法的具體用法?Java Array2DRowRealMatrix.setRowVector怎麽用?Java Array2DRowRealMatrix.setRowVector使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.commons.math3.linear.Array2DRowRealMatrix的用法示例。


在下文中一共展示了Array2DRowRealMatrix.setRowVector方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: createTableau

import org.apache.commons.math3.linear.Array2DRowRealMatrix; //導入方法依賴的package包/類
/**
 * 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;
}
 
開發者ID:biocompibens,項目名稱:SME,代碼行數:68,代碼來源:SimplexTableau.java

示例2: createTableau

import org.apache.commons.math3.linear.Array2DRowRealMatrix; //導入方法依賴的package包/類
/**
 * 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;
}
 
開發者ID:biocompibens,項目名稱:SME,代碼行數:67,代碼來源:SimplexTableau.java


注:本文中的org.apache.commons.math3.linear.Array2DRowRealMatrix.setRowVector方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。