【问题标题】:MiniZinc find the set of intMiniZinc 找到 int 的集合
【发布时间】:2018-11-11 09:16:46
【问题描述】:

我在 minizinc 中有一个脚本,它试图找到 int 集,但无法找到。问题陈述给出了一组 2 类特征,需要找到最小支持集,其长度应该小于一些 k,并且对于一些集合数组,它应该包含它们中的至少一个索引值。因此,假设解决方案是 {3,4,7} 并且集合数组(我们称之为 - atmostone) atmostone = [{1,2,3}, {4,5,6}, {7,8 ,9}] 所以解的交集和 atmostone 数组中的每个集合的长度必须正好为 1。

这些是我实现的约束,但错误是模型不一致。

include "globals.mzn";
include "alldifferent.mzn";

int: t; %number of attributes
int: k; %maximum size of support set
int: n; %number of positive instances
int: m; %number of negative instances
int: c; %number of atMostOne Constraints
array [1..n, 1..t] of 0..1: omegap;
array [1..m, 1..t] of 0..1: omegan;
array [int] of set of int: atMostOne;

set of int: K = 1..k;
set of int: T = 1..t;    
var set of T: solution;


function array[int] of var opt int : set2array(var set of int: a) = 
  [i | i in a];

% constraint alldifferent(solution);
constraint length(set2array(solution)) <= k;
constraint forall(i in 1..length(atMostOne))(length(set2array(solution intersect atMostOne[i])) <= 1);
constraint forall(i in 1..n, j in 1..m)(not(omegap[i, fix(solution)] == omegan[j, fix(solution)]));

solve satisfy;

这是错误:

Compiling support_send.mzn

  WARNING: model inconsistency detected
Running support_send.mzn
=====UNSATISFIABLE=====
% Top level failure!
Finished in 88msec

更新:

数据:

t=8; %number of attributes
k=3; %maximum size of support set
n=5; %number of positive instances
m=3; %number of negative instances
c=4; %number of atMostOne Constraints
omegap=[| 0,0,1,0,1,0,0,0 |
1,0,1,1,0,0,0,1|
0,1,0,1,0,0,1,1|
0,1,1,0,1,1,0,1|
0,0,1,0,1,1,1,1
|];
omegan=[| 1,1,0,0,1,0,1,1|
0,1,0,0,1,1,0,0|
1,0,0,1,1,0,0,1
|];

atMostOne =
[{1,2,3},
{4,5,6},
{3,5},
{7,8}];

任何帮助将不胜感激。

谢谢。

【问题讨论】:

  • 您能分享确切的数据定义吗? (dzn 文件)
  • @Dekker1 用数据更新了问题。
  • 您的数据现在不一致,数据文件中声明的任何内容都没有声明,并且 T 未定义,与您的模型。能否请您完整提供两者,以便我们运行您正在运行的程序?
  • @Dekker1 更新了完整代码。

标签: optimization constraint-programming minizinc


【解决方案1】:

您的模型中的问题源于您设置的变量solutions

第一个问题是由set2array 函数引起的。您可能认为这会返回一个数组,其中包含位于您的数组中的整数;然而,事实并非如此。相反,它返回一个 可选整数 数组。这意味着您的集合中所有可能的值都存储在数组中,但其中一些可能只是标记为不存在。在这种情况下,它几乎与拥有一个布尔变量数组相同,只是说明一个值是否位于集合中。

请注意,约束length(set2array(solution)) &lt;= k 是不可能满足的。因为solutionk 有更多可能,所以数组的长度总是会更大。您可能想要强制执行的约束是card(solution) &lt;= k。函数card(X) 返回集合的基数/大小。在第二个约束中可以发现同样的问题。

您的最终约束有一个不同的问题:它包含表达式fix(solution)。在您的模型的上下文中,您不能编写它,因为不会在编译时修复。该表达式的计算结果也为set of int。虽然您可以使用集合来访问数组、数组切片,但目前还不允许使用变量集合。我建议尝试为这个约束找到一个不同的公式。 (因为我不知道它应该做什么,恐怕我无法提出任何建议)

最后一点,注释掉的约束alldifferent(solution) 是不必要的。因为solution 是一个集合,所以保证它只包含一次值。

【讨论】:

    猜你喜欢
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多