coin-or / pulp

A python Linear Programming API
http://coin-or.github.io/pulp/
Other
2.1k stars 384 forks source link

Gurobi environment handling #571

Closed torressa closed 1 year ago

torressa commented 2 years ago

Details for the issue

What did you do?

Using a single-use token license.

$ python3
>>> import pulp
>>> pulp.list_solvers(onlyAvailable=True)
Set parameter TokenServer to value "localhost"
No parameters matching '_test' found
/Users/david.torres-sanchez/.pyenv/versions/3.10.2/lib/python3.10/site-packages/pulp/apis/gurobi_api.py:357: UserWarning: GUROBI error: Set parameter TokenServer to value "localhost"
Set parameter LogFile to value "gurobi.log"

Failed to set up a license

Error 10009: Request denied: use limit (1) exceeded

.
  warnings.warn("GUROBI error: {}.".format(out))
['GUROBI', 'PULP_CBC_CMD']

Two tokens are required and not released until the python shell is exited (and the object is freed). So when you run the command again, GUROBI is no longer available

What did you expect to see?

What did you see instead?

Useful extra information

It would be good if environment handling was taken care of. This is an issue for Gurobi users using cloud, WLS or token license types. E.g. https://github.com/snakemake/snakemake/issues/1801

Possible solution: WIP:

What operating system are you using?

I'm using python version:

I installed PuLP via:

Did you also

pchtsp commented 2 years ago

I think the error comes because we needed to call some function of the gurobi python to know if it's correctly licensed. That's why we had gurobipy.setParam("_test", 0) (this is the problematic line in question I'm guessing). Is there a way to check if a valid and licensed version of Gurobi is available without using a license? Or is there a way to return the license after successfully testing any random parameter? If the licensed is "single-use" I understand it's spent once we call it?

Sorry for all the questions but I checked the WIP and was overly complicated. If we can find a simple solution to check for licensing without using a license, that would be great.

Tomkourou commented 2 years ago

Hi both, do we have any update on a fix for this?

stumitchell commented 2 years ago

right @pchtsp Seems we should call

Python: Gurobi 9.0.0 or newer: Call Model.dispose() on all Model objects. Then, if using custom-made environments, call Env.dispose() on all Env objects. Otherwise, call disposeDefaultEnv(). Gurobi 8.1.1 or older: Delete all Model objects, delete all Env objects (if used), then call disposeDefaultEnv().

https://support.gurobi.com/hc/en-us/articles/360013417731-How-do-I-release-a-shared-limited-use-license-

@torressa and @Tomkourou are you able to test this by adding the line

gurobipy.disposeDefaultEnv()

after the line gurobipy.setParam("_test", 0)

torressa commented 2 years ago

Thanks, everyone. Sorry for the delay!

@torressa and @Tomkourou are you able to test this by adding the line gurobipy.disposeDefaultEnv()

That would work for this specific bug. In general, however, the current lack of environment handling still suffers from:

My commit https://github.com/torressa/pulp/commit/c766603fb1502acddc6af6e7cb51c4dfb5c17c6e addresses these points somewhat (still needs double checking).

tgi-climact commented 1 year ago

Hi ! I'm still having the same issue. Any news on this ?

torressa commented 1 year ago

This has been merged. Will be fixed in the next release.