0% found this document useful (0 votes)
6 views28 pages

67 (1) - Merged

The document contains multiple experiments related to programming in C++, including implementations of Breadth First Search, Depth First Search, a Tic-Tac-Toe game using minimax algorithm, and a Bayesian network for inference. Each experiment includes a clear aim, code snippets, and outputs demonstrating the functionality of the programs. The final section discusses value and policy iteration in a grid world, showcasing reinforcement learning concepts.

Uploaded by

igcmlsa
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)
6 views28 pages

67 (1) - Merged

The document contains multiple experiments related to programming in C++, including implementations of Breadth First Search, Depth First Search, a Tic-Tac-Toe game using minimax algorithm, and a Bayesian network for inference. Each experiment includes a clear aim, code snippets, and outputs demonstrating the functionality of the programs. The final section discusses value and policy iteration in a grid world, showcasing reinforcement learning concepts.

Uploaded by

igcmlsa
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
You are on page 1/ 28

Experiment -1

Aim – write a programme to conduct uniformed search ( Breadth First


Search)
Breadth First Search
#include <bits/stdc++.h>
using namespace std;
class Graph (
int V;
vector<list<int>> adj:
public: Graph(int V);
void addEdge(int v, int w);
void BFS(int s);
Graph::Graph(int V) (
this->V = V;
adj.resize(V);
}
6
void Graph::addEdge(int v, int w) (
adj[v].push_back(w);
}void Graph::BFS(int s) {
vector<bool> visited;
visited.resize(V, false);
list<int> queue;
visited[s] = true;
queue.push_back(s);while (!queue.empty())
}
s = queue.front();
cout << s <<" ";
queue.pop_front();
for (auto adjacent adj[s])
{ if (!visited[adjacent])
visited[adjacent] = true; queue.push_back(adjacent);
}
}
7
int main()
{
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
cout << "Following is Breadth First Traversal " << "(starting from vertex 2) \n";
g.BFS(2);
return 0;
}

Output: -Following is Breadth First Traversal (starting from vertex 2):2031


Experiment -2

Aim: Write a program to conduct uninformed search (Depth First


Search).
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std;

class Graph {
public:
unordered_map<int, vector<int>> adjList;

void addEdge(int u, int v) {


adjList[u].push_back(v);
}

void dfsUtil(int node, unordered_set<int>& visited) {


visited.insert(node);
cout << node << " ";
for (int neighbor : adjList[node]) {
if (visited.find(neighbor) == visited.end()) {
dfsUtil(neighbor, visited);
}
}
}

void dfs(int start) {


unordered_set<int> visited;
cout << "Depth First Search (DFS) traversal: ";
dfsUtil(start, visited);
cout << endl;
}
};

int main() {
Graph g;
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 3);
g.addEdge(1, 4);
g.addEdge(2, 5);
g.addEdge(2, 6);

g.dfs(0);

return 0;
}

Output: Depth First Search (DFS) traversal: 0 1 3 4 2 5 6


Experiment- 3
Aim : write a program to conduct game search.
#include <bits/stdc++.h>
using namespace std;

#define COMPUTER 1
#define HUMAN 2
#define SIDE 3 // Length of the board
#define COMPUTERMOVE 'O'
#define HUMANMOVE 'X'

// Function to show the board


void showBoard(char board[SIDE][SIDE]) {
cout << "\n";
for (int i = 0; i < SIDE; i++) {
cout << "\t\t\t";
for (int j = 0; j < SIDE; j++) {
cout << board[i][j];
if (j < SIDE - 1) cout << " | ";
}
cout << "\n";
if (i < SIDE - 1)
cout << "\t\t\t---------\n";
}
cout << "\n";
}

// Function to show game instructions


void showInstructions() {
cout << "\t\t\tTic-Tac-Toe\n\n";
cout << "Choose a cell numbered from 1 to 9 as below and play\n\n";
cout << "\t\t\t 1 | 2 | 3 \n";
cout << "\t\t\t-----------\n";
cout << "\t\t\t 4 | 5 | 6 \n";
cout << "\t\t\t-----------\n";
cout << "\t\t\t 7 | 8 | 9 \n\n";
}

// Function to initialize the board


void initialize(char board[SIDE][SIDE]) {
srand(time(NULL));
for (int i = 0; i < SIDE; i++)
for (int j = 0; j < SIDE; j++)
board[i][j] = ' ';
}

