解决Prolog中有关时间限制的难题 [英] Solving a puzzle in Prolog about time constraints

查看:122
本文介绍了解决Prolog中有关时间限制的难题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

停留在Prolog问题上.我知道答案(因为我首先是在纸上做的),但是我不知道如何让Prolog提出答案.

Stuck on a Prolog problem. I know the answer (because I did it on paper first), but I cannot figure out how to get Prolog to come up with the answer.

问题:

比尔每晚都吃零食,水果和水果各不相同 每天晚上坚果.从下面的陈述中,找出比尔的目的 上周每个晚上的零食.

Bill eats a snack every night, having a different fruit and different nuts each night. From the statements below, identify what Bill had for a snack for each weeknight last week.

a)苹果在一周中的食用时间比芒果晚.

a) The apple was eaten later in the week than the mango.

b)香蕉在一周中的食用时间比杏仁和杏仁都少. 花生,但比梨早.

b) The banana was eaten later in the week than both the almonds and peanuts, but earlier in the week than the pear.

c)腰果在一周中的食用时间早于香蕉和香蕉. 杏,但比花生晚.

c) The cashews were eaten earlier in the week than both the banana and the apricot, but later in the week than the peanuts.

d)杏仁之后的晚上没有吃山核桃.

d) The pecans were not eaten the evening after the almonds.

e)比尔一晚上吃了核桃.

e) Bill ate walnuts one night.

请注意,问题出在大约5个工作日(周一至周五), 并提到了5种水果和5种坚果.您的程序应该 解决问题并打印出解决方案,这将是一组5 三元组,例如(星期一,苹果,山核桃),...(星期五,芒果,核桃).

Note that the problem is about 5 weeknights (Monday through Friday), and mentions 5 kinds of fruit and 5 kinds of nuts. Your program should solve the problem and print out the solution, which will be a set of 5 triples like (Monday, apple, pecans), ... (Friday, mango, walnuts).

很显然,这些不是正确的答案,而只是显示的值 您将看到什么样的解决方案.

Clearly, these are not the correct answers, but just values to show you what the solution will look like.

到目前为止的代码:

before_in_week(X, Y, Days) :-
    nth1(Xi, Days, X),
    nth1(Yi, Days, Y),
    Xi < Yi.

print_solve([Head|Tail]) :-
    write(Head),
    nl,
    print_solve(Tail).  

solve(A) :-
  % all triples
  A = [[day1, fruit1, nut1],
       [day2, fruit2, nut2],
       [day3, fruit3, nut3],
       [day4, fruit4, nut4],
       [day5, fruit5, nut5]],

  Days = [monday, tuesday, wednesday, thursday, friday],
  Days = [day1, day2, day3, day4, day5],

  Fruits = [apple,banana,pear,mango,apricot],
  permutation(Fruits, [fruit1, fruit2, fruit3, fruit4, fruit5]),

  Nuts = [almonds,pecans,cashews,peanuts,walnuts],
  permutation(Nuts, [nut1, nut2, nut3, nut4, nut5]),

  % clue 1 - mango before apple
  fruit5 \= mango,
  member([C1,mango,_], A),
  member([C2,apple,_], A), before_in_week(C1,C2,Days),
  % clue 2 - banana after almonds and peanuts, but before pear
  fruit5 \= banana,
  member([C1,banana,_], A),
  member([C2,pear,_], A), before_in_week(C1,C2,Days),
  member([C3,_,almonds], A), before_in_week(C3,C1,Days),
  member([C4,_,peanuts], A), before_in_week(C4,C1,Days),
  % clue 3 - cashews before banana and apricot, but after peanuts
  nut5 \= peanuts,
  member([C1,_,cashews], A),
  member([C2,_,peanuts], A), before_in_week(C1,C2,Days),
  member([C3,banana,_], A), before_in_week(C3,C1,Days),
  member([C4,apricot,_], A), before_in_week(C4,C1,Days),
  % clue 4 - pecans not night after almonds
  nut5 \= almonds,
  % clue 5 - ate walnuts one night


  print_solve(A).

推荐答案

首先,实际上不需要手动打印任何内容.如果您输入查询solve(A).

First, there is really no need to print anything manually. Prolog's top level does this for you, if you enter the query solve(A). yet,

第二,没有解决方案.这确实是您感兴趣的.有一种非常简单且非常通用的方法来缩小故障源的范围.只需将所有目标一个接一个地概括即可.我喜欢这样在前面添加*来做到这一点:

second, there is no solution. That is really what you are interested in. There is a very simple and very general method to narrow down the source of failure. Simply generalize away all the goals, one after the other. I like to do this by adding a * in front like so:


:- op(950, fy, *).
*_0.

solve(A) :-
  * A = [[day1, fruit1, nut1], [day2, fruit2, nut2], [day3, fruit3, nut3],
         [day4, fruit4, nut4], [day5, fruit5, nut5]],

  Days = [monday|_/*[tuesday, wednesday, thursday, friday]*/],
  Days = [day1|_/*[day2, day3, day4, day5]*/],

  * Fruits = [apple,banana,pear,mango,apricot],
  * permutation(Fruits, [fruit1, fruit2, fruit3, fruit4, fruit5]),

  * Nuts = [almonds,pecans,cashews,peanuts,walnuts],
  * permutation(Nuts, [nut1, nut2, nut3, nut4, nut5]),

  % clue 1 - mango before apple
  * fruit5 \= mango,
  * member([C1,mango,_], A),
  * member([C2,apple,_], A), before_in_week(C1,C2,Days),
  % clue 2 - banana after almonds and peanuts, but before pear
  * fruit5 \= banana,
  * member([C1,banana,_], A),
  * member([C2,pear,_], A), before_in_week(C1,C2,Days),
  * member([C3,_,almonds], A), before_in_week(C3,C1,Days),
  * member([C4,_,peanuts], A), before_in_week(C4,C1,Days),
  % clue 3 - cashews before banana and apricot, but after peanuts
  * nut5 \= peanuts,
  * member([C1,_,cashews], A),
  * member([C2,_,peanuts], A), before_in_week(C1,C2,Days),
  * member([C3,banana,_], A), before_in_week(C3,C1,Days),
  * member([C4,apricot,_], A), before_in_week(C4,C1,Days),
  % clue 4 - pecans not night after almonds
  * nut5 \= almonds.
  % clue 5 - ate walnuts one night

在此程序片中,它是对原始程序的概括,它可以归结为无法成功执行

In this program slice, which is a generalization of your original program, it boils down to the inability to succeed for

Days = [monday|_], Days = [day1|_]

您必须在此处进行一些更改. day1是一个常量,而应该是一个变量.

You have to change there something. day1 is a constant, it rather should be a variable.

稍后,将所有X \= const替换为dif(X, const).

这篇关于解决Prolog中有关时间限制的难题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