Skip to main content

I want to convert this formula into code

Awaiting user input



  • Ronald van der Velden
    Gurobi Staff Gurobi Staff

    Have you tried it step-by-step and where did you get stuck exactly? I would start creating a variable containing the value of the inner sum. Then try to create the max and finally the outer sum.

  • 석찬 이
    First Comment
    First Question
    T = ['t1','t2','t3','t4','t5','t6','t7','t8','t9']         #index of trailer
    U = ['u1','u2','u3']                                       #index of unload dock
    B = ['b1','b2','b3']                                       #index of time bucket
    S = ['s1','s2','s3']                                       #index of output_station

    num_trailer = len(T)
    num_unload_docks = len(U)
    num_time_bucket = (len(T) // len(U))
    num_output_station = len(S)

    Lmax = quicksum(max(quicksum(t[d, s] * x[t, b] for t in range(num_trailer) for b in range(num_time_bucket)))for s in range(num_output_station)for b in range(num_time_bucket))

    Tried to convert sigma into code. Is this the right code?

  • Ronald van der Velden
    Gurobi Staff Gurobi Staff
    • The inner quicksum seems incorrect - you reference d which is not defined anywhere.
    • Then you take the max over just a single quicksum. You need multiple things to take the maximum; I guess you will need to move one of the two iterator variables/loops outside the quicksum.
    • For the maximum, you cannot use the built-in Python max function since it only works for scalar values, not Gurobi variables. You will want a new, auxiliary variable that will be equal to the maximum of the inner quicksums. Have a look at the max_ function for details.
    • Finally, you can sum over the auxiliary variables to get the final result.

Please sign in to leave a comment.