0% found this document useful (0 votes)
28 views4 pages

Ai Assign-1

The document discusses three AI problems: the water jug problem, monkey and banana problem, and 8 queens problem. It provides detailed explanations of each problem statement, representations, and solutions using search algorithms or Prolog for the first two problems and different algorithms like backtracking for the 8 queens problem.

Uploaded by

Harshit kumar
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)
28 views4 pages

Ai Assign-1

The document discusses three AI problems: the water jug problem, monkey and banana problem, and 8 queens problem. It provides detailed explanations of each problem statement, representations, and solutions using search algorithms or Prolog for the first two problems and different algorithms like backtracking for the 8 queens problem.

Uploaded by

Harshit kumar
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

‭ ame- Harshit‬

N
‭Roll No.-2K21/CO/183‬

‭AI ASSIGNMENT-1‬

‭QUESTION 1:- WATER JUG PROBLEM‬


‭ he‬‭Water Jug Problem‬‭is a classic puzzle in the field of‬‭Artificial Intelligence‬‭. It involves two jugs‬
T
‭of water—one with a capacity of‬‭4 litres‬‭and the other with a capacity of‬‭3 litres‬‭. The jugs have no‬
‭measuring markers, and the task is to find a way to pour exactly‬‭2 litres‬‭of water into the‬‭4-litre jug‬‭.‬
‭Here’s how we can approach this problem:‬

‭State Representation‬‭:-‬

‭ .‬ L
1 ‭ et’s denote the 4-litre jug as‬‭“x”‬‭and the 3-litre jug as‬‭“y”‬‭.‬
‭2.‬ ‭We can represent the state of the system as a pair‬‭(x, y)‬‭, where both jugs are initially empty:‬
‭(0, 0)‬‭.‬

‭Goal State‬‭:-‬

‭1.‬ O
‭ ur goal is to reach a state where the 4-litre jug contains‬‭2 litres‬‭of water:‬‭(2, n)‬‭(where‬‭n‬‭can‬
‭be any value).‬

‭Production Rules‬‭:-‬

‭➢‬ W ‭ e’ll define a set of production rules to transform the state from the start state to the goal‬
‭state:‬
‭1.‬ ‭Fill the 4-litre jug‬‭: If‬‭x < 4‬‭, fill the 4-litre jug to its maximum capacity:‬‭(4, y)‬‭.‬
‭2.‬ ‭Fill the 3-litre jug‬‭: If‬‭y < 3‬‭, fill the 3-litre jug to its maximum capacity:‬‭(x, 3)‬‭.‬
‭3.‬ ‭Pour from 4-litre jug to 3-litre jug‬‭: If‬‭x > 0‬‭, pour some water from the 4-litre jug into the‬
‭3-litre jug:‬‭(x - d, d)‬‭.‬
‭4.‬ ‭Pour from 3-litre jug to 4-litre jug‬‭: If‬‭y > 0‬‭, pour some water from the 3-litre jug into the‬
‭4-litre jug:‬‭(d, y - d)‬‭.‬
‭5.‬ ‭Empty the 4-litre jug‬‭: If‬‭x > 0‬‭, empty the 4-litre jug on the ground:‬‭(0, y)‬‭.‬
‭6.‬ ‭Empty the 3-litre jug‬‭: If‬‭y > 0‬‭, empty the 3-litre jug on the ground:‬‭(x, 0)‬‭.‬
‭7.‬ ‭Fill the 4-litre jug from the 3-litre jug‬‭: If‬‭x + y >= 4‬‭and‬‭y > 0‬‭, pour water from the 3-litre‬
‭jug into the 4-litre jug until it is full:‬‭(4, y - (4 - x))‬‭.‬
‭8.‬ ‭Fill the 3-litre jug from the 4-litre jug‬‭: If‬‭x + y >= 3‬‭and‬‭x > 0‬‭, pour water from the 4-litre‬
‭jug into the 3-litre jug until it is full:‬‭(x - (3 - y), 3)‬‭.‬
‭9.‬ ‭Transfer all water from 3-litre jug to 4-litre jug‬‭: If‬‭x + y <= 4‬‭and‬‭y > 0‬‭, pour all the water‬
‭from the 3-litre jug into the 4-litre jug:‬‭(x + y, 0)‬‭.‬
‭10.‬‭Transfer all water from 4-litre jug to 3-litre jug‬‭: If‬‭x + y <= 3‬‭and‬‭x > 0‬‭, pour all the water‬
‭from the 4-litre jug into the 3-litre jug:‬‭(0, x + y)‬‭.‬
‭11.‬‭Pour 2 litres from 3-litre jug to 4-litre jug‬‭: From state‬‭(0, 2)‬‭, pour 2 litres of water from the‬
‭3-litre jug into the 4-litre jug:‬‭(2, 0)‬‭.‬
‭12.‬‭Empty 2 litres from 4-litre jug‬‭: From state‬‭(2, y)‬‭, empty 2 litres from the 4-litre jug on the‬
‭ground:‬‭(0, y)‬‭.‬
‭13.‬‭Search Algorithms‬‭:‬
‭○‬ ‭To solve the water jug problem, we can use search algorithms like:‬
‭i.‬ ‭Breadth-First Search (BFS)‬‭: Visits nodes in order of their distance from the‬
‭starting node, prioritising the nearest nodes first.‬
‭ii.‬ ‭Depth-First Search (DFS)‬‭: Visits nodes in order of their depth, exploring‬
‭deeper before backtracking.‬

‭SOLUTION :-‬

