pythonpulpcoin-or-cbc

infeasible solution in Python, pulp using COIN-OR


I am trying to solve a Linear Programing problem using COIN-OR in python. I have done all the work but there seems to be an error which I am not able to identify. Many a times the solution comes out to be either infeasible or incorrect according the contraints that I am trying to force. The values, the binary variable are also incorrect i.e. ~0.1^10 or ~ 0.999.....

Kindly help me to locate the error or try to guide to resolve the issue.

I am attaching

Model1 main class takes the input and create a new output file and then PanelTwo class methods build the Distance matrix and the Njg matrix.

There seems to be error in the constructor method of the Model1 class which builds theLP and solves the LP

Here is the zip file

the code for the declaring the variables and constraints are:

M = 100000 * prob.schoolNum

    sModel = []
    for i in range(prob.schoolNum):
        sModel.append(i)
    gModel = []
    for i in xrange(prob.gradeNum):
        gModel.append(i)
    Beta = []

    for i in xrange(prob.schoolNum):
        temp = 0
        for j in xrange(prob.gradeNum):
            temp = temp + prob.Njg[i][j]
        if temp < prob.Nmax:
            Beta.append(0)
        else:
            Beta.append(1)


    # x = students of grade g transfer from school i to j
    x = LpVariable.matrix("x_igj_", (sModel, gModel, sModel), 0, 1, LpBinary)
    y = LpVariable.matrix("status of school", (sModel), 0, 1, LpBinary)
    # o = resulting students in grade in school
    o = LpVariable.matrix("o", (sModel, gModel), 0, None, LpInteger)

    # oHelper  = summation of o for all g rades
    oHelper = LpVariable.matrix("oH", (gModel), 0, None, LpInteger)
    #Njg_Helper = Total students in a particular school
    Njg_helper = LpVariable.matrix("NH", (sModel), 0, None, LpInteger)






    formulation = LpProblem("School Consolidation Model", LpMinimize)

    formulation += lpSum(((prob.Njg[i][g] * x[i][g][j] for j in sModel) for g in gModel) for i in sModel)

    for i in sModel:
        for j in sModel:
            for g in gModel:
                formulation += x[i][g][j] * prob.D[i][j] <= prob.d1

    for i in sModel:
        for j in sModel:
            for g in gModel:
                formulation += x[i][g][j] <= y[j]
    for i in sModel:
        for g in gModel:
            formulation += lpSum(x[i][g][j] for j in sModel) <= 1 - y[i]

    for j in sModel:
        formulation += ((lpSum(prob.Njg[j][g] for g in gModel) - prob.Nmax) * (1 - y[j])) <= 0

    for i in sModel:
        for j in sModel:
            if i != j and Beta[i] * Beta[j] != 1:
                formulation += (prob.D[i][j] - prob.d2) >= (y[i] + y[j] - 2) * M

    for g in gModel:
        formulation += lpSum(o[j][g] for j in sModel) == oHelper[g]
        formulation += lpSum(prob.Njg[i][g] for i in sModel) == oHelper[g]
        for j1 in sModel:
            formulation += lpSum(prob.Njg[i1][g]*x[i1][g][j1] for i1 in sModel) == o[j1][g]-prob.Njg[j1][g]*y[j1]

    formulation.solve()

Solution

  • You could have bugs in the model. I would try the following: