Date : 08/02/2020
Many AI problems are hard to solve because they are difficult to be characterized. Not only the problem but a path to the solution is also hard to be characterized. While considering chess game ,If we go and write every chess move, we may not be able to complete it in our lifetime.
Solving AI Problems
The first problem is called 3-4 gallon water jug problem as described in the book by Elaine Rich. Webegin with two empty jugs of the capacity of 3 and 4 gallons respectively. We have an infinite supply of water available and we want the 4-gallon jug to be filled with exactly 2 gallons of water.
Another problem is called 8-5-3 milk jug problem which begins with 8-gallon milk jug completely full while other two are empty. We want exactly 4 gallons of milk in the 8-gallon jug.
In either case, there are no other measuring devices available to us.
One solution for the first problem is
0-0, 0-4, 3-1, 0-1, 1-0, 1-4, 3-2.
(Where n-m indicates quantity of water in 3 and 4-gallon jugs respectively)
One solution to the second problem is
8-0-0, 3-5-0, 3-2-3, 6-2-0, 6-0-2, 1-5-2, 1-4-3, 4-4-0.
Where n-m-l are respective volumes of milk in 8, 5 and 3-gallon jugs.
Applying rules to solve the problem
Now let us see the sequence of rules applied for the solution.Consider 8-5-3 as (X-Y-Z)
1. 8-0-0 -> 3-5-0 ( X+Y =8 > 5)
2. 3-5-0 -> 3-2-3 ( Y + Z = 5 > 3)
3. 3-2-3 -> 6-2-0 ( X +Z =6)
4. 6-2-0 -> 6-0-2 ( Y+Z = 2 < 3)
5. 6-0-2 -> 1-5-2 ( X + Y = 6 >5)
6. 1-5-2 -> 1-4-3 ( Y + Z = 7 > 3)
7. 1-4-3 -> 4-4-0 ( X + Z =4)
4-4-0 is a final state.
We have an initial state called (8,0,0), we apply a typical rule, record the new status, apply another rule and again look at the status and continue until we reach a state which is qualified as a final state.
code for above question is given :-
FIRST CODE OF AI
#include<iostream>
using namespace std;
int main()
{
int limX = 8, limY = 5, limZ = 3;
int x = 8, y = 0, z = 0;
int a,b,c;
for(int i = 0; i < 2; i++)
{
// Minus water from x
if((x - (limY - y)) >= 0)
{
a = x - (x - (limY - y));
y += a; // Add Required water
x -= a;
}
else
{
y += x;
x = 0;
}
if((y - (limZ - z)) >= 0)
{
b = y - (y - (limZ - z));
z += b;
y -= b;
}
else
{
z += y;
y = 0;
}
if((z - (limX - x)) >= 0)
{
c = z - (z - (limX - x));
x += c;
z -= c;
}
else
{
x += z;
z = 0;
}
if(i>0)
{
break;
}
else
{
z = y;
y = 0;
}
}
cout<<x<<y<<z;
}
Comments
Post a Comment