demo/lp_demo.cc
changeset 54 e99a7fb6bff5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/demo/lp_demo.cc	Mon Mar 01 02:26:24 2010 +0100
     1.3 @@ -0,0 +1,68 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2010
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +///\file
    1.23 +///\brief Demo program for the LP solver interface.
    1.24 +///
    1.25 +/// This demo program shows how the LEMON LP solver interface can be used.
    1.26 +/// A simple linear programming (LP) problem is formulated and solved using
    1.27 +/// the default LP solver (e.g. GLPK).
    1.28 +///
    1.29 +/// \include lp_demo.cc
    1.30 +
    1.31 +#include <iostream>
    1.32 +#include <lemon/lp.h>
    1.33 +
    1.34 +using namespace lemon;
    1.35 +
    1.36 +int main()
    1.37 +{
    1.38 +  // Create an instance of the default LP solver class
    1.39 +  // (it will represent an "empty" problem at first)
    1.40 +  Lp lp;
    1.41 +
    1.42 +  // Add two columns (variables) to the problem
    1.43 +  Lp::Col x1 = lp.addCol();
    1.44 +  Lp::Col x2 = lp.addCol();
    1.45 +
    1.46 +  // Add rows (constraints) to the problem
    1.47 +  lp.addRow(x1 - 5 <= x2);
    1.48 +  lp.addRow(0 <= 2 * x1 + x2 <= 25);
    1.49 +  
    1.50 +  // Set lower and upper bounds for the columns (variables)
    1.51 +  lp.colLowerBound(x1, 0);
    1.52 +  lp.colUpperBound(x2, 10);
    1.53 +  
    1.54 +  // Specify the objective function
    1.55 +  lp.max();
    1.56 +  lp.obj(5 * x1 + 3 * x2);
    1.57 +  
    1.58 +  // Solve the problem using the underlying LP solver
    1.59 +  lp.solve();
    1.60 +
    1.61 +  // Print the results
    1.62 +  if (lp.primalType() == Lp::OPTIMAL) {
    1.63 +    std::cout << "Objective function value: " << lp.primal() << std::endl;
    1.64 +    std::cout << "x1 = " << lp.primal(x1) << std::endl;
    1.65 +    std::cout << "x2 = " << lp.primal(x2) << std::endl;
    1.66 +  } else {
    1.67 +    std::cout << "Optimal solution not found." << std::endl;
    1.68 +  }
    1.69 +
    1.70 +  return 0;
    1.71 +}