Solve
Add the parts of the application that solve the problem.
After you have declared the decision variables and added the constraints and objective function to the model, your application is ready to search for a solution.
Step 9: Search for a solution
Go to Step 9 in the file, and add this line to make your application search for a solution.
if ( cplex.Solve() ) {
Step 10: Display the solution
Go to the comment Step 10 in the file, and add these lines to enable your application to display any solution found in Step 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]);
}
}
Step 11: Save the model to a file
If you want to save your model to a file in LP format, go to the comment Step 11 in your application file, and add this line.
cplex.ExportModel(“lpex1.lp”);
If you have followed the steps in this tutorial interactively, you now have a complete application that you can compile and execute.