Is there an xsum constraint function similar to python-mip in or-tools, which can constrain the total sum?

134 views
Skip to first unread message

Bob Michael

unread,
Sep 12, 2022, 10:49:29 PM9/12/22
to or-tools-discuss
Using xsum in python-mip can achieve something like this:
“model += xsum(x.var for x in group.items) >=1”
The sum cannot be greater than 1. Does or-tools have such a function?

Laurent Perron

unread,
Sep 12, 2022, 11:07:38 PM9/12/22
to or-tools-discuss
You mean the sum must be greater or equal to 1. There are plenty of sample code in ortools/linear_solver/samples.

Should be with the linear solver python API

solver.Add(sum(..) >= 1)

Now, if you use the CP-SAT native API, 

You can write 

model.Add(sum(..) >= 1)

If all variables are literals (Boolean variables or their logical negation), you can also write 

model.AddAtLeastOne(list of literals).

Now, with CP-SAT,, you can also write indicator constraints

model.Add(linear equation).OnlyEnforceIf(b1, b2.Not())

Indicating that the linear equation should be enforced only if b1 is true and b2 is false. (This is an implication). 

--
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/7e5d4193-34bf-49e2-8419-d536e58cede0n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages