【问题标题】:Eterate over an array in minizinc and output it在 minizinc 中迭代一个数组并输出它
【发布时间】:2023-01-07 06:03:32
【问题描述】:

我想遍历 minizinc 中的一个数组并输出结果:像这样

数组[1..4] of int: my_array = [4,5,1,3];

输出 [forall(c in 1..4)("元素索引 c 是:(array[c])")];

输出应该是这样的 元素索引 1 是:4 元素索引 1 是:5 元素索引 1 是 :1 元素索引 1 是 :3

有没有办法在 Minizinc 中执行此操作,因为我不会在手册中找到它。

谢谢

数组[1..4] of int: my_array = [4,5,1,3];

输出 [forall(c in 1..4)("元素索引 c 是:(array[c])")];

输出应该是这样的 元素索引 1 是:4 元素索引 1 是:5 元素索引 1 是 :1 元素索引 1 是 :3

【问题讨论】:

    标签: minizinc


    【解决方案1】:

    (我假设指数应该改变,即从 1..4 开始)。

    output 部分需要一些不同的语法。这是执行此操作的一种方法:

    array[1..4] of int: my_array = [4,5,1,3];
    solve satisfy;
    output [
      "element index (c) is: (my_array[c])
    "
      | c in 1..4
    ];
    

    输出是

    element index 1 is: 4
    element index 2 is: 5
    element index 3 is: 1
    element index 4 is: 3
    

    output 部分之外还有另一种方法可以用于调试固定数组中的索引和值:使用 trace。这是打印相同内容的变体,但在 constraint 部分的 forall 循环中使用跟踪:

    array[1..4] of int: my_array = [4,5,1,3];
    solve satisfy;
    constraint
      forall(c in 1..4) (
       trace("element index (c) is: (my_array[c])
    ")
      )
    ;
    

    注意:使用trace 不会以任何可理解的方式打印决策变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-11
      • 2010-11-17
      • 1970-01-01
      • 2018-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-07
      相关资源
      最近更新 更多