繁体   English   中英

如何在 MiniZinc 中提高我的图形着色 model 的性能?

[英]How to improve the performance of my graph coloring model in MiniZinc?

我创建了一个 model 来解决 MiniZinc 中的图形着色问题:

include "globals.mzn";

int: n_nodes;               % Number of nodes
int: n_edges;               % Number of edges
int: domain_ub;             % Number of colors
array[int] of int: edges;   % All edges of graph as a 1D array
array[1..n_edges, 1..2] of int: edges2d = array2d(1..n_edges, 1..2, edges);

array[1..n_nodes] of var 1..domain_ub: colors;

constraint forall (i in 1..n_edges) (colors[edges2d[i,1]] != colors[edges2d[i,2]]);

solve :: int_search(colors, dom_w_deg, indomain_random)
    satisfy;

为了解决大问题(大约 400-500 个节点),我从 colors 数量的上限开始,并解决连续的满意度问题,将数量递减 1 直到它变得无法满足或超时。 这种方法给了我不错的结果。

为了改进我的结果,我在上面的 model 中添加了对称破坏约束:

constraint colors[1] = 1;
constraint forall (i in 2..n_nodes) ( colors[i] in 1..max(colors[1..i-1])+1 );

然而,这降低了我在速度方面和质量方面的结果。

为什么我的 model 在添加附加约束后表现不佳? 我应该如何 go 关于添加对称破坏约束?

对于值完全对称的情况下的对称性破坏,我建议使用seq_precede_chain约束,它会破坏这种对称性。 正如@hakank 评论的那样,在与对称破坏一起使用时,使用indomain_random可能不是一个好主意, indomain_min是一个更安全的选择。

对于一般的图形着色,运行一个团查找算法可能有助于提高性能,并在找到的每个团上发布all_different约束。 在为每个实例生成 minizinc 程序时必须这样做。 为了进行比较,请参阅使用预先计算的 cliques 的 Gecode 图形着色示例

我知道这是一个老问题,但我正在研究同样的问题,我想写下我对这个主题的发现,也许它对将来的某人有用。

要改进 model,解决方案是使用对称破坏约束,就像您所做的那样,但在 Minizinc 中有一个名为value_precede的全局约束,可以在这种情况下使用。

% A new color J is only allowed to appear after colors 0..J-1 have been seen before (in any order)
constraint forall(j in 1..n-1)(value_precede(j, j+1, map));

更改搜索启发式结果并没有太大改善,我尝试了不同的配置,使用dom_w_degindomain_min获得了最佳结果(与我的数据文件相比)。

改善结果的另一种方法是接受小于域中颜色数量的任何足够好的解决方案。 但是这个 model 并不总是导致获得最佳结果。

include "globals.mzn";

int: n; % Number of nodes
int: e; % Number of edges
int: maxcolors = 17; % Domain of colors

array[1..e,1..2] of int: E; % 2d array, rows = edges, 2 cols = nodes per edge
array[0..n-1] of var 0..maxccolors: c;   % Color of node n

constraint forall(i in 1..e)(c[E[i,1]] != c[E[i,2]] ); % Two linked nodes have diff color
constraint c[0] == 0; % Break Symmetry, force fist color == 0

% Big symmetry breaker. A new color J is only allowed to appear after colors
% 0..J-1 have been seen before (in any order)
constraint forall(i in 0..n-2)( value_precede(i,i+1, c)  );

% Ideally solve would minimize(max(c)), but that's too slow, so we accept any good
% enough solution that's less equal our heuristic "maxcolors"
constraint max(c) <= maxcolors;
solve :: int_search(c, dom_w_deg, indomain_min, complete) satisfy;

output [ show(max(c)+1), "\n", show(c)]

可以在此处找到清晰完整的解释: https://maxpowerwastaken.gitlab.io/model-idiot/posts/graph_coloring_and_minizinc/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM