Hi Abbas,
Here is an example of using the conflict refiner using the Java API:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import ilog.concert.*;
import ilog.cplex.*;
/**
* Read a problem and use the conflict refiner.
*
* See the usage message below for more details.
*/
public class ConflictEx1 {
/**
* Display a usage message and exit.
*/
static void usage() {
System.out.println("Usage: java ConflictEx1 filename");
System.out.println(" filename Name of a file, with .mps, .lp, or .sav");
System.out.println(" extension, and a possible, additional .gz");
System.out.println(" extension");
System.exit(2);
}
/**
* Invoke the conflict refiner on a model and display the results.
*/
public static void main(String[] args) throws Exception {
if (args.length != 1) {
usage();
}
// Create the modeler/solver object
try (IloCplex cplex = new IloCplex()) {
// Read model from file with name args[0] into cplex optimizer object.
cplex.importModel(args[0]);
// Access the IloLPMatrix object that has been read from a file in
// order to access variables which are the columns of the LP. The
// method importModel() guarantees that exactly one IloLPMatrix
// object will exist, which is why no tests or iterators are
// needed in the following line of code.
IloLPMatrix lp = (IloLPMatrix)cplex.LPMatrixIterator().next();
IloRange[] rng = lp.getRanges();
// Calculate the number of non-boolean variables.
final int numVars = cplex.getNcols() - cplex.getNbinVars();
// Find the number of SOSs in the model.
final int numSOS = cplex.getNSOSs();
// Gather array of constraints to be considered by the conflict
// refiner.
List<IloConstraint> constraints = new ArrayList<IloConstraint>();
for (int i = 0; i < rng.length; ++i) {
constraints.add(rng[i]);
}
// Add variable bounds to the constraints array.
for (IloNumVar v : lp.getNumVars()) {
if (v.getType() != IloNumVarType.Bool) {
constraints.add(cplex.lowerBound(v));
constraints.add(cplex.upperBound(v));
}
}
// Add SOSs to the constraints array.
if (numSOS > 0) {
Iterator s1 = cplex.SOS1iterator();
while (s1.hasNext()) {
IloSOS1 cur = (IloSOS1)s1.next();
constraints.add(cur);
}
Iterator s2 = cplex.SOS2iterator();
while (s2.hasNext()) {
IloSOS2 cur = (IloSOS2)s2.next();
constraints.add(cur);
}
}
// Define preferences for the constraints. Here, we give all
// constraints a preference of 1.0, so they will be treated
// equally.
double[] prefs = new double[constraints.size()];
for (int i = 0; i < prefs.length; ++i) {
prefs[i] = 1.0;
}
IloConstraint[] cons = constraints.toArray(
new IloConstraint[constraints.size()]);
// Run the conflict refiner. As opposed to letting the conflict
// refiner run to completion (as is done here), the user can set
// a resource limit (e.g., a time limit, an iteration limit, or
// node limit) and still potentially get a "possible" conflict.
if (cplex.refineConflict(cons, prefs)) {
// Display the solution status.
System.out.println("Solution status = " + cplex.getCplexStatus());
// Get the conflict status for the constraints that were specified.
IloCplex.ConflictStatus[] conflict = cplex.getConflict(cons);
// Count the number of conflicts found for each constraint group and
// print the results.
int numConConflicts = 0;
int numBoundConflicts = 0;
int numSOSConflicts = 0;
for (int i = 0; i < cons.length; ++i) {
IloConstraint c = cons[i];
if (conflict[i] == IloCplex.ConflictStatus.Member ||
conflict[i] == IloCplex.ConflictStatus.PossibleMember) {
if (c instanceof IloRange)
numConConflicts++;
else if (c instanceof IloNumVarBound)
numBoundConflicts++;
else
numSOSConflicts++;
}
}
// Display a conflict summary.
System.out.println("Conflict Summary:");
System.out.println(" Constraint conflicts = " + numConConflicts);
System.out.println(" Variable Bound conflicts = " + numBoundConflicts);
System.out.println(" SOS conflicts = " + numSOSConflicts);
// Write the identified conflict in the LP format.
final String confFile = "ConflictEx1.java.lp";
System.out.printf("Writing conflict file to '%s'....%n", confFile);
cplex.writeConflict(confFile);
// Display the entire conflict subproblem.
try (BufferedReader br = new BufferedReader(new FileReader(confFile))) {
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}
else {
System.out.println("A conflict was not identified.");
System.out.println("Exiting....");
return;
}
}
}
}
#CPLEXOptimizers#DecisionOptimization