[54] | 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 MIP solver interface. |
---|
| 21 | /// |
---|
| 22 | /// This demo program shows how the LEMON MIP solver interface can be used. |
---|
| 23 | /// A simple mixed integer programming (MIP) problem is formulated and solved |
---|
| 24 | /// using the default MIP solver (e.g. GLPK). |
---|
| 25 | /// |
---|
| 26 | /// \include mip_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 MIP solver class |
---|
| 36 | // (it will represent an "empty" problem at first) |
---|
| 37 | Mip mip; |
---|
| 38 | |
---|
| 39 | // Add two columns (variables) to the problem |
---|
| 40 | Mip::Col x1 = mip.addCol(); |
---|
| 41 | Mip::Col x2 = mip.addCol(); |
---|
| 42 | |
---|
| 43 | // Add rows (constraints) to the problem |
---|
| 44 | mip.addRow(x1 - 5 <= x2); |
---|
| 45 | mip.addRow(0 <= 2 * x1 + x2 <= 25); |
---|
| 46 | |
---|
| 47 | // Set lower and upper bounds for the columns (variables) |
---|
| 48 | mip.colLowerBound(x1, 0); |
---|
| 49 | mip.colUpperBound(x2, 10); |
---|
| 50 | |
---|
| 51 | // Set the type of the columns |
---|
| 52 | mip.colType(x1, Mip::INTEGER); |
---|
| 53 | mip.colType(x2, Mip::REAL); |
---|
| 54 | |
---|
| 55 | // Specify the objective function |
---|
| 56 | mip.max(); |
---|
| 57 | mip.obj(5 * x1 + 3 * x2); |
---|
| 58 | |
---|
| 59 | // Solve the problem using the underlying MIP solver |
---|
| 60 | mip.solve(); |
---|
| 61 | |
---|
| 62 | // Print the results |
---|
| 63 | if (mip.type() == Mip::OPTIMAL) { |
---|
| 64 | std::cout << "Objective function value: " << mip.solValue() << std::endl; |
---|
| 65 | std::cout << "x1 = " << mip.sol(x1) << std::endl; |
---|
| 66 | std::cout << "x2 = " << mip.sol(x2) << std::endl; |
---|
| 67 | } else { |
---|
| 68 | std::cout << "Optimal solution not found." << std::endl; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | return 0; |
---|
| 72 | } |
---|