src/demo/lp_demo.cc
changeset 1435 8e85e6bbefdf
parent 1381 998e8def9676
equal deleted inserted replaced
5:68c245239a71 -1:000000000000
     1 #ifdef HAVE_CONFIG_H
       
     2 #include <config.h>
       
     3 #endif
       
     4 
       
     5 #include <iostream>
       
     6 
       
     7 
       
     8 #ifdef HAVE_GLPK
       
     9 #include <lemon/lp_glpk.h>
       
    10 #elif HAVE_CPLEX
       
    11 #include <lemon/lp_cplex.h>
       
    12 #endif
       
    13 
       
    14 using namespace lemon;
       
    15 
       
    16 #ifdef HAVE_GLPK
       
    17 typedef LpGlpk LpDefault;
       
    18 #elif HAVE_CPLEX
       
    19 typedef LpCplex LpDefault;
       
    20 #endif
       
    21 
       
    22 int main()
       
    23 {     
       
    24  //The following example is taken from the documentation of the GLPK library.
       
    25  //See it in the GLPK reference manual and among the GLPK sample files (sample.c)
       
    26   LpDefault lp;
       
    27   typedef LpDefault::Row Row;
       
    28   typedef LpDefault::Col Col;
       
    29 
       
    30   lp.max();
       
    31 
       
    32   Col x1 = lp.addCol();
       
    33   Col x2 = lp.addCol();
       
    34   Col x3 = lp.addCol();
       
    35 
       
    36   //One solution
       
    37   //   Row p = lp.addRow();
       
    38   //   Row q = lp.addRow();
       
    39   //   Row r = lp.addRow();
       
    40   //   lp.setRow(p,x1+x2+x3 <=100);  
       
    41   //   lp.setRow(q,10*x1+4*x2+5*x3<=600);  
       
    42   //   lp.setRow(r,2*x1+2*x2+6*x3<=300);  
       
    43 
       
    44   //A more elegant one
       
    45   //Constraints
       
    46   lp.addRow(x1+x2+x3 <=100);  
       
    47   lp.addRow(10*x1+4*x2+5*x3<=600);  
       
    48   lp.addRow(2*x1+2*x2+6*x3<=300);  
       
    49   //Nonnegativity of the variables
       
    50   lp.colLowerBound(x1, 0);
       
    51   lp.colLowerBound(x2, 0);
       
    52   lp.colLowerBound(x3, 0);
       
    53   //Objective function
       
    54   lp.setObj(10*x1+6*x2+4*x3);
       
    55   
       
    56   lp.solve();
       
    57 
       
    58   if (lp.primalStatus()==LpSolverBase::OPTIMAL){
       
    59     printf("Z = %g; x1 = %g; x2 = %g; x3 = %g\n", 
       
    60 	   lp.primalValue(), 
       
    61 	   lp.primal(x1), lp.primal(x2), lp.primal(x3));
       
    62   }
       
    63   else{
       
    64     std::cout<<"Optimal solution not found!"<<std::endl;
       
    65   }
       
    66 
       
    67 
       
    68   //Here comes the same problem written in C using GLPK API routines
       
    69 
       
    70 //   LPX *lp;
       
    71 //       int ia[1+1000], ja[1+1000];
       
    72 //       double ar[1+1000], Z, x1, x2, x3;
       
    73 // s1:   lp = lpx_create_prob();
       
    74 // s2:   lpx_set_prob_name(lp, "sample");
       
    75 // s3:   lpx_set_obj_dir(lp, LPX_MAX);
       
    76 // s4:   lpx_add_rows(lp, 3);
       
    77 // s5:   lpx_set_row_name(lp, 1, "p");
       
    78 // s6:   lpx_set_row_bnds(lp, 1, LPX_UP, 0.0, 100.0);
       
    79 // s7:   lpx_set_row_name(lp, 2, "q");
       
    80 // s8:   lpx_set_row_bnds(lp, 2, LPX_UP, 0.0, 600.0);
       
    81 // s9:   lpx_set_row_name(lp, 3, "r");
       
    82 // s10:  lpx_set_row_bnds(lp, 3, LPX_UP, 0.0, 300.0);
       
    83 // s11:  lpx_add_cols(lp, 3);
       
    84 // s12:  lpx_set_col_name(lp, 1, "x1");
       
    85 // s13:  lpx_set_col_bnds(lp, 1, LPX_LO, 0.0, 0.0);
       
    86 // s14:  lpx_set_obj_coef(lp, 1, 10.0);
       
    87 // s15:  lpx_set_col_name(lp, 2, "x2");
       
    88 // s16:  lpx_set_col_bnds(lp, 2, LPX_LO, 0.0, 0.0);
       
    89 // s17:  lpx_set_obj_coef(lp, 2, 6.0);
       
    90 // s18:  lpx_set_col_name(lp, 3, "x3");
       
    91 // s19:  lpx_set_col_bnds(lp, 3, LPX_LO, 0.0, 0.0);
       
    92 // s20:  lpx_set_obj_coef(lp, 3, 4.0);
       
    93 // s21:  ia[1] = 1, ja[1] = 1, ar[1] =  1.0; /* a[1,1] =  1 */
       
    94 // s22:  ia[2] = 1, ja[2] = 2, ar[2] =  1.0; /* a[1,2] =  1 */
       
    95 // s23:  ia[3] = 1, ja[3] = 3, ar[3] =  1.0; /* a[1,3] =  1 */
       
    96 // s24:  ia[4] = 2, ja[4] = 1, ar[4] = 10.0; /* a[2,1] = 10 */
       
    97 // s25:  ia[5] = 3, ja[5] = 1, ar[5] =  2.0; /* a[3,1] =  2 */
       
    98 // s26:  ia[6] = 2, ja[6] = 2, ar[6] =  4.0; /* a[2,2] =  4 */
       
    99 // s27:  ia[7] = 3, ja[7] = 2, ar[7] =  2.0; /* a[3,2] =  2 */
       
   100 // s28:  ia[8] = 2, ja[8] = 3, ar[8] =  5.0; /* a[2,3] =  5 */
       
   101 // s29:  ia[9] = 3, ja[9] = 3, ar[9] =  6.0; /* a[3,3] =  6 */
       
   102 // s30:  lpx_load_matrix(lp, 9, ia, ja, ar);
       
   103 // s31:  lpx_simplex(lp);
       
   104 // s32:  Z = lpx_get_obj_val(lp);
       
   105 // s33:  x1 = lpx_get_col_prim(lp, 1);
       
   106 // s34:  x2 = lpx_get_col_prim(lp, 2);
       
   107 // s35:  x3 = lpx_get_col_prim(lp, 3);
       
   108 // s36:  printf("\nZ = %g; x1 = %g; x2 = %g; x3 = %g\n", Z, x1, x2, x3);
       
   109 // s37:  lpx_delete_prob(lp);
       
   110 //       return 0;
       
   111 
       
   112   return 0;
       
   113 }