Search code examples
algorithmprologschedulingclpfd

what algorithm for a scheduling program


I have this problem of scheduling tasks. Each task has a suggested start time T (it needs to start at [T-10, T+10]), takes L minutes to complete and uses a number of resources [R1, R2,...]. When a resource is being used, no other task can use it. Given that only the start time is flexible, my goal is to schedule the tasks so that they can access any resource they need or point out all the conflicts that needs resolving.

Which algorithm can I use for this purpose? Thank you.


Solution

  • Since you've tagged this as prolog, I recommend implementing it in constraint logic programming (CLP) and using the algorithms built into your CLP implementation. Partial example:

    :- use_module(library(clpfd)).
    
    on_time([]).
    on_time([Task|Tasks]) :-
        Task = task(TSuggested,TActual,L,Rs),
        TActual #>= TSuggested - 10,
        TActual #=< TSuggested + 10,
        on_time(Tasks).
    

    Another predicate would check that no two tasks use the same resource concurrently:

    nonoverlap(R,Task1,Task2) :-
        Task1 = task(_,T1,L1,Rs2),
        Task2 = task(_,T2,L2,Rs2),
        ((member(R,Rs1), member(R,Rs2)) ->
            T2 #> T1+L1     % start Task2 after Task1 has finished
            #\/             % OR
            T1 #> T2+L2     % start Task1 after Task2 has finished
        ;
            true            % non-conflicting, do nothing
        ).
    

    Finally, call labeling on all the constrained variables to give them consistent values. This uses CLP(fd), which works for integer time units. CLP(R) does the same for real-valued time but it slightly more complicated. Links are for SWI-Prolog but SICStus and ECLiPSe have similar libraries.