メインコンテンツへスキップ

How to constrain my variables

進行中

コメント

5件のコメント

  • Jonasz Staszek
    • Community Moderator Community Moderator
    • Gurobi-versary
    • Thought Leader
    • First Question

    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

    0
  • FEI XUE
    • Gurobi-versary
    • Conversationalist
    • Curious

    Mr.Jonasz

    Thanks for your reply.

    This is my code.

    space=gurobipy.Model('matchpattern')
    n=6
    nn=15
    apij=space.addVars(range(1,nn+1), range(1,n+1), range(1,n+1),vtype=gurobipy.GRB.BINARY)、

    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.

    0
  • Jonasz Staszek
    • Community Moderator Community Moderator
    • Gurobi-versary
    • Thought Leader
    • First Question

    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

    0
  • FEI XUE
    • Gurobi-versary
    • Conversationalist
    • Curious

    Mr.Jonasz

    Thanks for your reply.

    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]]])
    0
  • FEI XUE
    • Gurobi-versary
    • Conversationalist
    • Curious

    Mr.Jonasz

    Thanks for your reply.

    I'm doing a matching problem, please let me describe it in more detail.


    When there are 6 people, there will be a matching situation like the one shown in the figure. The second column of them is called the match pattern. When n is 6, there are 15 different match patterns.
    apij represents whether i and j match in the match pattern p, or 1 if they match. So for the 15 match patterns, the situation for all apij should 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]]])

    How can I program to get  the varible apij that meet the conditions.

    0

サインインしてコメントを残してください。