[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-glpk] Perl and GnuLP kit
From: |
Gabor Retvari |
Subject: |
Re: [Help-glpk] Perl and GnuLP kit |
Date: |
Fri, 11 Aug 2006 00:35:15 +0200 |
User-agent: |
KMail/1.9.3 |
On Thursday 10 August 2006 15:30, padma priya wrote:
> Hi
> I need info on how to use GnuLP kit with perl code. I
> want to solve a LP algorithm with Perl code. Can u pls
> send across details with sample code if possible on
> how to use th GnuLP kit.
> Thanks in advance
> Padma Priya v
Hello there,
I wrote the Math::GLPK module to facilitate doing linear programming by GLPK
from within Perl scripts. The module and all its dependencies are available
from here:
http://opt.tmit.bme.hu/~retvari/Math-GLPK.html
(note that this is a temporary place, since the main project page
http://qosip.tmit.bme.hu/~retvari/Math-GLPK.html
seems to be down currently).
Installing this module requires installing a recent version of
Math::MatrixSparse and Math::GLPK::Solve, all available from
http://opt.tmit.bme.hu/~retvari/
Then, solving LPs is as easy as:
#!/usr/bin/perl
# Solves a simple dual minimum cost multicommodity flow problem
#############################################################
#
# Sample problem: x is a column-vector of unknowns
#
# max [-1 0 0 1 0 -1 0 1 0 0 0 0 0 0 0 0 -1 -1 -1 -1] * x
#
# s.t.
#
# [-1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 -1 -0 -0 -0] [ 1 ]
# [0 -1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 -0 -1 -0 -0] [ 1 ]
# [0 -1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 -0 -0 -1 -0] [ 1 ]
# [0 0 -1 1 0 0 0 0 0 0 0 1 0 0 0 0 -0 -0 -0 -1] * x = [ 1 ]
# [0 0 0 0 -1 1 0 0 0 0 0 0 1 0 0 0 -1 -0 -0 -0] [ 1 ]
# [0 0 0 0 0 -1 1 0 0 0 0 0 0 1 0 0 -0 -1 -0 -0] [ 1 ]
# [0 0 0 0 0 -1 0 1 0 0 0 0 0 0 1 0 -0 -0 -1 -0] [ 1 ]
# [0 0 0 0 0 0 -1 1 0 0 0 0 0 0 0 1 -0 -0 -0 -1] [ 1 ]
#
# x >= 0
use strict;
use warnings;
use Math::MatrixSparse;
use Math::GLPK qw(:constants);
# create the problem
# it has 8 constraints (rows) and 20 variables (columns)
# it is of the form
# max cx: Ax = b, x >= 0
print "Testing Math::GLPK by solving sample problem...\n";
my $lp = Math::GLPK->new(8, 20);
# set the name
$lp->prob_name("sample problem");
# set the direction of the optimization to MAX
$lp->obj_dir($LPX_MAX);
# set all variables to be non-negative
$lp->set_all_var_bnds($LPX_LO, 0);
# defined and add the objective (row vector)
$lp->objective( Math::MatrixSparse->new_from_rows([
[ qw( -1 0 0 1 0 -1 0 1 0 0 0 0 0 0 0 0 -1 -1 -1 -1) ]
]) );
# define and add the right-hand side (column vector)
$lp->rhs( Math::MatrixSparse->new_from_cols([
[qw(1 1 1 1 1 1 1 1)]
]) );
# define and the constraint matrix
$lp->constraint( Math::MatrixSparse->new_from_rows([
[qw(-1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 -1 -0 -0 -0)],
[qw(0 -1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 -0 -1 -0 -0)],
[qw(0 -1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 -0 -0 -1 -0)],
[qw(0 0 -1 1 0 0 0 0 0 0 0 1 0 0 0 0 -0 -0 -0 -1)],
[qw(0 0 0 0 -1 1 0 0 0 0 0 0 1 0 0 0 -1 -0 -0 -0)],
[qw(0 0 0 0 0 -1 1 0 0 0 0 0 0 1 0 0 -0 -1 -0 -0)],
[qw(0 0 0 0 0 -1 0 1 0 0 0 0 0 0 1 0 -0 -0 -1 -0)],
[qw(0 0 0 0 0 0 -1 1 0 0 0 0 0 0 0 1 -0 -0 -0 -1)]
]) );
# use the presolver
$lp->int_parm($LPX_K_PRESOL, 1);
# apply scaling, before solving the problem
$lp->int_parm($LPX_K_SCALE, 1);
# set the debug level to maximum
$lp->int_parm($LPX_K_MSGLEV, 3);
# solve the problem by the simplex method
$lp->simplex;
# check if optimal solution is obtained
$lp->is_optimal or die "Could not obtain optimal solution!\n";
# print the results
print "Optimal objective is: " . $lp->get_obj_val . "\n";
print "Optimal solution is:\n";
$lp->get_opt_sol->pretty_print;
exit 0;
etc., see the test.pl file in the distribution.
If you have any problems or questions, feel free to contact me.
best regards,
gabor
p.s.: Math::GLPK currently only supports GLPK version <= 4.8 out of the box. I
plan to release an update pretty soon...