KeyError(3,3) when running objective function
Answeredxc_origin = np.array([0]) # x-cordinate for the port of origin defined as an array
xc_destination = np.array([10]) # x-cordinate for the port of destination defined as an array
xc_hub = np.array([5]) # x-cordinate for the fuel hub defined as an array
yc_origin = np.array([5]) # y-cordinate for the port of origin defined as an array
yc_destination = np.array([5]) # y-cordinate for the port of destination defined as an array
yc_hub = np.array([0]) # y-cordinate for the fuel hub defined as an array
num_points_network = 27 # Defining numer of points we wish to generate between the two ports and the fuel hub. This value are arbitrarly
generate_random_cordinates = True # if True we genrate random network of points between the ports and hub(s) (Realistic scenario)
if generate_random_cordinates:
xc_random = np.random.randint(low=np.min(xc_origin)+1, high=np.max(xc_destination)-1, size=num_points_network) # generating random values for the locations between the two x cordinates for the ports
if len(yc_hub) <= 2:
y_pot_hub_max =10
else:
y_pot_hub_max = np.max(yc_hub)-1
yc_random = np.random.randint(low = np.min(yc_hub)+1, high = y_pot_hub_max, size=num_points_network) # generating random values for the locations between the two y cordinates for the hubs
xc = np.concatenate((xc_origin, xc_destination, xc_hub, xc_random), axis=0) # adding all x-cordinate arrays together
yc = np.concatenate((yc_origin, yc_destination, yc_hub, yc_random), axis=0) # adding all y-cordinate arrays together
origin_index = [int(i) for i in range(len(xc_origin))]
destination_index = [int(i+len(xc_origin)) for i in range(len(xc_destination))]
hub_index = [int(i+len(xc_origin)+len(xc_destination))for i in range(len(xc_hub))]
random_index = [int(i+len(xc_origin)+len(xc_destination)+len(xc_hub)) for i in range(len(xc_random))]
P = origin_index + destination_index # Index position for Location of port of Origin, O and port of destination, D
H = hub_index # Index position for energy hub
L = random_index # Index position for replenishment locations
N = P+H+L # Defining all possible nodes (replenishment locations)
V = [0] # Initial position of vessel
C_S = 1 # Cost of sailing from node 𝑖 to 𝑗 [$/𝑛𝑚]
C_LOC = 4 # Lost opportunity cost per unit energy storage capacity [$/𝑚3]
K_V = 10 # Total storage capacity of vessel [𝑚3]
E_V = 1 # Energy consumption per distance traveled [𝑚3/𝑛𝑚]
C_FHS = 1 # Cost of supplying fuel from a fuel hub per distance unit [$/𝑛𝑚]
C_FHE = 1 # Fixed cost of establishing an energy replenishment location
A = [(i,j) for i in N for j in N if i!=j] # defining all possible arcs
D_ij = {(i,j): np.hypot(xc[i]-xc[j],yc[i]-yc[j]) for i,j in A} # Sailing distance between node 𝑖 and node 𝑗 [𝑛𝑚]
mdl = Model("Optimal location of energy replenishment for a network of locations")
x_ij = mdl.addVars(A, vtype = GRB.BINARY) # If veseel 𝑣 visits the enrgy replenishment station $j$, 0 otherwise
k_ES = mdl.addVar(vtype = GRB.CONTINUOUS) # Energy storage capacity of the vessel
e_ij = mdl.addVars(A, vtype = GRB.CONTINUOUS) # energy consumption for the vessel from node 𝑖 to to node 𝑗
# Objective function
mdl.modelSense = GRB.MINIMIZE
mdl.setObjective(quicksum(C_S * D_ij[i,j] * x_ij[i,j] + C_LOC * k_ES for i,j in A)
+ quicksum((C_FHS * D_ij[hub_index[0],j] + C_FHE) * x_ij[i,j] for i in N for j in L if j != H))
When i run the code above i get the following error:
KeyError(3,3)
The error seems to occur in the objective function.
How can I resolve the issue?
Any help would be appreciated
0
-
Official comment
This post is more than two 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. -
Hi Lasse,
You use x_ij and D_ij in your objective function, each of which are indexed by A. The definition of A if such that pairs of the form (i,i) such as (3,3) do not exist, hence the KeyError. Maybe the underlying problem is as simple as a typo?
- Riley
0
Post is closed for comments.
Comments
2 comments