‭ ater Jug Problem‬‭where we need to pour exactly‬‭2 litres‬‭of water into the‬‭4-litre jug‬‭using the‬
W
‭3-litre jug‬‭. Here’s one possible sequence of actions:‬

‭ .‬
1 ‭ tart with both jugs empty:‬‭(0, 0)‬‭.‬
S
‭2.‬ ‭Fill the 3-litre jug:‬‭(0, 3)‬‭.‬
‭3.‬ ‭Pour water from the 3-litre jug into the 4-litre jug:‬‭(3, 0)‬‭.‬
‭4.‬ ‭Fill the 3-litre jug again:‬‭(3, 3)‬‭.‬
‭5.‬ ‭Pour water from the 3-litre jug into the 4-litre jug until the 4-litre jug is full:‬‭(4, 2)‬‭.‬
‭6.‬ ‭Empty the 4-litre jug:‬‭(0, 2)‬‭.‬
‭7.‬ ‭Pour the remaining 2 litres from the 3-litre jug into the 4-litre jug:‬‭(2, 0)‬‭.‬

‭Now the 4-litre jug contains exactly 2 litres of water, which is our desired outcome!‬
‭QUESTION 2 :-‬‭Monkey and Banana Problem‬

‭ he‬‭Monkey and Banana Problem‬‭is a classic puzzle in the field of‬‭Artificial Intelligence‬‭. Let’s‬
T
‭delve into this intriguing scenario:‬

‭Problem Statement:‬‭Suppose we have the following situation:‬

‭‬
● ‭ ‬‭hungry monkey‬‭is in a room, standing near the door.‬
A
‭●‬ ‭The monkey is currently on the floor.‬
‭●‬ ‭Bananas‬‭are tantalizingly hung from the center of the ceiling.‬
‭●‬ ‭There’s a‬‭block‬‭(or chair) in the room, positioned near the window.‬

‭ he monkey desires those bananas, but alas, they are out of reach! How can our clever simian friend‬
T
‭obtain the coveted fruit?‬

‭Observations:‬

‭ .‬
1 ‭ he monkey can reach the block if both are at the same level.‬
T
‭2.‬ ‭If the block isn’t at the center, the monkey can drag it there.‬
‭3.‬ ‭When both the monkey and the block are on the floor, the monkey can climb onto the block.‬
‭4.‬ ‭Once the monkey is on the block (with the block at the center), it can finally reach the‬
‭bananas.‬

‭Solution Using Prolog:‬

‭We can represent this problem using‬‭Prolog‬‭predicates. Here’s how we can solve it step by step:‬

‭1.‬ ‭State Representation‬‭:‬


‭○‬ ‭We define a state as a tuple: (MonkeyPosition, BlockPosition, BananaPosition,‬
‭HasBanana).‬
‭○‬ ‭Initially: state(atdoor, onfloor, atwindow, hasnot).‬
‭2.‬ ‭Actions‬‭:‬
‭○‬ ‭grasp: If the monkey is on top of the block and doesn’t have the banana, it can grasp‬
‭it.‬
‭○‬ ‭climb: Move from the floor to the top of the block.‬
‭○‬ ‭drag(P1, P2): Move the block from position P1 to P2.‬
‭○‬ ‭walk(P1, P2): Move the monkey from position P1 to P2.‬
‭3.‬ ‭Recursive Rule‬‭:‬
‭○‬ ‭Define a canget(State) predicate that recursively explores possible actions until the‬
‭monkey reaches the desired state (has banana).‬
‭4.‬ ‭Example Execution‬‭:‬
‭○‬ ‭Query: canget(state(atdoor, onfloor, atwindow, hasnot)).‬
‭○‬ ‭Output: true.‬
‭QUESTION 3 :-‬‭The 8 Queens Problem‬

‭ he‬‭8 Queens Problem‬‭involves placing‬‭eight queens‬‭on an‬‭8×8 chessboard‬‭in such a way that no‬
T
‭two queens threaten each other. Specifically, no two queens should be in the same row, column, or‬
‭diagonal. This problem can be generalized to the‬‭n queens problem‬‭, where we aim to place n queens‬
‭on an n×n chessboard.‬

‭Approaches to Solving the 8 Queens Problem‬

‭Backtracking Algorithm‬‭:‬


‭ ‬ T ‭ he most common approach for solving the 8 Queens Problem is through‬‭backtracking‬‭.‬
‭➢‬ ‭The‬‭isSafe‬‭function checks if it’s safe to place a queen on a certain row and column by‬
‭verifying that no other queens threaten it.‬
‭➢‬ ‭The algorithm recursively explores different possibilities, backtracking when necessary.‬

‭Genetic Algorithms (GA)‬‭:‬

‭ )‬ A
1 ‭ nother effective approach is using‬‭Genetic Algorithms‬‭.‬
‭2)‬ ‭GA mimics natural selection, creating a population of random solutions and evaluating them‬
‭using a fitness function.‬
‭3)‬ ‭Over successive generations, the algorithm evolves better solutions.‬

‭Other Methods‬‭:‬

‭1.‬ T
‭ he 8 Queens Problem can also be tackled using‬‭hill climbing‬‭,‬‭branch and bound‬‭, and other‬
‭optimization techniques.‬

I‭ n summary, the 8 Queens Problem challenges us to find a configuration where eight queens coexist‬
‭harmoniously on a chessboard.‬

‭SOLUTION‬‭:-‬

‭Q1‬

‭Q7‬

‭Q5‬

‭Q2‬ ‭Q8‬

‭Q4‬

‭Q6‬

‭Q3‬

You might also like