LEMON Library for Efficient Models and Optimization in Networks Balázs Dezs o

Page created by Freddie Jensen
 
CONTINUE READING
LEMON Library for Efficient Models and Optimization in Networks Balázs Dezs o
LEMON
Library for Efficient Models and Optimization in Networks

                      Balázs Dezső

                ELTE, Operációkutatási tanszék

 1   Introduction
 2   Graphs
 3   Mapping data to nodes and edges
 4   Undirected graphs
 5   Algorithms
 6   LP interface
 7   Lemon graph format
 8   GLemon
LEMON

LEMON is a software library and optimization framework that
    is written in C++ heavily using template programming
    contains efficient implementations of the most common data
    structures and algorithmic building block used in
    (combinatorial) optimization.
    freely available (open source)
Design goals/preferences
    Running time efficiency
        Appropriate for production use
    Ease of use
        Flexible existing building block
        Easy-to-implement new algorithms
Graphs

A “graph” is not just a data structure, but rather a “concept”
    More graph implementations for diverging purposes,
    users can also write their own graph structures,
    there are graph-adaptors (see later)
    the algorithms work with arbitrary graph type.
Graphs

A “graph” is not just a data structure, but rather a “concept”
    More graph implementations for diverging purposes,
    users can also write their own graph structures,
    there are graph-adaptors (see later)
    the algorithms work with arbitrary graph type.

Implemented Graph types
ListGraph, the “Swiss army knife” graph structure.
SmartGraph, a memory efficient graph type.
FullGraph
        ...
Graph operations

Instantiation of a graph
using namespace lemon;
ListGraph g;
Graph operations

Instantiation of a graph
using namespace lemon;
ListGraph g;

Adding nodes and edges to the graph
ListGraph::Node a,b;
a=g.addNode(); b=g.addNode();
ListGraph::Edge e=g.addEdge(a,b);
Graph operations

Instantiation of a graph
using namespace lemon;
ListGraph g;

Adding nodes and edges to the graph
ListGraph::Node a,b;
a=g.addNode(); b=g.addNode();
ListGraph::Edge e=g.addEdge(a,b);

Erasing
g.erase(e); g.erase(a);
Graph operations

Instantiation of a graph
using namespace lemon;
ListGraph g;

Adding nodes and edges to the graph
ListGraph::Node a,b;
a=g.addNode(); b=g.addNode();
ListGraph::Edge e=g.addEdge(a,b);

Erasing
g.erase(e); g.erase(a);

Iteration on nodes
for(ListGraph::NodeIt n(g);n!=INVALID;++n) {...}
Graph operations

Adding nodes and edges to the graph
ListGraph::Node a,b;
a=g.addNode(); b=g.addNode();
ListGraph::Edge e=g.addEdge(a,b);

Erasing
g.erase(e); g.erase(a);

Iteration on nodes
for(ListGraph::NodeIt n(g);n!=INVALID;++n) {...}

Iteration on edges
for(ListGraph::EdgeIt e(g);e!=INVALID;++e)
for(ListGraph::InEdgeIt e(g,n);e!=INVALID;++e)
for(ListGraph::OutEdgeIt e(g,n);e!=INVALID;++e)
Graph Adaptors

Shortest paths from a point to everywhere
bfs(g,s).predMap(pred).run();

Shortest paths from everywhere to a point
bfs(revGraphAdaptor(g),s).predMap(pred).run();

Implemented adaptors
RevGraphAdaptor, Reverses the edges of the graph
SubGraphAdaptor, can turn on/off the edges and the nodes of
         the graph
UndirGraphAdaptor, each edges appears in both directions
         ( undirected graphs )
       ...
More on Graph Adaptors

Edge disjoint paths between source and target
Maximum flow with uniform edge capacities
More on Graph Adaptors

Find maximum number of node disjoint paths
More on Graph Adaptors

Find maximum number of node disjoint paths
Splitting nodes to two parts
SplitGraphAdaptor split(graph);
More on Graph Adaptors

Find maximum number of node disjoint paths
Splitting nodes to two parts
SplitGraphAdaptor split(graph);
How to assign data to the edges: Maps

The usual way
class NodeData { ... };
class EdgeData { ... };
Graph g;
How to assign data to the edges: Maps

The usual way
class NodeData { ... };
class EdgeData { ... };
Graph g;

Dynamic assignment: Maps
ListGraph::NodeMap cost(g);
cost[v]=5.4;
double s=0;
for(ListGraph::NodeIt n(g);n!=INVALID;++n)
  s+=cost[n];
