KeyError: 1, я не знаю, что не так, сейчас я изучаю VRP с помощью Gurobi Optimization

У меня есть ошибка keyerror: 1 проблема с питоном, она находится в строке 172, ограничения №6 и, возможно, тоже будет для ограничений №7. Я пытаюсь добавить время в пути, разделив пункт назначения на j с помощью Ac[v] на скорость транспортного средства, у меня есть 5 транспортных средств с разной вместимостью и средней скоростью. Спасибо за любые предложения и помощь.

код, в котором есть проблема,

      from gurobipy import *
import os
import xlrd
#-------------------------------
num_node    = 5
num_vehicle     = 5
#-------------------------------
#SETS DEFINITION
N = ()  #Sets of all demand/customer node
V = ()  #Sets of vehicle

#PARAMETER VALUE
for n in range(1,num_node+1):
    N = N + (n,)
    Nd = (0,) + N
print ("Set Customer:", N)

for v in range(1,num_vehicle+1):
    V = V + (v,)
print ("Set Vehicle:",V)

#PARAMETER DEFINITION
D = {}      #distance between all node
T = {}      #travel time of vehicle between all node
S = {}      #speed of the vehicle
G = {}      #Demand for each customer
Q = {}      #Capacity of the vehicle
Cf = {}     #Fixed cost of the vehicle
Cv = {}     #Variable cost of the vehicle
Ac = {}     #Average speed of the vehicle
Q[1] = 3600
Q[2] = 14400
Q[3] = 18000
Q[4] = 28800
Q[5] = 36000
Cv[1] = 1234
Cv[2] = 1234
Cv[3] = 1234
Cv[4] = 1234
Cv[5] = 1234
Cf[1] = 123456
Cf[2] = 123456
Cf[3] = 123456
Cf[4] = 123456
Cf[5] = 123456
Ac[1] = 40/60
Ac[2] = 35/60
Ac[3] = 30/60
Ac[4] = 25/60
Ac[5] = 20/60
Z = 99999   #Sufficient big numberS
E = 0       #starting time of the day
L = 3000    #ending time of the day.
Et = {}     #star time
Lt = {}     #end time
St = 20     #service time


#DATA
#PARAMETER VALUE
book = xlrd.open_workbook(os.path.join("DroneA.xls"))
#sh = book.sheet_by_name("traveltimeTruck")
sd = book.sheet_by_name("distance")
#sh = book.sheet_by_name("traveltimeTruck")

for i in range(len(Nd)):
    for j in range(len(Nd)):
        D[Nd[i], Nd[j]] = sd.cell_value(i + 3, j + 3)
        #T[Nd[i], Nd[j]] = sh.cell_value(i + 3, j + 3)
        #print (Nd[i],Nd[j],T[Nd[i], Nd[j]])

#for i in range(len(Nd)):
#    for j in range(len(Nd)):
#        D[Nd[i], Nd[j]] = sh.cell_value(i + 3, j + 3)
#        T[Nd[i], Nd[j]] = sh.cell_value(i + 3, j + 3)
#        print (Nd[i],Nd[j],T[Nd[i], Nd[j]])

ld = book.sheet_by_name("demand")
for i in range(len(Nd)):
    G[Nd[i]] = ld.cell_value(i + 1, 1)
    #G[Nd[i]] = 3
    #print (Nd[i] ,G[Nd[i]])
    
tw = book.sheet_by_name("timewindow")
for i in range(len(Nd)):
    Et[Nd[i]] = tw.cell_value(i + 1, 1)
    Lt[Nd[i]] = tw.cell_value(i + 1, 2)
    #print (Nd[i],Et[Nd[i]])
    #print (Nd[i],Lt[Nd[i]])

#VARIABLE DEFINITION
x = {}  #binary vairable indicate that demand i and j are visited in a row by fleet v
g = {}  #accumulated load of vehicle v in node i
a = {}  #arrival time of vehicle v at node i

#--------------MODEL START--------------------------
m = Model("vrp_basic")
#--------------------------------------------------
#VARIABLE VALUE
for i in Nd:
    for j in Nd:
        for v in V:
            x[i,j,v] = m.addVar(vtype="B" , name="x[%s,%s,%s]"%(i,j,v))

