coin-or / python-mip

Python-MIP: collection of Python tools for the modeling and solution of Mixed-Integer Linear programs
Eclipse Public License 2.0
514 stars 89 forks source link

NameError: name 'cbclib' is not defined #314

Closed kasperdokter closed 1 year ago

kasperdokter commented 1 year ago

I know that others have run into the same exception (NameError: name 'cbclib' is not defined), but my setup is different. My goal is to run the solver in a docker container on 32 bit Raspberry Pi (arm/v6)

First I compiled the solver from source in my container:

# syntax=docker/dockerfile:1
FROM arm32v6/python:3.7-alpine3.15
RUN apk add --no-cache \
    bash \
    gcc \ 
    gfortran \
    git \
    g++ \ 
    libffi-dev \ 
    libgfortran \
    lapack-dev \
    make \
    musl-dev \ 
    patch \
    postgresql-dev \ 
    python3-dev
RUN wget https://raw.githubusercontent.com/coin-or/coinbrew/master/coinbrew
RUN chmod u+x coinbrew
RUN ./coinbrew fetch Cbc@master
RUN ./coinbrew build Cbc@master --prefix=/home/haroldo/prog/ --tests=none --enable-relocatable
ENV PMIP_CBC_LIBRARY="/home/haroldo/prog/lib/libCbcSolver.so"
ENV PATH=$PATH:/home/haroldo/prog/lib
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
RUN chmod u+x ./entrypoint.sh
ENTRYPOINT ["./entrypoint.sh"]

Cross compiling is absolutely necessary, as building CBC on the Raspberry Pi takes more than 12 hours (after which I killed the build). So I used the following commands on my Windows laptop to compile the above and push it to my docker hub, from where I could pull it to my Raspberry Pi:

docker buildx create --name raspberrypi --platform linux/arm/v6
docker buildx use raspberrypi
docker buildx inspect --bootstrap
docker login
docker buildx build --platform linux/arm/v6 -t kasperdokter/temp . --push

Running the container eventually leads to the following (partial) stack trace:

2022-11-18T09:26:01.562666317Z   File "/core/__init__.py", line 91, in get_pairing
2022-11-18T09:26:01.562837316Z     model = mip.Model()
2022-11-18T09:26:01.563002314Z   File "/usr/local/lib/python3.7/site-packages/mip/model.py", line 98, in __init__
2022-11-18T09:26:01.563173313Z     import mip.cbc
2022-11-18T09:26:01.563341312Z   File "/usr/local/lib/python3.7/site-packages/mip/cbc.py", line 600, in <module>
2022-11-18T09:26:01.563561311Z     Osi_getNumCols = cbclib.Osi_getNumCols
2022-11-18T09:26:01.563730310Z NameError: name 'cbclib' is not defined
kasperdokter commented 1 year ago

I was able to solve the issue myself, and here is my solution:

The Dockerfile has two problems, and they are caused by improper documentation of Python-MIP https://docs.python-mip.com/en/latest/install.html.

  1. ENV PMIP_CBC_LIBRARY="/home/haroldo/prog/lib/libCbcSolver.so" The solver has been renamed to libCbc.so.
  2. ENV PATH=$PATH:/home/haroldo/prog/lib The command line tool is in /home/haroldo/prog/bin, instead of the lib folder.

These small changes fixed my problems.

Snails8 commented 1 year ago

Hi there. I had the same problem and handled it in the following way. I hope this helps anyone else having the same problems,

FROM python:3.10.5-buster

# ....omit (ex) pip install ...)

# deal with m1
RUN apt-get install -y wget bash git gcc g++ gfortran  liblapack-dev libamd2 libcholmod3 libmetis-dev libsuitesparse-dev libnauty2-dev
RUN wget -nH https://raw.githubusercontent.com/coin-or/coinbrew/master/coinbrew
RUN chmod u+x coinbrew
RUN bash coinbrew fetch Cbc@master
RUN bash coinbrew build Cbc@master --no-prompt --prefix=/usr/local --tests=none --enable-cbc-parallel
ENV PMIP_CBC_LIBRARY="/usr/local/lib/libCbc.so"
ENV LD_LIBRARY_PATH="/home/haroldo/prog/lib"

....

points