Post

cplex

  1. include opl/bin folder to the path.
  2. After adding main source file, include the opl include directories.
  3. Link the lib folder of the opl directory.
  4. In linker->input add the following lib files to the “additional dependencies field” concert.lib cp.lib cplex2210.lib iljs.lib ilocplex.lib opl.lib icuuc.lib icuin.lib (these two for using optim.opl)

Framework for using Cplex api in c++

  1. firstly, define a Cplex Environment IloEnv
  2. Define an IloModel(IloEnv)
  3. Define sets as IloNumArray, two dimensional sets as IloArray<IloNumArray> and pass env to each of them
  4. If you wanna use .dat of opl ide, create a ifstream file, pointing to its location. Then use >> to add them into IloNumArray or IloArray<IloNumArray>. Note that you can’t use <,> inside your .dat file; just bare []
  5. Add IloNumVarArray
  6. Add constraints to model by using IloModel::add(IloExpr <= IloInt).
  7. after adding all constraints to IloMode, add the obj constraint as IloModel::add(IloMinimize(IloEnvironment,obj))
  8. Lastly, define an IloCplex mycplx(IloModel) and add mycplx.solve()
  9. For setting solve time limit, write: mycplex.setParam(IloCplex::Param::TimeLimit, 3600);

Useful Material

  • https://www-eio.upc.edu/lceio/manuals/cplex-11/html/refcppcplex/html/overview.html

Useful Tips:

  • Use c++ std::map for custom indexed arrays. (like int t[i][j] in Opl)
  • ALMOST ALL DATATYPES IN CPLEX CONCERT INHERIT FROM ILOARRAY. This class is a wrapper for something (referenced data like vector<int>) dynamically allocated on the heap. Wrapper means it’s a class containing the actual referenced data, not the data itself.
  • when passing any cplex container, remember that they are passed by reference, hence might get you in trouble once you IloArray::end() them. IloArray::copy() could be used for invocation instead. this Article
This post is licensed under CC BY 4.0 by the author.