output redirecting using COIN solver

875 views
Skip to first unread message

Andrea Peano

unread,
Sep 21, 2011, 11:08:32 AM9/21/11
to pulp-or-discuss
Hi,
I'm a new member of this Ggroup!

I'm working on win XP, puLP v1.4.6 and the version of CoinMP printed
by puLP is 2.30 (I use the COIN solver distribuited with puLP).

To create a log file of my python script, I do a stdout redirection to
a file at the beginning of the script. But... when the script executes
the "solve" method, then the log of Cbc is redirected to the default
stdout (the cmd console of windows in this case).

I would ask you if there is a way to print the output of Cbc inside
the correct file.

Thank you!
Andrea Peano

Stuart Mitchell

unread,
Sep 25, 2011, 6:39:13 PM9/25/11
to pulp-or...@googlegroups.com
Hmm I haven't tried this myself but see

http://docs.python.org/library/sys.html

and its documentation on

"""
sys.stdin
sys.stdout
sys.stderr
File objects corresponding to the interpreter’s standard input, output
and error streams. stdin is used for all interpreter input except for
scripts but including calls to input() and raw_input(). stdout is used
for the output of print and expression statements and for the prompts
of input() and raw_input(). The interpreter’s own prompts and (almost
all of) its error messages go to stderr. stdout and stderr needn’t be
built-in file objects: any object is acceptable as long as it has a
write() method that takes a string argument. (Changing these objects
doesn’t affect the standard I/O streams of processes executed by
os.popen(), os.system() or the exec*() family of functions in the os
module.)
"""

From memory I don't think redirecting sys.stdout works, but you can try.

Otherwise just capture the output from the screen to a file, or use a
solver that you can set the logfile (COIN_CMD, and GUROBI i think)

Stu

> --
> You received this message because you are subscribed to the Google Groups "pulp-or-discuss" group.
> To post to this group, send email to pulp-or...@googlegroups.com.
> To unsubscribe from this group, send email to pulp-or-discu...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pulp-or-discuss?hl=en.
>
>

--
Stuart Mitchell
PhD Engineering Science
Freelance Programmer and Optimisation Expert
www.stuartmitchell.com

Andrea Peano

unread,
Sep 26, 2011, 2:58:20 AM9/26/11
to pulp-or-discuss
True! Thank you, holy documentation!!
In fact, a right way to redirect output is the duplication of fd,
using os.dup(). This method works!

Andrea
> > For more options, visit this group athttp://groups.google.com/group/pulp-or-discuss?hl=en.

James Vogel

unread,
Nov 6, 2016, 4:38:32 PM11/6/16
to pulp-or-discuss
I know this is an extremely old necro, but its the first result on googling "redirect output pulp" and if anyone else is trying to do this I thought I would give some more verbose directions:

basically since pulp uses popen to launch the cmd interface you cannot redirect stdout/in the normal way and have to work with the OS file descriptors.  After some research and tinkering this works quite well:

from os import dup, dup2, close
import tempfile
with tempfile.TemporaryFile() as tmp_output:
    orig_std_out = dup(1)
    dup2(tmp_output.fileno(), 1)
    result = pulp_model.solve(solver=PULP_CBC_CMD(msg=True, maxSeconds=self.time_limit))
    dup2(orig_std_out, 1)
    close(orig_std_out)
    tmp_output.seek(0)
    [log.debug(line.decode('ascii')) for line in tmp_output.read().splitlines()]

step by step:
open a temp file for the redirected output.
duplicate stdout and save it as orig_std_out
redirect stdout to our tempfile
solve
copy duplicated stdout back
close duplicated stdout
seek to the front of the tempfile
log all lines in the tempfile

since I use "with"... the tempfile will be deleted when done as well

Tested and running with python 3.4

it should be trivial to do strerr as well but i leave that to you
Reply all
Reply to author
Forward
0 new messages