• Community Moderator

Hi Fei,

I am not sure I understand your question. Could you show us the mathematical model you are trying to code (or at least the part your question relates to)?

Best regards
Jonasz

Mr.Jonasz

This is my code.

space=gurobipy.Model('matchpattern')
n=6
nn=15

space.update()
space.setObjective(quicksum(apij[p,i,j] for i in range (1,n+1) for j in range(1,n+1) for p in range(1,nn+1)),GRB.MINIMIZE)

space.addConstrs(quicksum(apij[p,i,j] for i in range(1,n+1))==1 for j in range(1,n+1) for p in range(1,nn+1))
space.addConstrs(quicksum(apij[p,i,j] for j in range(1,n+1))==1 for i in range(1,n+1) for p in range(1,nn+1))

space.addConstrs(quicksum(apij[p,i,j] for p in range(1,nn+1))==3 for i in range(1,n+1) for j in range(1,n+1) if i!=j)

space.addConstrs(apij[p,i,j]==apij[p,j,i] for i in range (1,n+1) for j in range(1,n+1) for p in range(1,nn+1))
space.addConstrs(apij[p,i,i]==0 for i in range (1,n+1) for p in range(1,nn+1))

space.optimize()

print(apij)

In order to make the i and j two-dimensional matrices in apij different, I added the following condition.

space.addConstrs(apij[p1,1,j1]+apij[p1,2,j2]+apij[p2,1,j1]+apij[p2,2,j2]<=3 for p1 in range(1,nn+1) for p2 in range(1,nn+1) for j1 in range(1,n+1) for j2 in range(1,n+1) if p1!=p2 if j2!=1)

When n is equal to 8, then nn becomes 105. How should the constraints be changed so that the 105 i, j matrices of apij do not repeat each other.

• Community Moderator

I still have a difficult time understanding the constraint you are trying to implement.

Could you try to express it in mathematical terms (e.g. the way you would write down a mathematical model on a sheet of paper)?

Best regards
Jonasz

Mr.Jonasz

When n is 6, I want the result of variable apij to be as follows:

apij=np.array([[[0,1,0,0,0,0],[1,0,0,0,0,0],[0,0,0,1,0,0],[0,0,1,0,0,0],[0,0,0,0,0,1],[0,0,0,0,1,0]],\               [[0,1,0,0,0,0],[1,0,0,0,0,0],[0,0,0,0,1,0],[0,0,0,0,0,1],[0,0,1,0,0,1],[0,0,0,1,0,0]],\               [[0,1,0,0,0,0],[1,0,0,0,0,0],[0,0,0,0,0,1],[0,0,0,0,1,0],[0,0,0,1,0,0],[0,0,1,0,0,0]],\               [[0,0,1,0,0,0],[0,0,0,1,0,0],[1,0,0,0,0,0],[0,1,0,0,0,0],[0,0,0,0,0,1],[0,0,0,0,1,0]],\               [[0,0,1,0,0,0],[0,0,0,0,1,0],[1,0,0,0,0,0],[0,0,0,0,0,1],[0,1,0,0,0,0],[0,0,0,1,0,0]],\               [[0,0,1,0,0,0],[0,0,0,0,0,1],[1,0,0,0,0,0],[0,0,0,0,1,0],[0,0,0,1,0,0],[0,1,0,0,0,0]],\               [[0,0,0,1,0,0],[0,0,1,0,0,0],[0,1,0,0,0,0],[1,0,0,0,0,0],[0,0,0,0,0,1],[0,0,0,0,1,0]],\               [[0,0,0,1,0,0],[0,0,0,0,1,0],[0,0,0,0,0,1],[1,0,0,0,0,0],[0,1,0,0,0,0],[0,0,1,0,0,0]],\               [[0,0,0,1,0,0],[0,0,0,0,0,1],[0,0,0,0,1,0],[1,0,0,0,0,0],[0,0,1,0,0,0],[0,1,0,0,0,0]],\               [[0,0,0,0,1,0],[0,0,1,0,0,0],[0,1,0,0,0,0],[0,0,0,0,0,1],[1,0,0,0,0,0],[0,0,0,1,0,0]],\               [[0,0,0,0,1,0],[0,0,0,1,0,0],[0,0,0,0,0,1],[0,1,0,0,0,0],[1,0,0,0,0,0],[0,0,1,0,0,0]],\               [[0,0,0,0,1,0],[0,0,0,0,0,1],[0,0,0,1,0,0],[0,0,1,0,0,0],[1,0,0,0,0,0],[0,1,0,0,0,0]],\               [[0,0,0,0,0,1],[0,0,1,0,0,0],[0,1,0,0,0,0],[0,0,0,0,1,0],[0,0,0,1,0,0],[1,0,0,0,0,0]],\               [[0,0,0,0,0,1],[0,0,0,1,0,0],[0,0,0,0,1,0],[0,1,0,0,0,0],[0,0,1,0,0,0],[1,0,0,0,0,0]],\               [[0,0,0,0,0,1],[0,0,0,0,1,0],[0,0,0,1,0,0],[0,0,1,0,0,0],[0,1,0,0,0,0],[1,0,0,0,0,0]]])

