write the optimization code with Gurobi
OngoingHello
I have a mathematical model to minimize time violations. could you help me to Gurobi model.
\(P_ij^(k,w)\) = Binary variable=1, if truck k and w drive as a platoon on edge \(e_ij\)
\(ST^k\) = shortest path time of each truck
The objective function for time violation for all trucks \(Min z = ∑_k(x_ij^k*t_ij +WT^k ) –ST^k\)
constraints:
- Calculate travelling time of truck at edge ij \( t_ij=d_ij /v\)
- max (arrival time truck at platoon point i from origin point ) \( MT^k=max(P_ij^{k,w}*t_{o^k i}^k)\)
- the arrival time of all trucks at platoon point i = last truck arrived at platoon( \( MT^k)\)
- \( WT^k = MT^k\) – platoon arrival time of each truck point i
- \(WT^k=0 \)If travelling time of each truck == \( ST^k\)
- number of trucks in platoon <= 5
- total travelling time of truck <= deadline of each truck
saving_factor = 0.1
number_of_nodes = 6
node_list = []
for i in range(1,number_of_nodes+1):
node_list.append(i)
group = {0: (1, 5), 1: (2, 6)}
#group = {0:(1, 2), 1:(3, 4)}
graph = nx.DiGraph()
edges = [(1, 3, {'weight': 1}),(3, 1, {'weight': 1}),
(2, 3, {'weight': 2}),(3, 2, {'weight': 2}),
(4, 3, {'weight': 2}),(3, 4, {'weight': 2}),
(4, 5, {'weight': 2}),(5, 4, {'weight': 2}),
(4, 6, {'weight': 2}),(6, 4, {'weight': 2}),
(1, 5, {'weight': 4.9}),(5, 1, {'weight': 4.9}),
(2, 6, {'weight': 5.9}),(6, 2, {'weight': 5.9})]
speed = 1
graph.add_edges_from(edges)
road_network = graph
#shortest path time each truck = earliest_arrival_time_each_truck
earliest_arrival_time_each_truck={}
for h, locations in group.items():
earliest_arrival_time_each_truck[h] = nx.dijkstra_path_length(road_network, source=locations[0], target=locations[1])/speed
model = Model("Routing")
## Variables
x = {}
y = {}
for edge in road_network.edges():
x[edge] = {}
y[edge] = model.addVar(vtype=GRB.BINARY, name="{}".format("y"+str(edge)))
for h, _ in group.items():
x[edge][h] = model.addVar(vtype=GRB.BINARY, name="{}".format("x" +str(edge)+str(h)))
for v in road_network.nodes():
for h, locations in group.items():
if locations[0] == v:
b = 1
elif locations[1] == v:
b = -1
else:
b = 0
model.addConstr(quicksum(x[edge][h] for edge in road_network.out_edges(v)) -
quicksum(x[edge][h] for edge in road_network.in_edges(v)) == b, name='flow_' + str(v) + "_" + str(h))
for edge in road_network.edges():
for h, locations in group.items():
model.addConstr(x[edge][h] <= y[edge])
objective_function_main= gp.LinExpr()
total_travel_time_per_truck = {}
for h, _ in group.items():
total_travel_time_per_truck[h] = gp.LinExpr()
for e in road_network.edges():
total_travel_time_per_truck[h] += x[e][h] * (road_network.get_edge_data(*e)["weight"] / speed)
objective_function_main+= (total_travel_time_per_truck[h] + waitingtime[h] - earliest_arrival_time_each_truck[h])
here in this gurobi code I already implement constraints for vehicle platoon problem.
I need to add the above mention constraint to my code.
could you help me, please?
Thank you
0
-
Official comment
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.
Post is closed for comments.
Comments
2 comments