// Function to check if there are moves left


bool isMovesLeft(char board[SIDE][SIDE]) {
for (int i = 0; i < SIDE; i++)
for (int j = 0; j < SIDE; j++)
if (board[i][j] == ' ')
return true;
return false;
}

// Function to evaluate board


int evaluate(char board[SIDE][SIDE]) {
// Check rows and columns
for (int row = 0; row < SIDE; row++) {
if (board[row][0] == board[row][1] &&
board[row][1] == board[row][2]) {
if (board[row][0] == COMPUTERMOVE)
return +10;
else if (board[row][0] == HUMANMOVE)
return -10;
}
}
for (int col = 0; col < SIDE; col++) {
if (board[0][col] == board[1][col] &&
board[1][col] == board[2][col]) {
if (board[0][col] == COMPUTERMOVE)
return +10;
else if (board[0][col] == HUMANMOVE)
return -10;
}
}
// Check diagonals
if (board[0][0] == board[1][1] &&
board[1][1] == board[2][2]) {
if (board[0][0] == COMPUTERMOVE)
return +10;
else if (board[0][0] == HUMANMOVE)
return -10;
}
if (board[0][2] == board[1][1] &&
board[1][1] == board[2][0]) {
if (board[0][2] == COMPUTERMOVE)
return +10;
else if (board[0][2] == HUMANMOVE)
return -10;
}
return 0;
}

// Minimax function
int minimax(char board[SIDE][SIDE], int depth, bool isAI) {
int score = evaluate(board);

if (score == 10) return score - depth;


if (score == -10) return score + depth;
if (!isMovesLeft(board)) return 0;

if (isAI) {
int best = -1000;
for (int i = 0; i < SIDE; i++) {
for (int j = 0; j < SIDE; j++) {
if (board[i][j] == ' ') {
board[i][j] = COMPUTERMOVE;
best = max(best, minimax(board, depth + 1, !isAI));
board[i][j] = ' ';
}
}
}
return best;
} else {
int best = 1000;
for (int i = 0; i < SIDE; i++) {
for (int j = 0; j < SIDE; j++) {
if (board[i][j] == ' ') {
board[i][j] = HUMANMOVE;
best = min(best, minimax(board, depth + 1, !isAI));
board[i][j] = ' ';
}
}
}
return best;
}
}

// Function to find the best move for AI


pair<int, int> findBestMove(char board[SIDE][SIDE]) {
int bestVal = -1000;
pair<int, int> bestMove = {-1, -1};

for (int i = 0; i < SIDE; i++) {


for (int j = 0; j < SIDE; j++) {
if (board[i][j] == ' ') {
board[i][j] = COMPUTERMOVE;
int moveVal = minimax(board, 0, false);
board[i][j] = ' ';
if (moveVal > bestVal) {
bestMove = {i, j};
bestVal = moveVal;
}
}
}
}
return bestMove;
}

// Function to declare the winner


void declareWinner(int whoseTurn) {
if (whoseTurn == COMPUTER)
cout << "Computer wins!\n";
else
cout << "Human wins!\n";
}

// Function to start the game


void playTicTacToe(int whoseTurn) {
char board[SIDE][SIDE];
initialize(board);
showInstructions();

int moveCount = 0;
while (isMovesLeft(board)) {
showBoard(board);
if (whoseTurn == HUMAN) {
int move;
cout << "Enter your move (1-9): ";
cin >> move;
int x = (move - 1) / SIDE, y = (move - 1) % SIDE;
if (board[x][y] == ' ') {
board[x][y] = HUMANMOVE;
moveCount++;
if (evaluate(board) == -10) {
showBoard(board);
declareWinner(HUMAN);
return;
}
whoseTurn = COMPUTER;
} else {
cout << "Invalid move! Try again.\n";
}
} else {
pair<int, int> bestMove = findBestMove(board);
board[bestMove.first][bestMove.second] = COMPUTERMOVE;
moveCount++;
if (evaluate(board) == 10) {
showBoard(board);
declareWinner(COMPUTER);
return;
}
whoseTurn = HUMAN;
}
}

showBoard(board);
cout << "It's a tie!\n";
}

// Driver code
int main() {
cout << "Welcome to Tic-Tac-Toe!\n";
char playAgain;
do {
playTicTacToe(HUMAN);
cout << "Do you want to play again? (y/n): ";
cin >> playAgain;
} while (playAgain == 'y' || playAgain == 'Y');
return 0;
}
INDO GLOBAL COLLEGE OF ENGINEERING
ARTIFICIAL INTELLIGENCE PRACTICAL FILE

