How to output all feasible solutions?
Answeredfrom gurobipy import *
import numpy as np
import math
space=gurobipy.Model('matchpattern')
n=6
pij=space.addVars(range(1,n+1), range(1,n+1),vtype=gurobipy.GRB.BINARY)
space.update()
space.setObjective(quicksum(pij[i,j] for i in range (1,n+1) for j in range(1,n+1)),GRB.MINIMIZE)
space.addConstrs(quicksum(pij[i,j] for i in range(1,n+1))==1 for j in range(1,n+1))
space.addConstrs(quicksum(pij[i,j] for j in range(1,n+1))==1 for i in range(1,n+1))
space.addConstrs(pij[i,j]==pij[j,i] for i in range (1,n+1) for j in range(1,n+1))
space.addConstrs(pij[i,i]==0 for i in range (1,n+1))
space.optimize()
Here is my code, when n is 6, there should be 15 possible solutions, how can I output all 15 possible solutions as a matrix like this?
Looking forward your reply. Thank you very much.
([[[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]]])
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
If you know upfront that there will be 15 solutions, you can add these two lines before the optimize call:
space.setParam("PoolSolutions", 15)
space.setParam("PoolSearchMode", 2)Further info can be found here.
Best regards,
Jonasz0 -
Doctor Jonasz
Thank you very much.
It helps me a lot.
0
Post is closed for comments.
Comments
3 comments