AI RECORD Edited
AI RECORD Edited
AIM:
ALGORITHM:
PROGRAM:
OUTPUT:
RESULT:
Thus the python program has been successfully implemented and the output is verified.
EX NO: 2 SOLVE A PROBLEM USING DEPTH FIRST SEARCH
DATE: 3/2/24
AIM:
ALGORITHM:
1. Assign the values of the parent and children in the tree level.
2. Traverse the Tree from Root node to the left end node 3.
Make the traversal towards right end of each node.
4. After processing all the nodes, stop the process.
PROGRAM:
RESULT:
Thus the python program has been successfully implemented and the output is verified.
EX NO: 3 IMPLEMENT MINMAX ALGORITHM
DATE:10/2/24
AIM:
ALGORITHM:
● Represent the current state of the game. This could be a board configuration, a position
in a maze, or any relevant data structure.
● Determine the conditions that indicate the end of the game, such as a win, loss, or draw.
These conditions will be used to evaluate the utility of a game state.
● Assign a utility value to each terminal state. This value represents the outcome of the
game for the current player (e.g., +1 for a win, -1 for a loss, 0 for a draw).
● Generate all possible moves for the current player from the current game state. This
typically involves identifying empty spaces on the game board or legal moves in the
context of the specific game.
● Recursively apply the Minimax algorithm to evaluate each possible move. For each
move:
● If the game has reached a terminal state, return the utility value.
● If not, continue the recursion.
6. Maximize and Minimize:
● If it's the current player's turn (maximizing player), choose the move with the
maximum utility value among the available moves.
● If it's the opponent's turn (minimizing player), choose the move with the minimum
utility value among the available moves.
7. Backtrack:
● Propagate the chosen utility value back up the recursion stack to the original call,
updating the utility values of parent nodes.
● Once the recursion is complete, choose the move with the highest utility value from the
root node. This is the optimal move for the current player.
PROGRAM:
import math
OUTPUT:
RESULT:
Thus the python program has been successfully implemented and the output is verified.
EX NO: 4 IMPLEMENTATION OF A* ALGORITHM
DATE:17/2/24
AIM:
To implement A* Algorithm.
ALGORITHM:
2. Initialize Costs:
● Assign a cost of 0 to the start node.
● Calculate the heuristic estimate from the start node to the goal node.
3. Iterate Until Goal is Reached or Open Set is Empty:
● While the Open set is not empty:
1. Select the node with the lowest total cost (actual cost + heuristic) from the Open
set. This node is the current node.
2. Move the current node from the Open set to the Closed set.
if n is None:
print('Path does not exist!')
return None
# If the current node is the stop_node, reconstruct and return the path
if n == stop_node:
path = []
while parents[n] != n:
path.append(n)
n = parents[n]
path.append(start_node)
path.reverse()
print('Path found:', path)
return path
# Define function to return neighbors and their distances from the passed node def
get_neighbors(v):
return Graph_nodes.get(v, [])
OUTPUT:
RESULT:
Thus the python program has been successfully implemented and the output is verified.
EX NO: 9 SIMPLE PROLOG PROBLEM
DATE:23/3/24
AIM:
ALGORITHM:
PROGRAM:
RESULT:
Thus the program has been successfully implemented and the output is verified.
EX NO:10 UNIFICATION AND RESOLUTION USING
DATE: 6/4/24 PROLOG
AIM:
ALGORITHM:
UNIFICATION ALGORITHM:
PROGRAM:
RESOLUTION ALGORITHM:
OUTPUT:
RESULT:
Thus the program has been successfully implemented and the output is verified.
EX NO:11 IMPLEMENT THE BACKWARD CHAIN USING PROLOG
DATE: 13/4/24
AIM:
ALGORITHM:
PROGRAM:
backward_chain(Goal) :-
% Check if the goal is already a fact
Goal. backward_chain(Goal) :-
% If the goal is not a fact, check if it can be derived from rules
Rule, backward_chain(Rule).
% Step 3: Query the Backward Chaining Predicate %
Example query: Is a cat an animal?
% Query: backward_chain(is_a(cat, animal)).
OUTPUT:
RESULT:
Thus the program has been successfully implemented and the output is verified.
EX NO: 5 BLOCK WORD PROBLEM
DATE: 24/2/24
AIM:
ALGORITHM:
PROGRAM:
class BlocksWorld:
def init (self, initial_state):
self.state = initial_state def
display_state(self): for row in
self.state: print(" ".join(row))
print() def is_goal_state(self,
goal_state): return self.state ==
goal_state def
generate_successors(self):
successors = [] for i in
range(len(self.state)): for j in
range(len(self.state[i])):
if self.state[i][j] != " ":
# Try moving the block up
if i > 0 and self.state[i - 1][j] == " ":
new_state = [row.copy() for row in self.state]
new_state[i][j], new_state[i - 1][j] = new_state[i - 1][j], new_state[i][j]
successors.append(BlocksWorld(new_state))
# Try moving the block down
if i < len(self.state) - 1 and self.state[i + 1][j] == " ":
new_state = [row.copy() for row in self.state]
new_state[i][j], new_state[i + 1][j] = new_state[i + 1][j], new_state[i][j]
successors.append(BlocksWorld(new_state))
# Try moving the block left if j > 0
and self.state[i][j - 1] == " ":
new_state = [row.copy() for row in self.state]
new_state[i][j], new_state[i][j - 1] = new_state[i][j - 1], new_state[i][j]
successors.append(BlocksWorld(new_state))
# Try moving the block right if j < len(self.state[i]) - 1
and self.state[i][j + 1] == " ":
new_state = [row.copy() for row in self.state]
new_state[i][j], new_state[i][j + 1] = new_state[i][j + 1], new_state[i][j]
successors.append(BlocksWorld(new_state))
OUTPUT:
RESULT:
Thus the program has been successfully implemented and the output is verified.
EX NO: 6 IMPLEMENTATION OF SVM MODEL
DATE: 2/3/24
AIM:
ALGORITHM:
PROGRAM:
RESULT:
Thus the program has been successfully implemented and the output is verified.
EX NO: 7 IMPLEMENTATION OF ANN WITH BP
DATE: 9/3/24
AIM:
ALGORITHM:
1 Import Libraries
2 Load Dataset
3 Prepare Dataset
4 Split train and test set
5 Initialize Hyperparameters and Weights
6 Helper Functions
7 Backpropagation Neural Network
8 Plot MSE and Accuracy
PROGRAM:
# Calculating error
mse = mean_squared_error(A2, y_train) acc
= accuracy(A2, y_train)
results=results.append({"mse":mse,
"accuracy":acc},ignore_index=True )
# backpropagation E1 =
A2 - y_train dW1 = E1 *
A2 * (1 - A2) E2 =
np.dot(dW1, W2.T) dW2
= E2 * A1 * (1 - A1)
# weight updates
W2_update = np.dot(A1.T, dW1) / N
W1_update = np.dot(X_train.T, dW2) / N
W2 = W2 - learning_rate * W2_update W1
= W1 - learning_rate * W1_update
results.mse.plot(title="Mean Squared Error")
results.accuracy.plot(title="Accuracy")
# feedforward
Z1 = np.dot(X_test, W1)
A1 = sigmoid(Z1)
Z2 = np.dot(A1, W2) A2 =
sigmoid(Z2) acc =
accuracy(A2, y_test)
print("Accuracy: {}".format(acc))
OUTPUT:
RESULT:
Thus the program has been successfully implemented and the output is verified.
EX NO: 8a IMPLEMENTATION OF DECISION TREE
DATE:16/3/24
AIM:
ALGORITHM:
PROGRAM:
self.target]
self.entropy = self._get_entropy(self.data) if
self.entropy != 0: self.gains = [(feat,
self._get_gain(feat)) for feat in
self.features]
self._get_splitter()
residual_columns = [k for k in self.data.columns if k !=
self.splitter]
self.data[self.data[self.splitter]==val][residual_columns]
tmp_node = DecisionTree(df_tmp, self.target,
OUTPUT:
RESULT:
Thus the program has been successfully implemented and the output is verified.
EX NO: 8b IMPLEMENTATION OF K MEANS CLUSTERING
DATE: 16/3/24
AIM:
ALGORITHM:
PROGRAM:
RESULT:
Thus the program has been successfully implemented and the output is verified.