使用tupla(i,j)而不是单个变量(j)

时间:2019-04-02 21:20:40

标签: python optimization linear-programming pulp

我想为此模型p(i,j)创建一个tupla变量,而不只是变量p(j)。 我必须在这里以及影响它的变量上对其进行更改。

Current model example

代码

def buildBaseModel():
    global nVars,nClauses,kbRulesLit,kbRulesProbMin,kbRulesProbMax,kbRulesCard,Q
    # Create Model
    m = pulp.LpProblem("Profit maximising problem",pulp.LpMaximize)

    nPsatVars = 2 ** nVars

    # Create variables p_i -->HERE SHOULD BE p[i]_[j]
    p = {}    
    for j in range(nPsatVars):
        p[j]=pulp.LpVariable(lowBound=0,cat='Continuous',name='p_'+str(j))

    #m += sum([p[j] for j in range(nPsatVars)]) --> HERE CHANGE OBJECTIVE FUNCTION

    # Create variables pai_i
    pai = {}
    for j in range(1,Q + 1):
        #pai[j] = kbRulesProb[j]
        pai[j]= pulp.LpVariable(lowBound=kbRulesProbMin[j],upBound=kbRulesProbMax[j],cat='Continuous',name='pai_'+str(j))        
    for j in range(Q + 1,nClauses + 1):
        pai[j]= pulp.LpVariable(lowBound=0,cat='Continuous',name='pai_'+str(j))

    # Create variables qp_i
    qp = {}
    for j in range(1,Q+1): #not first rule
        qp[j]=pulp.LpVariable(lowBound=0,cat='Continuous',name='qp_'+str(j))

    # Create variables qn_i
    qn = {}
    for j in range(1,Q+1): #not first rule
        qn[j]=pulp.LpVariable(lowBound=0,cat='Continuous',name='qn_'+str(j))

    #construct the matrix A
    A=np.array([[1] * nPsatVars] * (nClauses + 1))#0
    x = {}
    for j in range(nVars):
        x[j] = 0
    for i in range(1,nClauses + 1):
        for j in range(nPsatVars):
            r = j
            #the value of x is the assignment of the Var,such as [0,1,1,0,0,0]
            for k in range(nVars):
                x[k] = (r >> k)%2
            t = 0
            for k in kbRulesLit[i]:
                if k == 0 and x[0] == 1:
                    t = 1
                if k > 0 and x[k] == 1:
                    t = 1
                if k < 0 and x[-k] == 0:
                    t = 1
            if t == 1:
                A[i][j] = 0

    #constrict
        m+=sum([p[j] for j in range(nPsatVars)])== 1.0 

        #constrict
        for i in range(1,Q + 1):
            m+=sum([p[j]*A[i][j] for j in range(nPsatVars)])-qp[i] <= kbRulesProbMax[i]

        for i in range(1,Q + 1):
            m+=sum([p[j]*A[i][j] for j in range(nPsatVars)])+qn[i] >= kbRulesProbMin[i]

        for i in range(Q + 1,nClauses + 1):
            m+=sum([p[j] * A[i][j] for j in range(nPsatVars)]) - pai[i] == 0 

    #USE GUROBI
    m.solve(pulp.GUROBI())
    pulp.LpStatus[m.status]

    if(m.status==-1):
        print("there is no solution for the CONDSAT problem")
    else :
        print("find solution\n")

    varsdict = {}
    for v in m.variables():
        print(v.name,"=", v.varValue)
    m.writeLP('c.lp')

Final Model that the code should do 因此,在最终模型中,我们将拥有p_i_j而不是像今天的代码那样生成的p_j。

除此之外,我需要更改模型的目标函数是添加一个目标函数,该目标函数将所有变量p(i,j)的总和最大化(需要在第一次更改后完成)

1 个答案:

答案 0 :(得分:0)

欢迎来到!!

尝试逐字实施粘贴的LP模型:

from pulp import *

prob = LpProblem("two_index", LpMaximize)

# ASSUME q is a constant
q = 0.5

# DECLARE sets of indices
set_I = range(1, 3)
set_J = range(0, 8)
set_K = range(1, 4)

# Declare variables n and p
p = LpVariable.dicts("p", (set_I, set_J))
n = LpVariable.dicts("n", (set_I, set_K))

# Define OBJECTIVE
prob += p[1][7] + p[2][7]

# Declare CONSTRAINTS
prob += p[1][0] + p[1][1] + p[1][2] + p[1][3] + p[1][4] + p[1][5] + p[1][6] + p[1][7] == 1 
prob += p[1][5] + p[1][7] - q*p[1][1] <= 0.75
prob += p[1][5] + p[1][7] + q*n[1][1] >= 0.71
prob += p[1][3] - q*p[1][2] <= 0.14
prob += p[1][3] + q*n[1][2] >= 0.14
prob += p[1][1] + q*p[1][3] <= 0.13
prob += p[1][1] - q*n[1][3] >= 0.13
prob += p[2][0] + p[2][1] + p[2][2] + p[2][3] + p[2][4] + p[2][5] + p[2][6] + p[2][7] == 1 
prob += p[2][5] + p[2][7] -  q*p[2][1] <= 0.35
prob += p[2][5] + p[2][7] +  q*n[2][1] >= 0.35
prob += p[2][3] - q*p[2][2] <= 0.41
prob += p[2][3] + q*n[2][2] >= 0.41
prob += p[2][1] + q*p[2][3] <= 0.29
prob += p[2][1] - q*n[2][3] >= 0.29

for i in set_I:
    for j in set_J:
        p[i][j] >= 0

    for k in set_K:
        q*p[i][k] >= 0
        q*n[i][k] >= 0

# Solve LP
prob.solve()
print (("Status:"), LpStatus[prob.status])

# Print result
for v in prob.variables(): 
    print(v.name, "=", v.varValue)
print("objective=", value(prob.objective))

这显然导致了无穷无尽的问题,很难确定这是否正确-我只是在发现模型时实施了模型(没有试图去理解它)。

Status: Unbounded
n_1_1 = 0.0
n_1_2 = 0.0
n_1_3 = -0.14
n_2_1 = 0.0
n_2_2 = 0.0
n_2_3 = -0.41
p_1_0 = 0.09
p_1_1 = 0.06
p_1_2 = 0.0
p_1_3 = 0.14
p_1_4 = 0.0
p_1_5 = 0.71
p_1_6 = 0.0
p_1_7 = 0.0
p_2_0 = 0.155
p_2_1 = 0.085
p_2_2 = 0.0
p_2_3 = 0.41
p_2_4 = 0.0
p_2_5 = 0.35
p_2_6 = 0.0
p_2_7 = 0.0
objective= 0.0