[gecode-users] getting all solutions + interrupting gecode

Mikael Zayenz Lagerkvist zayenz at gmail.com
Fri Dec 5 14:12:10 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;
    }

As for question 2, t is probably not a valid Space most of the time
when you might call the signal handler (the code is between deletion
of the Space and having a new solution computed). A cleaner interface
would be to write your own stop-object that stops the search when some
variable is set due to the signal handler having been called.

Cheers,
Mikael

On Fri, Dec 5, 2008 at 1:11 PM, Marco Correia <marco.v.correia at gmail.com> wrote:
> hi!
>
> Q1:
>
> I have a problem for which I want to get all solutions. I have departed from
> one of the examples, but now I'm subclassing from the object Space directly.
> I have the feeling I'm not doing things right:
>
> class MyProblem : public Space
> { /* ... */}
>
> int
> main(int argc, char* argv[])
> {
>   MyProblem* t = new MyProblem(args);
>   d = new DFS<MyProblem>(t);
>   while (t != NULL)
>   {
>        //t = d->next();        // with only this, it does not seem to release memory
>        TestLinear* n = d->next();
>        delete t;       // hum....
>        t = n;
>   }
>   return 0;
> }
>
> Q2:
>
> I'm registering a signal before the 'new MyProblem' line, to make gecode quit
> when I press Ctrl+C and show me the search statistics:
>
> signal(SIGINT, atExit);
>
> In the function atExit I use the methods from t and d (which I make global
> variables) to get the statistics. The last line of this function is a system
> call to exit(0).
>
> It seems to work sometimes, but other times it just crashes on the exit(0).
> Obviously I'm forgeting something, so I guess the solution to this is simple.
> Otherwise I can provide more details.
>
>
> Thanks!
> Marco
>
> --
> Marco Correia <mvc at netcabo.pt>
>
> _______________________________________________
> Gecode users mailing list
> users at gecode.org
> https://www.gecode.org/mailman/listinfo/gecode-users
>



-- 
Mikael Zayenz Lagerkvist, http://www.ict.kth.se/~zayenz/




More information about the gecode-users mailing list