Max*_*wer 1 constraint-programming operations-research set-cover minizinc
下面的程序(改编自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"
];
Run Code Online (Sandbox Code Playgroud)
但是,如果我替换a
上面的定义:
array[ALTERNATIVES] of var set of 1..num_objects: a;
这两条线在我看来是等效的:
var set of int: OBJECTS = 1..num_objects;
array[ALTERNATIVES] of OBJECTS: a;
Run Code Online (Sandbox Code Playgroud)
...突然出现以下错误:
MiniZinc:类型错误:必须对type-inst进行参数设置,但必须将其设为“ var int set”
这使我感到困惑。我什至改变了什么?在每种情况下a
都是一组整数集。类型实例var set of int
在每种情况下都是a ,但是第二个实例抛出错误,而第一个实例不是出于某种原因?如果有人可以向我解释这一点,我将非常感激。
(以下一些数据可以放在.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}
];
Run Code Online (Sandbox Code Playgroud)
You could write it as follows:
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"
];
Run Code Online (Sandbox Code Playgroud)
In your experiment
var set of int: OBJECTS = 1..num_objects;
array[ALTERNATIVES] of OBJECTS: a;
Run Code Online (Sandbox Code Playgroud)
a
is an array
of integers in the range 1..num_objects
.
But you intended an array
of sets of integers in that range.