lp.dox
author Peter Kovacs <kpeter@inf.elte.hu>
Mon, 22 Feb 2010 13:00:14 +0100
changeset 50 72867897fcba
parent 48 a5457a780c34
child 55 edb7d5759e0d
permissions -rw-r--r--
Minor improvements
kpeter@30
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
kpeter@30
     2
 *
kpeter@30
     3
 * This file is a part of LEMON, a generic C++ optimization library.
kpeter@30
     4
 *
kpeter@32
     5
 * Copyright (C) 2003-2010
kpeter@30
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@30
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@30
     8
 *
kpeter@30
     9
 * Permission to use, modify and distribute this software is granted
kpeter@30
    10
 * provided that this copyright notice appears in all copies. For
kpeter@30
    11
 * precise terms see the accompanying LICENSE file.
kpeter@30
    12
 *
kpeter@30
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@30
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@30
    15
 * purpose.
kpeter@30
    16
 *
kpeter@30
    17
 */
kpeter@30
    18
kpeter@30
    19
namespace lemon {
kpeter@30
    20
/**
kpeter@30
    21
[PAGE]sec_lp[PAGE] Linear Programming Interface
kpeter@30
    22
kpeter@48
    23
Linear programming (LP) is one of the most important general methods of
kpeter@48
    24
operations research. Countless optimization problems can be formulated
kpeter@48
    25
and solved using LP techniques.
kpeter@48
    26
Therefore, developing efficient LP solvers has been of high practical
kpeter@48
    27
interest for a long time.
kpeter@48
    28
Nowadays various efficient LP solvers are available, including both
kpeter@48
    29
open source and commercial software packages.
kpeter@48
    30
Therefore, LEMON does not implement its own solver, but it features
kpeter@48
    31
wrapper classes for several known LP packages providing a common
kpeter@48
    32
high-level interface for all of them.
kpeter@30
    33
kpeter@48
    34
The advantage of this approach is twofold. First, our C++ interface is
kpeter@48
    35
more comfortable than the typical native interfaces of the solvers.
kpeter@48
    36
Second, changing the underlying solver in a certain application using
kpeter@48
    37
LEMON's LP interface needs no effort. So, for example, one may try her
kpeter@48
    38
idea using an open source solver, demonstrate its usability for a customer
kpeter@48
    39
and if it works well, but the performance should be improved, then the
kpeter@48
    40
customer may decide to purchase and use a better commercial solver.
kpeter@48
    41
kpeter@48
    42
Currently, the following linear and mixed integer programming packages are
kpeter@48
    43
supported: GLPK, Clp, Cbc, ILOG CPLEX and SoPlex.
kpeter@48
    44
However, additional wrapper classes for new solvers can also be implemented
kpeter@48
    45
quite easily.
kpeter@48
    46
kpeter@48
    47
In this section, we will show two examples. The first one shows how simple
kpeter@48
    48
it is to formalize and solve an LP problem in LEMON, while the second one
kpeter@48
    49
shows how LEMON facilitates solving network optimization problems using LP
kpeter@48
    50
solvers.
kpeter@30
    51
kpeter@30
    52
\code
kpeter@30
    53
  Lp lp;
kpeter@30
    54
kpeter@30
    55
  Lp::Col x1 = lp.addCol();
kpeter@30
    56
  Lp::Col x2 = lp.addCol();
kpeter@30
    57
kpeter@30
    58
  lp.addRow(0 <= x1 + x2 <= 100);
kpeter@30
    59
  lp.addRow(2 * x1 <= x2 + 32);
kpeter@30
    60
kpeter@30
    61
  lp.colLowerBound(x1, 0);
kpeter@30
    62
  lp.colUpperBound(x2, 100);
kpeter@30
    63
kpeter@30
    64
  lp.max();
kpeter@30
    65
  lp.obj(10 * x1 + 6 * x2);
kpeter@30
    66
  lp.solve();
kpeter@30
    67
kpeter@50
    68
  std::cout << "Objective function value: " << lp.primal() << std::endl;
kpeter@50
    69
  std::cout << "x1 = " << lp.primal(x1) << std::endl;
kpeter@50
    70
  std::cout << "x2 = " << lp.primal(x2) << std::endl;
kpeter@30
    71
\endcode
kpeter@30
    72
kpeter@30
    73
\ref LpBase::Col "Lp::Col" type represents the variables in the LP problems,
kpeter@30
    74
while \ref LpBase::Row "Lp::Row" represents the constraints. The numerical
kpeter@30
    75
operators can be used to form expressions from columns and dual
kpeter@30
    76
expressions from rows. Due to the suitable operator overloads,
kpeter@30
    77
a problem can be described in C++ conveniently, directly as it is
kpeter@30
    78
expressed in mathematics.
kpeter@30
    79
kpeter@30
    80
The following example solves a maximum flow problem with linear
kpeter@30
    81
programming. Several other graph optimization problems can also be
kpeter@30
    82
expressed as linear programs and this interface helps to solve them easily
kpeter@30
    83
(though usually not so efficiently as by a direct combinatorial method).
kpeter@30
    84
kpeter@30
    85
\code
kpeter@30
    86
  Lp lp;
kpeter@48
    87
  ListDigraph::ArcMap<Lp::Col> f(g);
kpeter@30
    88
  lp.addColSet(f);
kpeter@30
    89
kpeter@30
    90
  // Capacity constraints
kpeter@48
    91
  for (ListDigraph::ArcIt a(g); a != INVALID; ++a) {
kpeter@30
    92
    lp.colLowerBound(f[a], 0);
kpeter@30
    93
    lp.colUpperBound(f[a], capacity[a]);
kpeter@30
    94
  }
kpeter@30
    95
kpeter@30
    96
  // Flow conservation constraints
kpeter@48
    97
  for (ListDigraph::NodeIt n(g); n != INVALID; ++n) {
kpeter@30
    98
    if (n == src || n == trg) continue;
kpeter@30
    99
    Lp::Expr e;
kpeter@48
   100
    for (ListDigraph::OutArcIt a(g,n); a != INVALID; ++a) e += f[a];
kpeter@48
   101
    for (ListDigraph::InArcIt a(g,n); a != INVALID; ++a) e -= f[a];
kpeter@30
   102
    lp.addRow(e == 0);
kpeter@30
   103
  }
kpeter@30
   104
kpeter@30
   105
  // Objective function
kpeter@30
   106
  Lp::Expr o;
kpeter@48
   107
  for (ListDigraph::OutArcIt a(g,src); a != INVALID; ++a) o += f[a];
kpeter@48
   108
  for (ListDigraph::InArcIt a(g,src); a != INVALID; ++a) o -= f[a];
kpeter@30
   109
kpeter@30
   110
  lp.max();
kpeter@30
   111
  lp.obj(o);
kpeter@30
   112
  lp.solve();
kpeter@50
   113
kpeter@50
   114
  std::cout << "Max flow value: " << lp.primal() << std::endl;
kpeter@30
   115
\endcode
kpeter@30
   116
kpeter@30
   117
[TRAILER]
kpeter@30
   118
*/
kpeter@32
   119
}