【问题标题】:Why does this two-line change break this minizinc set-cover program?为什么这个两行更改会破坏这个 minizinc set-cover 程序?
【发布时间】:2019-08-08 22:00:59
【问题描述】:

下面的程序(改编自http://www.hakank.org/minizinc/set_covering4b.mzn)是集合覆盖问题的解决方案(问题末尾提供了示例数据)。这运行正确。

int: num_alternatives;
int: num_objects;
par set of int: ALTERNATIVES = 1..num_alternatives;

% costs for the alternatives
array[ALTERNATIVES] of int: costs; 

% objects covered by the alternatives
array[ALTERNATIVES] of var set of 1..num_objects: a;

% decision variable: which alternative to choose
array[ALTERNATIVES] of var bool: x; 

% the objective to minimize
var int: z = sum(i in 1..num_alternatives) (x[i]*costs[i]); 
solve minimize z;

constraint
   forall(j in 1..num_objects) (
     sum(i in 1..num_alternatives) (x[i] * bool2int(j in a[i])) >= 1
   )
;

output
[
  "x: " ++ show(x) ++ "\n" ++ 
  "a: " ++ show(a) ++ "\n"
];

但是,如果我替换上面的 a 定义:

array[ALTERNATIVES] of var set of 1..num_objects: a;

这两行在我看来是等价的:

var set of int: OBJECTS = 1..num_objects;  
array[ALTERNATIVES] of OBJECTS: a;

...突然出现以下错误:

MiniZinc:类型错误:type-in​​st 必须是 par 集,但是是 `var set of 诠释'

这让我很困惑。我什至改变了什么?在每种情况下,a 都是一个整数集数组。在每种情况下,类型实例都是var set of int,但第二个会引发错误,而第一个不会出于某种原因?


这里有一些数据可以放在 .mzn 代码文件的底部,以生成一个独立的、可运行的示例:

% data
num_alternatives =  10;
costs = [ 19, 16, 18, 13, 15, 19, 15, 17, 16, 15];
num_objects = 8;

% the alternatives and the objects they contain
a = [
  {1,6},
  {2,6,8},
  {1,4,7},
  {2,3,5},
  {2,5},
  {2,3},
  {2,3,4},
  {4,5,8},
  {3,6,8},
  {1,6,7}
];

【问题讨论】:

    标签: constraint-programming minizinc operations-research set-cover


    【解决方案1】:

    你可以这样写:

    int: num_alternatives;
    int: num_objects;
    set of int: ALTERNATIVES = 1..num_alternatives;
    set of int: OBJECTS = 1..num_objects;
    
    % costs for the alternatives
    array[ALTERNATIVES] of int: costs; 
    
    % objects covered by the alternatives
    array[ALTERNATIVES] of var set of OBJECTS: a;
    
    % decision variable: which alternative to choose
    array[ALTERNATIVES] of var bool: x; 
    
    % the objective to minimize
    var int: z = sum(i in ALTERNATIVES) (x[i]*costs[i]); 
    solve minimize z;
    
    constraint
       forall(j in OBJECTS) (
         sum(i in ALTERNATIVES) (x[i] * (j in a[i])) >= 1
       )
    ;
    
    output
    [
      "x: " ++ show(x) ++ "\n" ++ 
      "a: " ++ show(a) ++ "\n"
    ];
    

    在你的实验中

    var set of int: OBJECTS = 1..num_objects;  
    array[ALTERNATIVES] of OBJECTS: a;
    

    aarray 范围内的整数1..num_objects。 但是您打算在该范围内使用 array 的整数集。

    【讨论】:

      猜你喜欢
      • 2016-08-16
      • 2015-12-25
      • 2011-07-23
      • 2021-05-27
      • 2012-10-08
      • 1970-01-01
      • 1970-01-01
      • 2011-09-27
      • 2013-02-03
      相关资源
      最近更新 更多