constraint for inequality

I have an inequality constraint like that: a >= b + c - d*(1-x) with a,b,c,d is constant numbers, x is binary variable. How can I write it into a Constraint() class using from_func() method?

dwavebinarycsp.Constraint.from_func(myfunc_, [a,b,c,d,x], dwavebinarycsp.BINARY, "inequality")

However, a, b, c, d is constant numbers, not variables. So I tried:

dwavebinarycsp.Constraint.from_func(myfunc_(a,b,c,d), [x], dwavebinarycsp.BINARY, "inequality")

But the second version cannot put x into myfunc_ function.

Does anyone know how to handle this situation?



1 comment
  • Hi Le Hoa,

    What does your  myfunc_ look like?

    My approach would be to reframe your constraint as

    # Collect the non-variable terms
    z = (a - b - c)/d + 1

    # Reframe the constraint
    z  >= x


    In this form it's a bit easier to see that if z >= 1 the constraint will always be satisfied (since the maximum value of x is 1).  So you could do this: 

    # Function to evaluate z >= x (where z = (a - b - c)/d + 1)
    def myfunc_(z):
    if z < 1:
    return lambda x: x == 0
    return lambda x: True

    const = dwavebinarycsp.Constraint.from_func(myfunc_(z), ['x'], dwavebinarycsp.BINARY, name='myfunc_{}'.format(z))


    I think it's worth mentioning that there's a more performant way of defining this constraint as well. When the constraint is always satisfied we don't actually need the constraint at all. So you could evaluate z while you're constructing your BQM. If z >= 1 you can fix the variable to true using fix_variable()

    Let me know if this helps and if you have any more questions! 

    Comment actions Permalink

Please sign in to leave a comment.

Didn't find what you were looking for?

New post