0% found this document useful (0 votes)
34 views12 pages

MCAIII - Lab Assessement

The monkey banana problem is solved using logic programming in Prolog. The program defines possible moves like climb, drag, and walk to change the monkey's state. It uses recursion to find a path of moves to reach a state where the monkey can get the banana, by calling the move predicates until reaching a state with the predicate canget.

Uploaded by

kanika chaudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views12 pages

MCAIII - Lab Assessement

The monkey banana problem is solved using logic programming in Prolog. The program defines possible moves like climb, drag, and walk to change the monkey's state. It uses recursion to find a path of moves to reach a state where the monkey can get the banana, by calling the move predicates until reaching a state with the predicate canget.

Uploaded by

kanika chaudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

COER UNIVERSITY

7th K.M. on Roorkee Haridwar Road (NH-58),


Post Box No. 27, Vardhmanpuram,
Roorkee- 247667 District. Haridwar (Uttarakhand)
Website: www.coeruniversity.in

Lab Assessment File

Submitted By: Students Name


Submitted To: Mr. Shalendra
Department: Computer Science and Applications
Semester: MCA III
Academic Session: 2023-2024
Subject name: Artificial Intelligence
Subject code: MCA392
INDEX
Student Name:…………………….. Roll No………………………
Sub Name:…………………………… Code:…………………………
Course:……........ Semester:…………. Section:…………..

S.No. Assessment Date Faculty Remarks


Signature
1 Assessment 1
2 Assessment 2
3 Assessment 3
4 Assessment 4
5 Assessment 5
6 Assessment 6
7 Assessment 7
8 Assessment 8
9 Assessment 9
10 Assessment 10
11 Assessment 11
12 Assessment 12
13 Assessment 13
14 Assessment 14
15 Assessment 15
16 Assessment 16
17 Assessment 17
18 Assessment 18
19 Assessment 19
20 Assessment 20
21 Assessment 21
22 Assessment 22
Assessment 1

Problem statement 1 – write simple relation and facts and find out answer to queries.

In Prolog we can make some statements by using facts. Facts either consist of a particular item or a
relation between items. Facts have some simple rules of syntax. Facts should always begin with a
lowercase letter and end with a full stop. The facts themselves can consist of any letter or number
combination, as well as the underscore _ character. However, names containing the characters -, +,
*, /, or other mathematical operators should be avoided.

Write simple fact for following:


1). Ram likes mango.
2). Seema is a girl.
3). Bill likes Cindy.
4). Rose is red.
5). John owns gold.

Program:
Clauses
Likes (ram, mango). girl (Seema). red(rose). Likes (bill, Cindy). Owns (john, gold).
Output:
Goal queries
?-likes(ram, What).
What= mango
?-likes(Who, cindy). Who= cindy
?-red(What).
What= rose
?-owns(Who, What).
Who= john
What= gold.

Outcome
Student will understand how to write simple facts using prolog.
Assessment 2

Problem statement 2 – This program is designed to answer questions about relationships within a
given family tree. The program will tell you who the mother, father, sister, brother, aunt, uncle,
grandmother, grandfather, brother-in-law, sister-in-law, mother-in-law, father-in-law, ancestor, and
descendent of someone is. The standard form is predicate (someone, relation).
where the relation will be the mother or father or so on.

male(james1).
male(charles1).
male(charles2).
male(james2).
male(george1).
male(paul).
male(sam).

female(catherine).
female(elizabeth).
female(sophia).
female(claudia).
female(fay).

/* parent ( child, parent). */


parent(charles1, james1).
parent(elizabeth, james1).
parent(charles2, charles1).
parent(catherine, charles1).
parent(james2, charles1).
parent(sophia, elizabeth).
parent(george1, sophia).
parent(george1, sam).
parent(catherine, fay).
parent(charles2, fay).
parent(james2, fay).
parent(sophia, paul).
parent(elizabeth,claudia).
parent(charles1, claudia).

/* married ( A,B) - A is married to B */


married ( james1, claudia).
married( claudia, james1).
married(charles1, fay).
married(fay, charles1).
married(elizabeth, paul).
married(paul, elizabeth).
married(sophia, sam).
married(sam, sophia).

Outcome
/* rules*/
/* Dad is the father of Child if he is male and is the child's parent */

father (Child, Dad): - male (Dad), parent (Child, Dad).

/* Mom is the mother of Child if she is female and the child's parent*/
mother (Child, Mom): - female (Mom), parent (Child, Mom).

/* Bro is the brother of Sibling if he is male and has the same parents as the sibling*/

brother (Sibling, Bro): - male(Bro), father(Sibling, Father), father(Bro, Father), Bro \= Sibling,
mother(Sibling, Mother), mother(Bro, Mother).

/* Sis is the sister of Sibling if she is female and has the same parents as the sibling*/

sister (Sibling, Sis) :- female(Sis), father(Sibling, Father), father(Sis, Father), Sis \=


