Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Einsteins problem in prolog 1

Status
Not open for further replies.

billpr37

Programmer
Jan 15, 2013
4
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?
 
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:
%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
 

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.
 
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))
 
Sorry, I made a mistake, I wanted to say disjunction and I wrote conjunction.
 
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.
 
I wrote exactly that, and I get the desired solution. What do you get ? false ? Be ware of typo.
 
You are right!!I get the correct solution. It was a mistyping in rule12 thank you!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top