/**
\page quicktour Quick Tour to LEMON
Let us first answer the question "What do I want to use LEMON for?"
.
LEMON is a C++ library, so you can use it if you want to write C++
programs. What kind of tasks does the library LEMON help to solve?
It helps to write programs that solve optimization problems that arise
frequently when designing and testing certain networks, for example
in telecommunication, computer networks, and other areas that I cannot
think of now. A very natural way of modelling these networks is by means
of a graph (we will always mean a directed graph by that and say
undirected graph otherwise).
So if you want to write a program that works with
graphs then you might find it useful to use our library LEMON. LEMON
defines various graph concepts depending on what you want to do with the
graph: a very good description can be found in the page
about \ref graphs "graphs".
You will also want to assign data to the edges or nodes of the graph, for
example a length or capacity function defined on the edges. You can do this in
LEMON using so called \b maps. You can define a map on the nodes or on the edges of the graph and the value of the map (the range of the function) can be practically almost of any type. Read more about maps \ref maps-page "here".
Some examples are the following (you will find links next to the code fragments that help to download full demo programs: save them on your computer and compile them according to the description in the page about \ref getsart How to start using LEMON):
- First we give two examples that show how to instantiate a graph. The
first one shows the methods that add nodes and edges, but one will
usually use the second way which reads a graph from a stream (file).
- The following code fragment shows how to fill a graph with data. It creates a complete graph on 4 nodes. The type Listgraph is one of the LEMON graph types: the typedefs in the beginning are for convenience and we will suppose them later as well.
\code
typedef ListGraph Graph;
typedef Graph::NodeIt NodeIt;
Graph g;
for (int i = 0; i < 3; i++)
g.addNode();
for (NodeIt i(g); i!=INVALID; ++i)
for (NodeIt j(g); j!=INVALID; ++j)
if (i != j) g.addEdge(i, j);
\endcode
See the whole program in file \ref helloworld.cc.
If you want to read more on the LEMON graph structures and concepts, read the page about \ref graphs "graphs".
- The following code shows how to read a graph from a stream (e.g. a file). LEMON supports the DIMACS file format: it can read a graph instance from a file
in that format (find the documentation of the DIMACS file format on the web).
\code
Graph g;
std::ifstream f("graph.dim");
readDimacs(f, g);
\endcode
One can also store network (graph+capacity on the edges) instances and other things in DIMACS format and use these in LEMON: to see the details read the documentation of the \ref dimacs.h "Dimacs file format reader".
- If you want to solve some transportation problems in a network then
you will want to find shortest paths between nodes of a graph. This is
usually solved using Dijkstra's algorithm. A utility
that solves this is the \ref lemon::Dijkstra "LEMON Dijkstra class".
The following code is a simple program using the \ref lemon::Dijkstra "LEMON
Dijkstra class" and it also shows how to define a map on the edges (the length
function):
\code
typedef ListGraph Graph;
typedef Graph::Node Node;
typedef Graph::Edge Edge;
typedef Graph::EdgeMap LengthMap;
Graph g;
//An example from Ahuja's book
Node s=g.addNode();
Node v2=g.addNode();
Node v3=g.addNode();
Node v4=g.addNode();
Node v5=g.addNode();
Node t=g.addNode();
Edge s_v2=g.addEdge(s, v2);
Edge s_v3=g.addEdge(s, v3);
Edge v2_v4=g.addEdge(v2, v4);
Edge v2_v5=g.addEdge(v2, v5);
Edge v3_v5=g.addEdge(v3, v5);
Edge v4_t=g.addEdge(v4, t);
Edge v5_t=g.addEdge(v5, t);
LengthMap len(g);
len.set(s_v2, 10);
len.set(s_v3, 10);
len.set(v2_v4, 5);
len.set(v2_v5, 8);
len.set(v3_v5, 5);
len.set(v4_t, 8);
len.set(v5_t, 8);
std::cout << "The id of s is " << g.id(s)<< std::endl;
std::cout <<"The id of t is " << g.id(t)<<"."< dijkstra_test(g,len);
dijkstra_test.run(s);
std::cout << "The distance of node t from node s: " << dijkstra_test.dist(t)< If you want to design a network and want to minimize the total length
of wires then you might be looking for a minimum spanning tree in
an undirected graph. This can be found using the Kruskal algorithm: the
class \ref lemon::Kruskal "LEMON Kruskal class" does this job for you.
The following code fragment shows an example:
Ide Zsuzska fog irni!
- Many problems in network optimization can be formalized by means
of a linear programming problem (LP problem, for short). In our
library we decided not to write an LP solver, since such packages are
available in the commercial world just as well as in the open source
world, and it is also a difficult task to compete these. Instead we
decided to develop an interface that makes it easier to use these
solvers together with LEMON. The advantage of this approach is
twofold. Firstly our C++ interface is more comfortable than the
solvers' native interface. Secondly, changing the underlying solver in
a certain software using LEMON's LP interface needs zero effort. So,
for example, one may try his idea using a free solver, demonstrate its
usability for a customer and if it works well, but the performance
should be improved, then one may decide to purchase and use a better
commercial solver.
So far we have an
interface for the commercial LP solver software \b CLPLEX (developed by ILOG)
and for the open source solver \b GLPK (a shorthand for Gnu Linear Programming
Toolkit).
We will show two examples, the first one shows how simple it is to formalize
and solve an LP problem in LEMON, while the second one shows how LEMON
facilitates solving network optimization problems using LP solvers.
- The following code shows how to solve an LP problem using the LEMON lp
interface. The code together with the comments is self-explanatory.
\code
//A default solver is taken
LpDefault lp;
typedef LpDefault::Row Row;
typedef LpDefault::Col Col;
//This will be a maximization
lp.max();
//We add coloumns (variables) to our problem
Col x1 = lp.addCol();
Col x2 = lp.addCol();
Col x3 = lp.addCol();
//Constraints
lp.addRow(x1+x2+x3 <=100);
lp.addRow(10*x1+4*x2+5*x3<=600);
lp.addRow(2*x1+2*x2+6*x3<=300);
//Nonnegativity of the variables
lp.colLowerBound(x1, 0);
lp.colLowerBound(x2, 0);
lp.colLowerBound(x3, 0);
//Objective function
lp.setObj(10*x1+6*x2+4*x3);
//Call the routine of the underlying LP solver
lp.solve();
//Print results
if (lp.primalStatus()==LpSolverBase::OPTIMAL){
printf("Z = %g; x1 = %g; x2 = %g; x3 = %g\n",
lp.primalValue(),
lp.primal(x1), lp.primal(x2), lp.primal(x3));
}
else{
std::cout<<"Optimal solution not found!"<The second example shows how easy it is to formalize a max-flow
problem as an LP problem using the LEMON LP interface: we are looking
for a real valued function defined on the edges of the digraph
satisfying the nonnegativity-, the capacity constraints and the
flow-conservation constraints and giving the largest flow value
between to designated nodes.
In the following code we suppose that we already have the graph \c g,
the capacity map \c cap, the source node \c s and the target node \c t
in the memory. We will also omit the typedefs.
\code
//Define a map on the edges for the variables of the LP problem
typename G::template EdgeMap x(g);
lp.addColSet(x);
//Nonnegativity and capacity constraints
for(EdgeIt e(g);e!=INVALID;++e) {
lp.colUpperBound(x[e],cap[e]);
lp.colLowerBound(x[e],0);
}
//Flow conservation constraints for the nodes (except for 's' and 't')
for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) {
LpDefault::Expr ex;
for(InEdgeIt e(g,n);e!=INVALID;++e) ex+=x[e];
for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e];
lp.addRow(ex==0);
}
//Objective function: the flow value entering 't'
{
LpDefault::Expr ex;
for(InEdgeIt e(g,t);e!=INVALID;++e) ex+=x[e];
for(OutEdgeIt e(g,t);e!=INVALID;++e) ex-=x[e];
lp.setObj(ex);
}
//Maximization
lp.max();
//Solve with the underlying solver
lp.solve();
\endcode
The complete program can be found in file \ref lp_maxflow_demo.cc. After compiling run it in the form:
./lp_maxflow_demo < ?????????.lgf
where ?????????.lgf is a file in the lemon format containing a maxflow instance (designated "source", "target" nodes and "capacity" map).
See the whole code in \ref lp_demo.cc.
*/