当前位置: 首页>>代码示例>>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;未经允许,请勿转载。