2

I am trying to solve an MIP using PuLP on a Mac. I have already added the directory '/Applications/CPLEX_Studio1210/cplex/python/3.7/x86-64_osx', to my PYTHONPATH. But whenever I try

prob.solve(CPLEX_CMD()),

I receive the following error message:

File "/Users/xxxx/opt/anaconda3/envs/pulposm/lib/python3.7/site-packages/pulp/solvers.py", line 468, in actualSolve
    raise PulpSolverError("PuLP: cannot execute "+self.path)
pulp.solvers.PulpSolverError: PuLP: cannot execute cplex.
  • 2
    I think the [docs](https://coin-or.github.io/pulp/guides/how_to_configure_solvers.html) are quite readable in regards to this and although i skimmed only over it: i don't think setting `PYTHONPATH` will help you (at least not alone). The solver (imho) is called as a subprocess (i'm ignoring non-process based solver-usage; e.g. dlls) and the `PATH` environment-variable is the one to look out for (check it by calling the cplex in your terminal without any path). Those are the basics. But read the docs, as there are some caveats if dynamic linking is used in the background. – sascha Jun 04 '20 at 13:10

2 Answers2

3

This is closely related to this stackoverflow question, but slightly different because you are on a Mac rather than Windows. The gist of the answer is the same, though. As mentioned in the comments, you need to set the PATH environment variable (not PYTHONPATH) so that the cplex binary can be executed by PuLP.

In your case, this should look something like this:

$ export PATH=$PATH:/Applications/CPLEX_Studio1210/cplex/bin/x86-64_osx

See also, this stackoverflow question about setting environment variables on OS X and making them persist.

Alternately, you can set the path argument to the location of the cplex executable in the CPLEX_CMD constructor (see the source code).

rkersh
  • 4,447
  • 2
  • 22
  • 31
-2

can you try

import pulp
import cplex
bus_problem = pulp.LpProblem("bus", pulp.LpMinimize)
nbBus40 = pulp.LpVariable('nbBus40', lowBound=0, cat='Integer')
nbBus30 = pulp.LpVariable('nbBus30', lowBound=0, cat='Integer')

# Objective function
bus_problem += 500 * nbBus40 + 400 * nbBus30, "cost"

# Constraints
bus_problem += 40 * nbBus40 + 30 * nbBus30 >= 300
bus_problem.solve(pulp.CPLEX())

print(pulp.LpStatus[bus_problem.status])
for variable in bus_problem.variables():
    print ("{} = {}".format(variable.name, variable.varValue))

from https://medium.com/@alexfleischer_84755/optimization-simply-do-more-with-less-zoo-buses-and-kids-part2-python-java-c-cc04558e49b5

Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15