这是一项家庭作业,也是我第一次体验Prolog。我的目标是从人员列表和任务列表中创建任务列表。如果某人具有与任务匹配的字母标识符,那么该人员ID和任务ID将被匹配并放置在分配列表中。我的函数打印出一个列表,但它看起来并不像是在比较所有元素。输入样本:schedule([p1,p2,p3],[t1,t2],Result)。样本输出看起来像[[p1,t1],[p2,t2] [p3,t1],[p3,t2]]。
到目前为止我所拥有的:
%%
%% person(ID, TASK_CAPABILITIES, AVAILABLE_HOURS)
%%
%% How many hours each person has available and what classes of tasks they
%% are capable of performing.
%%
person(p1, [c,a], 20).
person(p2, [b], 10).
person(p3, [a,b], 15).
person(p4, [c], 30).
%%
%% task(ID, REQUIRED_HOURS, TASK_CLASS)
%%
%% How long each task requires and what class it falls under.
%%
task(t1, a, 5).
task(t2, b, 10).
task(t3, c, 15).
task(t4, c, 10).
task(t5, a, 15).
task(t6, b, 10).
%test arithmetic functions
add(X, Y, Z) :- Z is X + Y.
subtract(X,Y,Z) :- Z is X - Y.
schedule([],[],[]).
schedule(People,
[Task|OtherTasks],
[[PersonId, TaskId]|RestOfAssignments]):-
member(PersonId, People),
person(PersonId, PersonCapabilities,_),
member(TaskId, [Task|OtherTasks]),
task(TaskId, TaskType,_),
member(TaskType, PersonCapabilities),
schedule( _, OtherTasks, RestOfAssignments).
我写作背后的理由是人员列表将与每个任务进行比较,然后该任务将被下一个任务所取代,并且比较将重复。我在这个函数的跟踪中看到的是,任务正从列表中删除,但只与前两个People进行比较。我的问题是如何获取计划功能来检查每项任务的完整人员列表?
答案 0 :(得分:0)
您的问题看起来很糟糕,并且您正在简化太多......代码应该考虑到时间可用性以及成员资格。忽略这个问题,选择/ 3而不是member / 2可以帮助建模一个天真的解决方案:
schedule([],_,[]).
% peek a suitable task for PersonId
schedule([PersonId|People], Tasks, [[PersonId, TaskId]|RestOfAssignments]):-
select(TaskId, Tasks, RestTasks),
person(PersonId, PersonCapabilities,_),
task(TaskId, TaskType,_),
memberchk(TaskType, PersonCapabilities),
schedule(People, RestTasks, RestOfAssignments).
% if no suitable task for PersonId
schedule([_PersonId|People], Tasks, Assignments):-
schedule(People, Tasks, Assignments).
产生这些解决方案
?- schedule([p1,p2,p3],[t1,t2],Result).
Result = [[p1, t1], [p2, t2]] ;
Result = [[p1, t1], [p3, t2]] ;
Result = [[p1, t1]] ;
Result = [[p2, t2], [p3, t1]] ;
Result = [[p2, t2]] ;
Result = [[p3, t1]] ;
Result = [[p3, t2]] ;
Result = [].