Passing **kwargs to gurobi via pulp

910 views
Skip to first unread message

Guido

unread,
Jun 25, 2014, 11:38:45 AM6/25/14
to pulp-or...@googlegroups.com
hi all,

i'm using pulp with gurobi LP-Optimizer to setup LP-Problems and solve them. btw. pulp is a great tool and works fine :) thanks for the work!

according to the documentation i tried to pass the "Method"-Parameter as follows:

prob.solve(pulp.GUROBI(msg=0), **{'Method': 2})


as i'm not that familiar with **kwargs i tried it slightly different


prob.solve(pulp.GUROBI(msg=0), **{'Method': 2})


but none of both approaches worked out.


has someone of you experience with pulp in combination with gurobi and keyworded arguments?


i would be really thankful get some from you, guys!


thanks in advance.


cheers,

guido



Stuart Mitchell

unread,
Jun 25, 2014, 6:06:36 PM6/25/14
to pulp-or...@googlegroups.com

prob.solve(pulp.GUROBI(msg=0, Method=2))

Kwargs get included the same way as other parameters and are really a bit of python syntax that catches all other parameters.

--
You received this message because you are subscribed to the Google Groups "pulp-or-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pulp-or-discu...@googlegroups.com.
To post to this group, send email to pulp-or...@googlegroups.com.
Visit this group at http://groups.google.com/group/pulp-or-discuss.
For more options, visit https://groups.google.com/d/optout.

Guido

unread,
Jun 30, 2014, 5:53:36 AM6/30/14
to pulp-or...@googlegroups.com
hey stuart,

thanks for your reply! it works for me.

I actually tried to pass the method argument to the solver "pulp.GUROBI_CMD" which is somehow slightly faster compared to the solver "pulp.GUROBI" (which is used in your solution). Do you now if its possible to pass the method argument to pulp.GUROBI_CMD?

cheers,
guido

Stuart Mitchell

unread,
Jul 1, 2014, 7:44:33 PM7/1/14
to pulp-or...@googlegroups.com
well there is currently a bug in the distribution for this :-(

 m.solve(pulp.GUROBI_CMD(options={'ResultFile': 'tmp.sol'}))
 
But I have just added a fix to trunk that I will distribute after tests.

--
Stuart Mitchell
PhD Engineering Science
Extraordinary Freelance Programmer and Optimisation Guru

Guido

unread,
Jul 4, 2014, 3:52:55 AM7/4/14
to pulp-or...@googlegroups.com
Thanks for the optional arguments passing example and many thanks for the fast bug fix. I haven't tried it so far, but I'll give you some feedback when I installed the latest version of PuLP.

cheers,
Guido

Roland Vincze

unread,
Jul 21, 2016, 4:02:18 PM7/21/16
to pulp-or-discuss
Hi!

I try to specify options to the Gurobi solver with no success. I tried:

1. model.solve( solver = GUROBI(options={"LogFile" : logfilename}) )
it's still logging to the console

2. model.solve( solver = pulp.GUROBI_CMD(options={"LogFile" : logfilename}) )
I get "ValueError: too many values to unpack"
also I'm not sure from where to import GUROBI_CMD (pulp, pulp.solvers or pulp.pulp)

(logfilename is a correct path in a regular string format)

Thank you in advance.

Regards,
Roland
Message has been deleted

Roland Vincze

unread,
Jul 22, 2016, 7:28:59 AM7/22/16
to pulp-or...@googlegroups.com
Hi!

I tried that way too, but the error is thrown when calling the function GUROBI_CMD()


(sent from mobile)

On 22 Jul 2016, at 13:24, Stuart Mitchell <s...@stuartmitchell.com> wrote:

try removing the "solver=" in the above lines

1. model.solve(GUROBI(options={"LogFile" : logfilename}) )

2. model.solve(pulp.GUROBI_CMD(options={"LogFile" : logfilename}) )


For more options, visit https://groups.google.com/d/optout.



--
Stuart Mitchell
PhD Engineering Science
Extraordinary Freelance Programmer and Optimisation Guru

--
You received this message because you are subscribed to a topic in the Google Groups "pulp-or-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pulp-or-discuss/kzepw7kuFSA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pulp-or-discu...@googlegroups.com.

To post to this group, send email to pulp-or...@googlegroups.com.

Stuart Mitchell

unread,
Jul 22, 2016, 7:41:38 AM7/22/16
to pulp-or...@googlegroups.com
yeah sorry haven't been coding python lately

I also don't have gurobi installed but if you don't want to log to the console

model.solve( solver = GUROBI(msg=false, options={"LogFile" : logfilename}) )

or 
model.solve( solver = GUROBI(options={"LogFile" : logfilename, "LogToConsole" : false}) )

For Gurobi_CMD options should be in a list of tuples

2. model.solve(pulp.GUROBI_CMD(options=[("LogFile", logfilename)]) )

so that they are similar to the other command line solvers

Stu

Roland Vincze

unread,
Jul 26, 2016, 8:43:41 AM7/26/16
to pulp-or...@googlegroups.com
Hi!

Thank you, it worked, however only with GUROBI_CMD:

        model.solve( pulp.GUROBI_CMD( options=[("LogFile", filename), ("LogToConsole", 0)] ) )

Matt Bailey

unread,
Oct 14, 2018, 4:41:10 PM10/14/18
to pulp-or-discuss
So happy to find this! I have been looking for decent examples of how to pass parameter options to GUROBI_CMD and CPLEX_CMD to allow for setting a time limit or the mipgap. I've figured that out now (It'd be nice to see something like these in the PuLP docs).

