Skip to main content

JijModeling Cheat Sheet

caution

This document is out-dated. Go back to latest page

Summations

Sum over variables by index

i=0N1xi\sum_{i=0}^{N-1} x_i
N = jm.Placeholder('N')
x = jm.BinaryVar('x',shape=(N,))
i = jm.Element('i', belong_to=(0, N))
jm.sum(i,x[i])

Sum over variables with coefficients

i=0N1aixi\sum_{i=0}^{N-1} a_ix_i
N = jm.Placeholder('N')
a = jm.Placeholder('a', ndim=1)
x = jm.BinaryVar('x', shape=(N,))
i = jm.Element('i', belong_to=(0, N))
jm.sum(i,a[i] * x[i])

Sum over variables with indices in the set

iCxi\sum_{i\in C} x_i
N = jm.Placeholder('N')
C = jm.Placeholder('C', ndim=1)
x = jm.BinaryVar('x', shape=(N,))
i = jm.Element('i', belong_to=C)
jm.sum(i,x[i])

Sum over graph nodes in edges

(u,v)Exuxv\sum_{(u,v)\in E} x_u x_v
V = jm.Placeholder('V')
E = jm.Placeholder('E', ndim=2)
x = jm.BinaryVar('x', shape=(V,))
e = jm.Element('e', belong_to=E)
jm.sum(e, x[e[0]]*x[e[1]])

Sum with condition

i=0N1j=0i>jN1Jijxixj\sum_{i = 0}^{N-1}\sum_{j = 0|i>j}^{N-1} J_{ij} x_i x_j
N = jm.Placeholder('N')
J = jm.Placeholder('J', ndim=2)
x = jm.BinaryVar('x', shape=(N,))
i = jm.Element('i', belong_to=(0, N))
j = jm.Element('j', belong_to=(0, N))
jm.sum([i,(j,i>j)],J[i,j] * x[i] * x[i])

Sum over all elements in matrix except for diagonal elements

i=0N1j=0ijN1Jij\sum_{i = 0}^{N-1}\sum_{j = 0|i\neq j}^{N-1} J_{ij}
N = jm.Placeholder('N')
J = jm.Placeholder('J', ndim=2)
i = jm.Element('i', belong_to=(0, N))
j = jm.Element('j', belong_to=(0, N))
jm.sum([i,(j,i!=j)],J[i,j])

The range of a sum depends on the index of another sum.

i=0N1j=0ai1xj\sum_{i = 0}^{N-1}\sum_{j = 0}^{a_i - 1} x_j
N = jm.Placeholder('N')
x = jm.BinaryVar('x', shape=(N,))
a = jm.Placeholder('a', ndim=1)
i = jm.Element('i', belong_to=(0,N))
j = jm.Element('j', belong_to=a[i])
jm.sum(i, jm.sum(j, x[j]))

Constraints

One-hot constraint

i=0N1xi=1\sum_{i=0}^{N-1} x_i = 1
N = jm.Placeholder('N')
x = jm.BinaryVar('x',shape=(N,))
i = jm.Element('i', belong_to=(0, N))
jm.Constraint('onehot_constraint' , jm.sum(i,x[i]) == 1)

K-hot constraint

i=0N1xi=K\sum_{i=0}^{N-1} x_i = K
K = jm.Placeholder('K')
N = jm.Placeholder('N')
x = jm.BinaryVar('x',shape=(N,))
i = jm.Element('i', belong_to=(0, N))
jm.Constraint('k-hot_constraint' , jm.sum(i,x[i]) == K)

K-hot constraint for each index of 2-dim Binary variable

i=0N1xij=Kj (j)\sum_{i=0}^{N-1} x_{ij} = K_j\ (\forall j)
K = jm.Placeholder('K', ndim=1)
N = jm.Placeholder('N')
x = jm.BinaryVar('x', shape=(N,N))
i = jm.Element('i', belong_to=(0, N))
j = jm.Element('j', belong_to=(0, N))
jm.Constraint('k-hot_constraint' , jm.sum(i,x[i,j]) == K[j], forall=j)

K-hot constraint for each set

iCαxi=Kα\sum_{i\in C_\alpha} x_i = K_\alpha
K = jm.Placeholder('K', ndim=1)
C = jm.Placeholder('C', ndim=2)
N = jm.Placeholder('N') # for binary index
M = jm.Placeholder('M') # for set index

x = jm.BinaryVar('x', shape=(N,))
a = jm.Element('a', belong_to=(0, M))
a.set_latex(r'\alpha')
i = jm.Element('i', belong_to=C[a])

jm.Constraint('k-hot_constraint' , jm.sum(i,x[i]) == K[a], forall=a)

Knapsack Constraint (Linear inequality constraint)

i=0N1wixiW\sum_{i=0}^{N-1} w_ix_i \leq W
w = jm.Placeholder('w', ndim=1)
W = jm.Placeholder('W')
N = jm.Placeholder('N')
x = jm.BinaryVar('x', shape=(N,))
i = jm.Element('i', belong_to=(0, N))
jm.Constraint('weight', jm.sum(i,w[i] * x[i])<=W)

Big-M inequality

ti+cijM(1xij)tj,i,j(ji)t_i + c_{ij} - M(1 - x_{ij})\leq t_j, \forall i,\forall j (j\neq i)
c = jm.Placeholder('c', ndim=2)
N = c.len_at(0, latex='N')
M = jm.Placeholder('M')

x = jm.BinaryVar('x', shape=(N,N))
e = jm.Placeholder('e', ndim=1)
l = jm.Placeholder('l', ndim=1)
t = jm.IntegerVar('t',shape=(N,),lower_bound=e,upper_bound=l)
i = jm.Element('i', belong_to=(0,N))
j = jm.Element('j',belong_to=(0,N))

jm.Constraint('Big-M',t[i] + c[i,j] - M * (1 - x[i,j]) <= t[j],forall=[(i),(j,j!=i)])