【问题标题】:Prolog Jobs PuzzleProlog 工作拼图
【发布时间】:2016-07-20 04:37:51
【问题描述】:

我是 prolog 的新手,必须完成这项任务。 我很困惑如何与我所拥有的事实和我自己得出的结论建立关系。 在这个问题的代码部分,你会找到一个给定信息的列表和我从数据中得出的结论。 如果我能在正确的方向上得到指导以完成我的任务,我将不胜感激。 提前谢谢你!

   %There are three people: John, Jim, and Mary and each has two jobs.
   %The jobs are gardener, veterinarian's  assistant, dishwasher, nurse,
   %high school math teacher, and biology tutor.
   %You are given the following information:
   %1) The nurse went out with the veterinarian's assistant last night.
   %2) Mary is friends with the biology tutor but she stayed home last night.
   %3) Jim likes animals but he failed math and had to drop out of college.
   %4) Jim does not know the gardener.
   %5) Mary and the biology tutor used to be married.

   %My Conclusions from Facts:
   %6) Mary is not the biologyTutor because she did not go out with the
   % biologyTutor and she was married to the biology tutor (2&5)
   %7) Jim is not the highSchoolMathTeacher because he failed math (3)
   %8) Jim is not the gardener because he does not know the gardener (4)
   %9) Mary is not the nurse/vetAssistant because she did not go out (1&2)
   %10) The biologyTutor went out last night (1&2)
   %Who holds which jobs? Include a report explaining your strategy.

   %Exepcted Answers:
   %Mary: gardener, highSchoolMathTeacher
   %Jim: vetAssistant, dishWasher
   %John: nurse, biologyTutor

   %Given Information, aka FACTS:
   %person(Name).
   person(mary).
   person(jim).
   person(john).

   %job(JobName).
   job(gardener).
   job(vetAssistant).
   job(dishWasher).
   job(nurse).
   job(highSchoolMathTeacher).
   job(biologyTutor).

   went_Out(nurse,vetAssistant).
   friends(mary,biologyTutor).
   likes(jim,animals).
   unknown(jim,gardener).
   divorced(mary,biologyTutor).

对原始问题的更新:

这就是我所拥有的,我遇到了一个非常奇怪的错误:

%list of people
person(mary).
person(jim).
person(john).
%list of jobs
job(gardener).
job(vetAssistant).
job(dishWasher).
job(nurse).
job(highSchoolMathTeacher).
job(biologyTutor).

%length of variables are 2 &
%Solution is person and corresponding variables
length(MaryJobs,2),
length(JimJobs,2),
length(JohnJobs,2),
Solution = [mary-MaryJobs,jim-JimJobs,john-JohnJobs],

%query to find the jobs AllJobs is a list containing variables of the jobs
findAll(Jobs,job(Job),AllJobs),
AllJobs = [Gardener,VetAssistant,DishWasher,Nurse,MathTeacher,BioTutor],

%Note: im not sure about flatten 
flatten([MaryJobs,JimJobs,JohnJobs],Jobs),
permutation(Jobs,AllJobs),

% 6 & 9; Mary is not the Nurse, VetAssistant, or BioTutor
\+ member(Nurse,MaryJobs),
\+ member(VetAssistant,MaryJobs),
\+ member(BioTutor, MaryJobs),

% 7 & 8 & 3 ; Jim is not the MathTeacher or Gardener
\+ member(MathTeacher,JimJobs),
\+ member(Gardener, JimJobs),

%Mary is the Gardener because Jim does not know the
%Gardener, therefore he cannot have gone out with the Gardener.
\+ member(Gardener, JohnJobs),

%Jim must not KNOW Mary because she is the Gardener
%John and Mary must have been married
%Conclusion: Jim is not the Bio Tutor
\+ member(BioTutor, JimJobs),

%logically, since Jim likes animals, it would make sense if he
%were the VetAssistant and since this is true, John is the nurse
\+ member(VetAssistant, JohnJobs),
\+ member(Nurse, JimJobs),
%logically since jim dropped out of college, it would make sense
%if he were to be the dishwasher
\+ member(DishWasher, MaryJobs),
\+ member(DishWasher, JohnJobs).
%Automatically this is should conclude that Mary is the MathTeacher

