0
votes

I am trying to model the following equation using Python PuLP

enter image description here

I have written the following Python Code

prob = LpProblem('Resource', LpMaximize)

# x variables

xs = [LpVariable("x{0}{1}{2}".format(i + 1, j + 1, k + 1), cat = "Binary")

    for i in range(0, 3)
    for j in range(0, 5)
    for k in range(0, 2)
]

print("\nX Variable\n")

for i in range(0, len(xs)):
    print(xs[i])

# y variables

ys = [LpVariable("y{0}{1}".format(i + 1, j + 1), cat = "Binary")

    for i in range(0, 3)
    for j in range(0, 5)
]

print("\nY Variable\n")
for i in range(0, len(ys)):
    print(ys[i])


for j in range(0, 5):
    for k in range(0, 2):
    for i in range(0, 3):
    con = "x{0}{1}{2} <= y{3}{4}".format(i + 1, j + 1, k + 1, i + 1, j + 1)
prob += LpAffineExpression(LpElement(con))
print(con)

status = prob.solve()

This gives the following PuLP error:

Traceback (most recent call last): File "C:\Python34\Cloud 3.py", line 446, in resource(request, pmachine, l, q) File "C:\Python34\Cloud 3.py", line 136, in resource status = prob.solve() File "C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\pulp.py", line 1643, in solve status = solver.actualSolve(self, **kwargs) File "C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\solvers.py", line 1303, in actualSolve return self.solve_CBC(lp, **kwargs) File "C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\solvers.py", line 1325, in solve_CBC tmpMps, rename = 1) File "C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\pulp.py", line 1431, in writeMPS if mip and v.cat == LpInteger: AttributeError: 'LpElement' object has no attribute 'cat'

What does AttributeError: 'LpElement' object has no attribute 'cat' and why is this error produced?

1

1 Answers

0
votes

The AttributeError: 'LpElement' object has no attribute 'cat' is produced when there is an incompatibility between the problem variable which in this case is prob = LpProblem('Resource', LpMaximize) and the constraint being added.

This can be fixed by getting rid of the redundant LpAffineExpression(LpElement(con)) calls when the variables are defined using the PuLP specified LpVariable.dicts instead of dynamic generation of variables as has been done above.

A better approach incorporating the changes would be as follows:

prob = LpProblem('Resource', LpMaximize)

xdict = LpVariable.dicts('', ["x{0}{1}{2}".format(i + 1, j + 1, k + 1)
        for i in range(0, len(request))
        for j in range(0, len(pmachine))
        for k in range(0, pmachine[j].npj)
    ],
    0, 1, cat = 'Binary')

ydict = LpVariable.dicts('', ["y{0}{1}".format(i + 1, j + 1)
        for i in range(0, len(request))
        for j in range(0, len(pmachine))
    ],
    0, 1, cat = 'Binary')

for i in range(0, len(request)):
    for j in range(0, len(pmachine)):
        for k in range(0, pmachine[j].npj):
            con = xdict["x{0}{1}{2}".format(i + 1, j + 1, k + 1)] <= ydict["y{0}{1}".format(i + 1, j + 1)]
            prob += con
            print(con)