Please share C++ sample code

Questions and problems about using polymake go here.
kisas
Posts: 12
Joined: 03 Oct 2014, 21:22

Please share C++ sample code

Postby kisas » 03 Oct 2014, 21:35

Dear all, I apologize for this newbie question. I'm trying to use the C++ package provided by polymake to generate the facets and rays of a polyhedra (or a polytope) because C++ is more efficient. I will start with a lp file, then calculate all the lattice points, and then calculate the convex hull and output its constraints. Can anyone share such a C++ code, also with your make file, so that I can get a quick start from modifying the sample code. Thank you very much!

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

Re: Please share C++ sample code

Postby blorenz » 07 Oct 2014, 12:16

Dear kisas,

First note that all core algorithms in polymake are written in C or C++, the perl shell provides an easy interface that still comes with the efficiency of the compiled code running in the background.

This means that you can do:

Code: Select all

# load the lp file $pf = lp2poly("test.lp"); # polymake cannot compute integer points with float coordinates so we convert it $pr = convert_to<Rational>($pf); # the next line is to make the following one faster due to a bug in polymake $pr->FACETS; # take the integer points and define a new polytope with these as input points $ih = integer_hull($pr); # print the facets of the integer hull print $ih->FACETS;
And all important computations, i.e. computing convex hulls and integer points, are done in C/C++ code and there is no need to write C++ code yourself.

Writing your own C++ client code is recommended if you implement larger algorithms and the documentation can be found here. You can take any C++ client that comes with polymake as an example and you do not need a Makefile as any .cc file in apps/polytope/src is compiled automatically if you run make in the main polymake folder.

Regards
Benjamin

kisas
Posts: 12
Joined: 03 Oct 2014, 21:22

Re: Please share C++ sample code

Postby kisas » 10 Oct 2014, 18:12

Thank you Benjamin ! That's a big relieve for me. I thought the core was written in both perl and c++.

By the way, can polymake takes 'lp-file'-like models? I mean, lp files have variable names only starting with 'x'. That is not convenient for me to compare with the original model which has various variable names, such as x,y1, y2, z1, z2.......... When I generate the facets, I would like them to be in the original variable names. So if I give polymake a lp file but with different variable names (x,y,z) (I can easily generate such a file in AMPL and do some find-and-replace in a text editor) do you think polymake can handle this?

Thank you!

Dear kisas,

First note that all core algorithms in polymake are written in C or C++, the perl shell provides an easy interface that still comes with the efficiency of the compiled code running in the background.

This means that you can do:

Code: Select all

# load the lp file $pf = lp2poly("test.lp"); # polymake cannot compute integer points with float coordinates so we convert it $pr = convert_to<Rational>($pf); # the next line is to make the following one faster due to a bug in polymake $pr->FACETS; # take the integer points and define a new polytope with these as input points $ih = integer_hull($pr); # print the facets of the integer hull print $ih->FACETS;
And all important computations, i.e. computing convex hulls and integer points, are done in C/C++ code and there is no need to write C++ code yourself.

Writing your own C++ client code is recommended if you implement larger algorithms and the documentation can be found here. You can take any C++ client that comes with polymake as an example and you do not need a Makefile as any .cc file in apps/polytope/src is compiled automatically if you run make in the main polymake folder.

Regards
Benjamin

User avatar
assarf
Developer
Posts: 74
Joined: 12 Oct 2011, 15:52

Re: Please share C++ sample code

Postby assarf » 13 Oct 2014, 09:38

Hey,

I do not know if I understood you right. But how you name your variables does not matter. Polymake should still parse the lp-file. And the names of your variables will be stored in the property COORDINATE_LABELS.

Regards
Benjamin

EDIT: but anyway you should only use proper lp-code. You might want to check: http://lpsolve.sourceforge.net/5.5/lp-format.htm
you said: "lp files have variable names only starting with 'x'"
As far as I know this is not true. Where did you get this information?
no signature

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

Re: Please share C++ sample code

Postby gawrilow » 13 Oct 2014, 12:33

... how you name your variables does not matter. Polymake should still parse the lp-file.
I'm afraid, it seems to be not completely true. When you look into the LPparser perl class, you'll see a "prefix" option there defaulting to "x". It suggests that all variables in fact should be named xNNN, because the deeper parsing code simply strips off the prefix and uses the rest as an index.

Our LPparser is quite aged, maybe it is still following an ancient syntactic rule for LP files, while the naming convention has been relaxed in the meanwhile?
It would not be overly hard to teach the parser to accept arbitrary variable names, but it has still to be done.

Or it's already there but I am too dumb to recognize it?

kisas
Posts: 12
Joined: 03 Oct 2014, 21:22

Re: Please share C++ sample code

Postby kisas » 14 Oct 2014, 18:25

Hi assarf,
Thanks for you response.

I was wrong that lp files only have var names starting with "x". I have been working with AMPL recently. Every LP files produced by AMPL have var names only starting with "x". So I thought that was a feature of LP files. Actually it is not. I found my old projects that call CPLEX from C/C++ code. My original var names were preserved in LP files, with a slight changes such as "[" and "]" are removed.



Hey,

I do not know if I understood you right. But how you name your variables does not matter. Polymake should still parse the lp-file. And the names of your variables will be stored in the property COORDINATE_LABELS.

Regards
Benjamin

EDIT: but anyway you should only use proper lp-code. You might want to check: http://lpsolve.sourceforge.net/5.5/lp-format.htm
you said: "lp files have variable names only starting with 'x'"
As far as I know this is not true. Where did you get this information?

kisas
Posts: 12
Joined: 03 Oct 2014, 21:22

Re: Please share C++ sample code

Postby kisas » 16 Oct 2014, 20:39

Hi gawrilow,
Thanks for your input. I tried a LP file with var names such as f12 u23. The polymake does work (although I haven't check if the output is correct, but at least it takes the input.)
I'm using the latest version of polymake. Maybe you were looking at an older version.
... how you name your variables does not matter. Polymake should still parse the lp-file.
I'm afraid, it seems to be not completely true. When you look into the LPparser perl class, you'll see a "prefix" option there defaulting to "x". It suggests that all variables in fact should be named xNNN, because the deeper parsing code simply strips off the prefix and uses the rest as an index.

Our LPparser is quite aged, maybe it is still following an ancient syntactic rule for LP files, while the naming convention has been relaxed in the meanwhile?
It would not be overly hard to teach the parser to accept arbitrary variable names, but it has still to be done.

Or it's already there but I am too dumb to recognize it?


Return to “Helpdesk”