alpar@1362: #include alpar@1381: alpar@1381: alpar@1381: #ifdef HAVE_GLPK alpar@1362: #include alpar@1381: #elif HAVE_CPLEX alpar@1381: #include alpar@1381: #endif alpar@1381: alpar@1362: using namespace lemon; alpar@1362: alpar@1381: #ifdef HAVE_GLPK alpar@1381: typedef LpGlpk LpDefault; alpar@1381: #elif HAVE_CPLEX alpar@1381: typedef LpCplex LpDefault; alpar@1381: #endif alpar@1381: alpar@1362: int main() alpar@1362: { alpar@1362: //The following example is taken from the documentation of the GLPK library. alpar@1362: //See it in the GLPK reference manual and among the GLPK sample files (sample.c) alpar@1381: LpDefault lp; alpar@1381: typedef LpDefault::Row Row; alpar@1381: typedef LpDefault::Col Col; alpar@1362: alpar@1362: lp.max(); alpar@1362: alpar@1362: Col x1 = lp.addCol(); alpar@1362: Col x2 = lp.addCol(); alpar@1362: Col x3 = lp.addCol(); alpar@1362: alpar@1362: //One solution alpar@1362: // Row p = lp.addRow(); alpar@1362: // Row q = lp.addRow(); alpar@1362: // Row r = lp.addRow(); alpar@1362: // lp.setRow(p,x1+x2+x3 <=100); alpar@1362: // lp.setRow(q,10*x1+4*x2+5*x3<=600); alpar@1362: // lp.setRow(r,2*x1+2*x2+6*x3<=300); alpar@1362: alpar@1362: //A more elegant one alpar@1362: //Constraints alpar@1362: lp.addRow(x1+x2+x3 <=100); alpar@1362: lp.addRow(10*x1+4*x2+5*x3<=600); alpar@1362: lp.addRow(2*x1+2*x2+6*x3<=300); alpar@1362: //Nonnegativity of the variables alpar@1362: lp.colLowerBound(x1, 0); alpar@1362: lp.colLowerBound(x2, 0); alpar@1362: lp.colLowerBound(x3, 0); alpar@1362: //Objective function alpar@1362: lp.setObj(10*x1+6*x2+4*x3); alpar@1362: alpar@1362: lp.solve(); alpar@1362: alpar@1362: if (lp.primalStatus()==LpSolverBase::OPTIMAL){ alpar@1362: printf("Z = %g; x1 = %g; x2 = %g; x3 = %g\n", alpar@1362: lp.primalValue(), alpar@1362: lp.primal(x1), lp.primal(x2), lp.primal(x3)); alpar@1362: } alpar@1362: else{ alpar@1362: std::cout<<"Optimal solution not found!"<