pysal / spopt

Spatial Optimization
https://pysal.org/spopt/
BSD 3-Clause "New" or "Revised" License
307 stars 46 forks source link

pulp.apis.core.PulpSolverError: Pulp: cannot execute cbc.exe #347

Closed p91g closed 1 year ago

p91g commented 1 year ago

Hello,

I am trying to execute the following:

solver = pulp.COIN_CMD(msg=True, warmStart=True)

pcenter_from_cm = PCenter.from_cost_matrix(
    distances, p_facilities=facilities, name="p-center-network-distance"
)

pcenter_from_cm = pcenter_from_cm.solve(solver)

But get the following error:

pcenter_from_cm = pcenter_from_cm.solve(solver)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\x\Documents\spopt_venv\lib\site-packages\spopt\locate\p_center.py", line 409, in solve
    self.problem.solve(solver)
  File "C:\Users\x\Documents\spopt_venv\lib\site-packages\pulp\pulp.py", line 1913, in solve
    status = solver.actualSolve(self, **kwargs)
  File "C:\Users\x\Documents\spopt_venv\lib\site-packages\pulp\apis\coin_api.py", line 137, in actualSolve
    return self.solve_CBC(lp, **kwargs)
  File "C:\Users\x\Documents\spopt_venv\lib\site-packages\pulp\apis\coin_api.py", line 146, in solve_CBC
    raise PulpSolverError(
pulp.apis.core.PulpSolverError: Pulp: cannot execute cbc.exe cwd: C:\Users\x\Documents\

Does anybody have any suggestions?

I have tried passing the msg=1 argument to the solver, to get more information.

I have checked the precision of the numbers, rounding them.

The cost distance matrix is is a pairwise distance matrix of one df of points - i.e. with itself. Could it be that it is due to duplicated variables / constraints?

jGaboardi commented 1 year ago

As per the error message, the problem appears to be with your pulp installation. How did you install pulp?

Also, please provide:

p91g commented 1 year ago

Thank you for the quick response!

I used pip install for pulp - version 2.7.0

Spopt - version 0.5.0

Example code

import spopt
from spopt.locate import PCenter
import geopandas as gpd
import shapely 
from shapely import Point
import pandas as pd
import numpy as np
import pulp
import scipy

grid = pd.read_csv(r"C:\Users\x\Documents\city_spatial_analysis\station_allocation\grid_coords.csv")
grid = grid[['index', 'lon', 'lat']]

# number of candidate facilities in optimal solution 
facilities = 35

# set the solver
solver = pulp.COIN_CMD(msg=True, warmStart=True)

# calculate network distance cost matrix
distances = scipy.spatial.distance.squareform(scipy.spatial.distance.pdist(grid [["lon", "lat"]]))

# replacing zeros with 50,000
distances = np.where(distances== 0, 50000, distances)
distances = np.round(distances)

pcenter_from_cm = PCenter.from_cost_matrix(
    distances, p_facilities=facilities, name="p-center-network-distance"
)

pcenter_from_cm = pcenter_from_cm.solve(solver)
jGaboardi commented 1 year ago

For the code chunk above to run (be on the way to a MWE), you will need to provide grid_coords.csv. However, I would suggest you try both conda install pulp and pip install pulp, then try to run your code again.

p91g commented 1 year ago

My bad, here are some simulated points taken from the tutorial page for p-center. The same error occurs. I have tried uninstalling and pip installing pulp again, but the same error happens. I am not using coda. I will try using anaconda and a new env perhaps.

import spopt
#from spopt.locate import MCLP, simulated_geo_points 
from spopt.locate import PMedian
from spopt.locate import PCenter, simulated_geo_points
import geopandas as gpd
import shapely 
from shapely import Point
#import matplotlib.pyplot as plt
#from matplotlib.patches import Patch
import pandas as pd
import numpy as np
import pulp
import scipy

import warnings
with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    # ignore deprecation warning - GH pysal/spaghetti#649
    import spaghetti

# quantity demand points
CLIENT_COUNT = 176

# number of candidate facilities in optimal solution
P_FACILITIES = 35

# random seeds for reproducibility
CLIENT_SEED = 5
FACILITY_SEED = 6

# set the solver
solver = pulp.COIN_CMD(msg=False, warmStart=True)

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    # ignore deprecation warning - GH pysal/libpysal#468
    lattice = spaghetti.regular_lattice((0, 0, 10, 10), 9, exterior=True)
ntw = spaghetti.Network(in_data=lattice)

streets = spaghetti.element_as_gdf(ntw, arcs=True)

streets_buffered = gpd.GeoDataFrame(
    gpd.GeoSeries(streets["geometry"].buffer(0.5).unary_union),
    crs=streets.crs,
    columns=["geometry"],
)

client_points = simulated_geo_points(
    streets_buffered, needed=CLIENT_COUNT, seed=CLIENT_SEED
)

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    # ignore deprecation warning - GH pysal/libpysal#468
    ntw.snapobservations(client_points, "clients", attribute=True)

clients_snapped = spaghetti.element_as_gdf(ntw, pp_name="clients", snapped=True)
clients_snapped.drop(columns=["id", "comp_label"], inplace=True)

cost_matrix = ntw.allneighbordistances(
    sourcepattern=ntw.pointpatterns["clients"],
    destpattern=ntw.pointpatterns["clients"],
)

cost_matrix = np.where(cost_matrix== 0, 50000, cost_matrix)
cost_matrix = np.round(cost_matrix)

pcenter_from_cm = PCenter.from_cost_matrix(
    cost_matrix, p_facilities=P_FACILITIES, name="p-center-network-distance"
)

pcenter_from_cm = pcenter_from_cm.solve(solver)
jGaboardi commented 1 year ago

Did you install spopt via pip or conda/mamba?

We need to update the docs here to say "highly recommend conda/etc. over pip" like we do across other PySAL packages, e.g. in spaghetti.

p91g commented 1 year ago

I installed with pip. I have now tried installing via conda and installing using conda install, but get the same error unfortunately.

jGaboardi commented 1 year ago

Try :

  1. pip install pulp
  2. then run pulptest (see here)
  3. then (without removing pulp) conda install pulp.
  4. then try running the code again
p91g commented 1 year ago

Thank you. Unfortunately that didn't work. The same error is there. I got the following for the pulp test. Nothing showing as unavailable but it does look different to the code the pulp page suggests i should see.

pulp.pulpTestAll() sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss Test that logic put in place for deprecation handling of indexs works Testing 'indexs' param continues to work for LpVariable.dicts Testing 'indexs' param continues to work for LpVariable.matrix Testing 'indices' argument works in LpVariable.dicts Testing 'indices' param continues to work for LpVariable.matrix Testing invalid status Testing continuous LP solution - export dict Testing export dict for LP Testing export dict MIP Testing maximize continuous LP solution Testing continuous LP solution - export JSON Testing continuous LP solution - export solver dict Testing continuous LP solution - export solver JSON Testing reading MPS files - binary variable, no constraint names Testing reading MPS files - integer variable Testing reading MPS files - maximize ................... Testing reading MPS files - noname Testing invalid var names Testing makeDict general behavior Testing makeDict default value behavior Testing measuring optimization time Testing that readsol can parse CPLEX mipopt solution Testing the availability of the function pulpTestAll Testing zero subtraction Testing inconsistent lp solution Testing continuous LP solution Testing maximize continuous LP solution Testing unbounded continuous LP solution Testing Long Names Testing repeated Names Testing zero constraint Testing zero objective Testing LpVariable (not LpAffineExpression) objective ............ Testing LpAffineExpression divide Testing MIP solution Testing MIP solution with floats in objective Testing Initial value in MIP solution Testing fixing value in MIP solution Testing MIP relaxation Testing feasibility problem (no objective) Testing an infeasible problem Testing an integer infeasible problem Testing another integer infeasible problem Testing column based modelling sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss Testing fractional constraints Testing elastic constraints (no change) Testing elastic constraints (freebound) Testing elastic constraints (penalty unchanged) Testing elastic constraints (penalty unbounded) Testing timeLimit argument ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss

Ran 1003 tests in 2.016s

OK (skipped=944)

jGaboardi commented 1 year ago

Can you post your full environment file (see here)?

p91g commented 1 year ago

Thank you so much for your help on this. Attached is the environment file.

spopt_env_copy.zip

Anran0716 commented 1 year ago

I encountered the similar issue about this too when running the code mclp.ipynb: Pulp: cannot execute cbc.exe cwd: C:\Users\zheng\Dropbox (UFL)\Gap analysis\code\spopt-main\spopt-main\notebooks Looking forward to the reply! Thank you so much!

gegen07 commented 1 year ago

Hey there! Installing pulp via pip/conda does not install COIN_CMD. To use this solver, you should have CBC installed in your environment. See the instruction here.

If this error persists, please let us know.

By the way, the pip install pulp installs automatically PULP_CBC_CMD so you can use that instead COIN_CMD.

Anran0716 commented 1 year ago

I encountered the similar issue about this too when running the code mclp.ipynb: Pulp: cannot execute cbc.exe cwd: C:\Users\zheng\Dropbox (UFL)\Gap analysis\code\spopt-main\spopt-main\notebooks Looking forward to the reply! Thank you so much!

I have solved my problem with this code: 'solver = pl.PULP_CBC_CMD(keepFiles=True)

p91g commented 1 year ago

Nice one, thank you! 'solver = pl.PULP_CBC_CMD(keepFiles=True)' also worked for me it seems. Appreciate the help.

jGaboardi commented 1 year ago

Thanks for troubleshooting @gegen07 .