in reply to GLPK integration

Hello jo37 a big thank you for porting GLPK to Perl/PDL.

I was wondering whether it is possible as it is to specify the problem as a MathProg program (e.g. https://iuuk.mff.cuni.cz/~bohm/texts/mathprog_intro.html).

I know there is examples/glpsol.c in the GLPK distribution which produces an executable which accepts a MathProg program and solves it. I guess it breaks down the program directives into GLPK API calls somehow, but it is too complex for me to decypher. I was wondering if you know if there is GLPK API call to do this.

I have modified trivially examples/glpsol.c so as to provide glpsol(int argc, char **argv) which then I provide via XS to Perl (published here https://github.com/hadjiprocopis/perl-algorithm-glpk-glpsol). This is better than shelling out to glpsol executable but it is quite lame not only because it works like the executable, with a CLI params array but also it requires the program to be written to disk and it writes the solution to disk as well. So, I am looking for a better solution, namely a proper GLPK API function which reads in MathProg and outputs solution matrix.

bw, bliako

Replies are listed 'Best First'.
Re^2: GLPK integration
by jo37 (Curate) on Jan 15, 2026 at 18:22 UTC

    Hello bliako,

    first of all thank you for pointing to the POD errors. Just published a new release.

    I'm afraid I cannot answer your question as I'm not an expert on GLPK at all. Actually, I never used it directly. The module uses a copy of a C function from Octave's core to connect to GLPK. This way I could call GLPK from PDL without any knowledge of the former. Though the Octave-to-GLPK API has a lot of parameters, I found it much easier to use than GLPK itself.

    You may call it laziness, but isn't that one of a programmer's virtues? 😉

    Greetings,
    🐻

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
Re^2: GLPK integration
by jo37 (Curate) on Jan 16, 2026 at 14:43 UTC

    Took a look at the sources of PDL::Opt::GLPK once again. There is glpk_aio.c, which is the mentioned copy from the Octave sources. This is the wrapper I utilized to link PDL to GLPK. It has a single C function that orchestrates the GLPK API calls and which is the interface that PDL::Opt::GLPK::glpk actually calls. In PDL::Opt::GLPK, the XS code to access it is generated from PD. You might use it in self-written XS code as well.

    At least you might use it as a blueprint for your own code.

    Greetings,
    🐻

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$

      ok thanks, your code demystifies how it is done. I will investigate how a MathProg translates to these matrices. Then passing the matrices to this convenient function will do the trick.

        Out of curiosity:
        What would be a use case to solve a linear program in MathProg format within a Perl program? My thoughts about it:

        If I had an LP in MathProg format, I'd use glpsol.
        Why would I want to feed it to a Perl wrapper?

        If I had some Perl data representing an LP, I could transform it into a format accepted by an LP solver and run the solver.
        Why would I want to convert it to MathProg instead?

        BTW: GLPK has a builtin MathProg generator. Just call the solver - it will generate the respective MathProg on request.

        Greetings,
        🐻

        $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$