minizinc-python icon indicating copy to clipboard operation
minizinc-python copied to clipboard

keyboard interrupt

Open hadipourh opened this issue 3 years ago • 1 comments

Hello all,

I would just like to report that we can not get the intermediate solution after a keyboard interruption. However, this feature is available when we use Minizinc through the terminal to solve an optimization problem. The current Python interface outputs the intermediate solution when the time limit reaches. So, is it possible to add this feature for the keyboard interruptions as well?

hadipourh avatar Nov 10 '22 11:11 hadipourh

This is slightly tricky. The simple workaround to do this is to actually capture the intermediate solutions, and keep (only) the incumbent. You can then trap the keyboard interrupt in your Python script, and then outputting the captured incumbent before exiting.

The problem is that this pays the overhead of output/parsing for all the intermediate solutions. Depending on the amount of solutions (read: if there are thousands), this might be a large overhead to pay (especially in Python).

Instead, we would have to use MiniZinc behaviour, and, when the solutions() function is interrupted, output the last solution that is given when the terminate() method is completed on the process. This was part of the initial design of the solutions() function, but I found that Python would throw an error if you yielded values after the async generator was already cancelled. This means we would have to provide our own terminate method for the solution() generator. This is not impossible, but might be a lot of work.

I'm not sure if it's worth it . Especially since it should be noted that MiniZinc Python is currently set to always output and parse intermediate solutions since there are still various solvers that cannot be trusted to output their final solutions when reaching the time limit or when interrupted.

Hopefully the workaround works for you. Otherwise, maybe we can investigate what is possible.

Dekker1 avatar Nov 10 '22 23:11 Dekker1