JijModeling Cheat Sheet
caution
This document is out-dated. Go back to latest page
Summations
Sum over variables by index
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
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
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
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
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
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.
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
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
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
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
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)
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
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)])