hadizadeh.ali
hadizadeh.ali

Reputation: 93

PuLP "symbol in wrong position" error and fix

PuLP sometimes gives errors when solving a linear optimization problem. For example, for the following code...

import pulp

model = pulp.LpProblem(sense=pulp.LpMaximize)
Q = pulp.LpVariable(name='Q', lowBound=0, cat='Integer')
B = pulp.LpVariable(name='B', lowBound=0, cat='Integer')
L = pulp.LpVariable(name='L', lowBound=0, cat='Integer')

model += (4*Q + 4*B + 8*L <= 800, 'Milk')
model += (8*Q + 6*B + 4*L <= 1800, 'Sauce')
model += (8*Q + 4*B       <= 500, 'Powder')
model += 2*Q + 4*B + L

status = model.solve()
print('\n\n')
for x in model.variables():
    print(f'{x.name} = {x.value()}')
print(f'Profit = {model.objective.value()}')

... the following is printed in the cmd prompt:

GLPSOL--GLPK LP/MIP Solver 5.0 Parameter(s) specified in the command line: --cpxlp C:\Users\hadiz\AppData\Local\Temp\0f4d3803095c416890dc6125a83f96e8-pulp.lp -o C:\Users\hadiz\AppData\Local\Temp\0f4d3803095c416890dc6125a83f96e8-pulp.sol Reading problem data from 'C:\Users\hadiz\AppData\Local\Temp\0f4d3803095c416890dc6125a83f96e8-pulp.lp'... C:\Users\hadiz\AppData\Local\Temp\0f4d3803095c416890dc6125a83f96e8-pulp.lp:13: symbol 'B' in wrong position CPLEX LP file processing error Traceback (most recent call last): File "1a.py", line 13, in status = model.solve() File "D:\Programs\Anaconda\Anaconda3-2019.10\install\lib\site-packages\pulp\pulp.py", line 1913, in solve status = solver.actualSolve(self, **kwargs) File "D:\Programs\Anaconda\Anaconda3-2019.10\install\lib\site-packages\pulp\apis\glpk_api.py", line 115, in actualSolve raise PulpSolverError("PuLP: Error while executing " + self.path) pulp.apis.core.PulpSolverError: PuLP: Error while executing glpsol.exe

Sometimes, for other codes similar to the above, instead of "symbol 'B' in wrong position", I get "symbol ':' in wrong position".

I'm using Anaconda3-2019.10. Python version: 3.7.4. PuLP version: 2.6.0.

I figured out a solution myself, so I thought I better write it here for future programmers. Your solutions are also very welcome.

Upvotes: 1

Views: 230

Answers (1)

hadizadeh.ali
hadizadeh.ali

Reputation: 93

PuLP seems to have issues with strings starting with 'B' or 'b'; that is, variable and constraint names. So, if in the following code, we write

B = pulp.LpVariable(name='_B', lowBound=0, cat='Integer')

the problem magically gets solved.

Upvotes: 1

Related Questions