我正在使用 Cplex 库用 Java 编写数学模型,我想知道是否有预定义函数来确定使用 Cplex 获得的解决方案是否可行或最优。
非常感谢您的帮助。
最佳答案
IloCplex.solve()方法返回:
A Boolean value reporting whether a feasible solution has been found. This solution is not necessarily optimal. If false is returned, a feasible solution may still be present, but IloCplex has not been able to prove its feasibility.
您可以查看IloCplex.getStatus()确定解决方案是否可行、最优等。
CPLEX 附带了许多示例来说明如何检查这些模型。例如,考虑 LPex1.java 中的以下代码片段:
// solve the model and display the solution if one was found
if ( cplex.solve() ) {
double[] x = cplex.getValues(var[0]);
double[] dj = cplex.getReducedCosts(var[0]);
double[] pi = cplex.getDuals(rng[0]);
double[] slack = cplex.getSlacks(rng[0]);
cplex.output().println("Solution status = " + cplex.getStatus());
cplex.output().println("Solution value = " + cplex.getObjValue());
int nvars = x.length;
for (int j = 0; j < nvars; ++j) {
cplex.output().println("Variable " + j +
": Value = " + x[j] +
" Reduced cost = " + dj[j]);
}
int ncons = slack.length;
for (int i = 0; i < ncons; ++i) {
cplex.output().println("Constraint " + i +
": Slack = " + slack[i] +
" Pi = " + pi[i]);
}
}
关于java - Java Cplex 中的最优解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56613368/