2

请您帮助优化工作 MiniZinc 代码:

任务:有一个有 6x 时隙的会议。有 3 位演讲者出席会议,每个演讲者都可以在特定时段使用。每个演讲者将呈现预定数量的时隙。

目标:制定演讲者最早结束的时间表。

示例:演讲者 A、B 和 C。谈话时长 = [1, 2, 1]

扬声器可用性:

+---+------+------+------+
|   | Sp.A | Sp.B | Sp.C |
+---+------+------+------+
| 1 |      | Busy |      |
| 2 | Busy | Busy | Busy |
| 3 | Busy | Busy |      |
| 4 |      |      |      |
| 5 |      |      | Busy |
| 6 | Busy | Busy |      |
+---+------+------+------+

链接到工作 MiniZinc 代码:http ://pastebin.com/raw.php?i=jUTaEDv0

我希望优化的内容:

% ensure allocated slots don't overlap and the allocated slot is free for the speaker
constraint 
    forall(i in 1..num_speakers) (
        ending_slot[i] = starting_slot[i] + app_durations[i] - 1
    ) /\
    forall(i,j in 1..num_speakers where i < j) (
        no_overlap(starting_slot[i], app_durations[i], starting_slot[j], app_durations[j])
    ) /\
    forall(i in 1..num_speakers) (
        forall(j in 1..app_durations[i]) (
            starting_slot[i]+j-1 in speaker_availability[i]
        )
    ) 
;

预期的解决方案:

+---+----------+----------+----------+
|   |   Sp.A   |   Sp.B   |   Sp.C   |
+---+----------+----------+----------+
| 1 | SELECTED | Busy     |          |
| 2 | Busy     | Busy     | Busy     |
| 3 | Busy     | Busy     | SELECTED |
| 4 |          | SELECTED |          |
| 5 |          | SELECTED | Busy     |
| 6 | Busy     | Busy     |          |
+---+----------+----------+----------+
4

2 回答 2

5

我是 hakank(原始模型的作者)。如果我理解正确,您现在的问题是如何呈现最佳解决方案的表格,而不是真正找到解决方案本身(我测试的所有 FlatZinc 求解器都立即解决了它)。

创建表格的一种方法是使用帮助矩阵(“m”),其中包含选择扬声器(1)、忙碌(-1)或不可用(0)的信息:

array[1..num_slots, 1..num_speakers] of var -1..1: m;

然后必须连接矩阵中的信息和其他决策变量(“starting_slot”和“ending_slot”):

% connect to matrix m
constraint
   forall(t in 1..num_slots) (
      forall(s in 1..num_speakers) (
         (not(t in speaker_availability[s]) <-> m[t,s] = -1) 
          /\
          ((t >= starting_slot[s] /\ t <= ending_slot[s]) <-> m[t,s] = 1)
     )
 )

;

然后矩阵“m”可以这样打印:

% ...
++
[ 
   if s = 1 then "\n" else " " endif ++
   if fix(m[t,s]) = -1 then 
      "Busy    " 
   elseif fix(m[t,s]) =  1 then 
      "SELECTED" 
   else
      "        "
   endif
 | t in 1..num_slots, s in 1..num_speakers

] ;

与往常一样,有不止一种方法可以做到这一点,但我选择了这个,因为它非常直接。

这是完整的模型: http ://www.hakank.org/minizinc/scheduling_speakers_optimize.mzn

更新:添加模型的输出:

Starting:  [1, 4, 3]
Durations: [1, 2, 1]
Ends:      [1, 5, 3]
z:         5

SELECTED Busy             
Busy     Busy     Busy    
Busy     Busy     SELECTED
         SELECTED         
         SELECTED Busy    
Busy     Busy             
----------
==========

更新2:另一种方法是使用cumulative/4而不是no_overlap/4,这应该更有效,即

constraint 
    forall(i in 1..num_speakers) (
    ending_slot[i] = starting_slot[i] + app_durations[i] - 1
    ) 
    % /\ % use cumulative instead (see below)
    % forall(i,j in 1..num_speakers where i < j) (
    %   no_overlap(starting_slot[i], app_durations[i], starting_slot[j], app_durations[j])
    % ) 
    /\
    forall(i in 1..num_speakers) (
    forall(j in 1..app_durations[i]) (
        starting_slot[i]+j-1 in speaker_availability[i]
           )
    ) 

    /\ cumulative(starting_slot, app_durations, [1 | i in 1..num_speakers], 1)
;

这是修改后的版本(给出相同的结果) http://www.hakank.org/minizinc/scheduling_speakers_optimize2.mzn (我也跳过了表示矩阵“m”并在输出部分进行所有表示。)

对于这个简单的问题实例,没有明显的区别,但对于较大的实例,这应该更快。(对于更大的实例,人们可能想要测试不同的搜索启发式而不是“求解最小化 z”。)

于 2013-12-23T20:18:06.203 回答
2

正如我对您之前的问题Constraint Programming: Scheduling Speaker in shortest time所评论的那样,累积约束适用于此。我没有方便的 Minizinc 代码,但是 ECLiPSe 中有模型(http://eclipseclp.org):

:- lib(ic).
:- lib(ic_edge_finder).
:- lib(branch_and_bound).

solve(JobStarts, Cost) :-
    AllUnavStarts = [[2,6],[1,6],[2,5]],
    AllUnavDurs   = [[2,1],[3,1],[1,1]],
    AllUnavRess   = [[1,1],[1,1],[1,1]],
    JobDurs = [1,2,1],
    Ressources = [1,1,1],
    length(JobStarts, 3),
    JobStarts :: 1..9,

    % the jobs must not overlap with each other
    cumulative(JobStarts, JobDurs, Ressources, 1),

    % for each speaker, no overlap of job and unavailable periods
    (
        foreach(JobStart,JobStarts),
        foreach(JobDur,JobDurs),
        foreach(UnavStarts,AllUnavStarts),
        foreach(UnavDurs,AllUnavDurs),
        foreach(UnavRess,AllUnavRess)
    do
        cumulative([JobStart|UnavStarts], [JobDur|UnavDurs], [1|UnavRess], 1)
    ),

    % Cost is the maximum end date
    ( foreach(S,JobStarts), foreach(D,JobDurs), foreach(S+D,JobEnds) do true ),
    Cost #= max(JobEnds),

    minimize(search(JobStarts,0,smallest,indomain,complete,[]), Cost).
于 2013-12-23T21:24:42.627 回答