Water-Jug Problem

Water Jug Problem:

Problem: You are given two jugs, a 4-gallon one and a 3-gallon one.Neither has any measuring mark on it.There is a pump that can be used to fill the jugs with water.How can you get exactly 2 gallons of water into the 4-gallon jug.

Solution:
The state space for this problem can be described as the set of ordered pairs of integers (x,y)
Where,
X represents the quantity of  water in the 4-gallon jug  X= 0,1,2,3,4
Y represents the quantity of water in 3-gallon jug Y=0,1,2,3
Start State: (0,0)
Goal State: (2,0)
Generate production rules for the water jug problem
Production Rules:
Rule
State
Process
1
(X,Y | X<4)
(4,Y)
{Fill 4-gallon jug}
2
(X,Y |Y<3)
(X,3)
{Fill 3-gallon jug}
3
(X,Y |X>0)
(0,Y)
{Empty 4-gallon jug}
4
(X,Y | Y>0)
(X,0)
{Empty 3-gallon jug}
5
(X,Y | X+Y>=4 ^ Y>0)
(4,Y-(4-X))
{Pour water from 3-gallon jug into 4-gallon jug until 4-gallon jug is full}
6
(X,Y | X+Y>=3 ^X>0)
(X-(3-Y),3)
{Pour water from 4-gallon jug into 3-gallon jug until 3-gallon jug is full}
7
(X,Y | X+Y<=4 ^Y>0)
(X+Y,0)
{Pour all water from 3-gallon jug into 4-gallon jug}
8
(X,Y | X+Y <=3^ X>0)
(0,X+Y)
{Pour all water from 4-gallon jug into 3-gallon jug}
9
(0,2)
(2,0)
{Pour 2 gallon water from 3 gallon jug into 4 gallon jug}


Initialization:
Start State: (0,0)
Apply Rule 2:
(X,Y | Y<3)    ->
(X,3)
{Fill 3-gallon jug}
Now the state is (X,3)

Iteration 1:
Current State: (X,3)
Apply Rule 7:
(X,Y | X+Y<=4 ^Y>0)
(X+Y,0)
{Pour all water from 3-gallon jug into 4-gallon jug}
Now the state is (3,0)

Iteration 2:
Current State : (3,0)
Apply Rule 2:
(X,Y | Y<3)    ->
(3,3)
{Fill 3-gallon jug}
Now the state is (3,3)

Iteration 3:
Current State:(3,3)
Apply Rule 5:
(X,Y | X+Y>=4 ^ Y>0)
(4,Y-(4-X))
{Pour water from 3-gallon jug into 4-gallon jug until 4-gallon jug is full}
Now the state is (4,2)

Iteration 4:
Current State : (4,2)
Apply Rule 3:
(X,Y | X>0)
(0,Y)
{Empty 4-gallon jug}
Now state is (0,2)

Iteration 5:
Current State : (0,2)
Apply Rule 9:
(0,2)
(2,0)
{Pour 2 gallon water from 3 gallon jug into 4 gallon jug}
Now the state is (2,0)

Goal Achieved.

State Space Tree:
 

