简体   繁体   English

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

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

I have created a model for solving the graph coloring problem 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;

In order to tackle big problems (around 400-500 nodes), I start with an upper bound of the number of colors and solve successive satisfaction problems decrementing the number by one till it becomes unsatisfiable or times out.为了解决大问题(大约 400-500 个节点),我从 colors 数量的上限开始,并解决连续的满意度问题,将数量递减 1 直到它变得无法满足或超时。 This method gives me decent results.这种方法给了我不错的结果。

In order to improve my results, I added symmetry breaking constraints to the above model:为了改进我的结果,我在上面的 model 中添加了对称破坏约束:

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

This, however, brings down my results both speed-wise and quality-wise.然而,这降低了我在速度方面和质量方面的结果。

Why is my model performing badly after adding the additional constraints?为什么我的 model 在添加附加约束后表现不佳? How should I go about adding the symmetry breaking constraints?我应该如何 go 关于添加对称破坏约束?

For symmetry breaking for cases where the values are fully symmetric, I would recommend theseq_precede_chain constraint, which breaks that symmetry.对于值完全对称的情况下的对称性破坏,我建议使用seq_precede_chain约束,它会破坏这种对称性。 As commented by @hakank, using indomain_random is probably not a good idea when used with symmetry breaking, indomain_min is a safer choice.正如@hakank 评论的那样,在与对称破坏一起使用时,使用indomain_random可能不是一个好主意, indomain_min是一个更安全的选择。

For graph coloring in general, it may help performance to run a clique-finding algorithm, and post all_different constraints over each cliques found.对于一般的图形着色,运行一个团查找算法可能有助于提高性能,并在找到的每个团上发布all_different约束。 That would have to be done when generating a minizinc program for each instance.在为每个实例生成 minizinc 程序时必须这样做。 For comparison, see the Gecode graph coloring example which uses pre-computed cliques .为了进行比较,请参阅使用预先计算的 cliques 的 Gecode 图形着色示例

I know this is an old question, but I was working on the same problem and I wanted to write what I found about this topic that maybe it will be useful to someone in the future.我知道这是一个老问题,但我正在研究同样的问题,我想写下我对这个主题的发现,也许它对将来的某人有用。

To improve the model the solution is to use symmetry breaking constraint, as you did, but in Minizinc there is a global constraint called value_precede which can be used in this case.要改进 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));

Changing the search heuristics the result does not improve much, I have tried different configurations and the best results are obtained using dom_w_deg and indomain_min (compared to my data files).更改搜索启发式结果并没有太大改善,我尝试了不同的配置,使用dom_w_degindomain_min获得了最佳结果(与我的数据文件相比)。

Another way to improve the results is to accept any good enough solution that's less than the number of colours in the domain.改善结果的另一种方法是接受小于域中颜色数量的任何足够好的解决方案。 But this model does not always lead to obtaining the optimal result.但是这个 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)]

A clear and complete explanation can be found here: https://maxpowerwastaken.gitlab.io/model-idiot/posts/graph_coloring_and_minizinc/可以在此处找到清晰完整的解释: 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