c++ - 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,
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++ 中,数组由 0 索引。因此,可以通过
arr[0]
to访问 N 元素数组arr[N-1]
。当你写:
for (ss = 0; ss < NumSem; s++) {
mod.add(Y[ss+1] <= Y[ss]);
}
您正在访问数组中的第 (N+1) 个元素(因为ss
will be (NumSem - 1)
,因此ss+1
is NumSem
),这是访问冲突。
您必须将访问限制在数组的范围内。
推荐阅读
- fortran - 逆矩阵的错误结果
- jquery - 函数返回零如何解决这个问题?
- python - 如何增加字符串中间的数字?
- java - 您可以在某个步骤中同时使用@Given 和@And 吗?
- javascript - 计算类似于excel的数字数组的颜色代码
- nginx - 如何使用 try_files 绕过 404 HTTP 错误
- excel - 通过VBA在Excel中输入内容后锁定范围内的单元格
- c++ - C ++获取指定线程的堆栈跟踪
- java - 如何使用 navasmdc/MaterialDesignLibrary?
- sql-server-2017 - 如果表有多个 FOR INSERT 触发器,其中一个写入审计表,另一个执行回滚,则审计回滚