PRACTICAL:-04

Aim:-

Write a program to infer from the Bayesian network.

#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <set>

class Node {
public:
std::string name;
std::set<Node*> parents; // Parents of the node
std::unordered_map<std::string, double> probabilities; // Conditional probabilities

Node(std::string name) : name(name) {}

void addParent(Node* parent) {


parents.insert(parent);
}

void setProbability(const std::string& parentState, double probability) {


probabilities[parentState] = probability;
}

double getProbability(const std::string& parentState) {


return probabilities[parentState];
}

void display() {
std::cout << "Node: " << name << "\n";
std::cout << "Parents: ";
for (auto parent : parents) {
std::cout << parent->name << " ";
}
std::cout << "\nProbabilities:\n";
for (const auto& prob : probabilities) {
std::cout << " P(" << name << "|" << prob.first << ") = " << prob.second << "\n";
}
}
INDO GLOBAL COLLEGE OF ENGINEERING
ARTIFICIAL INTELLIGENCE PRACTICAL FILE

};

class BayesianNetwork {
public:
std::vector<Node*> nodes;

void addNode(Node* node) {


nodes.push_back(node);
}

double infer(Node* target, const std::unordered_map<std::string, std::string>& evidence) {


// Simple inference: Calculate P(target | evidence)
double probability = 0.0;

// Check if the target node has parents


if (target->parents.empty()) {
// If no parents, return the prior probability
return target->getProbability("True");
}

// Iterate over all combinations of parent states


for (const auto& parent : target->parents) {
std::string parentState = evidence.at(parent->name);
double parentProb = parent->getProbability(parentState);
double targetProb = target->getProbability(parentState);
probability += parentProb * targetProb;
}

return probability;
}

void display() {
for (auto node : nodes) {
node->display();
std::cout << "\n";
}
}
};

int main() {
// Create nodes
Node* rain = new Node("Rain");
Node* traffic = new Node("Traffic");
Node* accident = new Node("Accident");
INDO GLOBAL COLLEGE OF ENGINEERING
ARTIFICIAL INTELLIGENCE PRACTICAL FILE

// Define relationships
traffic->addParent(rain);
accident->addParent(traffic);

// Set probabilities
rain->setProbability("True", 0.2); // P(Rain=True) = 0.2
rain->setProbability("False", 0.8); // P(Rain=False) = 0.8

traffic->setProbability("True", 0.7); // P(Traffic=True|Rain=True) = 0.7


traffic->setProbability("False", 0.3); // P(Traffic=False|Rain=True) = 0.3
traffic->setProbability("True", 0.4); // P(Traffic=True|Rain=False) = 0.4
traffic->setProbability("False", 0.6); // P(Traffic=False|Rain=False) = 0.6

accident->setProbability("True", 0.9); // P(Accident=True|Traffic=True) = 0.9


accident->setProbability("False", 0.1); // P(Accident=False|Traffic=True) = 0.1
accident->setProbability("True", 0.2); // P(Accident=True|Traffic=False) = 0.2
accident->setProbability("False", 0.8); // P(Accident=False|Traffic=False) = 0.8

// Create Bayesian Network


BayesianNetwork bn;
bn.addNode(rain);
bn.addNode(traffic);
bn.addNode(accident);

// Display the Bayesian Network


bn.display();

// Evidence: Rain is True


std::unordered_map<std::string, std::string> evidence;
evidence["Rain"] = "True"; // Evidence that it is raining

// Perform inference
double accidentProbability = bn.infer(accident, evidence);
std::cout << "P(Accident | Evidence) = " << accidentProbability << "\n";
}

Output:-

Node: Rain
Parents:
Probabilities:
P(Rain|False) = 0.8
P(Rain|True) = 0.2
INDO GLOBAL COLLEGE OF ENGINEERING
ARTIFICIAL INTELLIGENCE PRACTICAL FILE

Node: Traffic
Parents: Rain
Probabilities:
P(Traffic|False) = 0.6
P(Traffic|True) = 0.4

Node: Accident
Parents: Traffic
Probabilities:
P(Accident|False) = 0.8
P(Accident|True) = 0.2

terminate called after throwing an instance of 'std::out_of_range'