How to assign data to the edges: Maps

Node Map
ListGraph::NodeMap cost(g);

Edge Map
ListGraph::EdgeMap capacity(g);
How to assign data to the edges: Maps

Node Map
ListGraph::NodeMap cost(g);

Edge Map
ListGraph::EdgeMap capacity(g);

   Very efficient
       like reading or writing an array
       related data are close to each other (help data caching).
   Dynamic, i.e. we can allocate/free maps whenever we want
   Automatic, i.e. when we add new edges or nodes to the
   graph
       maps automatically allocate memory for them and
       initialize the allocated data structures
Maps II.

A “Map” is not a data structure but a “concept”
    We can easily implement own maps,
    there are map-adaptors (see later)
    there are a lot of special purpose maps.
    the algorithms can work on any map type
Maps II.

A “Map” is not a data structure but a “concept”
    We can easily implement own maps,
    there are map-adaptors (see later)
    there are a lot of special purpose maps.
    the algorithms can work on any map type

Example I.: Constant Map
class MyMap {
 public:
  typedef Key ListGraph::Edge;
  typedef Value double;
  Value operator[](Key k) const { return 2.3; }
};
Maps II.

A “Map” is not a data structure but a “concept”
    We can easily implement own maps,
    there are map-adaptors (see later)
    there are a lot of special purpose maps.
    the algorithms can work on any map type

Example I.: Constant Map
class MyMap : public MapBase {
 public:
   Value operator[](Key k) const { return 2.3; }
};
Special maps

map-adaptors
ConstMap: constant values
AddMap,SubMap...: arithmetics
ConvertMap: data type conversion
CombineMap, ComposeMap: metafunctions for maps
      ...

Special maps
InDegMap,OutDegMap: holds the degrees of the nodes
IterableBoolMap: bool valued map, linear time iteration on
         the true or false items
      ...
Undirected Graphs

The concept
An undirected graph is also a directed one at the same time: each
undirected edge correspond to two (oppositely oriented) directed
edges.

    Every algorithm working on directed graph can also be used
    with undirected graphs

ListUGraph, the “Swiss army knife” ugraph structure.
SmartUGraph, a memory efficient ugraph type.
FullUGraph
       ...
Bipartite Undirected Graphs

The concept
A bipartite undirected graph is also a general undirected one at
the same time.

    Every algorithm working on undirected or directed graph can
    also be used with bipartite undirected graphs

ListBpUGraph, the “Swiss army knife” bpugraph structure.
SmartBpUGraph, a memory efficient bpugraph type.
FullBpUGraph
       ...
Algorithms

Class type implementation

Function type implementation
Algorithms

Class type implementation
    The data structures (maps) of an algorithm can be changed.
    Complex initializations are possible
    Step-by-step execution is possible
    Custom stop conditions can be applied
    Multiple executions
    Complex queries

Function type implementation
Algorithms

Class type implementation
    The data structures (maps) of an algorithm can be changed.
    Complex initializations are possible
    Step-by-step execution is possible
    Custom stop conditions can be applied
    Multiple executions
    Complex queries

Function type implementation
    Simpler usage
        Template type don’t have to be explicitly given
    Complex initialization is not possible
    Single execution:
        This is the input / put the result here.
    Named parameters
Algorithms

Class type implementation
Bfs b(g);

b.run(s);

l=0;
for(Node n=t;n!=s;n=b.predNode(n)) l++;
Algorithms

Class type implementation
Bfs b(g);

b.init();
b.addSource(s1); b.addSource(s2);
b.start();

l=-1;
for(Node n=t;n!=INVALID;n=b.predNode(n)) l++;
Algorithms

Class type implementation
Bfs b(g);

b.init();
b.addSource(s1); b.addSource(s2);
b.start();

l=-1;
for(Node n=t;n!=INVALID;n=b.predNode(n)) l++;

Function type implementation
bfs(g,s).predMap(pred).distMap(dist).run();
Algorithms

Searches
    BFS, DFS
    Max cardinality search
    Euler tour
Algorithms

Searches

Trees
    Kruskal, Prim
    Min cost arborescence
Algorithms

Searches

Trees

Shortest paths
     Dijkstra (with Binary, Fibonacci or Radix heap)
     Bellman-Ford, Floyd-Warshall, Johnson (shortest paths)
     Longest path in a DAG
Algorithms

