2017-06-06 269 views
0

我已經表達如下,我想知道如果你能幫助我正式作爲ILP的約束,以便通過Gurobi優化器(Python)的解決:如何用Gurobi在Python中實現這個約束?

FORALL的情況下(Y),FORALL(j併購),forall(x in X): IF r [x] [y] = 1且c [y,j] = 1 THEN p [x,a] = 1,forall(a in {U [j] ,. ...,W [j] - 1}) 其中: r [x] [y],c [y,j]和p [x,a]是3個二元變量; U [j]與W [j]的2個正整數的變量,其中,U [J] +的β= W [j]的 (beta是正的常數)

我知道,這個約束可以寫爲一合取範式形式邏輯蘊涵:X∧Ÿ→Z 我已經嘗試此解決方案:z≥x+ Y-1與其它幾種可能性:( 在一起,但是,我曾與Gurobi求解

我的Python錯誤此約束的代碼如下:

對於y中的y:

對於j M中:

對於x在X:

用於在範圍(INT(U [J]),INT(W [j])):

M1.addConstr(r [x] [y] + c [y,j] -1 < = p [x,a],'TileRequirement_%s_%s_%s_%s'%(y,j ,x,a))

我總是得到錯誤在這條線:用於在範圍(INT(U [J]),INT(W [j])):,因爲這兩個U [J]和W [j]被定義爲正整數變量 那麼,有人可以幫助我嗎? 謝謝:)

問候 哈蒂嘉

回答

1

不能建立基於約束尚未將優化變量,如在:

for a in range(int(U[j]),int(W[j])) # optimized value unknown @ build-constr-time 

鑄造一樣,看起來也很危險,它這完全取決於gurobipy,如果這是可能的(但這裏沒有幫助)。

你的問題是難以閱讀,這是毫無動機的這些約束的信息,但總的思路可以是:

  • 擺脫由U[j]W[j]
  • 制定規定範圍內的你約束爲全範圍
    • 與一種修改
      • 引入一個以上活化變量a
      • (x^y)->z變爲:(a^x^y)->z == !a v !x v !y v z
      • 作爲線性表達:(1-a) + (1-x) + (1-y) + z >= 1
  • 現在使用的indicator-variables概念制定您的活化變量

是的,它很混亂,因此(因爲信息稀疏),我不會發布完整的解決方案。

+0

