I have non-consecutive values in sets/indices in my model. may I know how to write it in a general way in c++?
Example: Suppose M is the set of all nodes, and N is the set of all nodes that available. Let M={1,2,3,4,5} where N={1,2,4}. In cplex studio or AMPL you can simply write the constraint x[1]+x[2]+x[4] >=2 as
sum_{i in N} x[i] >= 2
but how can I write the same constraint in C++ in a general form? I know we can write an array of variables as
for (int i = 0; i < M; ++i)
total += x[i];
but what if we only need a part of the indices in a set?
Thanks
in N is the array with NN values then you can simply write
for (int i = 0; i < NN; ++i)
total += x[N[i]];
But what you could also do if you need to use c++ is to write your model in OPL and then call your model from C++ with the concert C++ OPL API
Many examples in CPLEX_Studio1210\\opl\\examples\\opl_interfaces\\cpp
It's C++. You have full control. If you have your N values in an array, a list, a set or whatever structure you like, then you can use the C++ standard iterators or similar to walk over that sequence of values, and just add the corresponding variables into a cplex expression. It's just software. There is nothing cplex-specific in this and there never should be. You have all the power of the usual C++ structures and enumerators/iterators available.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.