help-glpk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-glpk] Perl Module for the GLPK API


From: Retvari Gabor
Subject: [Help-glpk] Perl Module for the GLPK API
Date: Tue, 23 Mar 2004 22:15:49 +0100
User-agent: KMail/1.5.4

hi,

To make my life easier I have hacked up a simplistic SWIG-based interface to 
reach the GLPK API from within Perl programs. Currently, the interface 
consists of a low level GLPK binding (Math::GLPK::Solve), which exports all 
GLPK API functions (as of 4.4) and a higher level object-oriented module 
(Math::GLPK), which hides the complexity of GLPK. At the moment, only linear 
programs in the form min/max{cx: Ax=b, l <= x <= u} are supported.

Before going any further (e.g., to upload the stuff to CPAN) I would really 
like to hear some comments of GLPK and/or Perl experts on the potential 
improvements or weaknesses of the stuff. 

Here is a simple Perl program, which solves the linear program (with 
equalities instead of inequalities) described in the GLPK reference manual 
(p. 9):

#!/usr/bin/perl

use strict;
use warnings;

use Math::MatrixReal;
use Math::GLPK qw(:constants);

# create linear program
my $lp = Math::GLPK->new(3,3);

$lp->set_prob_name("sample problem");
$lp->set_obj_dir($LPX_MAX);
$lp->set_all_variable_bnds($LPX_LO, 0);

# set objective function
my $obj = Math::MatrixReal->new_from_rows([[qw(10 6 4)] ]);
$lp->objective($obj);

# set right-hand-side
my $rhs = Math::MatrixReal->new_from_cols([ [qw(100 600 300)] ]);
$lp->rhs($rhs);

# set constraint mx
my $A = Math::MatrixReal->new_from_rows([
 [qw(1 1 1)],
 [qw(10 4 5)],
 [qw(2 2 6)],
 ]);
$lp->constraint($A);

# and solve the problem
$lp->simplex;

# print some results
print "Optimal objective is: " . $lp->get_obj_val . "\n";
print "Optimal basis is:\n" . $lp->get_primal_basis . "\n";
$lp->print_prob("/tmp/prob");
$lp->print_sol("/tmp/log");

exit;

This will produce:

address@hidden:/export/devel/mod_glpk/Math-GLPK$ ./GLPK_test
      0:   objval =   0.000000000e+00   infeas =   1.000000000e+00 (0)
      3:   objval =   6.666666667e+02   infeas =   0.000000000e+00 (0)
OPTIMAL SOLUTION FOUND
Optimal objective is: 666.666666666667
Optimal basis is:
[  1.000000000000E+00  1.000000000000E+00  1.000000000000E+00 ]
[  1.000000000000E+01  4.000000000000E+00  5.000000000000E+00 ]
[  2.000000000000E+00  2.000000000000E+00  6.000000000000E+00 ]

lpx_write_prob: writing problem data to `/tmp/prob'...
lpx_print_sol: writing LP problem solution to `/tmp/log'...
address@hidden:/export/devel/mod_glpk/Math-GLPK$

If you find it useful or have some comments or simply you would like to use 
the modules, don't hesitate to contact me!

        cheers: gabor





reply via email to

[Prev in Thread] Current Thread [Next in Thread]