首页 > 解决方案 > IloBoolVarArray syntax help needed

问题描述

i am trying to solve LP using CPLEX in C++. i am little bit confused with the syntax. here is my problem. i have defined the integer constant like this:

const int NumberOfSemesters = 10

and defined one decision variable in CPLEX like this:

IloBoolVarArray Y(env, NumberOfSemesters); // equals to 1 if student 
                                          //takes at least one course in semster s

this decision variable output will be arrays of 1 and 0(i.e: [0,0,1,0,0,1])

then i defined one constraint such that:

//Constraint 1:student has no leave of absence     
for (ss = 0; ss < NumberOfSemesters; ss++) {
    mod.add(Y[ss + 1] <= Y[ss]);
}

now when i run the code i get this error which i do not underestand, enter image description here

i think the way i defined decision variable is wrong.i read the IBM website for IloBoolVarArray but the syntax confused me. any idea? or is anyone knows a source for learning CPLEX syntax in C++ with examples other than IBM website?

标签: c++cplex

解决方案


在 C++ 中,数组由 0 索引。因此,可以通过 arr[0]to访问 N 元素数组arr[N-1]。当你写:

for (ss = 0; ss < NumSem; s++) {
  mod.add(Y[ss+1] <= Y[ss]);
}

您正在访问数组中的第 (N+1) 个元素(因为sswill be (NumSem - 1),因此ss+1is NumSem),这是访问冲突。

您必须将访问限制在数组的范围内。


推荐阅读