Limbo 3.5.4
Loading...
Searching...
No Matches
Limbo.Parsers.LpParser

Introduction

LP format is among the various file format to describe optimization problems. The parser can read a special case of linear programming problem in the LP format compatible to Gurobi, which contains only differential constaints. The special linear programming problem can be solved by dual min-cost flow algorithm in the Limbo.Solvers package. Here is a sample file.

Minimize
b_5829890_x2
+ b_5880854_x2
Subject To
- 2 b_5829890_x2 + 2 b_5829890_x1 <= -64
b_5880854_x2 - b_5880854_x1 >= 32
Bounds
b_5829890_x1 >= 10
10 >= b_5880854_x1
1014 <= b_5829890_x2 <= 1917
1014 <= b_5880854_x2 <= 1917
Generals
b_5829890_x2 b_5880854_x2
Binary
b_5829890_x1
End

Examples

Flex/Bison Parser

See documented version: test/parsers/lp/test_bison.cpp

#include <iostream>
#include <fstream>
using std::cout;
using std::cin;
using std::endl;
using std::string;
{
public:
typedef LpParser::int64_t int64_t;
{
cout << "constructing LpDataBase" << endl;
}
void add_variable(string const& vname, double l, double r)
{
cout << l << " <= " << vname << " <= " << r << endl;
}
void add_constraint(string const& cname, LpParser::TermArray const& terms, char compare, double constant)
{
cout << cname << ": ";
for (LpParser::TermArray::const_iterator it = terms.begin(); it != terms.end(); ++it)
cout << " + " << it->coef << " " << it->var;
cout << " " << compare << " " << constant << endl;
}
void add_objective(bool minimize, LpParser::TermArray const& terms)
{
if (minimize)
cout << "Minimize\n";
else
cout << "Maximize\n";
for (LpParser::TermArray::const_iterator it = terms.begin(); it != terms.end(); ++it)
cout << " + " << it->coef << " " << it->var;
cout << endl;
cout << "Subject To\n";
}
void set_integer(string const& vname, bool binary)
{
if (binary)
cout << vname << ": BINARY\n";
else
cout << vname << ": INTEGER\n";
}
};
void test1(string const& filename)
{
cout << "////////////// test1 ////////////////" << endl;
LpParser::read(db, filename);
}
void test2(string const& filename)
{
cout << "////////////// test2 ////////////////" << endl;
LpParser::Driver driver (db);
//driver.trace_scanning = true;
//driver.trace_parsing = true;
driver.parse_file(filename);
}
int main(int argc, char** argv)
{
if (argc > 1)
{
test1(argv[1]);
test2(argv[1]);
}
else
cout << "at least 1 argument is required" << endl;
return 0;
}
Driver for Lp parser.
Custom class that inheritates LpParser::LpDataBase with all the required callbacks defined.
LpDataBase()
constructor
LpParser::int64_t int64_t
use int64_t in base type
void add_constraint(string const &cname, LpParser::TermArray const &terms, char compare, double constant)
add constraint that terms compare constant.
void add_variable(string const &vname, double l, double r)
add variable that l <= vname <= r.
void set_integer(string const &vname, bool binary)
set integer variables
void add_objective(bool minimize, LpParser::TermArray const &terms)
add object terms
Base class for lp database. Only pure virtual functions are defined. User needs to inheritate this ...
Definition LpDataBase.h:115
std::vector< Term > TermArray
array of terms
Definition LpDataBase.h:105
bool read(LpDataBase &db, const string &lpFile)
API for LpParser. Read LP file and initialize database by calling user-defined callback functions.
int main()
void test2(string const &filename)
test std::list
void test1()
test function API

Compiling and running commands (assuming LIMBO_DIR is exported as the environment variable to the path where limbo library is installed)

g++ -o test_bison test_bison.cpp -I $LIMBO_DIR/include -L $LIMBO_DIR/lib -llpparser
./test_bison benchmarks/problem.lp

All Examples

References