Complete Program: ilolpex7.cpp

The complete program, ilolpex7.cpp, appears here or online in the standard distribution.

#include <ilcplex/ilocplex.h>
ILOSTLBEGIN

static void usage (const char *progname);

int
main (int argc, char **argv)
{
   IloEnv env;
   try {
      IloModel model(env);
      IloCplex cplex(env);

      if (( argc != 3 )                             ||
          ( strchr ("podthbn", argv[2][0]) == NULL )  ) {
         usage (argv[0]);
         throw(-1);
      }

      switch (argv[2][0]) {
         case 'o':
            break;
         case 'p':
            cplex.setRootAlgorithm(IloCplex::Primal);
            break;
         case 'd':
            cplex.setRootAlgorithm(IloCplex::Dual);
            break;
         case 'b':
            cplex.setRootAlgorithm(IloCplex::Barrier);
            cplex.setParam(IloCplex::BarCrossAlg, IloCplex::NoAlg);
            break;
         case 'h':
            cplex.setRootAlgorithm(IloCplex::Barrier);
            break;
         case 'n':
            cplex.setRootAlgorithm(IloCplex::NetworkDual);
            break;
         default:
            break;
      }

      IloObjective   obj;
      IloNumVarArray var(env);
      IloRangeArray  rng(env);
      cplex.importModel(model, argv[1], obj, var, rng);

      cplex.extract(model);
      if ( !cplex.solve() ) {
         env.error() << "Failed to optimize LP" << endl;
         throw(-1);
      }

      env.out() << "Solution status = " << cplex.getStatus() << endl;
      env.out() << "Solution value  = " << cplex.getObjValue() << endl;

      for (IloInt i = 0; i < var.getSize(); ++i) {
         if ( var[i].getName() ) env.out() << var[i].getName();
         else                    env.out() << "Fake" << i;
         env.out() << ": " << cplex.getValue(var[i]);
         try {  // basis may not exist
            env.out() << '\t' << cplex.getStatus(var[i]);
         } catch (...) {
         }
         env.out() << endl;
      }
   }
   catch (IloException& e) {
      cerr << "Concert exception caught: " << e << endl;
   }
   catch (...) {
      cerr << "Unknown exception caught" << endl;
   }

   env.end();
   return 0;
}  // END main


static void usage (const char *progname)
{
   cerr << "Usage: " << progname << " filename algorithm" << endl;
   cerr << "   where filename is a file with extension " << endl;
   cerr << "      MPS, SAV, or LP (lower case is allowed)" << endl;
   cerr << "   and algorithm is one of the letters" << endl;
   cerr << "      o          default" << endl;
   cerr << "      p          primal simplex" << endl;
   cerr << "      d          dual simplex" << endl;
   cerr << "      b          barrier" << endl;
   cerr << "      h          barrier with crossover" << endl;
   cerr << "      n          network simplex" << endl;
   cerr << " Exiting..." << endl;
} // END usage

This example uses the ILOG CPLEX Callable Library query routine CPXgetcolname() to get the column names from a problem object. To do so, it applies the programming pattern we just described in Using Surplus Arguments for Array Allocations. It derives from the example lpex2.c, explained in the manual ILOG CPLEX Getting Started manual. This query-routine example differs from that simpler example in several ways:

This example assumes that the current problem has been read from a file by CPXreadcopyprob(). You can adapt the example to use other ILOG CPLEX query routines to get information about any problem read from a file.


Previous Page: Example: Using Query Routines  Return to Top Next Page: Complete Program: lpex7.c