[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Help-glpk] Semi-intelligent C++ wrapper for GLPK APIs
From: |
Robbie Morrison |
Subject: |
[Help-glpk] Semi-intelligent C++ wrapper for GLPK APIs |
Date: |
Sat, 3 Dec 2005 04:31:03 +1300 (NZDT) |
User-agent: |
SquirrelMail/1.4.3a |
Hello GLPK list
This posting should be of interest for those working directly
with the GLPK APIs.
An application programming interface (API) typically defines
the interface between client code and a given library.
The GLPK APIs are relatively low level in the sense that the
client code needs to know whether the current problem is LP
or MILP, which solver or solvers to call, how to interpret
return codes, and so forth.
I have written a C++ wrapper class which manages some of
this complexity from within that class.
The interface class is named 'SolverInterface_GLPK' and the
source files are named 'SiGlpk.*'. A unit test file is also
available as 'SiGlpk.UnitTest.C'. The code is licenced
under GNU GPL and thus fully compatible with GLPK.
At present, the class contains some functionality specific
to the application code I work with. But writing out this
localization should be quite straightforward. And
conversion to Java would also be relatively easy, I should
imagine.
I am happy to email the source to anyone who is interested
(in about two weeks time). If there is a certain level of
traffic, then I plan to release a generic version in about
six months time (my PhD write-up comes first otherwise).
A test version of the GLPK tutorial problem (page 9 of the
GLPK API manual) is given below. This should give some
clues to the features provided by this interface class.
Many thanks, as always, to Andrew Makhorin and the wider
GLPK community.
regards to all
Robbie
// -------------------------------------
// GLPK API tutorial problem, page 9
// -------------------------------------
#include <iostream>
#include <string>
#include "SiGlpk.h" // GLPK solver interface class
int main(int argc, char* argv[])
{
// Specify problem meta-information
int constraintCount = 3; // total number of constraints
int variableCount = 3; // total number of variables
int coeffsEstimate = 4; // underestimate, correct value is 9
// Create solver interface object
SolverInterface_GLPK* pSI = new SolverInterface_GLPK(
constraintCount,
variableCount,
coeffsEstimate);
// uncomment to use the optional simplex presolver
// pSI->initUseSimplexPresolver(); // sets LPX_K_PRESOL
// uncomment to use the interior point solver where appropriate
// pSI->initSetPreferredLPSolver(si::solver_interior);
// Build problem
// set objective sense
pSI->setObjectiveSense(si::maximize);
// cols as non-zero objective coefficients
pSI->loadObj(1, 10.0);
pSI->loadObj(2, 6.0);
pSI->loadObj(3, 4.0);
// rows as non-negative RHS constraints
pSI->loadRhs(1, 100, si::L); // L indicates '<= rhs'
pSI->loadRhs(2, 600, si::L);
pSI->loadRhs(3, 300, si::L);
// rows x cols as non-zero structural coefficients
pSI->loadCoeff(1, 1, 1.0);
pSI->loadCoeff(1, 2, 1.0);
pSI->loadCoeff(1, 3, 1.0);
pSI->loadCoeff(2, 1, 10.0);
pSI->loadCoeff(2, 2, 4.0);
pSI->loadCoeff(2, 3, 5.0);
pSI->loadCoeff(3, 1, 2.0);
pSI->loadCoeff(3, 2, 2.0);
pSI->loadCoeff(3, 3, 6.0);
// uncomment to convert to an MILP problem
// pSI->markVarInteger(2); // mark col 2 as integer
// Call solver
pSI->runSolver(); // reports to console as well
// Print calculated results
int vars = pSI->getVarCount();
std::cout << "is LP : "
<< pSI->telIsLP()
<< std::endl;
std::cout << "Z : "
<< pSI->getObjValue()
<< std::endl;
for (int var = 1; var <= vars; var++)
{
std::cout << "x"
<< var
<< " : "
<< pSI->getVarValue(var)
<< std::endl;
}
// Print known results
std::cout << "KNOWN LP RESULTS" << "\n"
<< "Z = 733.333" << "\n"
<< "x1 = 33.3333" << "\n"
<< "x2 = 66.6667" << "\n"
<< "x3 = 0" << "\n"
<< std::endl;
// Housekeeping
delete pSI; // free memory
return 0; // return from main()
} // code ends
---
Robbie Morrison
PhD student -- policy-oriented energy system simulation
Institute for Energy Engineering (IET)
Technical University of Berlin (TU-Berlin), Germany
University email (redirected) : address@hidden
Webmail (preferred) : address@hidden
[from Webmail client]
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Help-glpk] Semi-intelligent C++ wrapper for GLPK APIs,
Robbie Morrison <=