Skip to main content

Define a matrix as a variable and write it in constraint

Ongoing

Comments

6 comments

  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Yutian,

    I suspect you're using an old version of numpy.

    Can you either add run this in a python script:

    import numpy as np
    import gurobipy as gp
    print("numpy:", np.__version__)
    print("gurobipy:", gp.gurobi.version())

    or alternatively the command: pip list (from a terminal)

    Then share the output with us.

    - Riley

    0
  • Yutian He
    Conversationalist
    First Question

    Thanks for the quick reply. Here is the result.

    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    Ah, you actually have the opposite problem!

    https://support.gurobi.com/hc/en-us/articles/25787048531601-Resolved-in-gurobipy-v11-0-3-Compatibility-issues-with-numpy-2-0

    Upgrading to gurobipy 11.0.3 will fix this issue.

    - Riley

    0
  • Yutian He
    Conversationalist
    First Question

    Yes, that works! Thank you. And now I have a new question, since I want the constraints \sum_{i \in ma_1} pi_{ij} = 1 for every j \in ma_1. I wrote the constraint like this:

    for k in var_a1:
        expr1_3 = gp.quicksum(pi_a1[k][l]*vector_a1[l] for l in var_a1)
        print(f"{expr1_3 =}")
        m1.addConstr(expr1_3 == 1)


    But this constraint cannot be written by the m1.write("Fair_"+".lp"), I think there should be some other way to write the sum. And I meet the same question for this code:

    for s in var_new:
        expr1_3=(b[s+1]-b[s])*(eta[s]**2)
        print(f"{expr1_3 =}")
    m1.addConstr((sum(b[s+1]-b[s])*(eta[s]**2) for s in var_new) <= nu[0])

    Could you please help me for this? Thank you.

    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Yutian,

    What is the error you see?  Once I take care of the ma_1, ma_2 typo in your code it runs ok and writes the LP file ok.  Note though that your code will run slow.  The point of the matrix API is to replace this:

    for k in var_a1:
        expr1_3 = gp.quicksum(pi_a1[k][l]*vector_a1[l] for l in var_a1)
        print(f"{expr1_3 =}")
        m1.addConstr(expr1_3 == 1)

    with this:

    m1.addConstr(pi_a1@vector_a1 == 1)

    If you are needing to index your MVar, eg pi_a1[k][l], this is a good indication you should not use MVars - just use

    pi_a1 = m1.addVars(m_a1, m_a1, vtype=GRB.BINARY, name="pi_a1")
    pi_a2 = m1.addVars(m_a2, m_a2, vtype=GRB.BINARY, name="pi_a2")

    and it will run a lot faster.

    - Riley

     

    0
  • Yutian He
    Conversationalist
    First Question

    Hi Riley,

    m1.addConstr(pi_a1@vector_a1 == 1)


    This code works very well with the previous definition of the pi_a1, thank you!

    Now I have two questions:

    1. When I changed pi to the new definition you wrote, I got this error here:

    2. I'm going to set this constraint: \sum_{i from 1 to m-1} ((b_{i+1} - b_i)*eta_i^2) <= nu. When I run the following code:

    var_new = list(range(m-1))
    var3=list(range(1))
    b= array([0. , 0.125 , 0.14285714, 0.25 , 0.28571429, 0.375 , 0.42857143, 0.5 , 0.57142857, 0.625 , 0.71428571, 0.75 , 0.85714286, 0.875 , 1. ])

    eta = m1.addVars(var_new, vtype=GRB.CONTINUOUS, name = "eta")
    nu = m1.addVars(var3, vtype=GRB.CONTINUOUS, lb=0, name = "v")

    for s in var_new:
        expr1_3=(b[s+1]-b[s])*(eta[s]**2)
        # print(f"{expr1_3 =}")
    m1.addConstr((sum(b[s+1]-b[s])*(eta[s]**2) for s in var_new) <= nu[0])

    I got this error:

    I met this error several times, but have no idea how to fix it, could you please also help me to check it? Thank you!

    0

Please sign in to leave a comment.