IBM ILOG Solver User's Manual > Developing Solver Applications > Designing Models > Use a known solution for testing |
Use a known solution for testing |
INDEX
![]() |
The previous sections have explained how to design good models for various problems. However, if errors slip in when you implement the constraints of the problem, it can be very difficult to understand why the application finds wrong "solutions," even though the model is very good.
There is a simple way to fix such cases: use an instance of the problem with a known solution to test against the constraints. For testing a solution, you just have to assign the values corresponding to a solution before posting the constraints. Then no failure should happen within this function. If a failure occurs with the known solution, you know there is a problem among the constraints.
To find the source of the problem, use a trace event to locate the faulty constraint. See Chapter 34, Debugging and Tracing for more information.
For example, let's test a known solution of the map-coloring problem.
#include <ilsolver/ilosolver.h> ILOSTLBEGIN const char* Names[] = {"blue", "white", "red", "green"}; int main(){ IloEnv env; try { IloModel model(env); IloIntVar Belgium(env, 0, 3), Denmark(env, 0, 3), France(env, 0, 3), Germany(env, 0, 3), Netherlands(env, 0, 3), Luxembourg(env, 0, 3); // Test a solution model.add(Belgium == 0); model.add(Denmark == 1); model.add(France == 1); model.add(Germany == 0); model.add(Netherlands == 1); model.add(Luxembourg == 2); // Constraints model.add(Belgium != France); model.add(Denmark != Germany ); model.add(France != Germany); model.add(Belgium != Netherlands); model.add(Germany != Netherlands); model.add(France != Luxembourg); model.add(Luxembourg != Germany); model.add(Luxembourg != Belgium); IloSolver solver(model); // Search for a solution if (solver.solve()) { solver.out() << solver.getStatus() << " Solution" << endl; solver.out() << "Belgium: " << Names[(IloInt)solver.getValue(Belgium)] << endl; solver.out() << "Denmark: " << Names[(IloInt)solver.getValue(Denmark)] << endl; solver.out() << "France: " << Names[(IloInt)solver.getValue(France)] << endl; solver.out() << "Germany: " << Names[(IloInt)solver.getValue(Germany)] << endl; solver.out() << "Netherlands: " << Names[(IloInt)solver.getValue(Netherlands)] << endl; solver.out() << "Luxembourg: " << Names[(IloInt)solver.getValue(Luxembourg)] << endl; } } catch (IloException& ex) { cerr << "Error: " << ex << endl; } env.end(); return 0; }
© Copyright IBM Corp. 1987, 2009. Legal terms. | PREVIOUS NEXT |