0

I'm porting my old (from 2010) OR-tools CP solver models to the CP-SAT solver (in Python3). There are quite a few differences between these systems, but most are quite easy to port to the CP-SAT solver.

However, one thing I haven't found in the CP-SAT solver documentation is the "reflection" methods that the old CP solver supports, such as getting lower/upper bounds of a decision variables. E.g.

  # Old CP solver code
  solver = pywrapcp.Solver("Name")
  x = solver.IntVar(0,10, "x") 

  # ....
  x_lb = x.Min()  # lower bound
  x_ub = x.Max()  # upper bound
  # ... 
 

One can then use these lower/upper bounds of the variable to implement other constraints. (Here's a CP solver implementing the cumulative constraint using Min and Max methods: http://hakank.org/or_tools/furniture_moving.py )

Does the CP-SAT solver supports these kind of reflection methods?

hakank
  • 6,629
  • 1
  • 17
  • 27
  • 2
    I don't think so, you can do `x.Proto().domain[0]` and `x.Proto().domain[-1]` though. PS: big fan of your blog! – Stradivari Feb 18 '21 at 17:37
  • @Stradivari Ah, that's a great tip and might be exactly what I look for! (And thanks for your kind words!) – hakank Feb 18 '21 at 17:46
  • 1
    Hi! long time no see. – Laurent Perron Feb 18 '21 at 17:59
  • @LaurentPerron Hi Laurent! It was certainly a long time! Porting my old CP Solver models to CP-SAT should have been done long time ago, but I'm on it now. :-) – hakank Feb 18 '21 at 18:04
  • 1
    Using `x.Proto().domain` seems to work nicely: http://hakank.org/or_tools/furniture_moving_sat.py (I might tweak the model in other ways, though) Thanks @Stradivari ! If you write an answer, I'll accept it. – hakank Feb 18 '21 at 18:26

1 Answers1

4

You can access the domain via the underlying variable Proto.

x.Proto().domain

The first value is the min and last one the max.

x_lb = x.Proto().domain[0]
x_ub = x.Proto().domain[-1]

PS: Maybe you can use the AddCumulative constraint here instead.

Stradivari
  • 2,626
  • 1
  • 9
  • 21
  • 1
    Thanks. I will use `AddCumulative` as well, in another model. Building a decomposition of `cumulative` (with "reflections") is a part of the learning process and test of what a CP system supports... – hakank Feb 18 '21 at 18:43