[gecode-users] getting all solutions + interrupting gecode

Filip Konvička filip.konvicka at logis.cz
Fri Dec 5 14:44:27 CET 2008


Hi,

> For the first question, one thing you must remember is to delete the
> original instance of the problem also. An easy way to write a loop
> that goes through all solutions is for example:
>     MyProblem *root = new MyProblem();
>     DFS<MyProblem> dfs(root);
>     delete root;
>     while (MyProblem *sol = dfs.next()) {
>       // do something...
>       delete sol;
>     }

Just a technical note - this is quite error-prone, because throwing 
exception or returning in "do something..." results in memory leaks.

A better way of doing this would be using auto_ptr (or shared_ptr), like 
this:

typedef auto_ptr<MyProblem> APMyProblem;
APMyProblem root(new MyProblem());
DFS<MyProblem> dfs(root.get());
root.reset();
while(true) {
   APMyProblem sol(dfs.next());
   if ( !sol.get() )
     break;
   MyProblem& solution=*sol;
   // do something...
}

Cheers,
Filip





More information about the gecode-users mailing list