我正在 Prolog 中编写一个任务调度程序/计划程序,为此我计划使用 CLPFD library (在 SWIPL 上)。我想知道使用有限域来解决调度问题有多强大,以及如果我使用它会对 CPU 负载产生什么影响。
调度问题将基于本文第 10 页中所述的断言:“Constraint-Based Scheduling”。事实上,我的任务/事件将非常异构(有些是可抢占的,而另一些则不能),并且事件资源将具有不同的容量。现在,我正在研究一个简单的案例(不可抢占的析取调度),我得到了这样的结果:
/* Non-preemptive, disjunctive scheduling. *******************************/
planner :-
/* 'S' stands for start point.
'E' stands for end point. */
set(a1,S1,E1),
set(a2,S2,E2),
set(a3,S3,E3),
interval(intersection,[S1,E1],[S2,E2],[]), % Tests whether activities
interval(intersection,[S2,E2],[S3,E3],[]), % intersect. If they do,
interval(intersection,[S3,E3],[S1,E1],[]), % backtracking occurs and
(...). % an alternative solution
% will be looked for.
/* A set of times in which activity A executes (non-preemptive) */
set(A,[S],[E]) :-
/* 'A' is the activity.
'R' is release point and 'D' deadline point.
'Lst' stands for Latest Start Point.
'Eet' stands for Earliest End Point. */
preemptable(A,no),
rd(A,R,D),
p(A,P),
Lst is D-P,
Eet is R+P,
S in R..D,
E in R..D,
S #=< Lst,
E #>= Eet,
S #< E,
P #= E-S,
indomain(S),
indomain(E).
set(A,[],[]). /* When the activity can't be scheduled. */
它确实有效,而且速度非常快(事实上是瞬时的)。但这只是一个包含三个事件的简单案例,当我的最终程序中我将有数百个事件时,调度问题将比这复杂得多。
感谢您的建议!
最佳答案
总的来说,CLP(FD) 是解决此类问题的合适且行之有效的方法。但请注意,即使在 library(clpfd)
中,也有许多不同的方法来对问题进行建模:例如,您可以使用全局约束 serialized/2
或 cumulative/1
来表达它。其他 Prolog 系统通常会为您提供比 SWI-Prolog 更好的性能,但是您对问题建模和搜索解决方案的方式对性能的影响通常比任何特定实现的优化要大得多。
关于performance - Prolog 的 CLP over Finite Domains 库性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17040949/