- Adding new lp_demo.cc finished
authoralpar
Fri, 15 Apr 2005 21:15:30 +0000
changeset 1362b4330c52caeb
parent 1361 04733359bac2
child 1363 a32f990b73d9
- Adding new lp_demo.cc finished
- Several 'unused variable' warnings fixed in 'lp_skeleton.cc'
src/demo/Makefile.am
src/demo/lp_demo.cc
src/lemon/lp_skeleton.cc
     1.1 --- a/src/demo/Makefile.am	Fri Apr 15 20:46:18 2005 +0000
     1.2 +++ b/src/demo/Makefile.am	Fri Apr 15 21:15:30 2005 +0000
     1.3 @@ -30,6 +30,6 @@
     1.4  lp_demo_CXXFLAGS = $(GLPK_CFLAGS)
     1.5  lp_demo_LDFLAGS = $(GLPK_LIBS)
     1.6  
     1.7 -lp_demo_SOURCES = lp_maxflow_demo.cc
     1.8 -lp_demo_CXXFLAGS = $(GLPK_CFLAGS)
     1.9 -lp_demo_LDFLAGS = $(GLPK_LIBS)
    1.10 +lp_maxflow_demo_SOURCES = lp_maxflow_demo.cc
    1.11 +lp_maxflow_demo_CXXFLAGS = $(GLPK_CFLAGS)
    1.12 +lp_maxflow_demo_LDFLAGS = $(GLPK_LIBS)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/demo/lp_demo.cc	Fri Apr 15 21:15:30 2005 +0000
     2.3 @@ -0,0 +1,96 @@
     2.4 +#include <iostream>
     2.5 +#include <lemon/lp_glpk.h>
     2.6 +using namespace lemon;
     2.7 +
     2.8 +int main()
     2.9 +{     
    2.10 + //The following example is taken from the documentation of the GLPK library.
    2.11 + //See it in the GLPK reference manual and among the GLPK sample files (sample.c)
    2.12 +  LpGlpk lp;
    2.13 +  typedef LpGlpk::Row Row;
    2.14 +  typedef LpGlpk::Col Col;
    2.15 +
    2.16 +  lp.max();
    2.17 +
    2.18 +  Col x1 = lp.addCol();
    2.19 +  Col x2 = lp.addCol();
    2.20 +  Col x3 = lp.addCol();
    2.21 +
    2.22 +  //One solution
    2.23 +  //   Row p = lp.addRow();
    2.24 +  //   Row q = lp.addRow();
    2.25 +  //   Row r = lp.addRow();
    2.26 +  //   lp.setRow(p,x1+x2+x3 <=100);  
    2.27 +  //   lp.setRow(q,10*x1+4*x2+5*x3<=600);  
    2.28 +  //   lp.setRow(r,2*x1+2*x2+6*x3<=300);  
    2.29 +
    2.30 +  //A more elegant one
    2.31 +  //Constraints
    2.32 +  lp.addRow(x1+x2+x3 <=100);  
    2.33 +  lp.addRow(10*x1+4*x2+5*x3<=600);  
    2.34 +  lp.addRow(2*x1+2*x2+6*x3<=300);  
    2.35 +  //Nonnegativity of the variables
    2.36 +  lp.colLowerBound(x1, 0);
    2.37 +  lp.colLowerBound(x2, 0);
    2.38 +  lp.colLowerBound(x3, 0);
    2.39 +  //Objective function
    2.40 +  lp.setObj(10*x1+6*x2+4*x3);
    2.41 +  
    2.42 +  lp.solve();
    2.43 +
    2.44 +  if (lp.primalStatus()==LpSolverBase::OPTIMAL){
    2.45 +    printf("Z = %g; x1 = %g; x2 = %g; x3 = %g\n", 
    2.46 +	   lp.primalValue(), 
    2.47 +	   lp.primal(x1), lp.primal(x2), lp.primal(x3));
    2.48 +  }
    2.49 +  else{
    2.50 +    std::cout<<"Optimal solution not found!"<<std::endl;
    2.51 +  }
    2.52 +
    2.53 +
    2.54 +  //Here comes the same problem written in C using GLPK API routines
    2.55 +
    2.56 +//   LPX *lp;
    2.57 +//       int ia[1+1000], ja[1+1000];
    2.58 +//       double ar[1+1000], Z, x1, x2, x3;
    2.59 +// s1:   lp = lpx_create_prob();
    2.60 +// s2:   lpx_set_prob_name(lp, "sample");
    2.61 +// s3:   lpx_set_obj_dir(lp, LPX_MAX);
    2.62 +// s4:   lpx_add_rows(lp, 3);
    2.63 +// s5:   lpx_set_row_name(lp, 1, "p");
    2.64 +// s6:   lpx_set_row_bnds(lp, 1, LPX_UP, 0.0, 100.0);
    2.65 +// s7:   lpx_set_row_name(lp, 2, "q");
    2.66 +// s8:   lpx_set_row_bnds(lp, 2, LPX_UP, 0.0, 600.0);
    2.67 +// s9:   lpx_set_row_name(lp, 3, "r");
    2.68 +// s10:  lpx_set_row_bnds(lp, 3, LPX_UP, 0.0, 300.0);
    2.69 +// s11:  lpx_add_cols(lp, 3);
    2.70 +// s12:  lpx_set_col_name(lp, 1, "x1");
    2.71 +// s13:  lpx_set_col_bnds(lp, 1, LPX_LO, 0.0, 0.0);
    2.72 +// s14:  lpx_set_obj_coef(lp, 1, 10.0);
    2.73 +// s15:  lpx_set_col_name(lp, 2, "x2");
    2.74 +// s16:  lpx_set_col_bnds(lp, 2, LPX_LO, 0.0, 0.0);
    2.75 +// s17:  lpx_set_obj_coef(lp, 2, 6.0);
    2.76 +// s18:  lpx_set_col_name(lp, 3, "x3");
    2.77 +// s19:  lpx_set_col_bnds(lp, 3, LPX_LO, 0.0, 0.0);
    2.78 +// s20:  lpx_set_obj_coef(lp, 3, 4.0);
    2.79 +// s21:  ia[1] = 1, ja[1] = 1, ar[1] =  1.0; /* a[1,1] =  1 */
    2.80 +// s22:  ia[2] = 1, ja[2] = 2, ar[2] =  1.0; /* a[1,2] =  1 */
    2.81 +// s23:  ia[3] = 1, ja[3] = 3, ar[3] =  1.0; /* a[1,3] =  1 */
    2.82 +// s24:  ia[4] = 2, ja[4] = 1, ar[4] = 10.0; /* a[2,1] = 10 */
    2.83 +// s25:  ia[5] = 3, ja[5] = 1, ar[5] =  2.0; /* a[3,1] =  2 */
    2.84 +// s26:  ia[6] = 2, ja[6] = 2, ar[6] =  4.0; /* a[2,2] =  4 */
    2.85 +// s27:  ia[7] = 3, ja[7] = 2, ar[7] =  2.0; /* a[3,2] =  2 */
    2.86 +// s28:  ia[8] = 2, ja[8] = 3, ar[8] =  5.0; /* a[2,3] =  5 */
    2.87 +// s29:  ia[9] = 3, ja[9] = 3, ar[9] =  6.0; /* a[3,3] =  6 */
    2.88 +// s30:  lpx_load_matrix(lp, 9, ia, ja, ar);
    2.89 +// s31:  lpx_simplex(lp);
    2.90 +// s32:  Z = lpx_get_obj_val(lp);
    2.91 +// s33:  x1 = lpx_get_col_prim(lp, 1);
    2.92 +// s34:  x2 = lpx_get_col_prim(lp, 2);
    2.93 +// s35:  x3 = lpx_get_col_prim(lp, 3);
    2.94 +// s36:  printf("\nZ = %g; x1 = %g; x2 = %g; x3 = %g\n", Z, x1, x2, x3);
    2.95 +// s37:  lpx_delete_prob(lp);
    2.96 +//       return 0;
    2.97 +
    2.98 +  return 0;
    2.99 +}
     3.1 --- a/src/lemon/lp_skeleton.cc	Fri Apr 15 20:46:18 2005 +0000
     3.2 +++ b/src/lemon/lp_skeleton.cc	Fri Apr 15 21:15:30 2005 +0000
     3.3 @@ -31,37 +31,37 @@
     3.4      return ++row_num;
     3.5    }
     3.6    
     3.7 -  void LpSkeleton::_setRowCoeffs(int i, 
     3.8 -					       int length,
     3.9 -					       int  const * indices, 
    3.10 -					       Value  const * values )
    3.11 +  void LpSkeleton::_setRowCoeffs(int, 
    3.12 +				 int,
    3.13 +				 int  const *, 
    3.14 +				 Value  const *)
    3.15    {
    3.16    }
    3.17    
    3.18 -  void LpSkeleton::_setColCoeffs(int i, 
    3.19 -					       int length,
    3.20 -					       int  const * indices, 
    3.21 -					       Value  const * values)
    3.22 +  void LpSkeleton::_setColCoeffs(int, 
    3.23 +				 int,
    3.24 +				 int  const *, 
    3.25 +				 Value  const *)
    3.26    {
    3.27    }
    3.28    
    3.29 -  void LpSkeleton::_setColLowerBound(int i, Value value)
    3.30 +  void LpSkeleton::_setColLowerBound(int, Value)
    3.31    {
    3.32    }
    3.33    
    3.34 -  void LpSkeleton::_setColUpperBound(int i, Value value)
    3.35 +  void LpSkeleton::_setColUpperBound(int, Value)
    3.36    {
    3.37    }
    3.38    
    3.39 -  void LpSkeleton::_setRowLowerBound(int i, Value value)
    3.40 +  void LpSkeleton::_setRowLowerBound(int, Value)
    3.41    {
    3.42    }
    3.43    
    3.44 -  void LpSkeleton::_setRowUpperBound(int i, Value value)
    3.45 +  void LpSkeleton::_setRowUpperBound(int, Value)
    3.46    {
    3.47    }
    3.48    
    3.49 -  void LpSkeleton::_setObjCoeff(int i, Value obj_coef)
    3.50 +  void LpSkeleton::_setObjCoeff(int, Value)
    3.51    {
    3.52    }
    3.53  
    3.54 @@ -77,7 +77,7 @@
    3.55      return SOLVED;
    3.56    }
    3.57  
    3.58 -  LpSkeleton::Value LpSkeleton::_getPrimal(int i)
    3.59 +  LpSkeleton::Value LpSkeleton::_getPrimal(int)
    3.60    {
    3.61      return 0;
    3.62    }