Sibling, mother(Sibling, Mother), mother(Sis, Mother).

/* Auntie is the aunt of Kid if she is female and a sister of the kid's parent or she is female
and married to the kid's uncle. This is the definition of an Aunt.

1. The sister of one's father or mother.


2. The wife of one's uncle.
*/

aunt (Kid, Auntie): - female(Auntie), parent(Kid, Parent), sister(Parent, Auntie).


aunt (Kid, Auntie): - female(Auntie), parent(Kid, Person), brother(Person, Brother),
married(Auntie, Brother).

/* Unclebuck is the uncle of Kid if he is male and a brother of the kid's parent or he is male
and married to the kid's aunt. This is the definition of an Aunt.

1. The brother of one's mother or father.


2. The husband of one's aunt.
*/

uncle(Kid, UncleBuck) :- male(UncleBuck), parent(Kid, Parent), brother(Parent, UncleBuck).


uncle(Kid, UncleBuck) :- male(UncleBuck), parent(Kid, Person), sister(Person, Sister),
married(UncleBuck, Sister).

/* Grandmother is the grandmother of Grandchild if she is female and the parent of the
grandchild's parent */

grandmother (Grandchild, Grandmother):- female(Grandmother), parent(Grandchild, Parent),


parent(Parent, Grandmother).

/* Grandfather is the grandfather of GrandChild if he is male and the parent of the child's
parent */

grandfather(Grandchild, Grandfather) :- male(Grandfather), parent(Grandchild, Parent),


parent(Parent, Grandfather).

/*
BInLaw is the brother in law of SiblingInLaw
You are the brother in law if: you are male and The brother of one's spouse.
The husband of one's sister.The husband of the sister of one's spouse
*/

brother_in_law(SiblingInLaw, BInLaw) :- married(SiblingInLaw, Person), brother(Person,


BInLaw).
brother_in_law(SiblingInLaw, BInLaw) :- sister(SiblingInLaw, Sister), married(Sister, BInLaw).
brother_in_law(SiblingInLaw, BInLaw) :- married(SiblingInLaw, Person), sister(Person, Sister),
married(Sister, BInLaw).

/*
SInLaw is the sister in law of SiblingInLaw
You are the sister in law if: you are female and The sister of one's spouse.
The wife of one's brother. The wife of the brother of one's spouse.
*/

sister_in_law(SiblingInLaw, SInLaw) :- married(SiblingInLaw, Person), sister(Person, SInLaw).


sister_in_law(SiblingInLaw, SInLaw) :- brother(SiblingInLaw, Brother), married(Brother,
SInLaw).
sister_in_law(SiblingInLaw, SInLaw) :- married(SiblingInLaw, Person), brother(Person, Brother),
married(Brother, SInLaw).
/*
MInLaw is the mother in law of ChildInLaw You are the mother in law if: you are female
and your son is married to ChildInLaw or your daughter is married to ChildInLaw
*/

mother_in_law(ChildInLaw, MInLaw) :- female(MInLaw), married(ChildInLaw, Child),


parent(Child, MInLaw).

/*
FInLaw is the father in law of ChildInLaw
You are the father in law if: you are male and your son is married to
ChildInLaw or your daughter is married to ChildInLaw
*/

father_in_law(ChildInLaw, FInLaw) :- male(FInLaw), married(ChildInLaw, Child), parent(Child,


FInLaw).

/* Person is an ancestor of Descendent. You are the ancestor if you are the parent of the
person or you are the parent of the person's parent and so forth
*/

ancestor(Person, Ancestor) :- parent(Person, Ancestor).


ancestor(Person, Ancestor) :- parent(Person, Parent), ancestor(Parent, Ancestor).

/* Person is a descendent of Ancestor


You are the descendent if you are the child of the person or your parent is the child of the
person and so forth
*/

descendent(Person, Descendent) :- parent(Descendent, Person).


descendent(Person, Descendent) :- parent(Descendent, Someone), descendent(Person, Someone).
Assessment 3

Program Statement 3 - Write predicates one convert centigrade temperature to Fahrenheit, the other
checks if a temperature is below freezing.

Prolog expressions are comprised of the following truth-functional symbols, which have the same
interpretation as in the predicate calculus.

English Predicate Calculus PROLOG


and ^ ,
or v ;
if --> :-
not ~ not

Arithmetic:

c_to_f f is c * 9 / 5 +32
freezing f < = 32

Rules:
c_to_f(C,F) :-
F is C * 9 / 5 + 32.
freezing(F) :-
F =< 32.

Output:
Queries:
?- c_to_f(100,X).
X = 212
Yes
?- freezing(15)
.Yes
?- freezing(45).
No

OUTCOME: Student will understand how to write a program using the rules.
Assessment 4

Problem statement 4 – write a program to solve Monkey Banana problem.

The problem is as given below −


