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

Error with addVars

回答済み

コメント

4件のコメント

  • 正式なコメント
    Simranjit Kaur
    • 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 try Gurobot, our chatbot interface offering instant, expert-level support.
  • Matthias Miltenberger
    • Gurobi Staff

    Hi Neda,

    You need to define your loops differently. In several places you are using the same variable as iterator as well as range indicator, e.g.:

    name=["x"+str(i+1)+","+str(j+1) for i in range (k) for j in range(i)]
    [...]
    model.setObjective(quicksum(fuelcost*travelkm[i] for i in range (i)) [...]

    You should use a different name to define the dimensions of your problem to avoid these issues, e.g.:

    k = 3       #number of products
    i = 14     #number of routes
    v = 7       #number of vehicles

    should be:

    products = 3
    routes = 14
    vehicles = 7

    Then you can still use i,j,k as iterator variables in your loops and your code is much more readable.

    Cheers,
    Matthias

    0
  • Neda Türkoğlu
    • Gurobi-versary
    • First Question
    • First Comment

    Hi Matthias,

    Thank you for your help. I fixed that error. However now I get the following error;

    GurobiError: Unsupported type (<class 'gurobipy.gurobipy.TempConstr'>) for LinExpr addition argument

    Can you help me with this?

    Thanks.

    from gurobipy import GRB,Model,quicksum #*

    def Output(m):  
        # Print the result
        status_code = {1:'LOADED', 2:'OPTIMAL', 3:'INFEASIBLE', 4:'INF_OR_UNBD', 5:'UNBOUNDED'} #this is how a 'dictionary' 
                                                                                                #is defined in Python
        status = m.status
        
        print('The optimization status is ' + status_code[status])
        if status == 2:    
            # Retrieve variables value
            print('Optimal solution:')
            for v in m.getVars():
                print(str(v.varName) + " = " + str(v.x))    
            print('Optimal objective value: ' + str(m.objVal) + "\n")

    def ModelBetterImplemented(fuelcost, maintanencecost, laborcost, opportunitycost, demand, 
                               shortagecost, travelkm, traveltimeroute, traveltimevehicle, spoilagecost, spoilageamount,
                               vehiclecapacity, numofvehicles, maxhourvehicle, utility ):
        # Create the model
        model = Model('model')
        
        # Set parameters
        model.setParam('OutputFlag',True)
        
        products = 3       #number of products
        routes = 14       #number of routes
        vehicles=7          #number of vehicles
        
        # Add variables
        x = model.addVars(products, routes, lb=0, ub=GRB.INFINITY, vtype=GRB.CONTINUOUS, 
                          name=["x_"+str(i+1)+","+str(j+1) for i in range (products) for j in range(routes)])  
        s = model.addVars(products, routes, lb=0, ub=GRB.INFINITY, vtype=GRB.CONTINUOUS, 
                          name=["s_"+str(i+1)+","+str(j+1) for i in range (products) for j in range(routes)])  
        F = model.addVars(routes, lb=0, ub=GRB.INFINITY, vtype=GRB.CONTINUOUS, 
                          name=["F_"+str(j+1) for j in range(routes)])      
        
           
        # minimize cost  
        
        model.setObjective( quicksum( fuelcost*travelkm[i] for i in range (routes) ) 
                          + quicksum( maintanencecost*travelkm[i] for i in range (routes))
                          + quicksum( laborcost*traveltimeroute[i] for i in range (routes))
                          + quicksum( shortagecost*s[i,j] for i in range (products) for j in range(routes))
                          + quicksum( spoilagecost*spoilageamount[i][j] for i in range (products) for j in range(routes))
                          + quicksum( opportunitycost*(1-utility[v]) for v in range (vehicles)), GRB.MINIMIZE) 
        
           
        # demand/capacity constraint
        
        model.addConstrs( (quicksum( demand[i][j] for i in range (products) )  <= F[j] 
                            for j in range(routes)) )
        
        
        # demand/order constraint
        
        model.addConstrs( quicksum( x[i,j] + s[i,j] + spoilageamount[i][j] == demand[i][j] 
                           for i in range(products) for j in range(routes)))
        
        #employee/vehicle constraint
        # model.addConstr( 2*numofvehicles== numofemployees)
         
         
        #utility constraint
        model.addConstrs( utility[v] == traveltimevehicle[v]/ maxhourvehicle[v] for v in range(vehicles))
         
        
        # Optimize the model
        model.optimize()
        
        Output(model)
        
        # print the LP file
        model.write('model.lp')
        
        # print the sol file
        model.write('model.sol')

    ModelBetterImplemented(fuelcost=2, maintanencecost=1, laborcost=10, opportunitycost=3, spoilagecost=4, shortagecost=4, 
                           demand=[[20,35,30,40,45,50,20,25,27,15,20,35,30,40],
                                   [10,35,130,78,45,40,23,25,27,56,23,35,56,40],
                                   [50,45,30,40,45,50,20,25,25,13,23,45,45,76]],
                           travelkm=[200,305,540,670,230,370,286,470,430,435,234,567,348,346],
                           traveltimeroute=[6,7,6,5.6,8,6,7.4,6,6.8,7,5,8,7.3,6.2],
                           traveltimevehicle=[6,7,6,5.6,8,6,7.4,6,6.8,7,5,8,7.3,6.2],
                           spoilageamount=[[10,12,15,7,8,5,15,13,8,9,6,17,9,11],
                                           [10,12,15,7,8,5,15,13,8,9,6,17,9,12],
                                           [10,12,15,7,8,5,15,13,8,9,6,17,9,11]],
                           vehiclecapacity=2000,
                           numofvehicles=7,
                           maxhourvehicle=[16,16,16,16,16,16,14], 
                           utility=[0.5,0.5,0.5,0.45,0.5,0.5,0.5])
    0
  • Matthias Miltenberger
    • Gurobi Staff

    Hi Neda,

    You just need to remove the quicksum from this set of constraints - you cannot sum up constraints:

        model.addConstrs(
            x[i, j] + s[i, j] + spoilageamount[i][j] == demand[i][j]
            for i in range(products)
            for j in range(routes)
      )

    Cheers,
    Matthias

    0

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