Practical File AI
Practical File AI
Submitted To :-
Er. Manisha
INDEX
S. Date of Name of Experiment Date of Sign
No Practical Submission
#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);
}
void Graph::addEdge(int v, int w) { adj[v].push_back(w);
// Add w to v’s list.
} 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
adjecent : adj[s])
{
if (!visited[adjecent])
{ visited[adjecent] = true; queue.push_back(adjecent); }
}
} } 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: -
Experiment – 2
Aim: - Write a program to conduct uninformed search (Depth First Search).
Depth First Search
#include <iostream>
#include <list> using
namespace std;
class DFSGraph
{ int V; list<int> *adjList; void
DFS_util(int v, bool visited[]);
public: DFSGraph(int V)
{ this->V =
V;
adjList = new list<int>[V];
} void addEdge(int v, int
w){
adjList[v].push_back(w);
} void
DFS();
};
void DFSGraph::DFS_util(int v, bool visited[])
{
visited[v] = true; cout
<< v << " ";
list<int>::iterator i;
for(i = adjList[v].begin(); i != adjList[v].end();
++i) if(!visited[*i]) DFS_util(*i, visited);
}
void DFSGraph::DFS()
{ bool *visited = new
bool[V]; for (int i = 0; i < V;
i++) visited[i] = false; for
(int i = 0; i < V; i++) if
(visited[i] == false)
DFS_util(i, visited);
} int
main()
{
DFSGraph gdfs(5); gdfs.addEdge(0,
1); gdfs.addEdge(0, 2);
gdfs.addEdge(0, 3);
gdfs.addEdge(1, 2);
gdfs.addEdge(2, 4);
gdfs.addEdge(3, 3);
gdfs.addEdge(4, 4);
cout << "Depth-first traversal for the given graph:"<<endl;
gdfs.DFS();
return 0;
}
Output: -
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
#define COMPUTERMOVE 'O'
#define HUMANMOVE 'X'
void showBoard(char board[][SIDE])
{ printf("\n\n");
printf("\t\t\t %c | %c | %c \n", board[0][0],board[0][1], board[0][2]); printf("\t\t\t-------------
-\n");
printf("\t\t\t %c | %c | %c \n", board[1][0],board[1][1], board[1][2]); printf("\t\t\t-------------
-\n");
printf("\t\t\t %c | %c | %c \n\n", board[2][0],board[2][1], board[2][2]);
return; }
void showInstructions()
{ printf("\t\t\t Tic-Tac-Toe\n\n");
printf("Choose a cell numbered from 1 to 9 as below" " and play\n\n");
printf("\t\t\t 1 | 2 | 3 \n"); printf("\t\t\t--------------\n"); printf("\t\t\t 4 | 5 |
6 \n"); printf("\t\t\t--------------\n"); printf("\t\t\t 7 | 8 | 9 \n\n"); printf("-
\t-\t-\t-\t-\t-\t-\t-\t-\t-\n\n");
return; }
void initialise(char board[][SIDE], int moves[])
{ srand(time(NULL));
for (int i=0; i<SIDE; i++)
{
for (int j=0; j<SIDE; j++)
board[i][j] = ' ';
}
for (int i=0; i<SIDE*SIDE; i++)
moves[i] = i;
random_shuffle(moves, moves + SIDE*SIDE);
return; }
void declareWinner(int whoseTurn)
{
if (whoseTurn == COMPUTER)
printf("COMPUTER has won\n");
else printf("HUMAN has
won\n"); return; }
bool rowCrossed(char board[][SIDE])
{ for (int i=0; i<SIDE; i++)
{ if (board[i][0] == board[i][1] &&
board[i][1] == board[i][2]
&& board[i][0] != ' ') return
(true);
}
return(false); }
bool columnCrossed(char board[][SIDE])
{ for (int i=0; i<SIDE; i++)
{ if (board[0][i] == board[1][i] &&
board[1][i] == board[2][i]
&& board[0][i] != ' ') return
(true);
}
return(false); }
bool diagonalCrossed(char board[][SIDE])
{ if (board[0][0] == board[1][1] &&
board[1][1] == board[2][2] &&
board[0][0] != ' ')
return(true);
return(false);
}
bool gameOver(char board[][SIDE])
{ return(rowCrossed(board) || columnCrossed(board)
|| diagonalCrossed(board) );
}
void playTicTacToe(int whoseTurn)
{ char board[SIDE][SIDE];
int moves[SIDE*SIDE];
initialise(board, moves);
showInstructions(); int
moveIndex = 0, x, y;
while (gameOver(board) == false &&
moveIndex != SIDE*SIDE)
{
if (whoseTurn == COMPUTER)
{ x = moves[moveIndex] / SIDE; y =
moves[moveIndex] % SIDE;
board[x][y] = COMPUTERMOVE;
printf("COMPUTER has put a %c in cell %d\n",
COMPUTERMOVE,
moves[moveIndex]+1); showBoard(board);
moveIndex ++;
whoseTurn = HUMAN;
}
else if (whoseTurn == HUMAN)
{ x = moves[moveIndex] / SIDE; y
= moves[moveIndex] % SIDE;
board[x][y] = HUMANMOVE;
printf ("HUMAN has put a %c in cell %d\n",
HUMANMOVE,
moves[moveIndex]+1); showBoard(board);
moveIndex ++;
whoseTurn = COMPUTER;
}
}
if (gameOver(board) == false &&
moveIndex == SIDE * SIDE)
printf("It's a draw\n");
else
{
if (whoseTurn == COMPUTER) whoseTurn
= HUMAN;
else if (whoseTurn == HUMAN)
whoseTurn = COMPUTER;
declareWinner(whoseTurn);
}
return; }
int main()
{
playTicTacToe(COMPUTER);
return (0);
}
Output: -
Experiment – 4
#include <iostream>
#include <vector>
#include <map>
#include <string> using
namespace std;
// Node class for representing the nodes in the Bayesian
network class Node { public:
string name; // Name of the node
vector<string> parents; // Parents of the node
map<string, double> probabilities; // Conditional probabilities for the node
};
// Function to create a new node with the given name and parents
Node* create_node(string name, vector<string> parents) {
Node* node = new Node();
node->name = name; node-
>parents = parents; return
node;
}
// Function to add the conditional probabilities to the node void
add_probabilities(Node* node, map<string, double> probabilities) {
node->probabilities = probabilities;
}
// Function to print the Bayesian network void
print_bayesian_network(vector<Node*> nodes) {
cout << "Bayesian network:" << endl;
for (Node* node : nodes) {
cout << "Node " << node->name << endl;
cout << "Parents: ";
for (string parent : node->parents) {
cout << parent << " ";
} cout <<
endl;
cout << "Probabilities:" << endl; for (auto const&
prob : node->probabilities) { cout << prob.first << " -
> " << prob.second << endl;
}
cout << endl;
}}
int main() {
// Creating nodes
Node* A = create_node("A", {});
Node* B = create_node("B", {"A"});
Node* C = create_node("C", {"A"});
Node* D = create_node("D", {"B", "C"});
// Adding probabilities
map<string, double> probabilities_A = {{"true", 0.2}, {"false", 0.8}}; add_probabilities(A,
probabilities_A);
map<string, double> probabilities_B = {{"true|true", 0.9}, {"true|false", 0.5}, {"false|true",
0.1}, {"false|false", 0.5}};
add_probabilities(B, probabilities_B);
map<string, double> probabilities_C = {{"true|true", 0.3}, {"true|false", 0.7}, {"false|true",
0.7}, {"false|false", 0.3}};
add_probabilities(C, probabilities_C);
map<string, double> probabilities_D = {{"true|true|true", 0.8}, {"true|true|false", 0.4},
{"true|false|true", 0.7}, {"true|false|false", 0.3},
{"false|true|true", 0.2}, {"false|true|false", 0.6}, {"false|false|true",
0.3}, {"false|false|false", 0.7}}; add_probabilities(D, probabilities_D);
// Creating vector of nodes
vector<Node*> nodes = {A, B, C, D};
// Printing the Bayesian network
print_bayesian_network(nodes);
return 0;
}
Output: -
Experiment – 5
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <cassert> using
namespace std;
int game(int winningdoor, int selecteddoor, bool change=false) {
assert(winningdoor < 3);
assert(winningdoor >= 0);
Output: -
Experiment – 6
Aim: - Write a program to run value and policy iteration in a grid world.
#include <iostream>
#include <cstring> #include
<cmath> using namespace std; const
int rMax = 3, cMax = 4; const
double Ra = -3; const double
gamma = 1; const double pGood =
0.8; const double pBad = (1 -
pGood) / 2; const int N = 10000;
const double deltaMin = 1e-9;
double U[rMax][cMax]; double
Up[rMax][cMax]; double
R[rMax][cMax]; char
Pi[rMax][cMax];
int main()
{ int r, c; double delta = 0;
memset(Pi, 0, sizeof(Pi));
memset(Up, 0, sizeof(Up));
memset(U, 0, sizeof(U));
memset(R, Ra, sizeof(R));
R[0][3] = 100;
R[1][3] = -100;
do {
// Copy U to Up
memcpy(Up, U, sizeof(U));
// Update U
delta = 0;
for (r = 0; r < rMax; r++) { for
(c = 0; c < cMax; c++) {
double maxU = -1e9;
for (int i = -1; i <= 1; i++) { for (int j
= -1; j <= 1; j++) { if (abs(i) +
abs(j) != 1) continue;
int rr = r + i;
int cc = c + j;
if (rr < 0 || rr >= rMax || cc < 0 || cc >= cMax) continue;
double Uc = pGood * Up[rr][cc];
if (i != 0 && j != 0) {
Uc += pBad * Up[r][cc];
Uc += pBad * Up[rr][c]; }
maxU = max(maxU, Uc);
}
}
U[r][c] = R[r][c] + gamma * maxU; delta
= max(delta, abs(U[r][c] - Up[r][c])); }
}
} while (delta > deltaMin);
return 0;
}
Output: -
Experiment – 7
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <pgmpy/models/BayesianModel.h>
#include <pgmpy/estimators/MaximumLikelihoodEstimator.h>
#include <pgmpy/inference/VariableElimination.h>
int main() {
ifstream fin("heart.csv");
vector<vector<string>> data;
string line;
while (getline(fin, line)) {
vector<string>
row; string cell; for
(char c : line) { if
(c == ',') {
row.push_back(cell);
cell = "";
} else { cell
+= c;
}
}
row.push_back(cell);
data.push_back(row);
}
fin.close();
cout << "Few examples from the dataset are given below" << endl;
for (int i = 0; i < min(5, (int)data.size()); i++)
{ for (string& cell : data[i]) { cout << cell
<< " ";
}
cout << endl;
}
cout << "\n Learning CPD using Maximum likelihood estimators" << endl;
MaximumLikelihoodEstimator estimator(model);
estimator.fit(data);
cout << "\n Inferencing with Bayesian Network:" << endl; VariableElimination
HeartDisease_infer(model);
Output: -