Testing code that is executed when solve reaches non-optimal status

I’m looking for advice on the best way to test code that is executed only when the termination status for optimization returns a status of MathOptInterface.TIME_LIMIT.

The first approach I tried was to construct models that are difficult to deal with for the optimizer (but easy to understand for the human tester) and setting a short time limit (seconds => 1 with Cbc) to try to cause MathOptInterface.TIME_LIMIT to be hit. I haven’t had much success as the solver heuristics seem quite good.

I’ve also considered mocking the function call to JuMP.termination_status via (GitHub - invenia/Mocking.jl: Allows Julia function calls to be temporarily overloaded for purpose of testing or https://github.com/burrowsa/Fixtures.jl).

Is there any option I’m missing?

1 Like

Do you need the solver to have found a feasible solution before hitting the time limit?
If not, you can always set a time limit of 0s.

1 Like

Another approach is if you structure your code so that those actions are performed by a separate function which you just feed the right input in the test.

2 Likes

Look at MockOptimizer in MOI and how it’s used in JuMP:
https://github.com/jump-dev/MathOptInterface.jl/blob/master/src/Utilities/mockoptimizer.jl
https://github.com/jump-dev/JuMP.jl/blob/eaa22fa5d01d43046192dfd225c0335884974658/test/generate_and_solve.jl#L51

1 Like