Skip to content

CP-SAT: Int/Boolean Constraint #3208

Closed Answered by lperron
ngaihiang asked this question in CP-SAT questions
Discussion options

You must be logged in to vote
    model = cp_model.CpModel()
    xx = dict()
    for i in range(50):
        xx[i] = model.NewBoolVar('xx%i' % (i))

    b = model.NewBoolVar('b') # intermediate variable
    z = model.NewBoolVar('z') # intermediate variable

    # b implies sum of list is >20
    model.Add(sum(xx[i] for i in range(50)) > 20).OnlyEnforceIf(b)
    model.Add(sum(xx[i] for i in range(50)) <= 20).OnlyEnforceIf(b.Not())

    model.Add(z == 1).OnlyEnforceIf(b)
    model.Add(z == 0).OnlyEnforceIf(b.Not())

works flawlessly

you forgot the () after the last Not

and in you example, you can simple set model.Add(z == b), or even better only use one variable.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by lperron
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants