[Python] Use tupla(i,j) instead of a single variable(j)

Discussão em 'Python' iniciado por Stack, Maio 2, 2025.

  1. Stack

    Stack Membro Participativo

    I want to create a tupla variable for this model p(i,j) instead of only variable p(j). I have to change it here and on the variables that impacts it.

    Current model example

    Code



    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_[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:
    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[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[j] for j in range(nPsatVars)])-qp <= kbRulesProbMax

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

    for i in range(Q + 1,nClauses + 1):
    m+=sum([p[j] * A[j] for j in range(nPsatVars)]) - pai == 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 So at final model we will have p_i_j instead of p_j like this code generate today.

    besides this, I need help to change the objective function of the model is to add an objective function that maximize the sum of all variables p(i,j) (that needs to be done after the first change)

    Continue reading...

Compartilhe esta Página