Searches

Trees

Shortest paths

Flows
     Edmonds-Karp, Preflow-push max flow
     Suurballe, several min cost flow
     Hao-Orlin, Nagamochi-Ibaraki min cut
Algorithms

Searches

Trees

Shortest paths

Flows

Topology
     Connected components
     Biconnected components, Articulation nodes
     Strongly connected components
     Topological ordering
Algorithms

Searches

Trees

Shortest paths

Flows

Topology

Matching
     Bipartite matching (max size and min cost)
     General matching (max size and min cost)
Algorithms

Searches

Trees

Shortest paths

Flows

Topology

Matching

Planarity
     Planarity testing, planar embedding
     Planar drawing
Algorithms II.

(Meta)heuristics
    Parameterless Simulated Annealing
    Algorithms for facility location problems
LP interface

Uniform high level interface for different LP packages
Implemented bindings:
     GLPK: Open source (free) (GNU license)
  SOPLEX: Customizable (Academic license)
   CPLEX: Expensive (but better)

Simple usage
    high level modelling of a problem
    problems can be built up from blocks
    Expressions can be handled directly
LP interface

Example
 Lp lp;

 lp.max();

 Lp::Col x1 = lp.addCol();
 Lp::Col x2 = lp.addCol();
 Lp::Col x3 = lp.addCol();

 lp.addRow(x1+x2+x3
LP interface

Example: Using with graphs
  Lp lp;

  lp.max();

  ListGraph::NodeMap x(g);
  lp.addColSet(x);

  for (ListGraph::NodeIt it(g); it!=INVALID; ++it) {
    lp.addRow(x[g.target(it)] - x[g.source(it)]
LP interface

Using expression
  Lp lp;
  lp.max();
  ListGraph::NodeMap x(g);
  lp.addColSet(x);
  for (ListGraph::NodeIt it(g); it!=INVALID; ++it) {
    lp.addRow(x[g.target(it)] - x[g.source(it)]
Graph I/O

    @nodeset
    label coord         color
    3      (1.0, 4.0)   blue
    5      (2.3, 5.7)   red
    12     (7.8, 2.3)   green
    @edgeset
                 label weight   show
    3   5        a     4.3      true
    5   12       c     2.6      true
    3   12       g     3.4      false
    @nodes
    source 3
    target 12
    @edges
    observed c
    @attributes
    title "Lemon test file"
    copyright "Lemon Group"
    version 12
    @end
Graph I/O

   @nodeset
   label coord          color
   3       (1.0, 4.0)   blue
   5       (2.3, 5.7)   red
   12      (7.8, 2.3)   green
   @edgeset
                 label weight   show
   3    5        a     4.3      true
   5    12       c     2.6      true
   3    12       g     3.4      false
   @nodes
   source 3
   target 12
   @edges
   observed c
   @attributes
   title "Lemon test file"
   copyright "Lemon Group"
   version 12
   @end

GraphReader("input.lgf",g)
  .readNodeMap("coord",coord)
  .readEdgeMap("weight",weight)
  .readNode("source",s).readNode("target",t)
  .run();
Graph I/O

   @nodeset
   label coord          color
   3       (1.0, 4.0)   blue
   5       (2.3, 5.7)   red
   12      (7.8, 2.3)   green
   @edgeset
                 label weight   show
   3    5        a     4.3      true
   5    12       c     2.6      true
   3    12       g     3.4      false
   @nodes
   source 3
   target 12
   @edges
   observed c
   @attributes
   title "Lemon test file"
   copyright "Lemon Group"
   version 12
   @end

GraphWriter("output.lgf",g)
  .writeNodeMap("coord",coord)
  .writeEdgeMap("weight",weight)
  .writeNode("source",s).writeNode("target",t)
  .run();
Graph visualisation

      1               2

                          4

      0               3
Graph visualisation

               1              2

                                         4

               0              3

graphToEps(g,"out.eps").scale(10).nodeScale(2)
.nodeSizes(sizes).coords(coords).nodeShapes(shapes)
.nodeColors(composeMap(colorSet,colors))
.edgeColors(composeMap(colorSet,ecolors))
.edgeWidthScale(.3).edgeWidths(widths).nodeTexts(id)
.nodeTextSize(3).enableParallel().parEdgeDist(1)
.drawArrows().arrowWidth(1).arrowLength(1).run();
Graph editor
Downloads, documentation, news etc.

https://lemon.cs.elte.hu/
You can also read