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