Reading CPLEX Integer Program and generating facets via script

General discussion on polymake goes here.
UserCplex
Posts: 31
Joined: 11 Jan 2018, 13:37

Reading CPLEX Integer Program and generating facets via script

Postby UserCplex » 11 Jan 2018, 13:52

The question got resolved and this post of mine is a bit tangential. Hence, I have chosen to delete it.

Thanks.
Last edited by UserCplex on 13 Jan 2018, 19:29, edited 1 time in total.

blorenz
Developer
Posts: 141
Joined: 10 Jan 2011, 17:21

Re: Reading CPLEX Integer Program and generating facets via script

Postby blorenz » 11 Jan 2018, 14:39

Hello,

the load_commands function was made mostly for software demonstrations, you should use the script function for your task, see [1] for more details.
Then you can also call it directly from the command line and easily redirect the output (tee duplicates the output to the terminal and a file):

Code: Select all

polymake --script getfacets | tee facets.txt
Note that you will need to slightly modify your commands to make it a correct script file, the details of this also are given in [1]:

Code: Select all

# to load the application polytope use application "polytope"; # all variables in a script have to be # declared with 'use vars', 'declare' or 'my' use vars qw($f $p $s); $f=lp2poly('problem.lp'); $p = new Polytope<Rational>($f); $p->LATTICE_POINTS; $s = new Polytope(POINTS=>$p->LATTICE_POINTS, COORDINATE_LABELS=>$p->COORDINATE_LABELS); print_constraints($s);
Benjamin

[1] https://polymake.org/doku.php/scripting/start

User avatar
gawrilow
Main Author
Posts: 425
Joined: 25 Dec 2010, 17:40

Re: Reading CPLEX Integer Program and generating facets via script

Postby gawrilow » 11 Jan 2018, 14:50

Any sequence of valid perl/polymake commands stored in a text file can be executed using

Code: Select all

script("filename", any parameters...);
As parameters you might want to pass e.g. the file name:

Code: Select all

script("filename", "problem.lp");
In the script parameters are available in the array @ARGV:

Code: Select all

$f=lp2poly($ARGV[0]);
The only difference to the line-by-line execution in the interactive shell is that all variables you are implicitly introducing by assigning to them need a proper declaration when used in a script:

Code: Select all

declare $f=...;
or

Code: Select all

my $f=...;
Choose 'declare' only if you are going to access the object afterwards in the interactive session. 'my' variables are scoped to the script, the objects are automatically destroyed as soon as the script has finished.

Regarding output redirection: In general, you can open a file handle using standard perl function open() and print anything into it, like this:

Code: Select all

open my $out, ">facets.txt"; print $out "these are my facets:\n", $p->FACETS; close $out;
Utterly unfortunately, however, the function print_constraints you want to use do not allow to redirect the output, it always prints to STDOUT. There are two workarounds, both admittedly ugly:
1. Run your script in non-interactive mode with normal UNIX shell output redirection:

Code: Select all

polymake --script getfacets.sh >facets.txt
If you introduce parameters in your script as described above, you pass them on the command line as well:

Code: Select all

polymake --script getfacets.sh problem.lp >facets.txt
2. In the script itself, temporarily redirect STDOUT to the file:

Code: Select all

open my $saved_STDOUT, ">&=STDOUT"; close STDOUT; open STDOUT, ">facets.txt"; print_constraints($s); close STDOUT; open STDOUT, ">&=", $saved_STDOUT;
Again, you might want to pass the output file name as a further parameter:

Code: Select all

open STDOUT, ">$ARGV[1]";


Return to “General Discussion”