0
votes

I am programming a mathematical model in Java using the Cplex library and I wanted to know if there are predefined functions to determine if the solution obtained with Cplex is feasible or optimal.

Thanks a lot for your help.

1
What about the return value of solve() and the return value of getStatus()? - Progman
If CPLEX has found a solution, it will be feasible (subject to the small feasibility tolerance). Proving true optimality for mixed integer problems can be difficult, but if the search is ended early you should be able to get the gap between the best known solution and the best bound and hence know that you are at least within a known distance of the best possible solution. - TimChippingtonDerrick

1 Answers

1
votes

The IloCplex.solve() method returns:

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.

You can check IloCplex.getStatus() to determine if the solve was feasible, optimal, etc.

There are many examples that come with CPLEX that show how to check these models. For example, consider the following snippet from 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]);
    }
 }