Wim Verhaegen > Math-LP-0.03 > Math::LP



Annotate this POD


New  1
Open  0
View/Report Bugs
Module Version: 0.03   Source  


Math::LP - OO interface to linear programs


    use Math::LP qw(:types);             # imports optimization types
    use Math::LP::Constraint qw(:types); # imports constraint types

    # make a new LP
    $lp = new Math::LP;

    # make the variables for the LP
    $x1 = new Math::LP::Variable(name => 'x1');
    $x2 = new Math::LP::Variable(name => 'x2');

    # maximize the objective function to x1 + 2 x2
    $obj_fn = make Math::LP::LinearCombination($x1,1.0,$x2,2.0);  

    # add the constraint x1 + x2 <= 2
    $constr = new Math::LP::Constraint(
        lhs  => make Math::LP::LinearCombination($x1,1.0,$x2,1.0),
        rhs  => 2.0,
        type => $LE,
    # solve the LP and print the results
    $lp->solve() or die "Could not solve the LP";
    print "Optimum = ", $obj_fn->{value}, "\n";
    print "x1 = ", $x1->{value}, "\n";
    print "x2 = ", $x1->{value}, "\n";
    print "slack = ", $constr->{slack}, "\n";


The Math::LP package provides an object oriented interface to defining and solving mixed linear/integer programs. It uses the lp_solve library as the underlying solver. Please note that this is not a two way relation. An LP is defined using Math::LP, converted to an lp_solve data structure, and solved with lp_solve functions. It is not possible to grab an lp_solve structure somehow and convert it to a Math::LP object for manipulation and inspection. If you want to do that kind of stuff in Perl, use the Math::LP::Solve package instead.

That being said, the logical way of constructing an LP consists of

  1. Construct Math::LP::Variable objects, in the meanwhile marking integer variables
  2. Construct Math::LP::LinearCombination objects with the variables and use them as the objective function and constraints
  3. Solve the LP
  4. Fetch the variable values from the Math::LP::Variable objects, the slacks and dual values from the Math::LP::Constraint objects. and the row values (including the optimum) from the corresponding Math::LP::LinearCombination.



Holds the status of the last solve() call. Can be either $OPTIMAL, $MILP_FAIL, $INFEASIBLE, $UNBOUNDED, $FAILURE, $RUNNING, $FEAS_FOUND, $NO_FEAS_FOUND or $BREAK_BB.


A ref to a hash with all the Math::LP::Variable objects used in the LP indexed on their name.


A ref to an array with all Math::LP::Constraint objects used in the LP.


A Math::LP::LinearCombination object representing the objective function


The optimization type. Can be either $MAX or $MIN.



returns a new, empty LP


returns the number of rows, i.e. the number of constraints in the LP


returns the number of columns, i.e. the number of variables in the LP


registers the variable as belonging to the LP. The index field of the variable is set as a side effect. For this reason it is not allowed to use 1 variable in 2 LP objects.


adds a Math::LP::Constraint to the LP. The index field of the constraint is likewise set. It is thus also not allowed to use a single constraint in more than 1 LP. All variables present in the constraint are automatically registered.


sets the objective function of the LP, specified by the following parameters:


a Math::LP::LinearCombination forming the objective function. New variables in the linear combination are automatically added to the LP.


the optimization type, either $MAX or $MIN


shortcut for set_objective_function($lincomb,$MAX)


shortcut for set_objective_function($lincomb,$MIN)


Solves the LP, returns true if succeeded (i.e. the status value is $OPTIMAL), false otherwise. The status of the solver is available in the status field afterwards. The default is to solve using solve(). If however $lag_solve is specified and true, lag_solve() will be used.


Returns the value of the objective function obtained by the solver.


More info on the packages used in Math::LP is found in Math::LP::Object, Math::LP::Variable and Math::LP::LinearCombination.

The underlying wrapper to the lp_solve library is documented in Math::LP::Solve. It is based on the lp_solve library written by Michel Berkelaar and adapted by Jeroen Dirks. Documentation on lp_solve is distributed with the source code, and can be downloaded at ftp://ftp.ics.ele.tue.nl/pub/lp_solve/


Wim Verhaegen <wimv@cpan.org>


Copyright(c) 2000-2001 Wim Verhaegen. All rights reserved. This program is free software; you can redistribute and/or modify it under the same terms as Perl itself.

syntax highlighting: