demo/mip_demo.cc
author deba
Wed, 24 Oct 2007 16:31:49 +0000
changeset 2502 9c23c3762bc5
parent 2391 14a343be7a5a
child 2553 bfced05fa852
permissions -rw-r--r--
BpUGraphReader and Writer
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@2391
     5
 * Copyright (C) 2003-2007
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
deba@2492
    20
///\ingroup demos
deba@2492
    21
///\file
deba@2492
    22
///\brief Mixed integer program solver demo
deba@2492
    23
///
deba@2492
    24
/// This example shows how can we solve an integer program with lemon
deba@2492
    25
/// \c Mip interface and with default solver.
deba@2492
    26
///
deba@2492
    27
/// \include mip_demo.cc
deba@2492
    28
athos@2146
    29
#include <lemon/lp.h>
athos@2146
    30
athos@2146
    31
using namespace lemon;
athos@2146
    32
athos@2146
    33
int main(){
athos@2146
    34
athos@2146
    35
   Mip ilp;
athos@2146
    36
athos@2148
    37
athos@2146
    38
  typedef Mip::Row Row;
athos@2146
    39
  typedef Mip::Col Col;
athos@2146
    40
  
athos@2146
    41
  ilp.max();
athos@2146
    42
  
athos@2146
    43
  Col x1 = ilp.addCol();
athos@2146
    44
  Col x2 = ilp.addCol();
athos@2146
    45
  Col x3 = ilp.addCol();
athos@2146
    46
  
athos@2146
    47
  ilp.integer(x1,true);
athos@2146
    48
  ilp.integer(x2,true);
athos@2146
    49
  ilp.integer(x3,true);
athos@2146
    50
  
athos@2146
    51
  ilp.addRow(x1+x2+x3 <=100);  
athos@2146
    52
  ilp.addRow(10*x1+4*x2+5*x3<=600);  
athos@2146
    53
  ilp.addRow(2*x1+2*x2+6*x3<=300); 
athos@2146
    54
  
athos@2146
    55
  ilp.colLowerBound(x1, 0);
athos@2146
    56
  ilp.colLowerBound(x2, 0);
athos@2146
    57
  ilp.colLowerBound(x3, 0);
athos@2146
    58
  //Objective function
deba@2369
    59
  ilp.obj(10*x1+6*x2+4*x3);
athos@2146
    60
  
athos@2146
    61
  //Call the routine of the underlying LP solver
athos@2146
    62
  ilp.solve();
athos@2146
    63
  
athos@2146
    64
  //Print results
athos@2146
    65
  if (ilp.primalStatus()==LpSolverBase::OPTIMAL){
athos@2146
    66
    std::cout<<"Optimal solution found!"<<std::endl;
athos@2146
    67
    printf("optimum value = %g; x1 = %g; x2 = %g; x3 = %g\n", 
athos@2146
    68
           ilp.primalValue(), 
athos@2146
    69
           ilp.primal(x1), ilp.primal(x2), ilp.primal(x3));
athos@2146
    70
  }
athos@2146
    71
  else{
athos@2146
    72
    std::cout<<"Optimal solution not found!"<<std::endl;
athos@2146
    73
  }
athos@2146
    74
athos@2146
    75
}