×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Einsteins problem in prolog

Einsteins problem in prolog

Einsteins problem in prolog

(OP)
I have develop the following code to solve einsteins problem.The version of my problem is:there are 5 offices and 5 employees.Each office has different color.Each employee works in different department ,he drinks different drink,he has different cellphone and pc. The problem has the following constraints

the employee from informatics department has the red office.
the employee from management department has a macbook pro.
the employee who has the green office drinks coffe
the employee from economics department drinks tea.
the green office is right from orange office
the employee who has iphone has macbook air
the employee who has blackberry has the yellow office
the employee who has the middle office drinks milk
the employee from public relations has the first office
the employee who has nokia is next from employee with netbook
the employee who has blackberry is next from employee with windows 7 laptop
the employee who has android drinks juice
the employee from supplies department has erricson
the employee from public relations is next from to office

the programm have to find who has windows xp laptop and who drinks water

CODE

%office(position,color,department,computer,drink,cellphone)

solution1(Department1):-office(P1,Color1,Department1,windowsxplaptop,Drink1,Cellphone1),office(P2,Color2,Department2,Computer2,Drink2,Cellphone2),office(P3,Color3,Department3,Computer3,DrinK3,Cellphone3),office(P4,Color4,Department4,Computer4,Drink4,Cellphone4),office(P5,Color5,Department5,Computer5,Drink5,Cellphone5),P1\=P2,P2\=P3,P3\=P4,Department1\=Department2,Department2\=Department3,Department3\=Department4,Department4\=Department5.


solution2(Department1):-office(P1,Color1,Department1,Computer1,water,Cellphone1),office(P2,

Color2,Department2,Computer2,Drink2,Cellphone2),office(P3,Color3,Department3,Computer3,Drin

K3,Cellphone3),office(P4,Color4,Department4,Computer4,Drink4,Cellphone4),office(P5,Color5,D

epartment5,Computer5,Drink5,Cellphone5),P1\=P2,P2\=P3,P3\=P4,Department1\=Department2,Depar

tment2\=Department3,Department3\=Department4,Department4\=Department5.



%constraints

office(_,red,informatics,_,_,_).
office(_,_,management,macbookpro,_,_).
office(_,green,_,_,coffee,_).
office(_,_,economics,_,tea,_).
office(_,_,_,macbookair,_,iphone).
office(_,yellow,_,_,_,blackberry).
office(3,_,_,_,milk,_).
office(1,_,relations,_,_,_).
office(_,_,_,_,juice,android).
office(_,_,supplies,_,_,ericsson).
office(X,green,_,_,_,_)
office(X<Y,orange,_,_,_,_).
office(Z,_,_,netbook,_,_)
office(N is Z+1,_,_,_,_,nokia).%is next to
office(N is Z-1,_,_,_,_,nokia).%is next to
office(K,_,_,windows7laptop,_,_).
office(K is K+1,_,_,_,_,blackberry).
office(K is K-1,_,_,_,_,blackberry).
office(M,blue,_,_,_,_).
office(M is M+1,_,relations,_,_,_).
office(M is M-1,_,relations,_,_,_). 
solution1(X) tries to solve the first goal and solution2(X) the second

this problem with specific constraints can be solved but prolog cant find a solution.
Is something wrong with my code?

RE: Einsteins problem in prolog

You are lost. This is a very wrong way.
office(M is M+1,_,relations,_,_,_). doesn't do what you think because M is M+1 is not evaluate ans stays M is M+1 all along the code.
The first arg position is not usefull in the problem.
Try this kind of thing:

CODE --> Prolog

%office(color,department,computer,drink,cellphone)
init(office(_Color,_Department,_Computer,_Drink,_Cellphone)).

solve(Z) :-
	length(Z, 5),
	maplist(init,Z),
	rule1(Z),
	rule2(Z),
	rule3(Z),
	rule4(Z),
	rule5(Z),
	rule6(Z),
	rule7(Z),
	rule8(Z),
	rule9(Z),
	rule10(Z),
	rule11(Z),
	rule12(Z),
	rule13(Z),
	rule14(Z).

% the employee from informatics department has the red office.
rule1(L) :-
	member(office(red,informatics, _, _, _), L).

% the employee from management department has a macbook pro.
rule2(L) :-
	member(office(_,management,macbook_pro, _, _), L).

% the employee who has the green office drinks coffe
rule3(L) :-
	member(office(green,_,_,coffee, _), L).

	% the employee from economics department drinks tea.
rule4(L) :-
	member(office(_,economics,_,tea, _), L).
..... 
I don't understand the last rule % the employee from public relations is next from to office

RE: Einsteins problem in prolog

(OP)

The public relations office is next to blue office.This is the last rule.
How can i represent for example these rules "the green office is right from orange office","the employee who has blackberry is next from employee with windows 7 laptop" without position argument?I am a little confused.

RE: Einsteins problem in prolog

a is rigth from b in the list LThis means that L = [_,_...b,a, ...], so if you use append, you can write append(something, [b,a|otherthing], L) in Prolog append(_, [b ,a |_], L).
a is next b in the list Lmeans that either a is left to b, or b is left to a, the conjonction is written ";" in Prolog so
a is next to b in the list L can be written (append(_, [a ,b |_], L); append(_, [b ,a |_], L))

RE: Einsteins problem in prolog

Sorry, I made a mistake, I wanted to say disjunction and I wrote conjunction.

RE: Einsteins problem in prolog

(OP)
Thanks for the hint.
I wrote this
for example rule 14
rule14(L):-append(_,[office(_,publicrelations,_,_,_),office(blue,_,_,_,_)|_],L);append(_,[office(blue,_,_,_,_),office(_,publicrelations,_,_,_)|_],L).Is it correct?
I dont get the desired results.

RE: Einsteins problem in prolog

I wrote exactly that, and I get the desired solution. What do you get ? false ? Be ware of typo.

RE: Einsteins problem in prolog

(OP)
You are right!!I get the correct solution. It was a mistyping in rule12 thank you!!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close