简体   繁体   English

cplex C++ 中的集合、子集和索引

[英]set, subset and index in cplex c++

I have non-consecutive values in sets/indices in my model.我的模型中的集合/索引中有非连续值。 may I know how to write it in a general way in c++?我可以知道如何在 C++ 中以一般方式编写它吗?

Example: Suppose M is the set of all nodes, and N is the set of all nodes that available.示例:假设 M 是所有节点的集合,N 是所有可用节点的集合。 Let M={1,2,3,4,5} where N={1,2,4}.设 M={1,2,3,4,5},其中 N={1,2,4}。 In cplex studio or AMPL you can simply write the constraint x[1]+x[2]+x[4] >=2 as在 cplex studio 或 AMPL 中,您可以简单地将约束 x[1]+x[2]+x[4] >=2 写为

sum_{i in N} x[i] >= 2

but how can I write the same constraint in C++ in a general form?但是如何以通用形式在 C++ 中编写相同的约束? 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在 N 是具有 NN 值的数组,那么你可以简单地写

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但是,如果您需要使用 C++,您还可以做的是在 OPL 中编写您的模型,然后使用 Concert C++ OPL API 从 C++ 调用您的模型

Many examples in CPLEX_Studio1210\\opl\\examples\\opl_interfaces\\cpp CPLEX_Studio1210\\opl\\examples\\opl_interfaces\\cpp 中的许多示例

It's C++.它是 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.如果您在数组、列表、集合或任何您喜欢的结构中有 N 个值,那么您可以使用 C++ 标准迭代器或类似方法遍历该值序列,并将相应的变量添加到 cplex 表达式中。 It's just software.这只是软件。 There is nothing cplex-specific in this and there never should be.这里没有任何特定于 cplex 的东西,也不应该有。 You have all the power of the usual C++ structures and enumerators/iterators available.您拥有常用 C++ 结构和可用的枚举器/迭代器的所有功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM