demo/lp_demo.cc
changeset 1605 4fa618f7aa69
parent 1530 d99c3c84f797
child 1610 893dacc1866c
equal deleted inserted replaced
2:bd54eafb223a 3:19ffa0923f6f
       
     1 /* -*- C++ -*-
       
     2  * demo/graph_to_eps.cc - Part of LEMON, a generic C++ optimization library
       
     3  *
       
     4  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
       
     5  * (Egervary Research Group on Combinatorial Optimization, EGRES).
       
     6  *
       
     7  * Permission to use, modify and distribute this software is granted
       
     8  * provided that this copyright notice appears in all copies. For
       
     9  * precise terms see the accompanying LICENSE file.
       
    10  *
       
    11  * This software is provided "AS IS" with no warranty of any kind,
       
    12  * express or implied, and with no claim as to its suitability for any
       
    13  * purpose.
       
    14  *
       
    15  */
       
    16 
       
    17 /// \ingroup demos
       
    18 /// \file
       
    19 /// \brief A program demonstrating the LEMON LP solver interface
       
    20 ///
       
    21 /// This program is a simple application of the LEMON LP solver
       
    22 /// interface: we formulate a linear programming (LP) problem and then
       
    23 /// solve it using the underlying solver (GLPK or CPLEX for
       
    24 /// example). For the detailed documentation of the LEMON LP solver
       
    25 /// interface read \ref lemon::LpSolverBase "this".
       
    26 
     1 #ifdef HAVE_CONFIG_H
    27 #ifdef HAVE_CONFIG_H
     2 #include <config.h>
    28 #include <config.h>
     3 #endif
    29 #endif
     4 
    30 
     5 #include <iostream>
    31 #include <iostream>
    11 #include <lemon/lp_cplex.h>
    37 #include <lemon/lp_cplex.h>
    12 #endif
    38 #endif
    13 
    39 
    14 using namespace lemon;
    40 using namespace lemon;
    15 
    41 
       
    42 
       
    43 
    16 #ifdef HAVE_GLPK
    44 #ifdef HAVE_GLPK
    17 typedef LpGlpk LpDefault;
    45 typedef LpGlpk LpDefault;
       
    46 const char default_solver_name[]="GLPK";
    18 #elif HAVE_CPLEX
    47 #elif HAVE_CPLEX
    19 typedef LpCplex LpDefault;
    48 typedef LpCplex LpDefault;
       
    49 const char default_solver_name[]="CPLEX";
    20 #endif
    50 #endif
    21 
    51 
    22 int main()
    52 int main()
    23 {     
    53 {     
    24  //The following example is taken from the documentation of the GLPK library.
    54  //The following example is taken from the documentation of the GLPK library.
    27   //A default solver is taken
    57   //A default solver is taken
    28   LpDefault lp;
    58   LpDefault lp;
    29   typedef LpDefault::Row Row;
    59   typedef LpDefault::Row Row;
    30   typedef LpDefault::Col Col;
    60   typedef LpDefault::Col Col;
    31   
    61   
       
    62 
       
    63   std::cout<<"A program demonstrating the LEMON LP solver interface"<<std::endl; 
       
    64   std::cout<<"Solver used: "<<default_solver_name<<std::endl;
    32 
    65 
    33   //This will be a maximization
    66   //This will be a maximization
    34   lp.max();
    67   lp.max();
    35 
    68 
    36   //We add coloumns (variables) to our problem
    69   //We add coloumns (variables) to our problem
    52   //Call the routine of the underlying LP solver
    85   //Call the routine of the underlying LP solver
    53   lp.solve();
    86   lp.solve();
    54 
    87 
    55   //Print results
    88   //Print results
    56   if (lp.primalStatus()==LpSolverBase::OPTIMAL){
    89   if (lp.primalStatus()==LpSolverBase::OPTIMAL){
    57     printf("Z = %g; x1 = %g; x2 = %g; x3 = %g\n", 
    90     std::cout<<"Optimal solution found!"<<std::endl;
       
    91     printf("optimum value = %g; x1 = %g; x2 = %g; x3 = %g\n", 
    58 	   lp.primalValue(), 
    92 	   lp.primalValue(), 
    59 	   lp.primal(x1), lp.primal(x2), lp.primal(x3));
    93 	   lp.primal(x1), lp.primal(x2), lp.primal(x3));
    60   }
    94   }
    61   else{
    95   else{
    62     std::cout<<"Optimal solution not found!"<<std::endl;
    96     std::cout<<"Optimal solution not found!"<<std::endl;