Skip to main content

Writing belongs to constraint with gurobipy

Ongoing

Comments

9 comments

  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Could you post a minimal working example of what you are currently trying to implement?

    How to write this constraint when a variable belongs to a set.

    Depending on how you define your variable in your code, you can define a list of indices and loop over those, e.g.,

    import gurobipy as gp

    m=gp.Model()
    x = m.addVars(10)

    U = [0,1,2,3]
    m.addConstrs((x[u] <= 2) for u in U)

    constructs the constraints \( x_u \leq 2 \quad u \in U = \{0,1,2,3\}\).

    Best regards, 
    Jaromił

    0
  • Venkateswarlu Yampati
    Gurobi-versary
    Conversationalist
    First Question

    Hi Jaromil,

    Thanks for the reply. Here is the problem I am trying to solve:

    U_1 = {1,2,3,..L}

    U_2 = {1,2,3,..K}

    R=

    r^{u}_{t,f} is the cost function. I have to maximize the above objective function using constraints C1,C2, and C3 in the above problem I have posted before. Here is the code I wrote:

    T = (np.arange(1,21,1))
    #print(len(T))
    F = (np.arange(1,101,1))
    M = np.arange(1,11,1)
    E = (np.arange(1,6,1))
    U = (np.arange(1,6,1))


    assignment_model = grb.Model('Assignment')

       x =  assignment_model.addVars(M.shape[0], T.shape[0], F.shape[0], vtype = grb.GRB.CONTINUOUS, lb = 0, ub = 1,name = 'x')
        assignment_model.addConstrs((sum(x[u, t, f]  for u in range(E.shape[0]) for u in range(U.shape[0])) <= 1 for t in range(T.shape[0]) for f in range(F.shape[0])), name = 'one RB allocation')
        assignment_model.addConstrs((sum(x[u,t,f] for t in range(T.shape[0]) for f in range(F.shape[0])) >= 1 for u in range(U.shape[0]) ), name = 'latency requirement') 
        obj_fun = sum(se_e_power[p][u,t,f] * x[u,t,f] for u in range(E.shape[0]) for t in range(T.shape[0]) for f in range(F.shape[0])) + sum(se_u_power[p][u,t,f] * x[u,t,f] for u in range(U.shape[0]) for t in range(T.shape[0]) for f in range(F.shape[0]))
        assignment_model.setObjective(obj_fun, grb.GRB.MAXIMIZE)
        assignment_model.setParam('OutputFlag', False)
        assignment_model.optimize()
        #print('Optimization is done. Objective function value: %.2f' % assignment_model.objVal)
        value = assignment_model.objVal

    It looks like I wrote the third constraint as you suggested. But the output is not as expected. Please let me know if there is anything wrong with code.

    Thanks

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi Venkateswarlu,

    The constraint

    assignment_model.addConstrs((sum(x[u,t,f] for t in range(T.shape[0]) for f in range(F.shape[0])) >= 1 for u in range(U.shape[0]) ), name = 'latency requirement')

    Looks OK. Did you write the model to an LP file and inspect it to see whether all constraints look as expected?

    assignment_model.write("myLP.lp")

    The write method will write a human-readable LP file which you can inspect.

    Best regards, 
    Jaromił

    0
  • Venkateswarlu Yampati
    Gurobi-versary
    Conversationalist
    First Question

    Hi Jaromil,

    I am using the write command, but I do not see any output. I updated the version to latests but I do not see any output.

    Any suggestions?

    Thanks,

    Venkat

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi Venkat,

    The write method will generate a file in the folder from which you execute your script. It will not print anything to the console output or the log.

    Best regards, 
    Jaromił

    1
  • Venkateswarlu Yampati
    Gurobi-versary
    Conversationalist
    First Question

    I can see the output now

    0
  • Venkateswarlu Yampati
    Gurobi-versary
    Conversationalist
    First Question

    Hi,

    I guess I solved the issue after printing it. Thanks for the suggestions.

    One more question is what kind of algorthim the libraray is using. As my problem is convex optimization, I was looking into theorteical concept of solving the problem. It was explained there are many algorthims to solve it. What exactly the algorthim it uses when I write grb. Maximize(like sub-gradient or Newtons or lagrangian). Is there any documentation which explains the algaorthim the library is using?

    Thanks,

    Venkat

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi Venkat,

    You can find some basics on convex continuous programming at Linear Programming (LP) - A Primer on the Basics. There at the bottom, you will find the literature for the algorithms you seek.

    Best regards, 
    Jaromił

    0
  • Venkateswarlu Yampati
    Gurobi-versary
    Conversationalist
    First Question

    Hi Jaromil,

    Thanks for the information.

    I have a quick one. I am trying to write the following constraint but I am getting an error. Could you suggest me how to write the following expression? t,f are for all. R_min is a constant. 

    0

Please sign in to leave a comment.