what(): _Map_base::at
INDO GLOBAL COLLEGE OF ENGINEERING
ARTIFICIAL INTELLIGENCE PRACTICAL FILE

PRACTICAL:-05

Aim:-

Write a program to run value and policy iteration in a grid world.

#include <iostream>
#include <vector>
#include <iomanip>
#include <cmath>

const int GRID_SIZE = 4;


const double GAMMA = 0.9; // Discount factor
const double THRESHOLD = 0.01; // Convergence threshold
const double REWARD = 1.0; // Reward for reaching the goal
const double STEP_PENALTY = -0.1; // Penalty for each step

enum Action { UP, DOWN, LEFT, RIGHT };

struct State {
int x, y; // Position in the grid
double value; // Value of the state
Action bestAction; // Best action to take from this state
};

class GridWorld {
public:
GridWorld() {
// Initialize the grid
for (int i = 0; i < GRID_SIZE; ++i) {
for (int j = 0; j < GRID_SIZE; ++j) {
states.push_back({i, j, 0.0, UP});
}
}
// Set the goal state
goalState = &states[GRID_SIZE * (GRID_SIZE - 1) + (GRID_SIZE - 1)];
}

void valueIteration() {
bool converged = false;
while (!converged) {
converged = true;
INDO GLOBAL COLLEGE OF ENGINEERING
ARTIFICIAL INTELLIGENCE PRACTICAL FILE

for (auto& state : states) {


if (isGoal(state)) continue; // Skip goal state
double oldValue = state.value;
state.value = computeValue(state);
if (std::fabs(oldValue - state.value) > THRESHOLD) {
converged = false;
}
}
}
}

void printValues() {
for (int i = 0; i < GRID_SIZE; ++i) {
for (int j = 0; j < GRID_SIZE; ++j) {
std::cout << std::fixed << std::setprecision(2) << states[i * GRID_SIZE + j].value
<< "\t";
}
std::cout << std::endl;
}
}

private:
std::vector<State> states;
State* goalState;

bool isGoal(const State& state) {


return state.x == GRID_SIZE - 1 && state.y == GRID_SIZE - 1;
}

double computeValue(const State& state) {


double maxValue = -1e9; // Initialize with a very low value
for (Action action : {UP, DOWN, LEFT, RIGHT}) {
double val = computeValue(state, action);
maxValue = std::max(maxValue, val);
}
return maxValue;
}

double computeValue(const State& state, Action action) {


if (isGoal(state)) return REWARD;

int newX = state.x, newY = state.y;


switch (action) {
case UP: newX = std::max(0, state.x - 1); break;
INDO GLOBAL COLLEGE OF ENGINEERING
ARTIFICIAL INTELLIGENCE PRACTICAL FILE

case DOWN: newX = std::min(GRID_SIZE - 1, state.x + 1); break;


case LEFT: newY = std::max(0, state.y - 1); break;
case RIGHT: newY = std::min(GRID_SIZE - 1, state.y + 1); break;
}

State& nextState = states[newX * GRID_SIZE + newY];


return STEP_PENALTY + GAMMA * nextState.value;
}
};

int main() {
GridWorld grid;
grid.valueIteration();
std::cout << "State Values after Value Iteration:\n";
grid.printValues();
return 0;
}

Output:-

State Values after Value Iteration:


-0.47 -0.41 -0.34 -0.27
-0.41 -0.34 -0.27 -0.19
-0.34 -0.27 -0.19 -0.10
-0.27 -0.19 -0.10 0.00
INDO GLOBAL COLLEGE OF ENGINEERING
ARTIFICIAL INTELLIGENCE PRACTICAL FILE

PRACTICAL:-06

Aim:-

Write a program to do reinforcement learning in a grid world

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <algorithm>

const int GRID_SIZE = 4;


const double ALPHA = 0.1; // Learning rate
const double GAMMA = 0.9; // Discount factor
const double EPSILON = 0.1; // Exploration rate
const double REWARD = 1.0; // Reward for reaching the goal
const double STEP_PENALTY = -0.1; // Penalty for each step
const int EPISODES = 1000; // Number of episodes

enum Action { UP, DOWN, LEFT, RIGHT };

struct State {
int x, y; // Position in the grid
};

class QLearning {
public:
QLearning() {
// Initialize Q-values to zero
qValues.resize(GRID_SIZE, std::vector<std::vector<double>>(GRID_SIZE,
std::vector<double>(4, 0.0)));
srand(static_cast<unsigned int>(time(0))); // Seed for random number generation
}

void run() {
for (int episode = 0; episode < EPISODES; ++episode) {
State state = {0, 0}; // Start at the top-left corner
while (!isGoal(state)) {
Action action = chooseAction(state);
State nextState = takeAction(state, action);
double reward = getReward(nextState);
INDO GLOBAL COLLEGE OF ENGINEERING
ARTIFICIAL INTELLIGENCE PRACTICAL FILE

updateQValue(state, action, reward, nextState);


state = nextState;
}
}
}

void printQValues() {
for (int i = 0; i < GRID_SIZE; ++i) {
for (int j = 0; j < GRID_SIZE; ++j) {
std::cout << std::fixed << std::setprecision(2);
std::cout << "[" << qValues[i][j][UP] << ", " << qValues[i][j][DOWN] << ", "
<< qValues[i][j][LEFT] << ", " << qValues[i][j][RIGHT] << "]\t";
}
std::cout << std::endl;
}
}

private:
std::vector<std::vector<std::vector<double>>> qValues; // Q-values for each state and
action

bool isGoal(const State& state) {


return state.x == GRID_SIZE - 1 && state.y == GRID_SIZE - 1; // Goal is bottom-right
corner
}

Action chooseAction(const State& state) {


// Epsilon-greedy action selection
if (static_cast<double>(rand()) / RAND_MAX < EPSILON) {
return static_cast<Action>(rand() % 4); // Explore
} else {
return getBestAction(state); // Exploit
}
}

Action getBestAction(const State& state) {


std::vector<double> actions = qValues[state.x][state.y];
double maxQValue = *std::max_element(actions.begin(), actions.end());

// Get all actions with max Q-value (avoid bias in case of ties)
std::vector<Action> bestActions;
for (int action = 0; action < 4; ++action) {
if (qValues[state.x][state.y][action] == maxQValue) {
bestActions.push_back(static_cast<Action>(action));
INDO GLOBAL COLLEGE OF ENGINEERING
ARTIFICIAL INTELLIGENCE PRACTICAL FILE

}
}

// Choose randomly among the best actions to avoid bias


return bestActions[rand() % bestActions.size()];
}

State takeAction(const State& state, Action action) {


State newState = state;
switch (action) {
case UP: newState.x = std::max(0, state.x - 1); break;
case DOWN: newState.x = std::min(GRID_SIZE - 1, state.x + 1); break;
case LEFT: newState.y = std::max(0, state.y - 1); break;
case RIGHT: newState.y = std::min(GRID_SIZE - 1, state.y + 1); break;
}
return newState;
}

double getReward(const State& state) {


if (isGoal(state)) {
return REWARD; // Reward for reaching the goal
}
return STEP_PENALTY; // Penalty for each step
}

void updateQValue(const State& state, Action action, double reward, const State&
nextState) {
// Get the max Q-value for the next state
double maxNextQValue = *std::max_element(qValues[nextState.x][nextState.y].begin(),
qValues[nextState.x][nextState.y].end());

// Q-learning update formula


qValues[state.x][state.y][action] += ALPHA * (reward + GAMMA * maxNextQValue -
qValues[state.x][state.y][action]);
}
};

int main() {
QLearning qLearning;
qLearning.run();
std::cout << "Q-values after training:\n";
qLearning.printQValues();
return 0;
}
INDO GLOBAL COLLEGE OF ENGINEERING
ARTIFICIAL INTELLIGENCE PRACTICAL FILE

Output:-
Q-values after training:
[0.05, 0.12, 0.04, 0.18] [0.14, 0.31, 0.05, 0.26] [0.00, 0.46, -0.04, 0.05] [-
0.02, 0.60, -0.03, 0.00]
[-0.13, -0.13, -0.10, 0.31] [0.15, 0.29, 0.11, 0.46] [0.28, 0.56, 0.29, 0.62]
[0.36, 0.80, 0.40, 0.58]
[-0.08, -0.07, -0.07, 0.01] [0.03, 0.54, -0.04, -0.03] [0.08, 0.14, 0.07, 0.80]
[0.59, 1.00, 0.56, 0.73]
[-0.03, -0.03, -0.03, 0.10] [0.01, 0.01, -0.02, 0.78] [-0.02, 0.13, 0.05, 1.00]
[0.00, 0.00, 0.00, 0.00]

You might also like