Re: cpModel - constraints debug

1,154 views
Skip to first unread message
Message has been deleted
Message has been deleted

Ben Griffin

unread,
Feb 24, 2021, 5:26:28 AM2/24/21
to or-tools-discuss
I'm not the best at this, but I think you would have to log that yourself.
Because, if you follow these through - for instance the x < y, you can see that these are interpreted into lower constraint elements by the library.
So x < y  is deferred to 'AddLinearExpressionInDomain' which does different things according to the expression - but in some cases adds your values to an internal linear.vars array  and it's coefficient is added to a linear 'coeffs' array.  So very quickly, while the semantic content of your expression is retained, the structure of the expression itself is  discarded by the modeller.

On Wednesday, 24 February 2021 at 02:58:19 UTC all.of.the.offe...@gmail.com wrote:

I have seen this one: https://github.com/google/or-tools/issues/520

But this is for c++ only, and I cannot find any method that has debug or debugString for cpModel in source code. 
On Tuesday, February 23, 2021 at 9:56:52 PM UTC-5 Peter Meter wrote:
Hello!

How do I print constraints for the cpModel (python)? 
When I print the model with print(model) it prints things which are hard to understand (below). Is there a way to print constraints in the same way I defined them, i.e. x < y, etc. 

That what I can print now with print(model):
constraints {
all_diff {
vars: 800
vars: 802
vars: 804
vars: 806
...
}
}

Thank you!
Message has been deleted

Priidik Vilumaa

unread,
Feb 24, 2021, 10:00:08 AM2/24/21
to or-tools-discuss
There was a similar discussion a week ago. My method in a few sentences:

The way I debug opt models is I have a flag (a simple IF sentence) to turn each type of constraint on or off. Once I determine the constraint that is failing, I can have a closer look at the problem, perhaps print the constraints of that type that I am adding etc. And always start with a really small problem that can be solved on paper to optimality. Otherwise you will debug till you're crazy.

An alternative is to look for the data point that is causing infeasibility. 

In general I'd advise a small problem + print()

Best,
Priidik 

On Wednesday, February 24, 2021 at 4:26:58 PM UTC+2 all.of.the.offe...@gmail.com wrote:
I see. 
So how can one debug the model? 
I am new to or-tools and constraint/linear programming in general. 

How can I know that I didn't make some python mistake and created by accident a weird constraint? 

Thanks, 
Pete

Hakan Kjellerstrand

unread,
Feb 24, 2021, 10:25:40 AM2/24/21
to or-tools-discuss
Here's my own approach to modelling and debugging (slightly adapted from a text I wrote about another CP system to OR-tools):

Preamble: Debugging a constraint model requires a different approach than debugging a traditional non-constraint program. The reason is that in a constraint model, everything “happens at the same time” when solve() has been started. This means that the traditional “print debugging” does not work in the same way.

• Test early and often. For a beginner, it can be good practice to test the model after each new constraint is added.
Check the domains.
First test with a small instance that has a known output, in order to ensure that the model is correct. Testing is much simpler with small instances, since it will be faster to solve and easier to check the solutions.
If a model doesn’t work:
    – Remove one constraint after another and test again. Or: Comment all constraints and add them one after one.
    – Check the domains again.
• In order to ensure correctness of the model, it might be a good idea to count the number of solutions . For example, the 8-queens problem should give 92 solutions without symmetry breaking.
   A (good) Sudoku problem should have exactly one solution: try to find 2 solutions and if there are more than one solution the model is not correct. One can use StopSearch in a solution printer to stop after 2 solutions.
Print intermediate values in optimization problems via a solution printer.
If the model is too slow: Test different search strategies/solver.parameters to speed up the search.

--
You received this message because you are subscribed to the Google Groups "or-tools-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/or-tools-discuss/ad4736f2-e5a2-4570-a6c8-40d38409c3c1n%40googlegroups.com.


--
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages