我试图通过使用来自apache-commons的Simplex解算器来解决以下线性问题:org.apache.commons.math3.optim.linear.SimplexSolver

java - Apache公共(public)SimplexSolver ObjectiveFunction,用于最大化矩阵中的值之和-LMLPHP
n是行数m是列数L是每行总和的全局限制

这是我到目前为止的内容:

List<LinearConstraint> constraints = new ArrayList<>();

double[][] A = calculateAValues();
// m = count of columns
// constraint 1: the sum of values in all column must be <= 1
for(int i = 0; i < m; i++) {
    double[] v = new double[n];
    for(int j=0; j < n; j++) {
        v[j] = 1;
    }
    constraints.add(new LinearConstraint(v, Relationship.LEQ, 1));
}
// n = count of rows
// constraint 2: sum of a_i,j in all row must be <= L (Limit)
for(int i = 0; i < n; i++) {
    double[] v = new double[m];
    for(int j=0; j < m; j++) {
        v[j] =  A[i][j];
    }
    constraints.add(new LinearConstraint(v, Relationship.LEQ, L));
}

double[] objectiveCoefficients = new double[n * m];
for(int i = 0; i < n * m; ++i) {
    objectiveCoefficients[i] = 1;
}

LinearObjectiveFunction objective = new LinearObjectiveFunction(objectiveCoefficients, 0);
LinearConstraintSet constraintSet = new LinearConstraintSet(constraints);

SimplexSolver solver = new SimplexSolver();
PointValuePair solution = solver.optimize(objective, constraintSet, GoalType.MAXIMIZE);
return solution.getValue();

我很难正确设置目标函数,也可能缺少其他一些东西。到目前为止,我的所有尝试都产生了UnboundedSolutionException

最佳答案

误差似乎在线性约束的系数数组中。

您具有n*m变量,因此约束和目标函数的系数数组必须具有n*m长度。不幸的是,如果SimplexSolver比目标函数的数组短,x_ij <= 1会无声地扩展约束数组。因此,您的代码没有指定正确的约束条件,从而导致解决方案无止境。

约束1:所有列中值的总和必须

for(int j=0; j<m; j++)
{
    double[] v = new double[n*m];
    for(int i=0; i<n; i++)
        v[i*n + j] = 1;
    constraints.add(new LinearConstraint(v, Relationship.LEQ, 1));
}

约束2:所有行中a_i,j的总和必须
// n = count of rows
for(int i=0; i<n; i++)
{
    double[] v = new double[n*m];
    for(int j=0; j<m; j++)
        v[i*n + j] = A[i][j];
    constraints.add(new LinearConstraint(v, Relationship.LEQ, L));
}

目标对象:
double[] objectiveCoefficients = new double[n * m];
Arrays.fill(objectiveCoefficients, 1.0);
LinearObjectiveFunction objective = LinearObjectiveFunction(objectiveCoefficients, 0);

由于约束2,约束0 <= x_ij已经满足。
也许还可以使用NonNegativeConstraint明确指定ojit_code的约束,这使事情变得更加清楚:
SimplexSolver solver = new SimplexSolver();
PointValuePair solution = solver.optimize(objective, constraintSet,
     GoalType.MAXIMIZE, new NonNegativeConstraint(true));

关于java - Apache公共(public)SimplexSolver ObjectiveFunction,用于最大化矩阵中的值之和,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33328640/

10-12 17:02