Reputation: 185
I'm new to prolog and have to complete this assignment. I'm very confused on how to make relationships with the facts that I have and the conclusions I have made on my own. In the code section of this question, you will find a list of given information and my conclusions from the data. If I could be guided in the right direction in order to complete my assignment, I would appreciate it. Thank You in advance!
%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).
UPDATE TO THE ORIGINAL QUESTION:
So here's what I have and I'm getting a very weird error:
%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
If i take out the commas and replace with periods, i get errors. The main error is on the containing the code:
length(MaryJobs,2),
length(JimJobs,2),
length(JohnJobs,2),
Solution = [mary-MaryJobs,jim-JimJobs,john-JohnJobs],
The Error message I am receiving is:
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 ?-
Upvotes: 1
Views: 381
Reputation: 1557
Since this is an assignment where you must explain your workflow, I will not provide full working code, but rather give some ideas and pointers to how you can approach this problem.
MODEL
You're going to want a model representing the solution, created in such a way that you can access the required variables easily. Take for example:
length(MaryJobs,2),
length(JimJobs,2),
length(JohnJobs,2),
Solution = [mary-MaryJobs,jim-JimJobs,john-JohnJobs]
Now we have 3 variables representing the two jobs of respectively Mary, Jim and John.
Secondly, we need to use the job facts available in order to work with them, so we do something in the likes of:
findall(Job,job(Job),AllJobs),
AllJobs = [Gardener,VetAssistant,DishWasher,Nurse,MathTeacher,BioTutor].
At this point we have all the required information concerning our model and we can write the relation between our model and all available jobs like so:
flatten([MaryJobs,JimJobs,JohnJobs],Jobs),
permutation(Jobs,AllJobs)
Note that at this line Prolog will start branching between different options.
RULES
You have already written out most of the rules in English in your question, so all that really rests is translating those into Prolog. Let's look at an example:
"The VetAssistant went out with the Nurse + Mary didn't go out."
This implicitly says, as you yet stated: "Mary cannot be the Nurse, nor the VetAssistant"
So, simply in code:
\+ member(Nurse,MaryJobs),
\+ member(VetAssistant,MaryJobs)
If you follow these lines of thought and translate every one of your English rules into Prolog code, you will probably end up with a few solutions left (as I had myself). To get the final result, there's just one more 'hidden' implicit rule which you haven't mentioned yet. I could write it down here, but maybe you should try and go ahead first, after all, it's a lot of fun.
Good luck!
Upvotes: 0