如果我去掉逗号并用句点替换,我会得到错误。 主要错误在于包含代码:

length(MaryJobs,2),
length(JimJobs,2),
length(JohnJobs,2),
Solution = [mary-MaryJobs,jim-JimJobs,john-JohnJobs],

我收到的错误信息是:

 Warning: /Users/KaitlynChait/Desktop/School/CCNY/Summer 2016/Artificial Intelligence/CSC448_program_2/program2.pl:16:
Singleton variables: [Solution,Job]

ERROR: /Users/KaitlynChait/Desktop/School/CCNY/Summer 2016/Artificial Intelligence/CSC448_program_2/program2.pl:16:
Full stop in clause-body?  Cannot redefine ,/2

% /Users/KaitlynChait/Desktop/School/CCNY/Summer 2016/Artificial Intelligence/CSC448_program_2/program2.pl compiled 0.00 sec, 9 clauses
1 ?- 

【问题讨论】:

  • 你的作业的最后陈述非常明确:你必须解释你的策略。因此,请给我们一个提示(或询问)您认为可能的可计算解决方案
  • 你应该把所有的代码放在相关的谓词中。因此,在以length(MaryJobs,..) 开头的地方,插入一个谓词名称,例如:solve(Solution) :- length(...)。其次,在您的 findall 语句中,您有 JobJobs - 因此单例警告,请尝试将它们重命名为相同。

标签: prolog zebra-puzzle


【解决方案1】:

由于这是一项您必须解释工作流程的作业,因此我不会提供完整的工作代码,而是提供一些想法和指示,以帮助您解决此问题。

型号

您将需要一个代表解决方案的模型,该模型的创建方式使您可以轻松访问所需的变量。举个例子:

length(MaryJobs,2),
length(JimJobs,2),
length(JohnJobs,2),
Solution = [mary-MaryJobs,jim-JimJobs,john-JohnJobs]

现在我们有 3 个变量分别代表 Mary、Jim 和 John 的两个工作。

其次,我们需要使用可用的工作事实来处理它们,所以我们会做一些类似的事情:

findall(Job,job(Job),AllJobs),
AllJobs = [Gardener,VetAssistant,DishWasher,Nurse,MathTeacher,BioTutor].

此时,我们已经获得了有关模型的所有必需信息,我们可以编写模型与所有可用作业之间的关系,如下所示:

flatten([MaryJobs,JimJobs,JohnJobs],Jobs),
permutation(Jobs,AllJobs)

请注意,在这一行 Prolog 将开始在不同选项之间进行分支。

规则

您已经在问题中用英文写出了大部分规则,所以真正剩下的就是将这些规则翻译成 Prolog。我们来看一个例子:

“兽医助理和护士出去了 + 玛丽没有出去。”

正如您所说,这含蓄地说:“玛丽不能是护士,也不能是兽医助理”

所以,只需在代码中:

\+ member(Nurse,MaryJobs),
\+ member(VetAssistant,MaryJobs)

如果您遵循这些思路并将您的每一条英语规则翻译成 Prolog 代码,您最终可能会得到一些解决方案(就像我自己一样)。要获得最终结果,还有一个您尚未提及的“隐藏”隐式规则。我可以在这里写下来,但也许你应该先尝试一下,毕竟它很有趣。

祝你好运!

【讨论】:

  • 非常感谢!!这确实有助于我了解我的任务去哪里,但我必须问一些小问题才能保证。 Solution = [mary-MaryJobs,jim-JimJobs,john-JohnJobs] 这不仅是解决方案列表,但我不确定 - 语法。
  • (-)/2 仅表示对。有关更多信息,请参阅this 答案。您也可以省略名称,而是使用Solution = [MaryJobs,JimJobs,JohnJobs],我只是在返回最终结果时添加它们以使您更清楚。
  • 我是否保留相同的人员和工作列表?
  • 更好,但我试图理解语法,因为我只是试图运行一个测试,我得到了各种各样的错误。
  • 请参阅您的问题下方关于您收到的错误的评论。 (从手机发送,现在有点难以编写/解释,以后肯定会做!)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-15
相关资源
最近更新 更多