A hungry monkey is in a room, and he is near the door.
The monkey is on the floor.
Bananas have been hung from the center of the ceiling of the room.
There is a block (or chair) present in the room near the window.
The monkey wants the banana, but cannot reach it.

Program
move(state(middle,onbox,middle,hasnot),
grasp,
state(middle,onbox,middle,has)).
move(state(P,onfloor,P,H),
climb,
state(P,onbox,P,H)).
move(state(P1,onfloor,P1,H),
drag(P1,P2),
state(P2,onfloor,P2,H)).
move(state(P1,onfloor,B,H),
walk(P1,P2),
state(P2,onfloor,B,H)).
canget(state(_,_,_,has)).
canget(State1) :-
move(State1,_,State2),
canget(State2).

Output

| ?- [monkey_banana].
/*compiling*/

(31 ms) yes


| ?- canget(state(atdoor, onfloor, atwindow, hasnot)).

true ?

yes
| ?- trace
.
The debugger will first creep -- showing everything (trace)

yes
{trace}
| ?- canget(state(atdoor, onfloor, atwindow, hasnot)).
Call: canget(state(atdoor,onfloor,atwindow,hasnot)) ?
Call: move(state(atdoor,onfloor,atwindow,hasnot),_52,_92) ?

Exit:move(state(atdoor,onfloor,atwindow,hasnot),walk(atdoor,_80),state(_80,onfloor,atwindow,ha
snot)) ?
Call: canget(state(_80,onfloor,atwindow,hasnot)) ?
Call: move(state(_80,onfloor,atwindow,hasnot),_110,_150) ?
Exit:
move(state(atwindow,onfloor,atwindow,hasnot),climb,state(atwindow,onbox,atwindow,hasnot)) ?
Call: canget(state(atwindow,onbox,atwindow,hasnot)) ?
Call: move(state(atwindow,onbox,atwindow,hasnot),_165,_205) ?
Fail: move(state(atwindow,onbox,atwindow,hasnot),_165,_193) ?
Fail: canget(state(atwindow,onbox,atwindow,hasnot)) ?
Redo:
move(state(atwindow,onfloor,atwindow,hasnot),climb,state(atwindow,onbox,atwindow,hasnot)) ?
Exit:
move(state(atwindow,onfloor,atwindow,hasnot),drag(atwindow,_138),state(_138,onfloor,_138,has
not)) ?
Call: canget(state(_138,onfloor,_138,hasnot)) ?
Call: move(state(_138,onfloor,_138,hasnot),_168,_208) ?
Exit: move(state(_138,onfloor,_138,hasnot),climb,state(_138,onbox,_138,hasnot)) ?
Call: canget(state(_138,onbox,_138,hasnot)) ?
Call: move(state(_138,onbox,_138,hasnot),_223,_263) ?
Exit: move(state(middle,onbox,middle,hasnot),grasp,state(middle,onbox,middle,has)) ?
Call: canget(state(middle,onbox,middle,has)) ?
Exit: canget(state(middle,onbox,middle,has)) ?
Exit: canget(state(middle,onbox,middle,hasnot)) ?
Exit: canget(state(middle,onfloor,middle,hasnot)) ?
Exit: canget(state(atwindow,onfloor,atwindow,hasnot)) ?
Exit: canget(state(atdoor,onfloor,atwindow,hasnot)) ?

true ?
Assessment 5

Problem statement 5 – write a food table program. shows the facts, rules, goals and their English
meanings.

Facts
English meanings
food(burger). // burger is a food
food(sandwich). // sandwich is a food
food(pizza). // pizza is a food
lunch(sandwich). // sandwich is a lunch
dinner(pizza). // pizza is a dinner

Rules
meal(X) :- food(X).

// Every food is a meal OR


Anything is a meal if it is a food

Queries / Goals
?- food(pizza).
// Is pizza a food?
?- meal(X), lunch(X).
// Which food is meal and lunch?
?- dinner(sandwich).
// Is sandwich a dinner?
Assessment 6

Problem statement 6 – write student-professor relation table shows the facts, rules, goals and their
English meanings.

Facts
English meanings
studies(charlie, csc135).
// charlie studies csc135
studies(olivia, csc135).
// olivia studies csc135
studies(jack, csc131).
// jack studies csc131
studies(arthur, csc134).
// arthur studies csc134

teaches(kirke, csc135).
// kirke teaches csc135
teaches(collins, csc131).
// collins teaches csc131
teaches(collins, csc171).
// collins teaches csc171
teaches(juniper, csc134).
// juniper teaches csc134

Rules

professor(X, Y) :-
teaches(X, C), studies(Y, C).

// X is a professor of Y if X teaches C and Y studies C.

Queries / Goals

?- studies(charlie, What).

// charlie studies what? OR


What does charlie study?
?- professor(kirke, Students).

// Who are the students of professor kirke.

You might also like