Mr.Jonasz

apij=np.array([[[0,1,0,0,0,0],[1,0,0,0,0,0],[0,0,0,1,0,0],[0,0,1,0,0,0],[0,0,0,0,0,1],[0,0,0,0,1,0]],\               [[0,1,0,0,0,0],[1,0,0,0,0,0],[0,0,0,0,1,0],[0,0,0,0,0,1],[0,0,1,0,0,1],[0,0,0,1,0,0]],\               [[0,1,0,0,0,0],[1,0,0,0,0,0],[0,0,0,0,0,1],[0,0,0,0,1,0],[0,0,0,1,0,0],[0,0,1,0,0,0]],\               [[0,0,1,0,0,0],[0,0,0,1,0,0],[1,0,0,0,0,0],[0,1,0,0,0,0],[0,0,0,0,0,1],[0,0,0,0,1,0]],\               [[0,0,1,0,0,0],[0,0,0,0,1,0],[1,0,0,0,0,0],[0,0,0,0,0,1],[0,1,0,0,0,0],[0,0,0,1,0,0]],\               [[0,0,1,0,0,0],[0,0,0,0,0,1],[1,0,0,0,0,0],[0,0,0,0,1,0],[0,0,0,1,0,0],[0,1,0,0,0,0]],\               [[0,0,0,1,0,0],[0,0,1,0,0,0],[0,1,0,0,0,0],[1,0,0,0,0,0],[0,0,0,0,0,1],[0,0,0,0,1,0]],\               [[0,0,0,1,0,0],[0,0,0,0,1,0],[0,0,0,0,0,1],[1,0,0,0,0,0],[0,1,0,0,0,0],[0,0,1,0,0,0]],\               [[0,0,0,1,0,0],[0,0,0,0,0,1],[0,0,0,0,1,0],[1,0,0,0,0,0],[0,0,1,0,0,0],[0,1,0,0,0,0]],\               [[0,0,0,0,1,0],[0,0,1,0,0,0],[0,1,0,0,0,0],[0,0,0,0,0,1],[1,0,0,0,0,0],[0,0,0,1,0,0]],\               [[0,0,0,0,1,0],[0,0,0,1,0,0],[0,0,0,0,0,1],[0,1,0,0,0,0],[1,0,0,0,0,0],[0,0,1,0,0,0]],\               [[0,0,0,0,1,0],[0,0,0,0,0,1],[0,0,0,1,0,0],[0,0,1,0,0,0],[1,0,0,0,0,0],[0,1,0,0,0,0]],\               [[0,0,0,0,0,1],[0,0,1,0,0,0],[0,1,0,0,0,0],[0,0,0,0,1,0],[0,0,0,1,0,0],[1,0,0,0,0,0]],\               [[0,0,0,0,0,1],[0,0,0,1,0,0],[0,0,0,0,1,0],[0,1,0,0,0,0],[0,0,1,0,0,0],[1,0,0,0,0,0]],\               [[0,0,0,0,0,1],[0,0,0,0,1,0],[0,0,0,1,0,0],[0,0,1,0,0,0],[0,1,0,0,0,0],[1,0,0,0,0,0]]])