Ai Programs
Ai Programs
OBJECTIVE:
Write a program to solve water jug problem using
LISP/PROLOG.
THEORY:
You are on the side of the river. You are given a m liter jug
and a n liter jug where 0 < m < n. Both the jugs are initially
empty. The jugs don’t have markings to allow measuring
smaller quantities. You have to use the jugs to measure d
liters of water where d < n. Determine the minimum no of
operations to be performed to obtain d liters of water in one
of jug.
The operations you can perform are:
1. Empty a Jug
2. Fill a Jug
3. Pour water from one jug to the other until one of the jugs is
either empty or full.
Solution 1 (Always pour from m liter jug into n liter jug)
1. Fill the m litre jug and empty it into n liter jug.
2. Whenever the m liter jug becomes empty fill it.
3. Whenever the n liter jug becomes full empty it.
4. Repeat steps 1,2,3 till either n liter jug or the m liter jug
contains d litres of water.
Each of steps 1, 2 and 3 are counted as one operation that
we perform. Let us say algorithm 1 achieves the task in C1
no of operations.
Solution 2 (Always pour from n liter jug into m liter jug)
1. Fill the n liter jug and empty it into m liter jug.
2. Whenever the n liter jug becomes empty fill it.
3. Whenever the m liter jug becomes full empty it.
4. Repeat steps 1, 2 and 3 till either n liter jug or the m liter
jug contains d liters of water.
Let us say solution 2 achieves the task in C2 no of
operations. Now our final solution will be a minimum of C1
and C2. Now we illustrate how both of the solutions work.
Suppose there are a 3 liter jug and a 5 liter jug to measure 4
liters water so m = 3,n = 5 and d =
4. The associated Diophantine equation will be 3m + 5n = 4.
We use pair (x, y) to represent amounts of water inside the
3-liter jug and 5-liter jug respectively in each pouring step.
Using Solution 1, successive pouring steps are: (0,0)-
>(3,0)->(0,3)->(3,3)->(1,5)->(1,0)->(0,1)->(3,1)->(0,4) Hence
the no of operations you need to perform are 8.
Using Solution 2, successive pouring steps are: (0,0)-
>(0,5)->(3,2)->(0,2)->(2,0)->(2,5)->(3,4)
Hence the no of operations you need to perform are 6.
Therefore, we would use solution 2 to measure 4 liters of
water in 6 operations or moves.
PROGRAM:
OUTCOME:
2.TIC-TAC-TOE PROGRAM:
OBJECTIVE:
Implementation of BFS for tic-tac-toe problem using
PROLOG.
THEORY:
O X O
O X X
X O X
Implementation
In our program the moves taken by the computer and the
human are chosen randomly. We use rand() function for
this.
PROGRAM:
ordered_line(1, 2, 3). ordered_line(4, 5, 6). ordered_line(7, 8, 9).
ordered_line(1, 4, 7). ordered_line(2, 5, 8). ordered_line(3, 6,
9).
ordered_line(1, 5, 9). ordered_line(3, 5, 7).
move(C) :- rule1(C).
move(A) :- good(A), empty(A), !. good(1). good(3). good(7).
good(9). good(5). good(2). good(4). good(6). good(8).
all_full :- full(1), full(2), full(3), full(4), full(5),
full(6), full(7), full(8), full(9).
OUTCOME:
Implement of BFS for tic-tac-toe problem using PROLOG.
OBJECTIVE:
Implementation of TSP using heuristic approach using
Java/LISP/Prolog
THEORY:
Firstly, let’s introduce the TSP model: a directed graph G=(V,
A), where V is the set of vertices (locations) to be visited, and
cᵢⱼ, (i,j) ∈ A is the cost (usually distance, or a literal dollar cost)
of each edge (the path between two locations). The objective of
the TSP is to find the lowest-cost route that satisfies the
problem’s four main constraints, specified below.
Constraints (1) and (2) tell us that each vertex j/i should
connect to/be connected to exactly another one vertex i/j.
However, these two constraints aren’t enough to guarantee that
the model’s result has only one circuit.
Thus we have constraint (3), which says that the final solution
cannot be a collection of smaller routes (or subtours) — the
model must output a single route that connects all the vertices.
Finally, constraint (4) defines a variable xᵢⱼ, setting it equal to 1
if two vertices (i, j) in the graph are connected as part of the
final tour, and 0 if not.
In the graph above, let’s say that we choose the leftmost node
as our root, and use the algorithm to guide us to a solution.
There are three nodes connected to our root node: the first
node from the right, the second node from the left, and the third
node from the left. They can each connect to the root with costs
1+∈, 1+∈, and 1, respectively (where ∈ is an infinitesimally
small positive value).
Following the nearest neighbor algorithm, we should add the
vertex with minimal cost, meaning the third node from the left
should be our choice. Step by step, this algorithm leads us to
the result marked by the red line in the graph, a solution with an
objective value of 10. However, we can see that going straight
down the line from left to right and connecting back around
gives us a better route, one with an objective value of 9+5∈.
Generalizing this observation, as the number of nodes involved
increases, the difference between the Nearest Neighbor result
and the optimal one will be infinite. However, when using
Nearest Neighbor for the examples in TSPLIB (a library of
diverse sample problems for the TSP), the ratio between the
heuristic and optimal results averages out to about 1.26, which
isn’t bad at all. Given its ease of implementation and the fact
that its results are solid, the Nearest Neighbor is a good, simple
heuristic for the STSP.
Outcome:
We Learn Introduced heuristics for the TSP, including
algorithms based on the Assignment Problem for the ATSP and
the Nearest Neighbor algorithm for the STSP. Both of these
algorithms are frequently used in practice for well-defined
problems.
PROGRAM:
class GFG
{
// Hamiltonian Cycle
return ans;
// BACKTRACKING STEP
// Mark as visited
v[i] = true;
v[i] = false;
return ans;
// Driver code
int n = 4;
v[0] = true;
System.out.println(ans);
}
}
OUTCOME:
OBJECTIVE:
Implementation of Hill-climbing to solve 8- Puzzle Problem
THEORY:
Hill Climb Algorithm
1. Hill Climbing is a heuristic search used for mathematical
optimization problems in the field of Artificial Intelligence. It is
an iterative algorithm that starts with an arbitrary solution to a
problem, then attempts to find a better solution by making an
incremental change to the solution.
2. So, given a large set of inputs and a good heuristic function,
the algorithm tries to find the best possible solution to the
problem in the most reasonable time period.
3. Solution is not necessary, a optimal solution (Global Optimal
Maxima) but it is consider to be good solution according to time
period.
4. Mathematical optimization problems: Implies that hill-
climbing solves the problems where we need to maximize or
minimize a given real function by choosing values from the
given inputs.
5. For example, hill climbing can be applied to the travelling
salesman problem.where we need to minimize the distance
traveled by the salesman.
Greedy approach:
Hill-climbing algorithm search moves in the direction which
optimizes the cost.
public Node() {
}
return positionX;
}
return positionY;
}
return output;
}
}
<pre>import java.util.Stack;
import java.util.ArrayList;
newState = p.moveUp(currentState);
if (newState != null) {
fringe.push(newState);
}
newState = p.moveRight(currentState);
if (newState != null) {
fringe.push(newState);
}
newState = p.moveDown(currentState);
if (newState != null) {
fringe.push(newState);
}
newState = p.moveLeft(currentState);
if (newState != null) {
fringe.push(newState);
}
}
currentState = fringe.pop();
closed.add(currentState);
succesor(currentState);
if
(currentState.toString().equals(tmpNode.toString())) {
exist = true;
break;
}
}
if (!exist) {
moveCounter++;
newState.getCoordinate(emptyTile, 0);
tmpNum = newState.getNum(newPosition[0],
newPosition[1]);
if (currentState.getX(0) - 1 >= 0) {
newState = currentState;
newPosition[0] = newState.getX(0) - 1;
newPosition[1] = newState.getY(0);
swap(newState, newPosition);
}
return newState;
}
if (currentState.getY(0) + 1 <= 2) {
newState = currentState;
newPosition[0] = newState.getX(0);
newPosition[1] = newState.getY(0) + 1;
swap(newState, newPosition);
}
return newState;
}
if (currentState.getX(0) + 1 <= 2) {
newState = currentState;
newPosition[0] = newState.getX(0) + 1;
newPosition[1] = newState.getY(0);
swap(newState, newPosition);
}
return newState;
}
if (currentState.getY(0) - 1 >= 0) {
newState = currentState;
newPosition[0] = newState.getX(0);
newPosition[1] = newState.getY(0) - 1;
swap(newState, newPosition);
}
return newState;
}
}
OUTCOME:
5.MONKEY BANANA PROGRAM:
OBJECTIVE:
Implementation of Monkey Banana Problem using
LISP/PROLOG
THEORY:
Problem Statement
Suppose 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.
OUTCOME: