include "globals.mzn";
%Data
time_ID = [11,12,13,14,15];
eventId = [0011, 0012, 0013, 0021, 0022, 0031, 0041, 0051, 0061, 0071];
int:ntime = 5;
int:nevent = 10;
set of int: events =1..nevent;
set of int: time = 1..ntime;
array[1..nevent] of int:eventId;
array[1..nevent] of var time:event_time;
array[1..ntime] of int:time_ID;
solve satisfy;
constraint
forall(event in eventId)(
exists(t in time_ID)(
event_time[event] = t ));
output[ show(event_time) ];
我尝试使用上面的代码为事件分配时间。 但不是随机地为事件分配时间,而是返回错误"数组访问越界" 如何从时间数组中随机选择? 谢谢
答案 0 :(得分:0)
错误是因为您尝试在“event_time”数组中分配索引11(eventId数组中的第一个元素)。
只有1的分配是正确的,因为你没有对“event_time”数组做任何其他限制。如果您将解决方案的数量设置为 - 比如说 - 3,您将看到其他解决方案。而且,实际上,现在的约束并不是真正有意义的,因为它只是确保在“event_time”中对元素进行了一些赋值,但是这个约束由“event_time”域处理(即所有索引都是在1..ntime)范围内。