Show Menu
Cheatography

Xuan Thao VRP Cheat Sheet (DRAFT) by

This is an Ortools cheat sheet of Routing module

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Ortools

create dimension
dimens­ion­_name = 'Distance'
routin­g.A­ddD­ime­nsion(
transi­t_c­all­bac­k_i­ndex,
0, # no slack
300_000, # vehicle maximum travel distance
True, #start cumul to zero
dimens­ion­_name)
activate dimension
distan­ce_­dim­ension =
routing.GetDim­ens­ion­OrDie(dimen­sio­n_name)
add custom constraint to solver
routin­g.s­olv­er(­).Add( constr­ain­t_B­oolean )
which vehicle visiting i
routin­g.V­ehi­­cle­Var(i)
-1 if i not visited
value of dimension when reaching i
dimension.CumulVar(i)
Set value of dimension at i inside (a,b)
dimension.CumulVar(i).SetRange(a, b)
Increase speed for P&D
routing.AddPic­kup­And­Del­ivery(pickup_i, delive­ry_i)
hard limited veh/cus
force i dropped or served by veh1 or veh2
routin­g.V­ehi­­cle­Var(i).SetValues([-1, veh1, veh2])
when customer choose vehicle
Next visited node after i
rounting.NextVar(i)
NextVar(i) == j is true if j is the node immedi­ately reached from node i in the solution.
force j not after i
routing.nextVar(from).remove­Value(to);
drop node, or have not been visited in search process
routing.ActiveVar(i)
a Boolean variable that indicates if a node i is visited or not in the solution.
TW for customers
dimension.CumulVar(i).SetRange(a, b)
for i not depot_idx
TW for depot (for start point)
for veh_idx in range(num_vehicle):
start_­loc­_index = routing.Start(veh_idx)
node= manage­r.I­nde­xTo­Nod­e(s­tar­t_l­oc_­index )
a = TW[ node ][0]
b = TW[ node ][1]
time_dim.CumulVar(start­_lo­c_i­ndex).SetRange(a,b)
TW for endpoint
for veh_idx in range(num_vehicle):
end_lo­c_index = routing.End(veh_idx)
node= manager.IndexToNode(end_loc_index)
a = TW[ node ][0]
b = TW[ node ][1]
time_dim.CumulVar(end_l­oc_­index).SetRange(a,b)
Time visit at node i
time_var = time_d­ime­nsion.CumulVar(index)
Return the earliest/ lastest possible time to visit node i
solution.Min(time_var)
solution.Max(time_var)
Take an array of capacities (each vehicle has different capacity)
AddDim­ens­ion­Wit­hVe­hic­leC­apacity
Set dimension with different max val of dim and one callback
dimens­­io­n­_name = 'Capacity '
routin­g.AddDim­ens­ion­Wit­hVe­hic­leC­apacity(
transi­­t_­c­a­ll­­bac­­k_­i­ndex,
0, # no slack
[ 10000, 20000], # vehicle maximum travel distance
True, #start cumul to zero
dimens­­io­n­_­name)
Set dimension with different max val of dim and many callbacks
routing.AddDim­ens­ion­Wit­hVe­hic­leT­ran­sit­And­Cap­acity(
callba­ck_­fun­c_l­ist­_by_veh,
0, # no slack
[max_d­im_vel list],
# vehicle maximum travel time True, # start cumul to zero
dimens­ion­_name)
multiple callbacks depend on vehicl­e.param
def callba­ck(­fro­m_i­ndex, to_index, veh.param)
callba­ck_list = [parti­al(­cal­lback, veh.param) for veh in veh_list]
register multiple callback
regist­er_­cal­lba­ck_­list= [routi­ng.R­eg­ist­erT­ran­sit­Cal­lba­ck(f) for f in callba­ck_­list]
define cost function for many vehs with different costs callback
sum_veh_k time(i­,j)_k
for veh_id in range(­len­(ve­h_l­ist)): routin­g.S­etA­rcC­ost­Eva­lua­tor­OfV­ehi­cle­(ca­llb­a­c­k_l­ist­[ve­h_id], veh_id)
set arc cost for vehicle k moving from i to j
1.
def callba­ck_­cos­t(f­node, tnode, veh_id)
return 3 d(i,j,­veh_id) +40t (i,j, veh_id) cost_c­all­bac­k_list =[ partia­l(c­all­bac­k_cost, veh_id­=ve­h_id) for veh_id in veh_list] regist­er_­cos­t_c­all­bac­k_list= [routi­ng.R­eg­ist­erT­ran­sit­Cal­lba­ck(f) for f in cost_c­all­bac­k_list] for veh_id in range(­len­(ve­h_l­ist)): routin­g.S­etA­rcC­ost­Eva­lua­tor­OfV­ehi­cle­(re­gis­ter­_co­st_­cal­lba­ck_­lis­t[v­eh_id], veh_id)
create cost callback from i to j depend on vehicl­e.param
def cost(i, j, veh_id):
return d(i,j,­veh_id) + t(i,j,­veh_id)
cost_list = [parti­al(­cost, veh_id=i) for i in veh_id_list]
reg_co­st_list = [routi­ng.R­eg­ist­erT­ran­sit­Cal­lba­ck(f) for f in cost_list]
set different arc cost for different vehicle
routin­g.S­etA­rcC­ost­Eva­lua­tor­OfV­ehi­cle­(re­g_c­ost­_li­st[i], i )
 
staff_­at_end = [] for vehicle_id in range(­man­age­r.G­etN­umb­erO­fVe­hic­les()): index = routin­g.E­nd(­veh­icl­e_id) staff_­at_­end.ap­pen­d(s­taf­f_d­ime­nsi­on.C­um­ulV­ar(­ind­ex)­)so­lve­r.A­dd(­sol­ver.Su­m(s­taf­f_a­t_end) <= N)
SetFix­edC­ost­OfV­ehicle
SetMax­imu­mNu­mbe­rOf­Act­ive­Veh­icles
 

ortools 2

force vehicle i to visit its end location by the earliest possible time
routing.AddVar­iab­leM­ini­miz­edB­yFi­nalizer(time_dim.CumulVar(routing.Start(i)))
force vehicle i to leave its start location by the earliest possible time
routing.AddVar­iab­leM­ini­miz­edB­yFi­nalizer(time_dim.CumulVar(routing.End(i)))

CPSAT

create new Variable
x = model.N­ew­Int­Var(0, 10, 'x')
Implement b == (x >= 5)
model.A­dd(x >= 5).Onl­yEn­for­ceIf(b)
model.A­dd(x < 5).Onl­yEn­for­ceI­f(b.Not())