问题
给定一个 MiniZinc 字符串数组:
int: numStats;
set of int: Stats = 1..numStats;
array[Stats] of string: statNames;
...使用从 MiniZinc 数据文件加载的数据:
numStats = 3;
statNames = ["HEALTH", "ARMOR", "MANA"];
如何查找数组中特定字符串的索引?例如,ARMOR 位于位置 2。
上下文
我需要根据其统计数据的一些限制找到最佳的项目选择。该信息存储在声明如下的二维数组中:
int: numItems;
set of int: Items = 1..numItems;
array[Items, Stats] of float: itemStats;
因此,为了对通过所选项目获得的 ARMOR 的最小数量编写约束,我需要知道 ARMOR 在内部数组中的索引为 2。
由于数据文件是由外部程序生成的,并且统计数据的数量和顺序是动态的,因此我无法对约束中的索引进行硬编码。
一种解决方案(在我的情况下不起作用)
The 迷你锌教程使用一个有趣的技巧来实现类似的目标:
set of int: Colors = 1..3;
int: red = 1;
int: yellow = 2;
int: blue = 3;
array[Colors] of string: name = ["red", "yellow", "blue"];
var Colors: x;
constraint x != red;
output [ name[fix(x)] ];
不幸的是,由于 MiniZinc 数据文件中不允许变量声明,因此这个技巧在我的情况下不起作用。