SlideShare a Scribd company logo
AI LAB
By:
Subash Chandra Pakhrin
MSc in Information and Communication Engineering
Kathmandu, Nepal
philpakhrin@gmail.com
+977-9852050900
Lab 1: Monkey and Banana Problem
Monkey is on floor, at door. A block is on floor, at window. Banana is
hanging from roof at the middle of the room. Problem is “How the
monkey can get the banana”.
Prolog Code (Monkey and Banana Problem)
move(state(middle,onbox,middle,hasnot),
grasp,
state(middle,onbox,middle,has)).
move(state(P,onfloor,P,H),
climb,
state(P,onbox,P,H)).
Prolog Code (Monkey and Banana Problem)
move(state(P1,onfloor,P1,H),
push(P1,P2),
state(P2, onfloor,P2,H)).
move(state(P1,onfloor,B,H),
walk(P1,P2),
state(P2,onfloor,B,H)).
Prolog Code (Monkey and Banana Problem)
canget(state(_,_,_,has)).
canget(State1) :-
move(State1,_,State2),
canget(State2).
Lab 2: Water Jug Problem
You are given two jugs, a 4 liter one and a 3 liter one, a pump which has
unlimited water which you can use to fill the jug, and the ground on
which water may be poured. Neither jug has any measurement
marking on it.
How can you get exactly 2 liter of water in the 4 liter jug ?
Lab 2: Water Jug Problem
We will represent a state of the problem as a ordered pair (x,y) where x
represents the amount of water in the 4 liter jug and y represents the
amount of water in the 3 liter jug.
Note 0 <= x <= 4, and 0 <= y <= 3.
Our initial state: (0,0)
Goal Predicate - state = (2,y) where 0 <=y<=3
Lab 2: Water Jug Problem
Lab 2: Water Jug Problem
water_jug(X,Y):-X<4,Y>3,write('3L water jug is overflowed.'),nl.
water_jug(X,Y):-X>4,Y>3,write('Both water jugs overflowed.'),nl.
water_jug(X,Y):-(X=:=0,Y=:=0,nl,write('4L:0 & 3L:3 (Action: Fill 3L
jug.)'),YY is 3,water_jug(X,YY));
(X=:=0,Y=:=0,nl,write('4L:4 & 3L=0 (Action: Fill 4L jug.)'),XX is
4,water_jug(XX,Y));
Lab 2: Water Jug Problem
(X=:=2,Y=:=0,nl,write('4L:2 & 3L=0 (Action: Goal State Reached....)'));
(X=:=4,Y=:=0,nl,write('4L:1 & 3L:3 (Action: Pour water from 4L
to 3L jug.)'),XX is X-3, YY is 3, water_jug(XX,YY));
(X=:=0,Y=:=3,nl,write('4L:3 & 3L:0 (Action: Pour water from 3L
jug to 4L jug.)'),XX is 3,YY is 0,water_jug(XX,YY));
(X=:=1,Y=:=3,nl,write('4L:1 & 3L:0 (Action:
Empty 3L jug.)'),YY is 0,water_jug(X,YY));
Lab 2: Water Jug Problem
• (X=:=3,Y=:=0,nl,write('4L:3 & 3L:3 (Action: Fill 3L jug.)'),YY is 3,
water_jug(X,YY));
• (X=:=3,Y=:=3,nl,write('4L:4 & 3L:2 (Action:
Pour water from 3L jug to 4L jug until 4L jug is full.)'),XX is X+1,YY is Y-
1,water_jug(XX,YY));
• (X=:=1,Y=:=0,nl,write('4L:0 & 3L:1 (Action:
Pour water from 4L to 3L jug.)'),XX is Y,YY is X,water_jug(XX,YY));
• (X=:=0,Y=:=1,nl,write('4L:4 & 3L:1 (Action: Fill
4L jug.)'),XX is 4,water_jug(XX,Y));
Lab 2: Water Jug Problem
(X=:=4,Y=:=1,nl,write('4L:2 & 3L:3 (Action: Pour water from 4L jug to 3L
jug until 3L jug is full.)'),XX is X-2,YY is Y+2,water_jug(XX,YY));
(X=:=2,Y=:=3,nl,write('4L:2 & 3L:0 (Action:
Empty 3L jug.)'),YY is 0,water_jug(X,YY));
(X=:=4,Y=:=2,nl,write('4L:0 & 3L:2 (Action:
Empty 4L jug.)'),XX is 0,water_jug(XX,Y));
(X=:=0,Y=:=2,nl,write('4L:2 & 3L:0 (Action:
Pour water from 3L jug to 4L jug.)'),XX is Y,YY is X,water_jug(XX,YY)).
Lab 3: Farmer Crosses River Puzzle
• A farmer wants to crass a river and take with him a wolf, a goat, and a
cabbage.
• There is a boat that can fit himself plus either the wolf, the goat, or
the cabbage.
• If the wolf and the goat are alone on one shore, the wolf will eat the
goat. If the goat and the cabbage are alone on the shore, the goat will
eat the cabbage.
• How can the farmer bring the wolf, the goat, and the cabbage across
the river ?
Lab 3: Farmer Crosses River Puzzle
Lab 3: Farmer Crosses River Puzzle
other_bank(e,w).
other_bank(w,e).
% farmer, wolf, goat, cabbage
move([X,X,Goat,Cabbage],wolf,[Y,Y,Goat,Cabbage]) :- other_bank(X,Y).
move([X,Wolf,X,Cabbage],goat,[Y,Wolf,Y,Cabbage]) :- other_bank(X,Y).
Lab 3: Farmer Crosses River Puzzle
move([X,Wolf,Goat,X],cabbage,[Y,Wolf,Goat,Y]) :- other_bank(X,Y).
move([X,Wolf,Goat,Cabbage],nothing,[Y,Wolf,Goat,Cabbage]) :-
other_bank(X,Y).
safety_check(X,X,_).
safety_check(X,_,X).
Lab 3: Farmer Crosses River Puzzle
safe_status([Man,Wolf,Goat,Cabbage]) :-
safety_check(Man,Goat,Wolf), safety_check(Man,Goat,Cabbage).
solution([e,e,e,e],[]).
solution(Config,[Move|OtherMoves]) :-
move(Config,Move,NextConfig),
Lab 3: Farmer Crosses River Puzzle
safe_status(NextConfig),
solution(NextConfig,OtherMoves).
% length(X,7), solution([w,w,w,w],X).

More Related Content

PDF
Lab report for Prolog program in artificial intelligence.
PDF
PPTX
8 queen problem
DOCX
Artificial Intelligence Lab File
PPTX
Intelligence Agent - Artificial Intelligent (AI)
PDF
Ai 03 solving_problems_by_searching
PPT
Solving problems by searching
PDF
Ai lecture 12(unit03)
Lab report for Prolog program in artificial intelligence.
8 queen problem
Artificial Intelligence Lab File
Intelligence Agent - Artificial Intelligent (AI)
Ai 03 solving_problems_by_searching
Solving problems by searching
Ai lecture 12(unit03)

What's hot (20)

DOCX
Artificial Intelligence
PPTX
Knowledge Engineering in FOL.
PPTX
AI-03 Problems State Space.pptx
PDF
AI_unit IV Full Notes.pdf
PPTX
State space search and Problem Solving techniques
PPTX
Monkey & banana problem in AI
PPT
AI Lecture 3 (solving problems by searching)
PPT
Artificial Intelligence 1 Planning In The Real World
PPT
Hashing PPT
PPTX
Artificial Intelligence PEAS
PPTX
AI_Session 7 Greedy Best first search algorithm.pptx
PDF
AI_Unit I notes .pdf
PDF
ORGAN DONATION MANAGEMENT SYSTEM (PROJECT: ODMS)
PDF
Design and analysis of algorithms
PPT
Counting sort(Non Comparison Sort)
PPTX
Problem Formulation in Artificial Inteligence Projects
PDF
HOSPITAL MANAGEMENT SYSTEM PROJECT
PDF
I.INFORMED SEARCH IN ARTIFICIAL INTELLIGENCE II. HEURISTIC FUNCTION IN AI III...
PPT
Lower bound
PPTX
Daa unit 5
Artificial Intelligence
Knowledge Engineering in FOL.
AI-03 Problems State Space.pptx
AI_unit IV Full Notes.pdf
State space search and Problem Solving techniques
Monkey & banana problem in AI
AI Lecture 3 (solving problems by searching)
Artificial Intelligence 1 Planning In The Real World
Hashing PPT
Artificial Intelligence PEAS
AI_Session 7 Greedy Best first search algorithm.pptx
AI_Unit I notes .pdf
ORGAN DONATION MANAGEMENT SYSTEM (PROJECT: ODMS)
Design and analysis of algorithms
Counting sort(Non Comparison Sort)
Problem Formulation in Artificial Inteligence Projects
HOSPITAL MANAGEMENT SYSTEM PROJECT
I.INFORMED SEARCH IN ARTIFICIAL INTELLIGENCE II. HEURISTIC FUNCTION IN AI III...
Lower bound
Daa unit 5
Ad

Similar to Ncit 1st ai lab (20)

PPTX
Problem Formulation
PPSX
Cs 361 2015 lec 1-2
PPSX
Cs 361 2015 lec 1-2
PPTX
AI ppt on water jug problem by shivam sharma
PPTX
Presentaion on “MiniMax Algorithm and Water Jug Problem
PDF
Ai lecture 8(unit02)
PPT
Chapter 02-artificialPROBLEM SOLVING.ppt
PPT
(Radhika) presentation on chapter 2 ai
PPTX
INT 428 AI based problem LPU Problem space and search
DOC
Chapter 2 (final)
PPT
Problems, Problem spaces and Search
PDF
1_Introduction to Artificial Intelligence.pdf
DOC
Chapter2final 130103081315-phpapp02
PPTX
aiMODULE 1.pptx
PDF
Advanced Data Structures 2007
PPTX
AI-State Space Representation.pptx
PPTX
AI-State Space Representation.pptx
PPTX
Abstraction
PPTX
unit 2.pptx
PPTX
Water jug problem ai part 6
Problem Formulation
Cs 361 2015 lec 1-2
Cs 361 2015 lec 1-2
AI ppt on water jug problem by shivam sharma
Presentaion on “MiniMax Algorithm and Water Jug Problem
Ai lecture 8(unit02)
Chapter 02-artificialPROBLEM SOLVING.ppt
(Radhika) presentation on chapter 2 ai
INT 428 AI based problem LPU Problem space and search
Chapter 2 (final)
Problems, Problem spaces and Search
1_Introduction to Artificial Intelligence.pdf
Chapter2final 130103081315-phpapp02
aiMODULE 1.pptx
Advanced Data Structures 2007
AI-State Space Representation.pptx
AI-State Space Representation.pptx
Abstraction
unit 2.pptx
Water jug problem ai part 6
Ad

More from Subash Chandra Pakhrin (20)

PPTX
Torsion Angles, ASA Used for prediction of Non - Enzymatic PTM
PPTX
PPTX
Lstm covid 19 prediction
PPTX
PPTX
Characterization and identification of lysine succinylation sites based
PPTX
Cnn april 8 2020
PPTX
Deep Learning or Convolutional Neural Network
PDF
Constraint satisfaction problem
PPTX
Intelligent agents (bsc csit) lec 2
PPTX
Final slide4 (bsc csit) chapter 4
PPTX
Final slide (bsc csit) chapter 5
PPTX
Lec 6 bsc csit
PPTX
Two player games
PPTX
Knnowledge representation and logic lec 11 to lec 15
PPTX
Final slide (bsc csit) chapter 2
PPTX
Final slide (bsc csit) chapter 3
Torsion Angles, ASA Used for prediction of Non - Enzymatic PTM
Lstm covid 19 prediction
Characterization and identification of lysine succinylation sites based
Cnn april 8 2020
Deep Learning or Convolutional Neural Network
Constraint satisfaction problem
Intelligent agents (bsc csit) lec 2
Final slide4 (bsc csit) chapter 4
Final slide (bsc csit) chapter 5
Lec 6 bsc csit
Two player games
Knnowledge representation and logic lec 11 to lec 15
Final slide (bsc csit) chapter 2
Final slide (bsc csit) chapter 3

Recently uploaded (20)

PDF
Indian roads congress 037 - 2012 Flexible pavement
PDF
Hazard Identification & Risk Assessment .pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Empowerment Technology for Senior High School Guide
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PDF
Trump Administration's workforce development strategy
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PPTX
Introduction to pro and eukaryotes and differences.pptx
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PPTX
Introduction to Building Materials
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
HVAC Specification 2024 according to central public works department
Indian roads congress 037 - 2012 Flexible pavement
Hazard Identification & Risk Assessment .pdf
Chinmaya Tiranga quiz Grand Finale.pdf
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
Share_Module_2_Power_conflict_and_negotiation.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Empowerment Technology for Senior High School Guide
TNA_Presentation-1-Final(SAVE)) (1).pptx
Trump Administration's workforce development strategy
Unit 4 Computer Architecture Multicore Processor.pptx
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
Introduction to pro and eukaryotes and differences.pptx
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
Introduction to Building Materials
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
History, Philosophy and sociology of education (1).pptx
HVAC Specification 2024 according to central public works department

Ncit 1st ai lab

  • 1. AI LAB By: Subash Chandra Pakhrin MSc in Information and Communication Engineering Kathmandu, Nepal philpakhrin@gmail.com +977-9852050900
  • 2. Lab 1: Monkey and Banana Problem Monkey is on floor, at door. A block is on floor, at window. Banana is hanging from roof at the middle of the room. Problem is “How the monkey can get the banana”.
  • 3. Prolog Code (Monkey and Banana Problem) move(state(middle,onbox,middle,hasnot), grasp, state(middle,onbox,middle,has)). move(state(P,onfloor,P,H), climb, state(P,onbox,P,H)).
  • 4. Prolog Code (Monkey and Banana Problem) move(state(P1,onfloor,P1,H), push(P1,P2), state(P2, onfloor,P2,H)). move(state(P1,onfloor,B,H), walk(P1,P2), state(P2,onfloor,B,H)).
  • 5. Prolog Code (Monkey and Banana Problem) canget(state(_,_,_,has)). canget(State1) :- move(State1,_,State2), canget(State2).
  • 6. Lab 2: Water Jug Problem You are given two jugs, a 4 liter one and a 3 liter one, a pump which has unlimited water which you can use to fill the jug, and the ground on which water may be poured. Neither jug has any measurement marking on it. How can you get exactly 2 liter of water in the 4 liter jug ?
  • 7. Lab 2: Water Jug Problem We will represent a state of the problem as a ordered pair (x,y) where x represents the amount of water in the 4 liter jug and y represents the amount of water in the 3 liter jug. Note 0 <= x <= 4, and 0 <= y <= 3. Our initial state: (0,0) Goal Predicate - state = (2,y) where 0 <=y<=3
  • 8. Lab 2: Water Jug Problem
  • 9. Lab 2: Water Jug Problem water_jug(X,Y):-X<4,Y>3,write('3L water jug is overflowed.'),nl. water_jug(X,Y):-X>4,Y>3,write('Both water jugs overflowed.'),nl. water_jug(X,Y):-(X=:=0,Y=:=0,nl,write('4L:0 & 3L:3 (Action: Fill 3L jug.)'),YY is 3,water_jug(X,YY)); (X=:=0,Y=:=0,nl,write('4L:4 & 3L=0 (Action: Fill 4L jug.)'),XX is 4,water_jug(XX,Y));
  • 10. Lab 2: Water Jug Problem (X=:=2,Y=:=0,nl,write('4L:2 & 3L=0 (Action: Goal State Reached....)')); (X=:=4,Y=:=0,nl,write('4L:1 & 3L:3 (Action: Pour water from 4L to 3L jug.)'),XX is X-3, YY is 3, water_jug(XX,YY)); (X=:=0,Y=:=3,nl,write('4L:3 & 3L:0 (Action: Pour water from 3L jug to 4L jug.)'),XX is 3,YY is 0,water_jug(XX,YY)); (X=:=1,Y=:=3,nl,write('4L:1 & 3L:0 (Action: Empty 3L jug.)'),YY is 0,water_jug(X,YY));
  • 11. Lab 2: Water Jug Problem • (X=:=3,Y=:=0,nl,write('4L:3 & 3L:3 (Action: Fill 3L jug.)'),YY is 3, water_jug(X,YY)); • (X=:=3,Y=:=3,nl,write('4L:4 & 3L:2 (Action: Pour water from 3L jug to 4L jug until 4L jug is full.)'),XX is X+1,YY is Y- 1,water_jug(XX,YY)); • (X=:=1,Y=:=0,nl,write('4L:0 & 3L:1 (Action: Pour water from 4L to 3L jug.)'),XX is Y,YY is X,water_jug(XX,YY)); • (X=:=0,Y=:=1,nl,write('4L:4 & 3L:1 (Action: Fill 4L jug.)'),XX is 4,water_jug(XX,Y));
  • 12. Lab 2: Water Jug Problem (X=:=4,Y=:=1,nl,write('4L:2 & 3L:3 (Action: Pour water from 4L jug to 3L jug until 3L jug is full.)'),XX is X-2,YY is Y+2,water_jug(XX,YY)); (X=:=2,Y=:=3,nl,write('4L:2 & 3L:0 (Action: Empty 3L jug.)'),YY is 0,water_jug(X,YY)); (X=:=4,Y=:=2,nl,write('4L:0 & 3L:2 (Action: Empty 4L jug.)'),XX is 0,water_jug(XX,Y)); (X=:=0,Y=:=2,nl,write('4L:2 & 3L:0 (Action: Pour water from 3L jug to 4L jug.)'),XX is Y,YY is X,water_jug(XX,YY)).
  • 13. Lab 3: Farmer Crosses River Puzzle • A farmer wants to crass a river and take with him a wolf, a goat, and a cabbage. • There is a boat that can fit himself plus either the wolf, the goat, or the cabbage. • If the wolf and the goat are alone on one shore, the wolf will eat the goat. If the goat and the cabbage are alone on the shore, the goat will eat the cabbage. • How can the farmer bring the wolf, the goat, and the cabbage across the river ?
  • 14. Lab 3: Farmer Crosses River Puzzle
  • 15. Lab 3: Farmer Crosses River Puzzle other_bank(e,w). other_bank(w,e). % farmer, wolf, goat, cabbage move([X,X,Goat,Cabbage],wolf,[Y,Y,Goat,Cabbage]) :- other_bank(X,Y). move([X,Wolf,X,Cabbage],goat,[Y,Wolf,Y,Cabbage]) :- other_bank(X,Y).
  • 16. Lab 3: Farmer Crosses River Puzzle move([X,Wolf,Goat,X],cabbage,[Y,Wolf,Goat,Y]) :- other_bank(X,Y). move([X,Wolf,Goat,Cabbage],nothing,[Y,Wolf,Goat,Cabbage]) :- other_bank(X,Y). safety_check(X,X,_). safety_check(X,_,X).
  • 17. Lab 3: Farmer Crosses River Puzzle safe_status([Man,Wolf,Goat,Cabbage]) :- safety_check(Man,Goat,Wolf), safety_check(Man,Goat,Cabbage). solution([e,e,e,e],[]). solution(Config,[Move|OtherMoves]) :- move(Config,Move,NextConfig),
  • 18. Lab 3: Farmer Crosses River Puzzle safe_status(NextConfig), solution(NextConfig,OtherMoves). % length(X,7), solution([w,w,w,w],X).