demo/lp_demo.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Mon, 01 Mar 2010 02:30:00 +0100
changeset 58 10b6a5b7d4c0
permissions -rw-r--r--
Improve Algorithms section (it is still under construction)
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     5  * Copyright (C) 2003-2010
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 ///\file
    20 ///\brief Demo program for the LP solver interface.
    21 ///
    22 /// This demo program shows how the LEMON LP solver interface can be used.
    23 /// A simple linear programming (LP) problem is formulated and solved using
    24 /// the default LP solver (e.g. GLPK).
    25 ///
    26 /// \include lp_demo.cc
    27 
    28 #include <iostream>
    29 #include <lemon/lp.h>
    30 
    31 using namespace lemon;
    32 
    33 int main()
    34 {
    35   // Create an instance of the default LP solver class
    36   // (it will represent an "empty" problem at first)
    37   Lp lp;
    38 
    39   // Add two columns (variables) to the problem
    40   Lp::Col x1 = lp.addCol();
    41   Lp::Col x2 = lp.addCol();
    42 
    43   // Add rows (constraints) to the problem
    44   lp.addRow(x1 - 5 <= x2);
    45   lp.addRow(0 <= 2 * x1 + x2 <= 25);
    46   
    47   // Set lower and upper bounds for the columns (variables)
    48   lp.colLowerBound(x1, 0);
    49   lp.colUpperBound(x2, 10);
    50   
    51   // Specify the objective function
    52   lp.max();
    53   lp.obj(5 * x1 + 3 * x2);
    54   
    55   // Solve the problem using the underlying LP solver
    56   lp.solve();
    57 
    58   // Print the results
    59   if (lp.primalType() == Lp::OPTIMAL) {
    60     std::cout << "Objective function value: " << lp.primal() << std::endl;
    61     std::cout << "x1 = " << lp.primal(x1) << std::endl;
    62     std::cout << "x2 = " << lp.primal(x2) << std::endl;
    63   } else {
    64     std::cout << "Optimal solution not found." << std::endl;
    65   }
    66 
    67   return 0;
    68 }