cplex
First steps to add and link CPLEX files in visual studio:
- include
opl/binfolder to the path. - After adding main source file, include the opl include directories.
- Link the lib folder of the opl directory.
- In
linker->inputadd 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 usingoptim.opl)
Framework for using Cplex api in c++
- firstly, define a Cplex Environment
IloEnv - Define an
IloModel(IloEnv) - Define sets as
IloNumArray, two dimensional sets asIloArray<IloNumArray>and passenvto each of them - If you wanna use
.datof opl ide, create aifstreamfile, pointing to its location. Then use>>to add them intoIloNumArrayorIloArray<IloNumArray>. Note that you can’t use<,>inside your.datfile; just bare[] - Add
IloNumVarArray - Add constraints to model by using
IloModel::add(IloExpr <= IloInt). - after adding all constraints to
IloMode, add the obj constraint asIloModel::add(IloMinimize(IloEnvironment,obj)) - Lastly, define an
IloCplex mycplx(IloModel)and addmycplx.solve() - 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::mapfor custom indexed arrays. (likeint 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.