Decision Optimization

 View Only
  • 1.  docplex Python - Constraints with bitwise operators

    Posted Mon May 16, 2022 02:38 PM
    Dear IBM Community,

    Thank you so much for your time reading this!

    I'm using docplex in order to solve a model in Python. I would like to add a constraint as follows:


    where xi is a binary variable in our Model, and Mi,j is a matrix of zeros and ones (it is a constant). x = x1, x2, ... xm should be a list of zeros and ones corresponding to the above constraint. The addition and multiplication operations are actually bitwise addition and multiplication (computed with operators ^ and & in Python).

    I'm having difficulties in understanding how I should write this expression. Specifically:
    • It is supported to use ^ and & operators or we have a workaround for this?
    • It is mandatory to have this expression written inline or we have other methods to pass this expression?
    Can you please let me know what is a good approach to write this constraint?

    Thank you!

    ------------------------------
    Andrada Denisa
    ------------------------------


  • 2.  RE: docplex Python - Constraints with bitwise operators

    Community Leadership
    Posted Mon May 16, 2022 02:44 PM
    Hi Andrada,

    There are a number of examples for DOcplex, and they are referred to from the documentation at http://ibmdecisionoptimization.github.io/docplex-doc/index.html#mathematical-programming-modeling-for-python-using-docplex-mp-docplex-mp.  Did you study the examples in the section examples/mp/modeling already?

    ------------------------------
    Xavier
    ------------------------------



  • 3.  RE: docplex Python - Constraints with bitwise operators

    Posted Tue May 17, 2022 09:27 AM
    Edited by Andrada Tue May 17, 2022 09:29 AM
    Dear Xavier,

    Hope you are well!

    Thank you for sharing the location where we can find modeling examples for Docplex. It seems that Docplex automatically understands how to perform the correct operations if we specify the Variables as binary.

    I will post below the solution that worked for me in case other readers are curious about this post too.

    Exact location for Official Docplex modeling examples: http://ibmdecisionoptimization.github.io/docplex-doc/mp/samples.html
    Additional resources which I found helpful:
    • https://www.youtube.com/c/CodingPerspective
    • https://www.youtube.com/watch?v=Dr1cLQq15Fo (Knapsack problem example in Docplex)
    Solution:
    from docplex.mp.model import Model
    m = Model(name='test')
        x = m.binary_var_list(n, name="x")
        for j in range(len(M1[0])):
            m.add_constraint(sum(M1[i][j]*x[i] for i in range (len(M1))) == 1)

    Have a beautiful day ahead!


    ------------------------------
    Andrada
    ------------------------------