prob.solve(GUROBI_CMD(options=[("TimeLimit",300)])) 

or

prob.solve(GUROBI_CMD(options=[("MIPGap",0.2)]))

or

prob.solve(CPLEX_CMD(msg=1,options = ['set mip tolerances mipgap 0.2']))

or

prob.solve(CPLEX_CMD(msg=1,timelimit=30))

The issue I now have is that when these limits are hit they aren't returning the best feasible solution found to PuLP (unlike CBC). They are both throwing an error. This is the Gurobi_CMD error below. Stu, do you have any suggestions about that? (I'm using PuLP within SolverStudio if that matters).

Capture.JPG

Matt Bailey

unread,
Oct 14, 2018, 5:23:06 PM10/14/18
to pulp-or-discuss
I've gotten this same error with COIN_CMD if I add the "keepFiles=1" option in the call.

Matt Bailey

unread,
Oct 14, 2018, 9:07:39 PM10/14/18
to pulp-or-discuss
Stu, I was able to fix the error of the GUROBI_CMD() throwing an error. The issue was with the .sol file Gurobi creates:

# Solution for model OBJ
# Objective value = 127
x_0_17_0 0
x_0_17_1 0
x_0_17_10 0
......

PuLP assumes the variables and values start on the second line and not on the third line, so "name, value  = line.split()" in readsol (line 1972 in solvers.py) throws an error when trying to read the second line.

   def readsol(self, filename):
        """Read a Gurobi solution file"""
        with open(filename) as my_file:
            try:
                next(my_file) # skip the objective value
            except StopIteration:
                # Empty file not solved
                warnings.warn('GUROBI_CMD does provide good solution status of non optimal solutions')
                status = LpStatusNotSolved
                return status, {}, {}, {}, {}
            #We have no idea what the status is assume optimal
            status = LpStatusOptimal

#            shadowPrices = {}
#            slacks = {}
            shadowPrices = {}
            slacks = {}
            values = {}
            reducedCosts = {}
            next(my_file)  #ADDED THIS to Throw the second line out 
            for line in my_file:
                    name, value  = line.split()
                    values[name] = float(value)
        return status, values, reducedCosts, shadowPrices, slacks

Alessandro Silvestro

unread,
Apr 3, 2020, 8:35:20 AM4/3/20
to pulp-or-discuss
Thanks Matt and Stuart for your valuable contribution in passing arguments to Gurobi in PuLP, I have been looking all over internet.

Would you know perhaps as well how to retrieve the MIP relative optimality gap after time limit has been reached?

In case of prob.solve(GUROBI_CMD(options=[("TimeLimit",300)])) ,
I would need to retrieve the model.MIPGap at 300 secs.

I think model.MIPGap is the python Gurobi version but not the PuLP version, which I am struggling to find.

thanks a million!

Alessandro

Matt Bailey

unread,
Apr 3, 2020, 9:00:43 AM4/3/20
to pulp-or...@googlegroups.com
Unfortunately, I don't. I was setting the time and the gap and just looking at the output to see the gap if the time limit was hit.

solver = pulp.GUROBI_CMD(keepFiles=1,options=[("MIPFocus",1),("MIPGap",OptGap),("TimeLimit",SolverTimeLimit)])

I also had issues retrieving the optimization results, but never resolved it. if you turn keepFiles on you could pluck it from the written output file. Not ideal at all, but one possible workaround.

Matt


--
New posters to this group are moderated which can take up to 48 hours, so please be patient if your first post takes a while to turn up.
---
You received this message because you are subscribed to a topic in the Google Groups "pulp-or-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pulp-or-discuss/kzepw7kuFSA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pulp-or-discu...@googlegroups.com.

Franco Peschiera

unread,
Apr 5, 2020, 8:57:48 AM4/5/20
to pulp-or-discuss
Just to add to what Matt said.

You can pass an option "LogFile" to GUROBI_CMD with the path to your log file. Then, GUROBI writes a log of all the solving process.
Using Matt's example:

```python
solver = pulp.GUROBI_CMD(keepFiles=1, msg=1,options=[("MIPFocus",1),("MIPGap",OptGap),("TimeLimit",SolverTimeLimit), ("LogFile", PATH_TO_LOGFILE)])

```

More information on GUROBI command line parameters:

Then you have two options:

1. Open the log file and look for the information "by hand".
2. Use a new package I've been working on that parses the log file into a python dictionary with relevant information: https://github.com/pchtsp/orloge

regards,

Franco
To unsubscribe from this group and all its topics, send an email to pulp-or-discuss+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages