Jubecron Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Jubecron Forum

The JUBECRON forum - Jamaican Chat, Dating, Games, Anime, and just meeting people.
 
HomePortalSearchLatest imagesRegisterLog in

 

 Prolog Exercise 4

Go down 
AuthorMessage
PAPoUCH
Super Elite Lv2: Jr. Vet
Super Elite Lv2: Jr. Vet
PAPoUCH


Male
Number of posts : 1860
Age : 37
Mood : Prolog Exercise 4 Sunshine
Points : 298
Registration date : 2007-10-12

Character sheet
Weapon: Crowbar
Class: Thief Thief
HP:
Prolog Exercise 4 Left_bar_bleue50/50Prolog Exercise 4 Empty_bar_bleue  (50/50)

Prolog Exercise 4 Empty
PostSubject: Prolog Exercise 4   Prolog Exercise 4 Icon_minitimeTue Oct 12, 2010 7:33 pm

% Author: Kevin Stewart 0700410
% Date: 10/12/2010

atom_length(Arg1, Arg2). %returns length of Arg1 into Arg2

integer(Arg). %tests if Arg is integer returns true or false

Arg1 mod Arg2 %mod returns the remainder of deviding Arg1 by Arg2


Last edited by PAPoUCH on Tue Oct 19, 2010 7:36 pm; edited 1 time in total
Back to top Go down
http://www.penngosquad.forumotion.com
PAPoUCH
Super Elite Lv2: Jr. Vet
Super Elite Lv2: Jr. Vet
PAPoUCH


Male
Number of posts : 1860
Age : 37
Mood : Prolog Exercise 4 Sunshine
Points : 298
Registration date : 2007-10-12

Character sheet
Weapon: Crowbar
Class: Thief Thief
HP:
Prolog Exercise 4 Left_bar_bleue50/50Prolog Exercise 4 Empty_bar_bleue  (50/50)

Prolog Exercise 4 Empty
PostSubject: Exercise 4   Prolog Exercise 4 Icon_minitimeTue Oct 19, 2010 7:29 pm

QUESTION (Problem So1ving)

Formalize the following problem by implementing the necessary Prolog facts and rules.

People shop online everyday from different retailers for various things. Amazon and Ectow are cheap retailers, Ebay is expensive. Amazon’s origin is in Baltimore, Ebay is in Vancouver and Ectow is in Beijing. Amazon ships to Toronto and Beijing as well as all the cities in the country of its origin. Ebay ships to Beijing and Kingston only and Ectow ships to Kingston and Baltimore. Ectow and Amazon sells blackberrys and n95, Ebay sells camera.

A buyer making a purchase may have a preference regarding the cost, location of retailer that (s)he does transaction with, or it may be unimportant. Meya wants to buy a cheap blackberry or a N95 from a retailer who will ship to Jamaica. Rami wants a camera but doesn’t care about the cost, as long as the retailer will ship to Jamaica. Lisa wants to buy a cheap blackberry or a N95 from a retailer that is not originated in China, as long as they will ship the goods to Jamaica or USA.

Write a knowledge-base in PROLOG which encodes the above knowledge in as general a form as possible, and a predicate, which, when executed, generates a sequence of statements of the form:

can purchase from

All possibilities should be generated.

[For those whose Geography is a bit rusty, Toronto and Vancouver are in Canada, Beijing is in China, Baltimore in USA, and Kingston is in Jamaica].

Use the following vocabulary to assist you.
• person(X) means that the object denoted by x is a person.
• retailer (X,Y) X has cost Y.
• ships(X,Y) X can ship to Y
• origin(X,Y) X originates in Y
• located (X,Y) means that X is located in Y.

Assume other predicates as you see fit to generate all possibilities for Lisa, Meya and Rami, goods to purchase and the relevant retailers to use.









Solution (You may copy and paste solution into a prolog editor)

retailer(amazon,cheap).
retailer(ectow,cheap).
retailer(ebay,expensive).
origin(amazon,baltimore).
origin(ebay,vancouver).
origin(ectow,beijing).
ships(amazon,toronto).
ships(amazon,beijing).
ships(amazon,baltimore).
ships(ebay,beijing).
ships(ebay,kingston).
ships(ectow,beijing).
ships(ectow,kingston).
sells(ectow,blackberry).
sells(ectow,n95).
sells(amazon,blackberry).
sells(amazon,n95).
sells(ebay,camera).
located(toronto,canada).
located(vancouver,canada).
located(beijing,china).
located(baltimore,usa).
located(kingston,jamaica).
person(lisa).
person(meya).
person(rami).

%This function returns the retailer and product if the retailer sells blackberry or n95.

sells_either(Retailer,Prod):-sells(Retailer,Prod),Prod==n95;
sells(Retailer,Prod),Prod==blackberry.

meya :- person(Buyer),Buyer==meya,retailer(Retailer,cheap),
sells_either(Retailer,Prod),
ships(Retailer,Region),located(Region,jamaica),nl,
write(Buyer),write(' can purchase '),
write(Prod),write(' from '),write(Retailer).

rami :- person(Buyer),Buyer==rami,sells(Retailer,camera),
ships(Retailer,Region),located(Region,jamaica),nl,
write(Buyer),write(' can purchase camera from '),write(Retailer).




%This function returns the retailer if the retailer ships to a region that is located in jamaica or usa

ships_either(Retailer):-ships(Retailer,Region),located(Region,jamaica);
ships(Retailer,Region),located(Region,usa).

%This function checks if a retailer is located in a region found in china (retailer located in china).
% Note - not(locat_china(Retailer)) - will check for a retailer not located in china

locat_china(Retailer):-origin(Retailer,Region),located(Region,china).

lisa:-person(Buyer),Buyer==lisa,retailer(Retailer,cheap),
sells_either(Retailer,Prod),
not(locat_china(Retailer)), ships_either(Retailer),nl,
write(Buyer),write(' can purchase '),
write(Prod),write(' from '),write(Retailer).

%this function will call functions meya,rami and lisa to print results.

purchase:- meya,rami,lisa,fail.
Back to top Go down
http://www.penngosquad.forumotion.com
PAPoUCH
Super Elite Lv2: Jr. Vet
Super Elite Lv2: Jr. Vet
PAPoUCH


Male
Number of posts : 1860
Age : 37
Mood : Prolog Exercise 4 Sunshine
Points : 298
Registration date : 2007-10-12

Character sheet
Weapon: Crowbar
Class: Thief Thief
HP:
Prolog Exercise 4 Left_bar_bleue50/50Prolog Exercise 4 Empty_bar_bleue  (50/50)

Prolog Exercise 4 Empty
PostSubject: Re: Prolog Exercise 4   Prolog Exercise 4 Icon_minitimeTue Oct 19, 2010 7:33 pm

% Author: Kevin Stewart
% Date: 9/28/2010

cheap_retailer('Amazon').
cheap_retailer('Ectow').
expensive_retailer('Ebay').

origin('Amazon', 'Baltimore').
origin('Ebay', 'Vancouver').
origin('Ectow', 'Beijing').

ship_to('Amazon', 'Toronto').
ship_to('Amazon', 'Beijing').
ship_to('Amazon', 'Baltimore').
ship_to('Ebay', 'Beijing').
ship_to('Ebay', 'Kingston').
ship_to('Ectow', 'Kingston').
ship_to('Ectow', 'Baltimore').

sells('Amazon', 'blackberry').
sells('Amazon', 'n95').
sells('Ectow', 'blackberry').
sells('Ectow', 'n95').
sells('Ebay', 'camera').

goods('blackberry').
goods('n95').
goods('camera').

purchase(X):- X==meya, cheap_retailer(Y), sells(Y, Z), (Z==n95; Z==blackberry), ship_to(Y, 'Kingston'), nl,
write(X), write(' can purchase '), write(Z), write(' from '), write(Y), fail.
purchase(X):- X==rami, sells(Y, Z), Z==camera, ship_to(Y, 'Kingston'), nl,
write(X), write(' can purchase '), write(Z), write(' from '), write(Y), fail.
purchase(X):- X==lisa, cheap_retailer(Y), sells(Y, Z), (Z==blackberry; Z==n95), not(origin(Y,'Beijing')), (ship_to(Y, 'Kingston'); ship_to(Y, 'Baltimore')), nl,
write(X), write(' can purchase '), write(Z), write(' from '), write(Y), fail.
Back to top Go down
http://www.penngosquad.forumotion.com
Sponsored content





Prolog Exercise 4 Empty
PostSubject: Re: Prolog Exercise 4   Prolog Exercise 4 Icon_minitime

Back to top Go down
 
Prolog Exercise 4
Back to top 
Page 1 of 1
 Similar topics
-
» Prolog Exercise 1
» Prolog Exercise 3
» Prolog Exercise 2
» Prolog Exercise 5
» Prolog Lab 2

Permissions in this forum:You cannot reply to topics in this forum
Jubecron Forum :: School :: Schools (General)-
Jump to: