1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2010
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
20 ///\brief Demo program for the LP solver interface.
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).
26 /// \include lp_demo.cc
31 using namespace lemon;
35 // Create an instance of the default LP solver class
36 // (it will represent an "empty" problem at first)
39 // Add two columns (variables) to the problem
40 Lp::Col x1 = lp.addCol();
41 Lp::Col x2 = lp.addCol();
43 // Add rows (constraints) to the problem
44 lp.addRow(x1 - 5 <= x2);
45 lp.addRow(0 <= 2 * x1 + x2 <= 25);
47 // Set lower and upper bounds for the columns (variables)
48 lp.colLowerBound(x1, 0);
49 lp.colUpperBound(x2, 10);
51 // Specify the objective function
53 lp.obj(5 * x1 + 3 * x2);
55 // Solve the problem using the underlying LP solver
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;
64 std::cout << "Optimal solution not found." << std::endl;