Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I Need these code on Python This term project 2/2 is intended to give you practice programming in Prolog, which will rely on your understanding

I Need these code on Python This term project 2/2 is intended to give you practice programming in Prolog, which will rely on your understanding of backtracking and matching. The questions represent different types of use cases for Prolog, from database-like queries, to knowledge representation (concept definitions), to decision-making and problem-solving. I recommend you use SWI-Prolog (www.swi-prolog.org). 1. Write rules in Prolog to infer various kinship relationships in terms of basic predicates like parent(X,Y) and female(X) and male(Y). Input the following facts about people on The Simpsons: parent(bart,homer). parent(bart,marge). parent(lisa,homer). parent(lisa,marge). parent(maggie,homer). parent(maggie,marge). parent(homer,abraham). parent(herb,abraham). parent(tod,ned). parent(rod,ned). parent(marge,jackie). parent(patty,jackie). parent(selma,jackie). female(maggie). female(lisa). female(marge). female(patty). female(selma). female(jackie). male(bart). male(homer). male(herb). male(burns). male(smithers). male(tod). male(rod). male(ned). male(abraham). Write rules to define the following relationships: brother(), sister(), aunt(), uncle(), grandfather(), granddaughter(), ancestor(), descendant(), and unrelated(). Use the convention that relation(X,Y) means "the relation of X is Y)". For example, uncle(bart,herb) means the uncle of bart is herb. Use your rules to answer the following queries: ?- brother(rod,X). X = tod ; ?- sister(marge,X). X = selma ; X = patty ; ?- aunt(X,patty). X = bart ; X = lisa ; X = maggie ; ?- uncle(bart,X). X = herb ; ?- grandfather(maggie,X). X = abraham ; ?- granddaughter(jackie,lisa). true ?- ancestor(bart,X). X = homer ; X = marge ; X = abraham ; X = jackie ; ?- unrelated(tod,bart). true ?- unrelated(maggie,smithers). true ?- unrelated(maggie,selma). false 2. Using the following database, write a Prolog query to find all the surgeons who live in Texas and make over $100,000/yr. You will have to add some additional data, such as about different types of surgeons, or city-state relationships. occupation(joe,oral_surgeon). occupation(sam,patent_laywer). occupation(bill,trial_laywer). occupation(cindy,investment_banker). occupation(joan,civil_laywer). occupation(len,plastic_surgeon). occupation(lance,heart_surgeon). occupation(frank,brain_surgeon). occupation(charlie,plastic_surgeon). occupation(lisa,oral_surgeon). address(joe,houston). address(sam,pittsburgh). address(bill,dallas). address(cindy,omaha). address(joan,chicago). address(len,college_station). address(lance,los_angeles). address(frank,dallas). address(charlie,houston). address(lisa,san_antonio). salary(joe,50000). salary(sam,150000). salary(bill,200000). salary(cindy,140000). salary(joan,80000). salary(len,70000). salary(lance,650000). salary(frank,85000). salary(charlie,120000). salary(lisa,190000). 3. Write a prolog function to remove duplicates from a list: ?- remdups([1,3,4,2,4,3,6,8,6,5,4,2,3,4,9],X). X = [1, 8, 6, 5, 2, 3, 4, 9] For this problem, you will need to know how lists are represented in Prolog, and how matching (or unification) of [X|Y] with a list binds X to the head and Y to the tail. hint: define remdups() recursively; use member(.,.) 4. Implement prime factorization in Prolog. Here is an example: ?- factor(120,M). M = [5, 3, 2, 2, 2] ?- factor(7,P). P = [7] Note: you can use a simple sieve algorithm that iterates from 2 up to N (or sqrt(N)) and tests for divisibility using mod. For example: divisible(N,X) :- M is N mod X, M=0. You will probably want to write an auxilliary function that determines the smallest factor for a given number, and then recursively builds up a list of factors as you go. 5. SEND + MORE = MONEY is a classical "cryptarithmetic" puzzle: the variables S, E, N, D, M, O, R, Y represent digits between 0 and 9, and the task is finding values for them such that the following arithmetic operation is correct: S E N D + M O R E ---------------- M O N E Y Moreover, all variables must take unique values, and all the numbers must be wellformed (which implies that M > 0 and S > 0). 6. Write rules in Prolog to determine the best move in Tic-Tac-Toe for any given board configuration. Assume that the position of pieces is given by a predicate 'p'. For example, consider the following board state: x . x . . . o . o % assert these facts as the state description p(x,1,1). p(x,1,3). p(o,3,1). p(o,1,3). ?- ttt_move(x,R,C). % query go for win! % printed as a side-effect R = 1, C = 2 ; % solution Here is another example... x . . . . x o . o p(x,1,1). p(x,2,3). p(o,1,3). p(o,3,3). ?- ttt_move(x,R,C). move to block opponent! R = 3, C = 2 ; ?- ttt_move(o,R,C). go for win! R = 3, C = 2 ;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

=+6. What media are available to you?

Answered: 1 week ago

Question

Effective Delivery Effective

Answered: 1 week ago