Show Menu
Cheatography

Ortools CheatSheet Cheat Sheet (DRAFT) by

How to use Ortools in a nutshell

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

Numpy

Create Empty Array of size n*m
np.empty( (n,m), np.int_)
Create random array of size n*m in range [a b]
np.ran­dom.ra­ndint( a, b, (n, m))
Create array with all element equals to zeros
np.zeros( (n,m), np.int_)
Create array with all element equals to Inf
np.full( (1,n), np.inf)
Merge two array as a new array
np.con­cat­ena­te(­(a,b))
Copy new array with new address and same value as a
np.copy(a)
location = np.zer­os((n, 2), np.int_)

List

Appends an element to the end of the list
list.a­ppend()
Create array with all element equals to None
[None]*n
Delete item in an array by index
del a[index]
a = ["ap­ple­", "­ban­ana­", "­che­rry­"]
b = ["Fo­rd", "­BMW­", "­Vol­vo"]
for i in b:
a.appe­nd(i)

ortool­_cp_sat

declare the model
model = cp_mod­el.C­pM­odel()
declare variable with boolean value
model.N­ew­Boo­lVa­r(f­'[x­{I}­{j}]')
x = {}
for work in range(­num­ber­_wo­rker):
for task in range(­num­ber­_task):
x[work­,task] = model.N­ew­Boo­lVa­r(f­'x[­{wo­rk}­{ta­sk}]')

folium

 
folium.ma­p(l­oca­tion, tiles, zoom_s­tart)
 

Numba

Decorator
@jit, @njit
speed math
fastmath = True
cache function
cache= True
Parallel numba
nogil = True, parallel= True, for i in prange(n):
njit for empty list
@njit
@njit
def create_S()
S = List()

Matplo­tli­b.p­yplot

Draw points given long, lat
plt.plot( X, Y, 'ro')
Draw points vertically
plt.plot(list(zip(temp))­,'ro')
draw line
plt.plot( [x1, x2], [y1, y2],'ro )
change size of a figure
figure­(fi­gsi­ze=­(width, heigh), dpi=re­sol­ution)
show value on figure
plt.te­xt(x, y, value)
plt.plot( [3, 2], [5,6], 'r-',
marker­='o', marker­fac­eco­lor­='b', marker­edg­ecolor ='b', markersize =5, linewidth = 0.5, alpha=0.5 )

from matplo­tli­b.p­yplot import figure
figure­(fi­gsi­ze=(8, 6), dpi=80)

plt.te­xt(­5,5­,'Total demand = {} and number of sub tours = {}'
.forma­t(w­eig­ht.s­um­(),­len­(sh­ort­est­_pa­th)-1))

class

 
def __init__ (self,....)

Ortools

Restrict Vehicle
routin­g.V­ehi­cle­Var­(in­dex­).S­etV­alu­es([-1, 2,3,4])
different limit constraint
routin­g.A­ddD­ime­nsi­onW­ith­Veh­icl­eCa­pacity( demand­_ca­llb­ack­_index, 0, # null capacity slack data['­veh­icl­e_c­apa­cit­ies'], # vehicle maximum capacities True, # start cumul to zero 'Capac­ity')
find vehicle
Vehicl­eVar(i)
first node after depot
start_var = routin­g.N­ext­Var­(ro­uti­ng.S­ta­rt(­veh­icl­e_nbr))
 
SetVeh­icl­eFi­xedCost
 
AddDim­ens­ion­Wit­hVe­hic­leC­apacity
 
SetVeh­icl­eCost
 
 
Active­Var(i) == (NextV­ar(i) != i).
 
SetCum­ulV­arS­oft­Low­erB­oun­d(i­nde­x_end, 2, 100000)