kpeter@54: /* -*- mode: C++; indent-tabs-mode: nil; -*- kpeter@54: * kpeter@54: * This file is a part of LEMON, a generic C++ optimization library. kpeter@54: * kpeter@54: * Copyright (C) 2003-2010 kpeter@54: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport kpeter@54: * (Egervary Research Group on Combinatorial Optimization, EGRES). kpeter@54: * kpeter@54: * Permission to use, modify and distribute this software is granted kpeter@54: * provided that this copyright notice appears in all copies. For kpeter@54: * precise terms see the accompanying LICENSE file. kpeter@54: * kpeter@54: * This software is provided "AS IS" with no warranty of any kind, kpeter@54: * express or implied, and with no claim as to its suitability for any kpeter@54: * purpose. kpeter@54: * kpeter@54: */ kpeter@54: kpeter@54: ///\file kpeter@54: ///\brief Demo program for the LP solver interface. kpeter@54: /// kpeter@54: /// This demo program shows how the LEMON LP solver interface can be used. kpeter@54: /// A simple linear programming (LP) problem is formulated and solved using kpeter@54: /// the default LP solver (e.g. GLPK). kpeter@54: /// kpeter@54: /// \include lp_demo.cc kpeter@54: kpeter@54: #include kpeter@54: #include kpeter@54: kpeter@54: using namespace lemon; kpeter@54: kpeter@54: int main() kpeter@54: { kpeter@54: // Create an instance of the default LP solver class kpeter@54: // (it will represent an "empty" problem at first) kpeter@54: Lp lp; kpeter@54: kpeter@54: // Add two columns (variables) to the problem kpeter@54: Lp::Col x1 = lp.addCol(); kpeter@54: Lp::Col x2 = lp.addCol(); kpeter@54: kpeter@54: // Add rows (constraints) to the problem kpeter@54: lp.addRow(x1 - 5 <= x2); kpeter@54: lp.addRow(0 <= 2 * x1 + x2 <= 25); kpeter@54: kpeter@54: // Set lower and upper bounds for the columns (variables) kpeter@54: lp.colLowerBound(x1, 0); kpeter@54: lp.colUpperBound(x2, 10); kpeter@54: kpeter@54: // Specify the objective function kpeter@54: lp.max(); kpeter@54: lp.obj(5 * x1 + 3 * x2); kpeter@54: kpeter@54: // Solve the problem using the underlying LP solver kpeter@54: lp.solve(); kpeter@54: kpeter@54: // Print the results kpeter@54: if (lp.primalType() == Lp::OPTIMAL) { kpeter@54: std::cout << "Objective function value: " << lp.primal() << std::endl; kpeter@54: std::cout << "x1 = " << lp.primal(x1) << std::endl; kpeter@54: std::cout << "x2 = " << lp.primal(x2) << std::endl; kpeter@54: } else { kpeter@54: std::cout << "Optimal solution not found." << std::endl; kpeter@54: } kpeter@54: kpeter@54: return 0; kpeter@54: }