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