Sample file completed: works correctly and the code is very beautiful. I love LEMON.
authorathos
Thu, 07 Apr 2005 12:54:35 +0000
changeset 131888edb143a87a
parent 1317 83f80464f111
child 1319 6e277ba3fc76
Sample file completed: works correctly and the code is very beautiful. I love LEMON.
src/work/athos/lp/lp_sample.cc
     1.1 --- a/src/work/athos/lp/lp_sample.cc	Thu Apr 07 11:30:12 2005 +0000
     1.2 +++ b/src/work/athos/lp/lp_sample.cc	Thu Apr 07 12:54:35 2005 +0000
     1.3 @@ -1,15 +1,48 @@
     1.4 -//#include <stdio.h>
     1.5 -//#include <stdlib.h>
     1.6  #include <iostream>
     1.7  #include <lemon/lp_glpk.h>
     1.8  using namespace lemon;
     1.9  
    1.10  int main()
    1.11  {     
    1.12 + //The following example is taken from the documentation of the GLPK library.
    1.13 + //See it in the GLPK reference manual and among the GLPK sample files (sample.c)
    1.14    LpGlpk lp;
    1.15    typedef LpGlpk::Row Row;
    1.16    typedef LpGlpk::Col Col;
    1.17 -  //std::cout << "Hello" << std::endl;
    1.18 +
    1.19 +  lp.max();
    1.20 +
    1.21 +  Col x1 = lp.addCol();
    1.22 +  Col x2 = lp.addCol();
    1.23 +  Col x3 = lp.addCol();
    1.24 +
    1.25 +  //One solution
    1.26 +  //   Row p = lp.addRow();
    1.27 +  //   Row q = lp.addRow();
    1.28 +  //   Row r = lp.addRow();
    1.29 +  //   lp.setRow(p,x1+x2+x3 <=100);  
    1.30 +  //   lp.setRow(q,10*x1+4*x2+5*x3<=600);  
    1.31 +  //   lp.setRow(r,2*x1+2*x2+6*x3<=300);  
    1.32 +
    1.33 +  //A more elegant one
    1.34 +  //Constraints
    1.35 +  lp.addRow(x1+x2+x3 <=100);  
    1.36 +  lp.addRow(10*x1+4*x2+5*x3<=600);  
    1.37 +  lp.addRow(2*x1+2*x2+6*x3<=300);  
    1.38 +  //Nonnegativity of the variables
    1.39 +  lp.colLowerBound(x1, 0);
    1.40 +  lp.colLowerBound(x2, 0);
    1.41 +  lp.colLowerBound(x3, 0);
    1.42 +  //Objective function
    1.43 +  lp.setObj(10*x1+6*x2+4*x3);
    1.44 +  
    1.45 +  lp.solve();
    1.46 +  
    1.47 +  printf("\nZ = %g; x1 = %g; x2 = %g; x3 = %g\n", 
    1.48 +	 lp.primalValue(), 
    1.49 +	 lp.primal(x1), lp.primal(x2), lp.primal(x3));
    1.50 +
    1.51 +  //Here comes the same problem written in C using GLPK API routines
    1.52  
    1.53  //   LPX *lp;
    1.54  //       int ia[1+1000], ja[1+1000];
    1.55 @@ -17,29 +50,16 @@
    1.56  // s1:   lp = lpx_create_prob();
    1.57  // s2:   lpx_set_prob_name(lp, "sample");
    1.58  // s3:   lpx_set_obj_dir(lp, LPX_MAX);
    1.59 -  lp.max();
    1.60  // s4:   lpx_add_rows(lp, 3);
    1.61  // s5:   lpx_set_row_name(lp, 1, "p");
    1.62  // s6:   lpx_set_row_bnds(lp, 1, LPX_UP, 0.0, 100.0);
    1.63 -
    1.64 -//  Row p = lp.addRow();
    1.65 -
    1.66  // s7:   lpx_set_row_name(lp, 2, "q");
    1.67  // s8:   lpx_set_row_bnds(lp, 2, LPX_UP, 0.0, 600.0);
    1.68 -
    1.69 -//  Row q = lp.addRow();
    1.70 -
    1.71  // s9:   lpx_set_row_name(lp, 3, "r");
    1.72  // s10:  lpx_set_row_bnds(lp, 3, LPX_UP, 0.0, 300.0);
    1.73 -
    1.74 -//  Row r = lp.addRow();
    1.75 -
    1.76  // s11:  lpx_add_cols(lp, 3);
    1.77  // s12:  lpx_set_col_name(lp, 1, "x1");
    1.78  // s13:  lpx_set_col_bnds(lp, 1, LPX_LO, 0.0, 0.0);
    1.79 -
    1.80 -//  Col x1 = lp.addCol();
    1.81 -
    1.82  // s14:  lpx_set_obj_coef(lp, 1, 10.0);
    1.83  // s15:  lpx_set_col_name(lp, 2, "x2");
    1.84  // s16:  lpx_set_col_bnds(lp, 2, LPX_LO, 0.0, 0.0);
    1.85 @@ -65,5 +85,6 @@
    1.86  // s36:  printf("\nZ = %g; x1 = %g; x2 = %g; x3 = %g\n", Z, x1, x2, x3);
    1.87  // s37:  lpx_delete_prob(lp);
    1.88  //       return 0;
    1.89 +
    1.90    return 0;
    1.91  }