0

我正在研究约束规划问题,但停留在特定步骤并需要建议。

我的数据有一堆订单,每个订单都有一些 SKU。我想将这些订单分成不同的批次,然后计算一个批次/组中​​的唯一 SKU。例如

Order 1 - SKUs 1, 2, 3 
Order 2 - SKUs 2, 5 
Order 3 - SKUs 1, 3, 7
Order 4 - SKUs 3, 4, 6

现在,如果我将第 1 批中的订单 1 和 4 分组,而第 2 批中的订单 2 和 3 则以下将是每批中的唯一 SKU 计数:

Batch 1 - SKUs 1, 2, 3, 4, 6 = 5 SKUs
Batch 2 - SKUs 1, 2, 3, 5, 7 = 5 SKUs

我的代码如下

include "globals.mzn"; 

int: N_Orders = 14; % Number of orders
set of int: ORDERS = 1..N_Orders;
set of int: skuids = {1,2,3,4,5}; % Distinct sku ids across all orders
array[ORDERS] of set of skuids: oskuids = [{1,2,3},{1,3},{4},{4,5},{1},{1,4},{3,4},{5},{1,4,5},{1,2,3},{1,3},{4,5},{1},{1,4}]; % Distinct SKU ids in each order

% Orders per batch
ORDERS: x = 2; 

% Batches needed
int: N_Batches = 7;


% Define array that contains batch for each order
array[ORDERS] of var 1..N_Batches: obatch;
constraint global_cardinality(obatch, [i | i in (1..N_Batches-1)], [x | i in 1..(N_Batches-1)]); % Total orders in batch set to 'x'

% Distinct skus in each batch
array[1..N_Batches] of var int: skus_in_batch;
constraint forall(i in 1..N_Batches)(
             skus_in_batch[i] = card(array_union(o in ORDERS where obatch[o] = i)(oskuids[o]))
           );

solve satisfy;

在运行此代码时,我收到以下错误:

MiniZinc: type error: no function or predicate with this signature found: `array_union(array[int] of var opt set of int)'

如何修改代码以提供所需的结果?

4

2 回答 2

0

这是我在另一个论坛上从 MiniZinc 的架构师那里收到的答案,并意识到这种方法可以用于许多其他类似的情况,由于不支持的选项类型,我们会收到错误 -

表达方式

skus_in_batch[i] = card(array_union(o in ORDERS where   
                   obatch[o] = i)(oskuids[o]));

等效于

skus_in_batch[i] = card(array_union([ if obatch[o] = i then oskuids[o] else top endif | o in ORDERS]]);

并因此失败,因为 array_union 无法处理创建的可选集数组。您可以简单地将其重写为下面以避免选项类型。

skus_in_batch[i] = card(array_union([ if obatch[o] = i then oskuids[o] else {} endif | o in ORDERS]]);

or equivalently

skus_in_batch[i] = card(array_union(o in ORDERS)
                   (if obatch[o] = i then oskuids[o] else {} endif));
于 2018-08-29T17:05:17.250 回答
0

如果我理解正确,您可以简单地使用sum

constraint forall(i in 1..N_Batches)(
   skus_in_batch[i] = sum([obatch[o] = i | o in ORDERS])
);

然后是第一个解决方案

obatch = array1d(1..14 ,[7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1]);
skus_in_batch = array1d(1..7 ,[2, 2, 2, 2, 2, 2, 2]);
于 2018-08-23T04:57:58.737 回答