demo/mip_demo.cc
changeset 54 e99a7fb6bff5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/demo/mip_demo.cc	Mon Mar 01 02:26:24 2010 +0100
     1.3 @@ -0,0 +1,72 @@
     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 MIP solver interface.
    1.24 +///
    1.25 +/// This demo program shows how the LEMON MIP solver interface can be used.
    1.26 +/// A simple mixed integer programming (MIP) problem is formulated and solved
    1.27 +/// using the default MIP solver (e.g. GLPK).
    1.28 +///
    1.29 +/// \include mip_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 MIP solver class
    1.39 +  // (it will represent an "empty" problem at first)
    1.40 +  Mip mip;
    1.41 +
    1.42 +  // Add two columns (variables) to the problem
    1.43 +  Mip::Col x1 = mip.addCol();
    1.44 +  Mip::Col x2 = mip.addCol();
    1.45 +
    1.46 +  // Add rows (constraints) to the problem
    1.47 +  mip.addRow(x1 - 5 <= x2);
    1.48 +  mip.addRow(0 <= 2 * x1 + x2 <= 25);
    1.49 +  
    1.50 +  // Set lower and upper bounds for the columns (variables)
    1.51 +  mip.colLowerBound(x1, 0);
    1.52 +  mip.colUpperBound(x2, 10);
    1.53 +  
    1.54 +  // Set the type of the columns
    1.55 +  mip.colType(x1, Mip::INTEGER);
    1.56 +  mip.colType(x2, Mip::REAL);
    1.57 +  
    1.58 +  // Specify the objective function
    1.59 +  mip.max();
    1.60 +  mip.obj(5 * x1 + 3 * x2);
    1.61 +  
    1.62 +  // Solve the problem using the underlying MIP solver
    1.63 +  mip.solve();
    1.64 +
    1.65 +  // Print the results
    1.66 +  if (mip.type() == Mip::OPTIMAL) {
    1.67 +    std::cout << "Objective function value: " << mip.solValue() << std::endl;
    1.68 +    std::cout << "x1 = " << mip.sol(x1) << std::endl;
    1.69 +    std::cout << "x2 = " << mip.sol(x2) << std::endl;
    1.70 +  } else {
    1.71 +    std::cout << "Optimal solution not found." << std::endl;
    1.72 +  }
    1.73 +
    1.74 +  return 0;
    1.75 +}