謝謝您的回答。我已經嘗試了幾種使用Big-M的替代方案,但我每次都得到相同的錯誤:( –

+0

我已經告訴過你問題的根源是什麼(第一句),你需要很多修改來制定這個(並且它不是什麼初學者會喜歡)。 – sascha

0

# -*- coding: utf-8 -*- 
 
#programmer: Khadija HS 
 
#date: June 2017 
 
#name: B-C-MCT PLNE Model (Khadija.HS,2017) <---> BCMCT1.py 
 

 

 
""" 
 
Solve the B-C-MCTP (fixed Z & min Delta) sub-pb of 3-PSDPP (K-HS et al.,2016), where: 
 
    X: list of input tiles (tile x) 
 
    Y: list of output tiles (tile y) 
 
    Ry: requirement relation between X & Y 
 
     <---> is a List of Y list, each sub List define the input tiles required by each y 
 
     <---> rxy: incidence matrix (0-1): Input Tiles/Output Tiles (Configuration of each output tile % input tile) 
 
     <---> Ry is a list of list where Row <--- x & Column <--- y 
 
    alpha: prefetches times (uniform) 
 
    beta: computations times (uniform) 
 
    Delta: the Total completion time (to be determined) 
 
""" 
 

 
from gurobipy import * 
 
""" Find Yx: set of output tiles y that required the input tile x """ 
 
def OuputTileTe(Ry,X): 
 
    Yx=[] 
 
    for x in X: 
 
     Yx.append(OuputTileTex(Ry,x)) 
 
    return Yx 
 
""" Find B: List Ts for x """ 
 
def OuputTileTex(Ry,x): 
 
    B=[] 
 
    for y in range(len(Ry)): 
 
     if x in Ry[y]: 
 
      B.append(y) 
 
    return B 
 

 
""" Find N: Max Value of N (<---> sum(len(Ry),y in Y)) """ 
 
def NbPrefetchTile(S): 
 
    N=0   
 
    for k in range(0,len(S)): 
 
     N += len(S[k])  
 
    return N 
 

 

 

 
""" BCMCT1 - Model""" 
 
def BCMCT1(X,Y,Z,Ry,alpha,beta): 
 
    # DET VBLES: M,N,Z1,T,K,L 
 
    M=list(range(len(Y)))     # List of Computation steps 
 
    nb=NbPrefetchTile(Ry)     # Number of prefetches (Big Value of N) 
 
    N=range(nb)        # List of Prefetches steps 
 
    ListZ=range(Z)       # List of Buffers 
 
    T=range(alpha*len(X) + beta*len(Y))  # List of Start Date times (Computation+Prefetches) 
 
    K=range(alpha)       # Interval Time of a prefetch step 
 
    L=range(beta)       # Interval Time of a compute step 
 
    
 
    # DET VBLES: A,T1,B,Yx 
 
    A=alpha*nb + beta*len(Y)    # Big Value of Total Completion Time 
 
    T1=range(A)        # List of Start Date times (Computation+Prefetches) 
 
    minLen=min([len(elt) for elt in Ry]) #1,alpha+1 
 
    B=alpha*minLen + beta*len(Y)   # Value of LB2 
 
    Yx=OuputTileTe(Ry,X)     # List of output tile y, for x, x in X 
 
    
 
    
 

 

 
    # MODEL 
 
    M1=Model("BCMCT1")  
 

 

 
    # CONSTANT VARIABLES 
 
    r=[[0]*len(Y) for i in range(len(X))] 
 
    for x in X: 
 
     for y in Y: 
 
      if x in Ry[Y.index(y)]: 
 
       r[x][y]=1 
 
    
 

 
    # DECISION VARIABLES 
 
    c,p,q,U,W,a={},{},{},{},{},{} 
 

 
    for y in Y: 
 
     for j in M: 
 
      c[y,j]=M1.addVar(vtype=GRB.BINARY,name="c[%s,%s]"%(y,j)) #obj=beta, 
 

 
    for x in X: 
 
     for t in T: 
 
      p[x,t]=M1.addVar(vtype=GRB.BINARY,name="p[%s,%s]"%(x,t)) #obj=1, 
 

 
    for x in X: 
 
     for t in T: 
 
      q[x,t]=M1.addVar(vtype=GRB.BINARY,name="q[%s,%s]"%(x,t)) #obj=1, 
 
    
 
    for j in M: 
 
     U[j]=M1.addVar(vtype='I',name="U_%s"%j) 
 
     W[j]=M1.addVar(obj=1,vtype='I',name="W_%s"%j) 
 

 
    for j in M: 
 
     a[j]=M1.addVar(vtype=GRB.BINARY,name="a[%s]"%j) 
 
    
 

 
    # MODEL UPDATE 
 
    M1.update() 
 

 

 
    # OBJECTIVE 
 
    Obj=W[len(M)-1] 
 
    M1.setObjective(Obj, GRB.MINIMIZE) 
 

 

 
    # CONSTRAINTS  
 
    """ (1): Computation's Assignement Constraints """ 
 
    """ (a) """ 
 
    for j in M: 
 
     M1.addConstr(quicksum(c[y,j] for y in Y)==1,'ComputeAssign1_%s'%j) 
 
    """ (b) """ 
 
    for y in Y: 
 
     M1.addConstr(quicksum(c[y,j] for j in M)==1,'ComputeAssign2_%s'%y)  
 

 
     
 
    """ (2): Buffer's Constraints """ 
 
    for t in T: 
 
     M1.addConstr(quicksum(p[x,t] for x in X) <= Z,'BufferNb_%s'%t) 
 

 
     
 
    """ 3): Computation/Prefetch's Constraints """  
 
    """ (a) """ 
 
    for t in T: 
 
     M1.addConstr(quicksum(q[x,t] for x in X) <= 1,'PrefetchTileA_%s'%t) 
 

 
    """ (b) """ 
 
    for x in X: 
 
     for t in T[1:]: 
 
      for k in K: 
 
       M1.addConstr(p[x,t] - p[x,t-1] <= q[x,t-k],'PrefetchTileB_%s_%s_%s'%(x,t,k)) 
 

 
    """ (c) """ 
 
    for y in Y: 
 
     for j in M: 
 
      for x in X: 
 
       for t in T: 
 
        M1.addConstr(3 - r[x][y] - c[y,j] - a[j] + p[x,t] >= 1, 'TileRequirement_%s_%s_%s_%s'%(y,j,x,t)) 
 
        
 
    """ (5): Computation Time's Constraint """ 
 
    """ (a) """ 
 
    for j in M: 
 
     M1.addConstr(W[j] == U[j] + beta,'ComputeTime1_%s'%j) 
 

 
    """ (b) """ 
 
    for j in M[1:]: 
 
     M1.addConstr(W[j-1] <= U[j],'ComputeTime2_%s'%j) 
 
    
 

 
    # SOLUTION 
 
    M1.__data=c,p,q,U,W,a 
 
    return M1

附上我的詳細ILP 可能是,它會更容易理解有關約束數17 我的問題在哪裏,

L = range(beta)

K=range(alpha)

\Lambda (Big M)=alpha*Z*Y+beta*Y

r[x][y]= 1 if x in Ry and 0 otherwise (forall x in X & forall y in Y) : incidence matrix given as input data

我會給你一個例子,這是非常簡單的瞭解更多我的問題如下: 讓: X=[X1,X2,X3,X4] Y=[Y1,Y2,Y3] Ry=[(X1,X2,X3), (X2,X4),(X1,X3,X4)] Z=3 alpha=2, beta=4

目標是找到一個計算序列來計算Y1,Y2 & Y3以最大限度地減少Delta(全部完成時間)

最佳的解決方案是:Y2, Y3, Y1 (or Y2,Y1,Y3)\Delta=17

ILP Formulation