Skip to main content

I am facing some syntex error when going to run this code

Answered

Comments

3 comments

  • Riley Clement
    • Gurobi Staff

    Hi Abu,

    Your parentheses don't match up on these two lines

    term1 = Cp * Qs + Cmgc * (Qs * T - 0.5 * (1 - K) * P1 * T**2 + H2
    H2=costFunction(D, T, x1, tm1))


    - Riley

    0
  • Abu Hashan Md Mashud
    • Gurobi-versary
    • First Comment
    • First Question

    Thanks. I have changed the code like:

    import gurobipy as grb

    # Define the function TC_c1M
    Cp = 1
    Cmgc = 2
    P1 = 40
    K = 0.5
    D = [1, 2, 3]
    tfix = 1
    delta = 0.5
    tv = 1
    Td = 2
    WN = 3
    C1 = 4
    C2 = 5
    Vf = 6
    N = 100
    L = 50
    Qs = 600
    vera = 5
    tm1 = .3

    # Create a Gurobi model instance
    m = grb.Model()

    # Define decision variables
    T = m.addVar(lb=1, ub=200, name="T")
    x1 = m.addVar(lb=0, ub=3, name="x1")

    def costFunction(D, T, x1, tm1):
        total = 0
       
        for i in range(len(D)):
            H=(0.5 * x1**2 * T**2 - x1 * T * tm1 + 0.5 * tm1**2)
            value = D[i] *H 
            total += value
        return total

    def thetafunction(vera, N, L, A):
        deno = 0
        for i in range(0, L):
            val = vera**i + (N-L) * vera**(L-1)
            deno += val
        outp = A / deno
        return outp

    theta = thetafunction(vera, N, L, Qs)

    def TCc1M(T, x1):
        H2=costFunction(D, T, x1, tm1)
        term1 = Cp * Qs + Cmgc * (Qs * T - 0.5 * (1 - K) * P1 * T**2 + H2)
        
        term2 = tfix * Qs + (2 * delta * tv * Qs * Td * WN) / (C1 * Vf) + (2 * (1 - delta) * tv * Qs * Td * WN) / (C2 * Vf)
        return term1 + term2 

    # Set objective
    m.setObjective(TCc1M(T, x1), grb.GRB.MINIMIZE)

    # Optimize the model
    m.optimize()

    # Print the optimal solution
    print("Optimal solution:")
    for v in m.getVars():
        print(v.varName, v.x)

     

    But still errors like this showing:


      File "src\gurobipy\quadexpr.pxi", line 257, in gurobipy.QuadExpr._mul

    GurobiError: Invalid argument to QuadExpr multiplication

    0
  • Riley Clement
    • Gurobi Staff

    Hi Abu,

    There is a lot which needs fixing here and I think you are missing the basics of modelling with Gurobi.

    Firstly, you have not defined any constraints.  It would be useful for you to consult several of our examples in order to get a solid foundation for how to define models with Gurobi.

    Secondly, you cannot perform the square of a variable with v, using v**2.  You need to either use v*v or for more general powers use addGenConstrPow.

    Thirdly, you cannot directly multiply more than two variables together, but you can use auxiliary variables to achieve the result.  See this community post for more details.

    - Riley

    0

Please sign in to leave a comment.