demo/lp_demo.cc
changeset 1513 b2a79aaa6867
parent 1435 8e85e6bbefdf
child 1530 d99c3c84f797
equal deleted inserted replaced
0:ac879cf6e3fd 1:780f36c792b3
    24  //The following example is taken from the documentation of the GLPK library.
    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)
    25  //See it in the GLPK reference manual and among the GLPK sample files (sample.c)
    26   LpDefault lp;
    26   LpDefault lp;
    27   typedef LpDefault::Row Row;
    27   typedef LpDefault::Row Row;
    28   typedef LpDefault::Col Col;
    28   typedef LpDefault::Col Col;
       
    29   
    29 
    30 
       
    31   //This will be a maximization
    30   lp.max();
    32   lp.max();
    31 
    33 
       
    34   //We add coloumns (variables) to our problem
    32   Col x1 = lp.addCol();
    35   Col x1 = lp.addCol();
    33   Col x2 = lp.addCol();
    36   Col x2 = lp.addCol();
    34   Col x3 = lp.addCol();
    37   Col x3 = lp.addCol();
    35 
    38 
    36   //One solution
    39   //One solution
    51   lp.colLowerBound(x2, 0);
    54   lp.colLowerBound(x2, 0);
    52   lp.colLowerBound(x3, 0);
    55   lp.colLowerBound(x3, 0);
    53   //Objective function
    56   //Objective function
    54   lp.setObj(10*x1+6*x2+4*x3);
    57   lp.setObj(10*x1+6*x2+4*x3);
    55   
    58   
       
    59   //Call the routine of the underlying LP solver
    56   lp.solve();
    60   lp.solve();
    57 
    61 
       
    62   //Print results
    58   if (lp.primalStatus()==LpSolverBase::OPTIMAL){
    63   if (lp.primalStatus()==LpSolverBase::OPTIMAL){
    59     printf("Z = %g; x1 = %g; x2 = %g; x3 = %g\n", 
    64     printf("Z = %g; x1 = %g; x2 = %g; x3 = %g\n", 
    60 	   lp.primalValue(), 
    65 	   lp.primalValue(), 
    61 	   lp.primal(x1), lp.primal(x2), lp.primal(x3));
    66 	   lp.primal(x1), lp.primal(x2), lp.primal(x3));
    62   }
    67   }