简体   繁体   English

MiniZinc模型的优化

[英]Optimization of MiniZinc model

I have a MiniZinc model which is supposed to find d[1 .. n] and o[1..k, 0 .. n] such that x[k] = o[k,0] + d[1]*o[k,1] + d[2]*o[k,2] ... d[n]+o[k,n] and the sum of absolute values of o[k,i]'s is minimized. 我有一个MiniZinc模型,应该找到d [1 .. n]和o [1..k,0 .. n],以便x [k] = o [k,0] + d [1] * o [k,1] + d [2] * o [k,2] ... d [n] + o [k,n]和o [k,i]的绝对值之和最小。

I have many different x[i] and d[1..n] should remain the same for all of them. 我有许多不同的x [i]和d [1..n]对于所有它们都应保持相同。

I have a working model which is pasted below, which finds a good solution in the n=2 case really quickly (less than a second) however, if I go to n=3 (num_dims in the code) even after an hour I get no answer except the trivial one (xi=o0), even though the problem is somewhat recursive, in that a good answer for 2 dimensions can serve as a starting point for 3 dimensions by using o0 as xi for a new 2 dimensional problem. 我有一个粘贴在下面的工作模型,它在n = 2的情况下很快(不到一秒钟)找到了一个很好的解决方案,但是,即使我经过一个小时,我又去了n = 3(代码中的num_dims),除了小问题(xi = o0),其他问题也没有答案,即使问题在某种程度上是递归的,也可以通过将o0用作新的二维问题的xi,将2维的良好答案作为3维的起点。

I have used MiniZinc before, however, I do not have a background in OR or Optimization, thus I do not really know how to optimize my model. 我之前使用过MiniZinc,但是我没有OR或Optimization的背景知识,因此我真的不知道如何优化模型。 I would be helpful for any hints on how to do that, either by adding constraints or somehow directing the search. 通过添加约束或以某种方式指导搜索,我将对如何执行操作的任何提示提供帮助。 Is there a way to debug such performance problems in MiniZinc? 有没有办法在MiniZinc中调试此类性能问题?

My current model: 我当前的模型:

% the 1d offsets
array [1 .. num_stmts] of int : x;
x = [-10100, -10001, -10000, -9999, -9900, -101, -100, -99, -1, 1, 99, 100, 101, 9900, 9999, 10000, 10001, 10100];
int : num_stmts = 18;

% how many dimensions we decompose into
int : num_dims = 2;

% the dimension sizes
array [1 .. num_dims] of var int : dims;

% the access offsets
array [1 .. num_stmts, 1 .. num_dims] of var int : offsets;

% the cost function: make access distance (absolute value of offsets) as small as possible
var int : cost = sum (s in 1 .. num_stmts, d in 1 .. num_dims) (abs(offsets[s,d]));

% dimensions must be positive
constraint forall (d in 1 .. num_dims) (dims[d] >= 0);

% offsets * dimensions must be equal to the original offsets
constraint forall (s in 1 .. num_stmts) (
  x[s] = offsets[s,1] + sum(d in 2 .. num_dims) (offsets[s,d] * dims[d-1])
 );

% disallow dimension crossing
constraint forall (s in 1 .. num_stmts, d in 1 .. num_dims) (
  abs(offsets[s,d]) < dims[d]
 );

% all dims together need to match the array size
constraint product (d in 1..num_dims) (dims[d]) = 1300000;

solve minimize cost;

output ["dims = ", show(dims), "\n"] ++
       [ if d == 1 then show_int(6, x[s]) ++ " = " else "" endif ++ 
       " " ++ show_int(4, offsets[s, d]) ++ if d>1 then " * " ++ show(dims[d-1]) else "" endif ++
       if d == num_dims then "\n" else " + " endif  |
   s in 1 .. num_stmts, d in 1 .. num_dims];

Are you using the MiniZinc IDE? 您正在使用MiniZinc IDE吗? Have you tried using a different solver? 您是否尝试过使用其他求解器?

I was struggling with a problem of dividing n random positive integers into m groups ( m < n ) where the sum of each group was supposed to be as close as possible to some other number. 我在将n随机正整数划分为m组( m < n )的问题上苦苦挣扎,其中每个组的总和应该尽可能地接近其他数。

When n reached about 100 and m about 10, it took significantly longer time (30 min+) and the result was not satisfying. n达到约100, m达到约10时,花费的时间明显更长(30分钟以上),结果令人不满意。 This was using the default Gecode (bundled) solver. 这是使用默认的Gecode(捆绑)求解器。 By chance I went through each and everyone of the solvers and found that the COIN-OR CBC (bundled) found an optimal solution within 15 s. 偶然地,我遍历了每个求解器,发现COIN-OR CBC(捆绑)在15秒内找到了最佳解决方案。

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

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