demo/mip_demo.cc
author deba
Sat, 20 Dec 2008 22:45:48 +0000
changeset 2631 212ea31bf6b9
parent 2492 387f6ff851ef
permissions -rw-r--r--
Backport hg commit [52c04a2a652c] (ticket #197)
alpar@2391
     1
/* -*- C++ -*-
alpar@2391
     2
 *
alpar@2391
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@2391
     4
 *
alpar@2553
     5
 * Copyright (C) 2003-2008
alpar@2391
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@2391
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@2391
     8
 *
alpar@2391
     9
 * Permission to use, modify and distribute this software is granted
alpar@2391
    10
 * provided that this copyright notice appears in all copies. For
alpar@2391
    11
 * precise terms see the accompanying LICENSE file.
alpar@2391
    12
 *
alpar@2391
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@2391
    14
 * express or implied, and with no claim as to its suitability for any
alpar@2391
    15
 * purpose.
alpar@2391
    16
 *
alpar@2391
    17
 */
alpar@2391
    18
deba@2492
    19
///\ingroup demos
deba@2492
    20
///\file
deba@2492
    21
///\brief Mixed integer program solver demo
deba@2492
    22
///
deba@2492
    23
/// This example shows how can we solve an integer program with lemon
deba@2492
    24
/// \c Mip interface and with default solver.
deba@2492
    25
///
deba@2492
    26
/// \include mip_demo.cc
deba@2492
    27
athos@2146
    28
#include <lemon/lp.h>
athos@2146
    29
athos@2146
    30
using namespace lemon;
athos@2146
    31
athos@2146
    32
int main(){
athos@2146
    33
athos@2146
    34
   Mip ilp;
athos@2146
    35
athos@2148
    36
athos@2146
    37
  typedef Mip::Row Row;
athos@2146
    38
  typedef Mip::Col Col;
athos@2146
    39
  
athos@2146
    40
  ilp.max();
athos@2146
    41
  
athos@2146
    42
  Col x1 = ilp.addCol();
athos@2146
    43
  Col x2 = ilp.addCol();
athos@2146
    44
  Col x3 = ilp.addCol();
athos@2146
    45
  
athos@2146
    46
  ilp.integer(x1,true);
athos@2146
    47
  ilp.integer(x2,true);
athos@2146
    48
  ilp.integer(x3,true);
athos@2146
    49
  
athos@2146
    50
  ilp.addRow(x1+x2+x3 <=100);  
athos@2146
    51
  ilp.addRow(10*x1+4*x2+5*x3<=600);  
athos@2146
    52
  ilp.addRow(2*x1+2*x2+6*x3<=300); 
athos@2146
    53
  
athos@2146
    54
  ilp.colLowerBound(x1, 0);
athos@2146
    55
  ilp.colLowerBound(x2, 0);
athos@2146
    56
  ilp.colLowerBound(x3, 0);
athos@2146
    57
  //Objective function
deba@2369
    58
  ilp.obj(10*x1+6*x2+4*x3);
athos@2146
    59
  
athos@2146
    60
  //Call the routine of the underlying LP solver
athos@2146
    61
  ilp.solve();
athos@2146
    62
  
athos@2146
    63
  //Print results
athos@2146
    64
  if (ilp.primalStatus()==LpSolverBase::OPTIMAL){
athos@2146
    65
    std::cout<<"Optimal solution found!"<<std::endl;
athos@2146
    66
    printf("optimum value = %g; x1 = %g; x2 = %g; x3 = %g\n", 
athos@2146
    67
           ilp.primalValue(), 
athos@2146
    68
           ilp.primal(x1), ilp.primal(x2), ilp.primal(x3));
athos@2146
    69
  }
athos@2146
    70
  else{
athos@2146
    71
    std::cout<<"Optimal solution not found!"<<std::endl;
athos@2146
    72
  }
athos@2146
    73
athos@2146
    74
}