for i in Nd:
    for v in V:
        g[i,v] = m.addVar(vtype="C" , name="g[%s,%s]"%(i,v))

for i in Nd:
    for v in V:
        a[i,v] = m.addVar(vtype="C" , name="a[%s,%s]"%(i,v))

m.update()

#--------------Objective Function--------------------------
m.setObjective(
    quicksum(x[i,j,v] * D[i,j] * Cv[v]  for i in Nd for j in Nd for v in V)
    + quicksum(x[0,j,v] * Cf[v] for j in Nd for v in V)
)
m.ModelSense = 1 #MINIMIZATION
#--------------Constraint--------------
#2
for i in N:
    m.addConstr(
        quicksum(x[i,j,v] for j in Nd for v in V) == 1
        , "1[%s]"%i)

#3
for j in N:
    m.addConstr(
        quicksum(x[i,j,v] for i in Nd for v in V) == 1
        , "2[%s]"%j)

#4
for k in N:
    for v in V:
        m.addConstr(
            quicksum(x[i,k,v] for i in Nd) - quicksum(x[k,j,v] for j in Nd) == 0
            , "3[%s,%s]"%(k,v))

#5
for v in V:
    m.addConstr(
        quicksum(x[0,i,v] for i in N) <= 1
        , "4[%s]"%v)

for i in Nd:
    for j in N:
        for v in V:
            m.addConstr(
                g[j,v] >= g[i,v] + G[j] + (x[i,j,v] - 1) * Z
                , "5[%s,%s,%s]"%(i,j,v))        

for i in Nd:
    for v in V:
        m.addConstr(
            g[i,v] <= Q[v]
            , "6[%s,%s]"%(i,v))

#6
for i in Nd:
    for j in N:
        for v in V:
            m.addConstr(
               a[j,v] >= a[i,v] + (D[j]/Ac[v]) + St + (x[i,j,v] - 1) * Z
                )

#7
for i in N:
    for v in V:
        m.addConstr(
            a[i,v] + (D[i,0]/Ac[v]) <= L
            )
        
#8
for i in N:
    for v in V:
        m.addConstr(
            a[i,v] >= Et[i]
            )

#9
for i in N:
    for v in V:
        m.addConstr(
            a[i,v] <= Lt[i]
            )
        
#10
#--------------------------------------
m.update()
m.optimize()
#=======================END OF MODEL========================

#-------Print Output--------------
edges = [(i,j,v) for (i,j,v) in x if x[i,j,v].X > 0.9999]
print(edges)

def next_customer(node, vehicle,x):
    outs = -1
    for i in Nd:
        out = 0
        if(x[node,i,vehicle].X > 0.9999):
            out = i

        if(out > 0):
            outs = out

    return outs

def print_node(node, vehicle, x, cum):
    if(cum < num_node):
        if(node > 0 ):
            k = next_customer(node, vehicle,x)
            cum1 = cum + 1
            if(k > 0):
                print ("=>",k, end =" "),
                #print "[",d[k],":",
                #print '%1.2f'%t[node,k,vehicle],
                #print "]",
                if(k > 0):
                    print_node(k, vehicle, x, cum1)

    return k

for v in V:
    for j in Nd:
        if x[0,j,v].X > 0.9999 :
            print ("[",v,"]",0,"=>",j,end =" ")
            print_node(j, v, x, 0)
            print ("")
            for i in Nd:
                for k in Nd:
                    if x[i,k,v].X > 0.9999:
                        print ("(",'%3s'%i,",",'%3s'%k,")",": g(%s) ="%i,'%06.2f'%g[i,v].X,)
                        #print ": g(%s) ="%i,'%06.2f'%g[i,v].X,

print ("-------------------------------------------------")
print ("OPTIMAL VALUE=", m.ObjVal)
print ("RUNTIME=", m.Runtime)
print ("--------------------------------------------------")

Файл вы можете скачать здесь

И проблема со снимком экрана, как вы можете видеть здесь

0 ответов

Другие вопросы по тегам