メインコンテンツへスキップ

Error with setObjective

回答済み

コメント

10件のコメント

  • 正式なコメント
    Simranjit Kaur
    • Gurobi Staff Gurobi Staff
    This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?.
  • Jennifer Locke
    • Gurobi Staff Gurobi Staff

    Would you please post your code.    Thank you

    0
  • Jolly Ehiabhi
    • Gurobi-versary
    • First Question
    • Conversationalist
    from __future__ import division
    from gurobipy import *
    import random

    # Problem Input Size:
    workstation = 5
    ProcessingTime = 12
    Jobtype = 2
    JobScenario = 10
    #Input Data Preparation
    p_kj = {}
    q_ij = {}
    t_i = {}

    M = range(workstation)
    I = range(ProcessingTime)
    K = range(Jobtype)
    J = range(JobScenario)

    #print I
    for k in K:
    for j in J:
    for i in I:
    p_kj[k, j] = 12
    q_ij[i, j] = 5
    for k in K:
    p_kj[k, j] = 60
    for i in I:
    t_i[i] = random.randint(0,100)

    #---------------------------
    #variable:
    #---------------------------
    z = Model("ProcessingTime")
    ### First stage vars, job sequence (q_ij):
    q_ij = {}
    for i in I:
    for j in J:
    q_ij[i,j] = z.addVar(vtype= GRB.BINARY, lb=0, ub=1.0, name = "q_ij[%s,%s]"%(i,j))
    ### Second stage vars, starting time of operation on machine M_k of the job in position i (T_i):
    T_i = {}
    for i in I:
    T_i[i] = z.addVar(vtype= GRB.CONTINUOUS, lb =0, name = "T_i[%s]"%(i))
    ### second stage Vars, the cycle time for the manufacturing cell with the job in sequence q
    s = {}
    for s in q_ij:
    s = z.addVar(vtype= GRB.BINARY, lb=0, ub=1.0)
    z.update()

    #objective function:
    S = (q_ij[i,j] for i in I for j in J)

    z.setObjective(S, GRB.MINIMIZE)
    #constraints:
    for i in K:
    z.addConstr(((q_ij[i,j] for i in k)) >= (T_n+1[1,k]) - (T_1[1,k]), name = "constraint 1")
    for i in K:
    for j in J:
    z.addConstr(quicksum(p_kj[k,j] * q_ij[i,j] for j in J )>=(T_i[i, k+1] - T_i[i, k] for i in K), name = "Constraint 2" )
    for i in K:
    for j in J:
    z.addConstr(quicksum(p_kj[k,j] * q_ij[i,j] for j in J )>=(T_(i+1)[i+1, k] - T_i[i, k] for i in K), name = "Constraint 3" )
    for i in K:
    z.addConstr((T_(i+1)[i+1, k] - T_i[i, (k+1)] for i in K)>=0, name = "Constraint 4")
    for j in J:
    z.addConstr((quicksum(q_ij[i==1, j]) ==1) , name = "constraint 5")
    for i in I:
    z.addConstr((quicksum(q_ij[i, j==1]) ==1) , name = "constraint 6")
    z.update()
    z.params.MIPGAP = 0.0
    z.params.TimeLimit = 600
    z.optimize()

    sp_sol =z.objval
    print ("Status: ", z.status)
    print ("Objective value: ", sp_sol)

    for i in I:
    for j in J:
    if q_ij[i,j].x ==1:
    print ("q_(ij)[%s,%s]"% (i, j), q_(ij)[i, j].x)

    for i in K:
    if T_i[i]>= 0:
    print ("T_i[%s]"%(i))

    #-----------------------------------------------------------------------
    #Build and solve the mean value problem
    #------------------------------------------------------------------------
    zv = Model("avProcessingTime")

    ### First stage vars, job sequence (q_ij):
    qz_ij = {}
    for i in I:
    for j in J:
    qz_ij[i,j] = zv.addvar(vtype= GRB.BINARY, lb=0, ub=1.0, name = "qz_ij[%s,%s]"%(i,j))
    ### Second stage vars, starting time of operation on machine M_k of the job in position i (T_i):
    Tz_i = {}
    for i in k:
    Tz_i[i] = zv.addvar(vtype= GRB.CONTINUOUS, lb =0, name = "Tz_i[%s]"%(i))
    ### second stage Vars, the cycle time for the manufacturing cell with the job in sequence q
    sz = {}
    for s in q_ij:
    s = zv.addvar(vtype= GRB.BINARY, lb=0, ub=1.0)
    z.update()

    #objective function:
    S = minSz_a(q_ij)
    zv.setobjective(S, GRB.MINIMIZE)
    #constraints:
    for i in K:
    zv.addConstr((Sz_a(qz_ij[i,j] for i in k)) >= (Tz_n+1[1,k]) - (Tz_1[1,k]), name = "constraint 1")
    for i in K:
    for j in J:
    zv.addConstr(quicksum(pz_kj[k,j] * qz_ij[i,j] for j in J )>=(Tz_i[i, k+1] - Tz_i[i, k] for i in K), name = "Constraint 2" )
    for i in K:
    for j in J:
    zv.addConstr(quicksum(pz_kj[k,j] * qz_ij[i,j] for j in J )>=(Tz_(i+1)[i+1, k] - Tz_i[i, k] for i in K), name = "Constraint 3" )
    for i in K:
    zv.addConstr((Tz_(i+1)[i+1, k] - Tz_i[i, (k+1)] for i in K)>=0, name = "Constraint 4")
    for j in J:
    zv.addConstr((quicksum(qz_ij[i==1, j]) ==1) , name = "constraint 5")
    for i in I:
    zv.addConstr((quicksum(qz_ij[i, j==1]) ==1) , name = "constraint 6")
    zv.update()
    zv.params.MIPGAP = 0.0
    zv.params.TimeLimit = 600
    zv.optimize()

    sp_sol =zv.objval
    print ("Status: ", zv.status)
    print ("Objective value: ", sp_sol)

    for i in I:
    for j in J:
    if qz_ij[i,j].x ==1:
    print ("qz_ij[%s%s]"%(i,j), qz_ij[i,j].x)
    for i in K:
    if Tz_i[i]>= 0:
    print ("Tz_i[%s]"%(i))
    ############# Fix this as first-stage solution in stochastic model #############
    for i in I:
    if qm_ij [i,j].x > 0.99:
    q_ij[i,j].lb = 1.0
    else:
    q_ij[i,j].ub = 0.0
    z.update()
    z.optimize()

    print("Benefit achieved: %g" %(z.objval - sp_sol))

    -1
  • Eli Towle
    • Gurobi Staff Gurobi Staff

    Hi Jolly,

    There are several issues with this code. I'll note a few of them, which will hopefully give you a good start at getting this model built.

    You receive this particular error because S is defined to be a Python generator expression, which isn't an acceptable type for an objective function:

    S = (q_ij[i,j] for i in I for j in J)

    This probably meant to be a summation:

    S = quicksum(q_ij[i,j] for i in I for j in J)

    Several times, you create a constraint for each index, then loop over those same indices in a summation in the constraint. For example, consider these constraints:

    for i in K:
    z.addConstr(((q_ij[i,j] for i in k)) >= (T_n+1[1,k]) - (T_1[1,k]), name = "constraint 1")

    The index i is used to loop over the constraints being added. It is also used to loop in the left-hand side expression. The indices j and k used in the variable indices are not defined anywhere near these constraints. Additionally:

    • T_n and T_1 are not defined. If you're trying to reference the T variables, this should be done using T_i[i], where the i inside the brackets belongs to the set I. A variable with two indices like T[i,k] is never defined.
    • The generator expression on the left-hand side should probably be a summation using quicksum().
    • The lowercase k in "for i in k" on the left-hand side is likely meant to be uppercase.

    Consider exploring Gurobi's code examples to see how certain problems can be formulated in Python. Also, once your code is working, you may want to look into the Model.addVars() method. For example, this code:

    q_ij = {}
    for i in I:
        for j in J:
            q_ij[i,j] = z.addVar(vtype= GRB.BINARY, lb=0, ub=1.0, name = "q_ij[%s,%s]"%(i,j))

    can be replaced with a single call to Model.addVars() (changing the variable name q_ij to q for brevity):

    q = z.addVars(I, J, vtype=GRB.BINARY, name="q")

    The Model.addVars() method returns a tupledict object, which supports some simplified methods for summations and dot products. For instance, if your objective function is the summation of all q variables, you can do obtain this expression with q.sum().

    Finally, you can visually inspect a model by writing it to an LP file using Model.write(). This is helpful for verifying if the constraints you added are correct.

    I hope this helps. Thanks!

    Eli

    1
  • Jolly Ehiabhi
    • Gurobi-versary
    • First Question
    • Conversationalist

    Waoh. Thank you so much Eli. I will make the updates and rerun the model again.

    0
  • Jolly Ehiabhi
    • Gurobi-versary
    • First Question
    • Conversationalist

    These are the set of Constraints I am trying to code, Please I need help with this.

     

    this is how I coded them. I believe I am not coding it the right way.

     

    for  i in K:
    for j in J:
    z.addConstr(quicksum(p_kj[k,j] * q_ij[i,j] for j in J )>=(T_(i+1)[i+1, k] - T_i[i, k] for i in K), name = "Constraint 3" )
    for i in K:
    z.addConstr((T_(i+1)[i+1, k] - T_i[i, (k+1)] for i in K)>=0, name = "Constraint 4")
    for j in J:
    0
  • Eli Towle
    • Gurobi Staff Gurobi Staff

    Hi Jolly,

    I addressed a lot of the issues with this code in my previous response. The main problems are:

    • T_(i+1) is not a valid Python variable name. You only defined a Python variable T_i.
    • The left-hand side of one of your constraints is a generator expression, which doesn't make sense. In your case, both sides of each of your constraints should be linear expression objects formed by summations of variables and constants.
    • You have the same loops both inside and outside of the Model.addConstr() methods.

    One way to write these constraints is as follows. I'll use names that match what's written in the picture; you will have to make sure these are defined in your code.

    for i in range(1, n+1):
        for k in range(1, m+1):
            z.addConstr(T[i+1, k] >= T[i, k] + quicksum(p[k, j] * q[i, j] for j in range(1, n+1)))
    for k in range(1, m):
            z.addConstr(T[i+1, k] >= T[i, k+1])

    You can look at my previous response for an idea of how to add the T variables to your model using the Model.addVars() method.

    I hope this helps. Thanks!

    Eli

    0
  • Jolly Ehiabhi
    • Gurobi-versary
    • First Question
    • Conversationalist

    Hi Eli,

    Thank you for all the help, I made updates to the model but I am still getting errors. I have tried checking online on how to fix the problem. I am not getting any good material to help fix the error

    this is my updated model.

     

     

    from __future__ import division
    from gurobipy import *
    import random

    # Problem Input Size:
    workstation = 5
    ProcessingTime = 12
    NumberofJob = 60
    JobScenario = 2
    Jobposition = 60
    Cycletime = random.randint(6,15)
    n = 60
    m = 6

    #Input Data Preparation
    p_kj = {}
    q_ij = {}
    T_i = {}
    T_nk = {}
    T_ik = {}

    M = range(workstation)
    T = range(ProcessingTime)
    K = range(NumberofJob)
    J = range(JobScenario)
    I = range(Jobposition)
    S_a = range(Cycletime)

    #print J
    for k in K:
    for j in J:
    for i in I:
    p_kj[k, j] = 12
    q_ij[i, j] = 5

    for i in I:
    T_i[i] = random.randint(0,450)

    #---------------------------
    #variable:
    #---------------------------
    z = Model("ProcessingTime")
    ### First stage vars, job sequence (q_ij):
    q_ij = {}
    for i in I:
    for j in J:
    q_ij[i,j] = z.addVar(vtype= GRB.BINARY, lb=0, ub=1.0, name = "q_ij[%s,%s]"%(i,j))
    ### Second stage vars, starting time of operation on machine M_k of the job in position i (T_i):
    T_i = {}
    for i in I:
    T_i[i] = z.addVar(vtype= GRB.CONTINUOUS, lb =0, name = "T_i[%s]"%(i))
    ### Third stage Vars, the cycle time for the manufacturing cell with the job in sequence q
    p_kj = {}
    for k in K:
    for j in J:
    p_kj = z.addVar(vtype= GRB.BINARY, lb=0, ub=1.0, name = "p_kj[%s,%s]"%(k,j))
    ### Fourth stage Vars, the cycle time of the workstation M_k pacing the Cell
    T_nk = {}
    for k in M:
    T_nk[n+1,k] = z.addVar(vtype= GRB.CONTINUOUS, lb =0, name = "T_nk[%s,%s]"%(n+1,k))
    ### Fifth stage Vars, the cycle time of the workstation M_k
    T_ik = {}
    for i in I:
    for k in M:
    T_ik[i==1,k] = z.addVar(vtype= GRB.CONTINUOUS, lb =0, name = "T_ik[%s,%s]"%(i==1,k))
    T_ik = {}
    for i in I:
    for k in M:
    T_ik[i,k] = z.addVar(vtype= GRB.CONTINUOUS, lb =0, name = "T_ik[%s,%s]"%(i,k))
    z.update()

    #objective function:
    obj = quicksum((q_ij[i,j] for i in I for j in J))

    z.setObjective(obj, GRB.MINIMIZE)
    #constraints:
    for i in I:
    for j in J:
    for k in M:
    z.addConstr((quicksum(q_ij[i,j] for j in J)) >= (T_ik[i==1,k]) - (T_nk[n+1,k]), name = "constraint 1")
    for i in range(1, n+1):
    for k in range(1, m):
    z.addConstr((T_ik[i, k+1] >= T_ik[i,k] + quicksum(p_kj[k,j] * q_ij[i,j] for j in range(1, n+1))), name = "Constraint 2" )
    for i in range(1, n+1):
    for k in range(1, m+1):
    z.addConstr((T_ik[i+1, k] >= T_ik[i,k] + quicksum(p_kj[k,j] * q_ij[i,j] for j in range(1, n+1))), name = "Constraint 3" )
    for k in range(1,m):
    z.addConstr((T_ik[i+1, k] - T_ik[i,k+1] for k in M)>=0, name = "Constraint 4")
    for j in J:
    z.addConstr((quicksum(q_ij[i==1, j]) ==1) , name = "constraint 5")
    for i in I:
    z.addConstr((quicksum(q_ij[i, j==1]) ==1) , name = "constraint 6")
    z.update()
    z.params.MIPGAP = 0.0
    z.params.TimeLimit = 600
    z.optimize()

    sp_sol =z.objval
    print ("Status: ", z.status)
    print ("Objective value: ", sp_sol)

    for i in I:
    for j in J:
    if q_ij[i,j].x ==1:
    print ("q_(ij)[%s,%s]"% (i, j), q_(ij)[i, j].x)

    for i in K:
    if T_i[i]>= 0:
    print ("T_i[%s]"%(i))
    0
  • Gwyneth Butera
    • Gurobi Staff Gurobi Staff

    From a quick glance at you code, I think the problem is that you are missing the indices when you create these variables:

            p_kj = z.addVar(vtype= ....

    I hope that helps, Gwyneth

    0
  • Jolly Ehiabhi
    • Gurobi-versary
    • First Question
    • Conversationalist

    Thank you Eli and Gwyneth. The code is now running with no error.

    Jolly

    0

投稿コメントは受け付けていません。