如何在opl Cplex中查找集合中元素的数量

问题描述 投票:0回答:1

假设我有一个随时间变化的集合。我想确定每个时期集合中元素的数量。如何在opl CPLEX中完成上述问题的代码。随时间变化的集合在下面的元组中给出。

tuple TimeVaryingSet {
int Slot;
{int} values;
}
TimeVaryingSet Damagedlines[Slot] = [
<1,{30,67,17,54,12,49,11,48}>,
<2,{30,67,17,54,12,49,11,48}>,
<3,{17,54,12,49,11,48}>,
<4,{17,54,12,49,11,48}>,
<5,{17,54,12,49,11,48}>,
<6,{12,49,11,48}>,
<7,{11,48}>,
<8,{}>
];

现在我想创建一个数组,其中包含每个周期的集合元素数量。这里我考虑了8个时间段。如何在 opl cplex 中对上述问题进行编码

optimization set cplex
1个回答
0
投票

可以使用“卡”

range Slot=1..8;
tuple TimeVaryingSet {
int Slot;
{int} values;
}
TimeVaryingSet Damagedlines[Slot] = [
<1,{30,67,17,54,12,49,11,48}>,
<2,{30,67,17,54,12,49,11,48}>,
<3,{17,54,12,49,11,48}>,
<4,{17,54,12,49,11,48}>,
<5,{17,54,12,49,11,48}>,
<6,{12,49,11,48}>,
<7,{11,48}>,
<8,{}>
];

int nb[s in Slot]=card(Damagedlines[s].values);

execute
{
  writeln(nb);
}

给予

 [8 8 6 6 6 4 2 0]
© www.soinside.com 2019 - 2024. All rights reserved.