This is already possible in Z3 with soft time outs, see this answer: Z3 Time Restricted Optimization
The way to achieve what you want is to use incremental solving:
- Assert all your hard constraints. (i.e., those that need to be satisfied)
- Do a
check-sat
and get the model
- Assert all your "optimization" constraints.
- Use a soft-time out, as described here: Z3 Time Restricted Optimization
In the last step, you can adjust how much you want to wait, i.e., the penalty you are willing to pay.
Related note: You might also want to play with soft-constraints, which the solver can "satisfy" optionally, incurring penalties if it doesn't. Perhaps that's more appropriate for your use case. See here on how to do that: https://rise4fun.com/Z3/tutorialcontent/optimization#h23