简体   繁体   English

Prolog相互递归谓词

[英]Prolog Mutual recursive predicates

Good evening, 晚上好,

I am newbie in Prolog and I have a problem to solve. 我是Prolog的新手,有一个要解决的问题。 I tried to do it myself, but it does not work for me. 我尝试自己做,但是对我不起作用。 I searched my Prolog books for any help, but didn't find anything either. 我在Prolog书籍中搜索了任何帮助,但也没有找到任何帮助。 This question is really confusing me. 这个问题真的使我感到困惑。

The question is: 问题是:

We need to calculate the early start time for each task (earliest possible time that it could start given its prerequisites must be accomplished first). 我们需要计算每个任务的提早开始时间(考虑到先决条件必须首先完成的最早时间)。

• The early start time of a task is given by the latest early finish of its prerequisites. •任务的尽早开始时间是任务的尽早开始时间。

• The latest early finish of any list of tasks can be calculated by starting at zero and successively taking the maximum with the early finish time of each task. •任何任务列表的最新提前完成可以通过从零开始并依次取每个任务的提前完成时间的最大值来计算。

• The early finish time of a task is given by adding its duration to its early start time. •任务的提早完成时间是通过将其持续时间添加到提早开始时间而得出的。

Write definitions in Prolog for the predicates e_start, l_e_finish, e_finish. 在Prolog中为谓词e_start,l_e_finish,e_finish定义。 These predicates are mutually recursive. 这些谓词是相互递归的。

The tasks and their times are defined as follows: 任务及其时间定义如下:

duration(Task, Time),
duration(b, 10),
duration(k,5),
duration(a,2).

The prerequisites are defined as follows: 前提条件定义如下:

prerequisites(Task, PreqTask),
prerequisites(k,[b]),
prerequisites(b,[]),
prerequisites(a,[k]).

I tried to solve it but I think I need more explanation on how to do it properly as I just cannot get it right. 我试图解决它,但是我认为我需要更多有关如何正确执行它的解释,因为我无法正确地解决它。

My solution is: 我的解决方案是:

e_start(Task,Start):-
   prereqs(Task, X),
   l_e_finish(X,Start).

l_e_finish(Task,Finish) :-
    e_finish(Task,Finish),
    l_e_finish(Task,Finish1),
    duration(Task, Finish),
    max(Finish,Task,Finish1).

e_finish(Task,Finish):-
    duration(Task, Time),
    e_start(Task,Finish),
    Finish is Time+Finish.

Any help would me much appreciate. 任何帮助我将不胜感激。 Thank you fellow coders! 谢谢各位编码员!

Let's start by considering the case where you have no prerequisites. 让我们首先考虑没有先决条件的情况。 In that case, your earliest finish is simply your duration: 在这种情况下,您最早的完成时间就是您的持续时间:

early_finish(T, Finish) :- 
    duration(T, Finish).

What if you have prerequisites? 如果您有先决条件怎么办? Then your early finish is your duration plus the latest of those, which would look like this: 然后,您的提早完成时间就是您的持续时间加上最新的持续时间,如下所示:

early_finish(T, Finish) :-
    prerequisites(T, Prerequisites),
    maplist(early_finish, Prerequisites, PrerequisiteFinishes),
    maxlist(PrerequisiteFinishes, LastFinish),
    duration(T, Duration),
    Finish is LastFinish + Duration.

I assume you have a maxlist/2 predicate here that gives you the largest item in a list. 我假设您这里有一个maxlist/2谓词,该谓词为您提供列表中最大的项。 If you define maxlist/2 to give 0 for the empty list and you guarantee that all tasks have a prerequisite/2 definition, then you're done with just the one clause. 如果您将maxlist/2定义为空列表为0,并且保证所有任务都具有prerequisite/2定义,那么仅用one子句就可以完成。 If not, you will have to find a way to combine the logic of these two clauses. 如果没有,您将必须找到一种方法来组合这两个子句的逻辑。

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

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