求解
添加应用程序中用于对问题求解的部分。
声明决策变量并将约束和目标函数添加到模型后,应用程序即可搜索解法。
步骤 9:搜索解法
转至文件中的步骤 9,然后添加以下行以使应用程序搜索解法。
if ( cplex.Solve() ) {
步骤 10:显示解法
转至文件中的注释步骤 10,然后添加以下行以使应用程序能够显示在步骤 9 中找到的任何解法。
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().WriteLine(“Solution status = “
+ cplex.GetStatus());
cplex.Output().WriteLine(“Solution value = “
+ cplex.ObjValue);
int nvars = x.Length;
for (int j = 0; j < nvars; ++j) {
cplex.Output().WriteLine(“Variable :”
+ j
+” Value = “
+ x[j]
+” Reduced cost = “
+ dj[j]);
}
int ncons = slack.Length;
for (int i = 0; i < ncons; ++i) {
cplex.Output().WriteLine(“Constraint:”
+ i
+” Slack = “
+ slack[i]
+” Pi = “
+ pi[i]);
}
}
步骤 11:将模型保存到文件
如果要将模型保存到 LP 格式的文件,请转至应用程序文件中的注释步骤 11,然后添加以下行。
cplex.ExportModel(“lpex1.lp”);
如果您以交互方式执行了本教程中的步骤,那么现在您将具有可以编译和执行的完整应用程序。