toulbar2
Solving cost function networks

After creating a Weighted CSP, it can be solved using a local search method INCOP (see WeightedCSPSolver::narycsp) and/or an exact search method (see WeightedCSPSolver::solve).
Various options of the solving methods are controlled by ::Toulbar2 static class members (see files ./src/core/tb2types.hpp and ./src/tb2main.cpp).
A brief code example reading a wcsp problem given as a single command-line parameter and solving it:

#include "toulbar2lib.hpp"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv) {
tb2init(); // must be call before setting specific ToulBar2 options and creating a model
// Create a solver object
initCosts(); // last check for compatibility issues between ToulBar2 options and Cost data-type
// Read a problem file in wcsp format
solver->read_wcsp(argv[1]);
ToulBar2::verbose = -1; // change to 0 or higher values to see more trace information
// Uncomment if solved using INCOP local search followed by a partial Limited Discrepancy Search with a maximum discrepancy of one
// ToulBar2::incop_cmd = "0 1 3 idwa 100000 cv v 0 200 1 0 0";
// ToulBar2::lds = -1; // remove it or change to a positive value then the search continues by a complete B&B search method
// Uncomment the following lines if solved using Decomposition Guided Variable Neighborhood Search with min-fill cluster decomposition and absorption
// ToulBar2::lds = 4;
// ToulBar2::restart = 10000;
// ToulBar2::searchMethod = DGVNS;
// ToulBar2::vnsNeighborVarHeur = CLUSTERRAND;
// ToulBar2::boostingBTD = 0.7;
// ToulBar2::varOrder = reinterpret_cast<char*>(-3);
if (solver->solve()) {
// show (sub-)optimal solution
vector<Value> sol;
Cost ub = solver->getSolution(sol);
cout << "Best solution found cost: " << ub << endl;
cout << "Best solution found:";
for (unsigned int i=0; i<sol.size(); i++) cout << ((i>0)?",":"") << " x" << i << " = " << sol[i];
cout << endl;
} else {
cout << "No solution found!" << endl;
}
delete solver;
}
Definition toulbar2lib.hpp:458
virtual Cost read_wcsp(const char *fileName)=0
reads a Cost function network from a file (format as indicated by ToulBar2:: global variables)
static WeightedCSPSolver * makeWeightedCSPSolver(Cost initUpperBound)
WeightedCSP Solver factory.
Definition tb2solver.cpp:30
virtual bool solve(bool first=true)=0
simplifies and solves to optimality the problem
virtual const vector< Value > getSolution()=0
after solving the problem, return the optimal solution (warning! do not use it if doing solution coun...
See also
another code example in ./src/toulbar2test.cpp
Warning
variable domains must start at zero, otherwise recompile libtb2.so without flag WCSPFORMATONLY