首页 > 解决方案 > 将元组中的集合传输到 CPLEX 中的数组?

问题描述

{route} Routes ={
<1,{1,3},{8,5}>,
<2,{2,3},{7,9}>
};

我可以将两个元组中的最后一组提取为二维数组,并将第一个元组作为索引Routes吗?如果索引未显示在第一个矩阵中,则在矩阵中将其设为 0

意思是,

Arr=[
[8,0,5],
[0,7,9]
]

标签: arraysalgorithmtuplescplexopl

解决方案


tuple route
{
key int r;
{int} s1;
{int} s2;
}

{route} Routes ={
<1,{1,3},{8,5}>,
<2,{2,3},{7,9}>
};

sorted {int} indexes1={i.r | i in Routes};
sorted {int} indexes2=union (i in Routes) i.s1;

int res[i in indexes1][j in indexes2]=(j in item(Routes,<i>).s1)
    ?item(item(Routes,<i>).s2,ord(item(Routes,<i>).s1,j))
    :0;

execute
{
writeln(indexes1);
writeln(indexes2);
writeln(res);
} 

{1 2}
 {1 2 3}
 [[8 0 5]
         [0 7 9]]

推荐阅读