33 comments:

  1. Thanks for sharing information about Artificial Intelligence.
    Artificial Intelligence Solutions

    ReplyDelete
  2. Can u do it for 5 gallon jug to 2 gallon jug with exactly one gallon of water where 2 gallon jug is empty

    ReplyDelete
    Replies
    1. (0,0) //initial state
      (0,2) //rule 2
      (2,0) //rule 7
      (2,2) //rule 2
      (4,0) //rule 7
      (4,2) //rule 2
      (5,1) //rule 5
      (0,1) //rule 3
      (1,0) //rule 7 and goal state

      Delete
  3. Crafsol Technology is the largest artificial intelligence solutions in Pune, India, USA, South Africa, Indonesia, UK, France and Germany provides safer medical procedures, increase in productivity, improving the quality of the physically challenged etc.
    Artificial intelligence Solutions

    ReplyDelete
  4. kids will do (2,0) and (2,3) ..... send me solutions of (2,1) and (2,2)

    ReplyDelete
  5. I made a java program to do this...

    import java.util.*;

    public class WaterJugProblem
    {

    static List SolSet=new ArrayList<>();

    public static void main(String args[])
    {

    System.out.println("------------------WATER JUG PROBLEM SOLVER------------------\n");

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter capcity of the 2 jugs\n");

    int x=sc.nextInt();

    int y=sc.nextInt();

    System.out.println("Enter target capcity of the 2 jugs\n");

    int tx=sc.nextInt();

    int ty=sc.nextInt();

    sc.close();

    FindSolution(0,0,x,y,tx,ty,"",0);

    Comparator BestSolution = (p,q)-> p.solCount-q.solCount;

    Collections.sort(SolSet,BestSolution);

    System.out.println("Total "+SolSet.size()+" possible solutions was found for the problem\n");

    if(SolSet.size()!=0)
    {
    System.out.println("Best Solution is");

    System.out.println(SolSet.get(0).sol);
    }

    System.out.println("Solutions that involve more than 10 steps are ignored to avoid complexity\n");

    System.out.println("---------------------------------------------------------Designed By Arpan");



    }


    static void FindSolution(int x,int y,int xCapacity,int yCapacity,int xTarget,int yTarget,String sol,int count)
    {

    if(x==xTarget && y==yTarget) // If Goal state is found
    {
    SolSet.add(new Solution(sol,count));
    return;
    }


    int newX=x,newY=y;
    String newSol=sol;



    if(count>10) // Too many recursion , Ignore this recursion
    return;

    //Production Rules

    if(x0)
    {
    newSol=sol;

    newX=0;

    newY=y;

    newSol=newSol+"("+newX+","+newY+")";

    newSol=newSol+"Empty first jug\n";

    FindSolution(newX,newY,xCapacity,yCapacity,xTarget,yTarget,newSol,count+1);
    }

    if(y>0)
    {
    newSol=sol;

    newX=x;

    newY=0;

    newSol=newSol+"("+newX+","+newY+")";

    newSol=newSol+"Empty Second jug\n";

    FindSolution(newX,newY,xCapacity,yCapacity,xTarget,yTarget,newSol,count+1);
    }

    if((x+y)>xCapacity && y>0)
    {
    newSol=sol;

    newX=xCapacity;

    newY=y-(xCapacity-x);

    newSol=newSol+"("+newX+","+newY+")";

    newSol=newSol+"Pour water from the second jug into first jug until first jug is full\n";

    FindSolution(newX,newY,xCapacity,yCapacity,xTarget,yTarget,newSol,count+1);
    }

    if((x+y)>yCapacity && x>0)
    {
    newSol=sol;

    newX=x-(yCapacity-y);

    newY=yCapacity;

    newSol=newSol+"("+newX+","+newY+")";

    newSol=newSol+"Pour water from the first jug into second jug until second jug is full\n";

    FindSolution(newX,newY,xCapacity,yCapacity,xTarget,yTarget,newSol,count+1);
    }


    if((x+y)<=xCapacity && y>0)
    {
    newSol=sol;

    newX=x+y;

    newY=0;

    newSol=newSol+"("+newX+","+newY+")";

    newSol=newSol+"Pour all water from second jug into first jug\n";

    FindSolution(newX,newY,xCapacity,yCapacity,xTarget,yTarget,newSol,count+1);
    }

    if((x+y)0)
    {
    newSol=sol;

    newX=0;

    newY=y+x;

    newSol=newSol+"("+newX+","+newY+")";

    newSol=newSol+"Pour all water from first jug into second jug\n";

    FindSolution(newX,newY,xCapacity,yCapacity,xTarget,yTarget,newSol,count+1);
    }


    }
    }


    class Solution
    {
    String sol;

    int solCount;


    Solution(String sol,int solCount)
    {
    this.sol=sol;
    this.solCount=solCount;
    }
    }

    ReplyDelete
  6. Isn't rule 9 same as rule 7
    Is there a need for rule 9?

    ReplyDelete
  7. Nice idea, i am gonna try the casino heist guide
    same for my web site. thanks.http://pennykent.simplesite.com/

    ReplyDelete
  8. sure types of betting on various bet types may require skills or techniques working in game take effect and betting. Making these bets does not seem normal for many novices because it requires skills and training to profit profitably. Therefore, in this article, we will guide every other players to a gambling game that does not require any skill. If deserted luck, you can play. The original games were those that were known for their simplicity, such as fortunate wheels in the manner of slot machines. Because casino royale hotel movie single-handedly you have the maintenance to bet. The settle will be everyone's management. If you have ample horoscope luck, you can totally win big prizes even without any skill.

    ReplyDelete
  9. Today, playing online casinos is no question popular. You can pick to play in comfortably anytime, anywhere, anytime, anywhere, you can easily choose; you can pick to participate in the game 24 hours a day; in the manner of the urge on of modern technology, betting can be made more easily Because it works on both phones and computers but many people want to know what's stand-in along james casino royale cast with the two. perform online casino via mobile phone. Is a broadminded form of gambling where you can pick to bet easily anywhere, whether in the bathroom, bedroom, or car, even at great distances. creature able to entry especially those without a computer can easily bet. Many people who choose to gamble online tend to have a mobile phone, whether it's Android or iOS, to participate in the bet. Many would arrive in affable for gambling at an online casino via mobile phone. The screen format is the same. However, it may not be tolerable for anyone to enactment the game from the beginning, because sometimes it may not be dexterous to look the details clearly. You must pay attention to this. get nt look cool. accomplish nt trouble not quite your success next choosing to measure the game.

    ReplyDelete
  10. the Water Jug Problem is a strategic environment??

    ReplyDelete
  11. the Water Jug Problem is a stochastic environment ??

    ReplyDelete
  12. You have 3 jugs, measuring 12 gallons, 8 gallons, and 3 gallons, and a water faucet. You can fill the
    jugs up or empty them out from one to another or onto the ground. You need to measure out exactly
    one gallon.
    plz solve this

    ReplyDelete
    Replies
    1. Fill the 12 L jug completely then use it to fill 3L and 8L jug. 1L jug will be left in the first jug. Throw the remaining water from the 3 and 8L jug.

      Delete
  13. Electric body massager is a device that provides a massage or an invigorating feel to the body, without any human effort. It’s an electrical massager that does the work for you. It runs on electricity and provides a soothing and relaxing sensation to the body and mind. There are different types of massagers to choose from depending on the intensity of the pressure and the area of the body to be massaged. They are generally lightweight and easy to use. They can be used in a home or in a professional setting.
    Buy Electric Body Massager For men and women

    ReplyDelete
  14. This blog is really helpful for me as I was looking for the best water filter repair and installation services. Keep sharing such blogs.
    hard water softeners

    ReplyDelete
  15. I was looking for the best Cotton Candy Maker Price in Pakistan and blog was really helpful for me: check Cotton Candy Maker Price

    ReplyDelete
  16. Water jog problem provides an linking concept..
    I have one more topic of related with fashion href=https://www.nykaafashion.com/>Visit here free fashion online

    ReplyDelete
  17. Pure Water1 Presenting the Best Alkaline Water System. if you are looking that water system so you can Contact now at - 1-888- 755-2000, and also Visit Alkaline Water System!

    ReplyDelete
  18. The Water-Jug problem is a classic puzzle that has been widely used in the field of artificial intelligence (AI) and problem-solving domains in best cameras for video. It serves as an interesting and engaging example to showcase problem-solving strategies and algorithms.

    In the context of a commenting website, the Water-Jug problem can be used as a metaphor to describe the process of moderation and managing user comments. Imagine the jugs represent different aspects of comment moderation, such as filtering out inappropriate content, ensuring respectful discourse, and maintaining a positive and inclusive environment.

    Just like the Water-Jug problem requires finding a sequence of actions to measure a specific amount of water, the commenting website's goal is to ensure that the user comments meet certain standards and contribute positively to the community. This involves implementing various measures, cheap camera for photography, such as content filters, community guidelines, and user reporting systems.

    ReplyDelete