我最近开始学习minizinc,但是在我的程序中我有这种奇怪的行为。
.dzn
n = 5;
c = 2;.mzn
include "globals.mzn";
int: n;
int: c;
set of int: num_deliveries = 1..n-1;
int: headquarter = 1;
set of int: num_places = 1..n;
set of int: deliveries = 2..n;
set of int: couriers = 1..c;
set of int: num_max_deliveries = 1..n+2;
set of int: schedule_domain = 0..n;
int: first_place_idx = 1;
int: last_place_idx = n+2;
array[couriers,num_max_deliveries] of var schedule_domain: schedule;
array[1..2*n] of int: total = [schedule[i,j]|i,j in num_max_deliveries where i<=2 /\ j != first_place_idx /\ j!= last_place_idx];
output ["len_without_variable = \(length([ k | k in total where k != 0]))"];
var int: len_cleaned = length([ k | k in total where k != 0]);
output ["len_with_variable = \(len_cleaned)\n"];特别是,从这些代码行中,我得到了不同的结果,即使它们是相等的。
output ["len_without_variable = \(length([ k | k in total where k != 0]))"];
var int: len_cleaned = length([ k | k in total where k != 0]);
output ["len_with_variable = \(len_cleaned)\n"];为什么会发生这种事?
发布于 2022-02-16 16:50:20
下面是模型的一个输出:(添加了total ):
len_without_variable = 2
len_with_variable = 10
total: [3, 3, 0, 0, 0, 0, 0, 0, 0, 0]老实说,我不确定total中非零元素的两个计算是否应该是相同的。也许这是length对决策变量(带有where条件)的操作方式中的一个错误,并且应该是2(在本例中)。在解决这个问题之前,您可能应该避免像这样使用length。
len_without_variable的输出--在输出部分中定义的输出--操作解决方案的实际值和已知值。因此,这可能与在约束/变量定义中使用length不完全一样。
要计算非零值的数量,可以改用sum:
var 0..n num_non_zeros = sum([ 1 | k in total where k != 0]);但是,构造... where k != 0会创建临时变量,这使得模型超出了必要的范围,所以最好使用以下方法:
var 0..n num_non_zeros = sum([ total[i] != 0 | i in 1..n]);https://stackoverflow.com/questions/71139331
复制相似问题