簡體   English   中英

集合中的CVXPY等式約束

[英]CVXPY equality constraint in set

我有一個ILP問題,我需要將變量矩陣的值約束在{0,1}中。 我嘗試使用以下(簡化)代碼執行此操作,但它不允許我使用“|” 運營商。 我該怎么做?

def test_ILP(scores):
    N = scores.shape[0]
    eye_bool = np.eye(N)
    zero_when_equal = np.abs(np.eye(N) - 1)


    opt = cp.Variable(scores.shape)
    objective = cp.Maximize(
        cp.sum(cp.multiply(scores, opt * zero_when_equal))
    )


    constraints = [(opt == 0) | (opt == 1) | eye_bool]

    prob = cp.Problem(objective, constraints)
    prob.solve()
    return opt.value

根據@ sascha的注釋,對於布爾(或整數)約束,可以使用Variable構造函數中的參數。 對於這種情況:

def test_ILP(scores):
    N = scores.shape[0]
    eye_bool = np.eye(N)
    zero_when_equal = np.abs(np.eye(N) - 1)
    boolean_constraint = [(x, y) for x in range(N) for y in range(N) if x != y]

    opt = cp.Variable(scores.shape, boolean=boolean_constraint)
    objective = cp.Maximize(
        cp.sum(cp.multiply(scores, opt * zero_when_equal))
    )


    constraints = [#Rest of the constraints]

    prob = cp.Problem(objective, constraints)
    prob.solve()
    return opt.value

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM