Department of Computer Science & Engineering Practical File Subject: Artificial Intelligence Lab (BTCS 605-18) B. Tech - 6 Semester (Batch 2020-24)
Department of Computer Science & Engineering Practical File Subject: Artificial Intelligence Lab (BTCS 605-18) B. Tech - 6 Semester (Batch 2020-24)
Practical File
Subject: Artificial Intelligence Lab
(BTCS 605-18)
B. Tech – 6th Semester
[Batch 2020-24]
Informed Search
Uninformed 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);
}
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 – 3
Aim: - Write a program to conduct uninformed 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 – 4
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
vector<vector<pi> > graph;
void addedge(int x, int y, int cost)
{
graph[x].push_back(make_pair(cost, y));
graph[y].push_back(make_pair(cost, x));
}
void best_first_search(int actual_Src, int target, int n)
{
vector<bool> visited(n, false);
priority_queue<pi, vector<pi>, greater<pi> > pq;
pq.push(make_pair(0, actual_Src));
int s = actual_Src;
visited[s] = true;
while (!pq.empty()) {
int x = pq.top().second;
cout << x << " ";
pq.pop();
if (x == target)
break;
for (int i = 0; i < graph[x].size(); i++) {
if (!visited[graph[x][i].second]) {
visited[graph[x][i].second] = true;
pq.push(make_pair(graph[x][i].first,graph[x][i].second));
}
}
}
}
int main()
{
int v = 14;
graph.resize(v);
addedge(0, 1, 3);
addedge(0, 2, 6);
addedge(0, 3, 5);
addedge(1, 4, 9);
addedge(1, 5, 8);
addedge(2, 6, 12);
addedge(2, 7, 14);
addedge(3, 8, 7);
addedge(8, 9, 5);
addedge(8, 10, 6);
addedge(9, 11, 1);
addedge(9, 12, 10);
addedge(9, 13, 2);
int source = 0;
int target = 9;
cout << "Best First Search:"<<endl;
best_first_search(source, target, v);
return 0;
}
Output: -
Experiment – 5
class Vector2
{
int x, y;
public:
Vector2(int _x, int _y) : x(_x), y(_y) {}
Vector2() = default;
Vector2 operator +(const Vector2& other)
{
Vector2 temp;
temp.x = this->x + other.x;
temp.y = this->y + other.y;
return temp;
}
int getX() const { return x; }
int getY() const { return y; }
struct Node
{
Vector2 position;
int G, H, F;
Node* parent = nullptr;
Node() = default;
Node(const Node& other) = default;
Node(Vector2 pos):position(pos) {};
class Map
{
std::vector<char> data;
int size;
public:
Map() = default;
Map(int _size) : size(_size) {
data.resize(size * size);
for (int i = 0; i < size * size; ++i) data[i] = '.';
}
void display() const{
for (int i = 1; i <= size * size; ++i) {
std::cout << data[i - 1] << " ";
if (!(i % size)) std::cout << "\n";
}
}
bool getIfInDanger(Vector2 position) const {
if (position.y < 0) position.y = 0;
if (position.x < 0) position.x = 0;
if (position.y >= 20) position.y = size - 1;
if (position.x >= 20) position.x = size - 1;
return(data[position.getX() + (position.getY() * size)] == 'X');
}
void setElement(char&& asda, Vector2 position) {
data[position.getX() + (position.getY() * size)] = asda;
}
};
class Solver
{
Vector2 startPos, endPos;
std::vector<Vector2> directions;
Map map;
public:
Solver(Vector2 _startPos, Vector2 _endPos, int size) : startPos(_startPos),
endPos(_endPos){
Map temp(size);
map = temp;
directions.resize(8);
directions[0] = Vector2(-1, 1);
directions[1] = Vector2(-1, 0);
directions[2] = Vector2(-1, -1);
directions[3] = Vector2(0, 1);
directions[4] = Vector2(0, -1);
directions[5] = Vector2(1, 1);
directions[6] = Vector2(1, 0);
directions[7] = Vector2(1, -1);
}
bool aStar() {
Node startNode(startPos);
Node goalNode(Vector2(endPos.getX(), endPos.getY()));
if (map.getIfInDanger(startNode.position) || map.getIfInDanger(goalNode.position)) {
std::cout << "Either the start of this map is obstructed or so is the end.";
return false;
}
std::list<Node> openList;
std::list<Node> closedList;
startNode.calc(endPos);
openList.push_back(startNode);
while (!openList.empty()) {
auto current = Node(*std::min_element(openList.begin(), openList.end()));
current.calc(endPos);
closedList.push_back(current);
openList.remove(current);
if (current == goalNode) break;
successor.calc(endPos);
openList.push_back(successor);
}
else
if (successor.G < inOpen->G) successor.parent = &closedList.back();
}
}
if (!openList.size()) {
std::cout << "No path has been found\n";
return false;
}
map.display();
return true;
}
};
int main()
{
Solver solve(Vector2(0,0),Vector2(19,19), 20);
solve.aStar();
}
Output: -
Experiment – 6
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 – 7
Output: -
Experiment – 8
Output: -
Experiment – 9
Output: -
Experiment – 10
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);
// Print final U
for (r = 0; r < rMax; r++) {
for (c = 0; c < cMax; c++) {
cout << U[r][c] << " ";
}
cout << endl;
}
return 0;
}
Output: -
Experiment – 11
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);
Output: -