Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

ARTIFICIAL_INTELLIGENCE_LAB_MANUAL, Lab Reports of Artificial Intelligence

Lab Manual for Artificial Intelligence

Typology: Lab Reports

2020/2021
On special offer
30 Points
Discount

Limited-time offer


Uploaded on 03/21/2021

sriharshitha-deepala
sriharshitha-deepala 🇮🇳

11 documents

1 / 52

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
Discount

On special offer

Partial preview of the text

Download ARTIFICIAL_INTELLIGENCE_LAB_MANUAL and more Lab Reports Artificial Intelligence in PDF only on Docsity!

AMARAVATI

School of Computer Science

and Engineering

B.Tech (Computer Science and

Engineering)

CSE3002 – Artificial Intelligence

Lab Manual

  1. Write a program to solve Tic-Tac-toe problem. Rules of the Game
    • The game is to be played between two people (in this program between HUMAN and COMPUTER).
    • One of the player chooses ‘O’ and the other ‘X’ to mark their respective cells.
    • The game starts with one of the players and the game ends when one of the players has one whole row/ column/ diagonal filled with his/her respective character (‘O’ or ‘X’).
    • If no one wins, then the game is said to be draw. Source Code(Console Mode): import java.util.Arrays; import java.util.InputMismatchException; import java.util.Scanner; public class TicTacToe { static Scanner in; static String[] board; static String turn; public static void main(String[] args) { in = new Scanner(System.in); board = new String[9]; turn = "X"; String winner = null; populateEmptyBoard(); System.out.println("Welcome to 2 Player Tic Tac Toe."); System.out.println("--------------------------------");

printBoard(); System.out.println("X's will play first. Enter a slot number to place X in:"); while (winner == null) { int numInput; try { numInput = in.nextInt(); if (!(numInput > 0 && numInput <= 9)) { System.out.println("Invalid input; re-enter slot number:"); continue; } } catch (InputMismatchException e) { System.out.println("Invalid input; re-enter slot number:"); continue; } if (board[numInput-1].equals(String.valueOf(numInput))) { board[numInput-1] = turn; if (turn.equals("X")) { turn = "O"; } else { turn = "X"; } printBoard(); winner = checkWinner(); } else { System.out.println("Slot already taken; re-enter slot number:"); continue; } } if (winner.equalsIgnoreCase("draw")) { System.out.println("It's a draw! Thanks for playing."); } else { System.out.println("Congratulations! " + winner + "'s have won! Thanks for playing."); } } static String checkWinner() { for (int a = 0; a < 8; a++) { String line = null; switch (a) { case 0: line = board[0] + board[1] + board[2]; break; case 1: line = board[3] + board[4] + board[5]; break; case 2: line = board[6] + board[7] + board[8];

  1. Write a program to solve production system(Water Jug) problem. You are at the side of a river. You are given a m litre jug and a n litre 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 litres of water where d < n. Determine the minimum no of operations to be performed to obtain d litres 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. There are several ways of solving this problem including BFS and DP. In this article an arithmetic approach to solve the problem is discussed. The problem can be modeled by means of Diophantine equation of the form mx + ny = d which is solvable if and only if gcd(m, n) divides d. Also, the solution x,y for which equation is satisfied can be given using the Extended Euclid algorithm for GCD. For example, if we have a jug J1 of 5 litre (n = 5) and another jug J2 of 3 litre (m = 3) and we have to measure 1 litre of water using them. The associated equation will be 5n + 3m = 1. First of all this problem can be solved since gcd(3,5) = 1 which divides 1 (See this for detailed explanation). Using the Extended Euclid algorithm, we get values of n and m for which the equation is satisfied which are n = 2 and m = -3. These values of n, m also have some meaning like here n = 2 and m = -3 means that we have to fill J1 twice and empty J thrice. Now to find the minimum no of operations to be performed we have to decide which jug should be filled first. Depending upon which jug is chosen to be filled and which to be emptied we have two different solutions and the minimum among them would be our answer. Source Code: package waterJugProblem; public class Node { int x, y; //int height; //List listOfChildren = new ArrayList<>(); public Node(int x, int y){ this.x = x; this.y = y; //listOfChildren = null; } }

import java.util.Scanner; import static java.lang.System.*; import java.util.HashSet; import java.util.LinkedList; import java.util.Queue; import java.util.Set; public class UsingBFS { public static int MAX_CAPACITY_OF_LEFT_JUG, MAX_CAPACITY_OF_RIGHT_JUG, MAX_DEPTH_OF_TREE; public static Queue queue; public static Set alreadyEncountered; public static void main(String[] args) { Scanner sc = new Scanner(System.in); out.println("Enter the capacities of the two jugs respectively"); out.println("The capacity of the larger jug should be followed by the capacity of the smaller jug and they should not be equal"); MAX_CAPACITY_OF_LEFT_JUG = sc.nextInt(); MAX_CAPACITY_OF_RIGHT_JUG = sc.nextInt(); MAX_DEPTH_OF_TREE = 15; out.println("Enter the required capacity"); int t = sc.nextInt(); Node root = new Node(0, 0); // out.println("("+root.x+","+root.y+")"); //root.height = 0; queue = new LinkedList<>(); //To store nodes in order to process them in the BFS manner queue.add(root); alreadyEncountered = new HashSet<>(); //To store which states have already been visited alreadyEncountered.add(root); // int noOfChildrenAtThisLevel = 0; int numberOfNodesTraversed = 0; int level = 1; boolean flag = false; // Queue noOfChildrenOfNodes = new LinkedList<>(); while(!queue.isEmpty()){ numberOfNodesTraversed++; // if(noOfChildrenAtThisLevel==0 &&! noOfChildrenOfNodes.isEmpty()){ // noOfChildrenAtThisLevel = noOfChildrenOfNodes.poll(); // level++; // } int tempVariableForChildren = 0; Node node = queue.poll(); // out.println("("+node.x+","+node.y+")"); if(node.x==-1 && node.y==-1){ ++level; queue.add(new Node(-1, -1));

Node newNode4 = new Node(MAX_CAPACITY_OF_LEFT_JUG, node.y- MAX_CAPACITY_OF_LEFT_JUG+node.x); if(checkIfEncountered(newNode4)) ++tempVariableForChildren; } else if(node.x != 0){ // => y= if(node.x>MAX_CAPACITY_OF_RIGHT_JUG){ Node newNode = new Node(node.x- MAX_CAPACITY_OF_RIGHT_JUG, MAX_CAPACITY_OF_RIGHT_JUG); if(checkIfEncountered(newNode)) ++tempVariableForChildren; } else{ Node newNode = new Node(0, node.x); if(checkIfEncountered(newNode)) ++tempVariableForChildren; Node newNode2 = new Node(MAX_CAPACITY_OF_LEFT_JUG, node.x); if(checkIfEncountered(newNode2)) ++tempVariableForChildren; Node newNode3 = new Node(node.x, MAX_CAPACITY_OF_RIGHT_JUG); if(checkIfEncountered(newNode3)) ++tempVariableForChildren; } } else if(node.y != 0){ // => x= Node newNode = new Node(node.y, 0); if(checkIfEncountered(newNode)) ++tempVariableForChildren; Node newNode2 = new Node(node.y, MAX_CAPACITY_OF_RIGHT_JUG); if(checkIfEncountered(newNode2)) ++tempVariableForChildren; Node newNode3 = new Node(MAX_CAPACITY_OF_LEFT_JUG, node.y); if(checkIfEncountered(newNode3)) ++tempVariableForChildren; } else { // When x and y are both 0 Node newNode1 = new Node(MAX_CAPACITY_OF_LEFT_JUG, 0); if(checkIfEncountered(newNode1)) ++tempVariableForChildren; Node newNode2 = new Node(0, MAX_CAPACITY_OF_RIGHT_JUG); if(checkIfEncountered(newNode2)) ++tempVariableForChildren; }

//queue.add(new Node(-1, -1)); // noOfChildrenOfNodes.add(tempVariableForChildren); if(!flag) queue.add(new Node(-1, -1)); flag = true; } } public static boolean checkIfEncountered(Node node){ if(!alreadyEncountered.contains(node)){ queue.add(node); alreadyEncountered.add(node); return true; //Child added to the queue } return false; //No new child added to the queue } }

  1. Write a program to implement 8-Queens problem. The 8 Queen is the problem of placing 8 chess queens on an 8×8 chessboard so that no two queens attack each other. Naive Algorithm:

//if the state size is equal to n we have a valid configuration if(this.n == state.size()){ return state; } //branch computations for(int i = 0; i < this.n; i++){ //push i onto the stack state.add(i); //check if placement onto stack is valid according to game rules if(isValid(state)){ //if the placement is valid, we need to try to place something in the next column LinkedList tmp = this.compute(state); if(tmp != null){ //if compute returns something non null, then we have reached a valid configuration //and must return it back up the stack. return tmp; } } //if tmp is null here, then we have hit a "wall" and must backtrack state.removeLast(); } /* If we reach this point, then we need to backtrack. */ return null; } public String toString(){ StringBuilder s = new StringBuilder(); s.append(this.state.toString()); for(int i = 0; i < this.n; i++){ //s.append("\n"); //for(int z = 0; z < this.n; z++){s.append("---");} s.append("\n"); for(int j = 0; j < this.n; j++){ if(this.state.get(i) == j){ s.append("| Q"); } else{ s.append("| "); }

s.append("|"); } return s.toString(); } public void solve(){ this.state = compute(new LinkedList()); } } public class Main { public static void main(String argv[]){ Queens queens = new Queens(8); queens.solve(); System.out.println(queens); } }

  1. Compare Breadth First Search and Depth First Search for the given set of elements. Breadth First Traversal (or Search) for a graph is similar to Breadth First Traversal of a tree. The only catch here is, unlike trees, graphs may contain cycles, so we may come to the same node again. To avoid processing a node more than once, we use a boolean visited array. For example, in the following graph, we start traversal from vertex 2. When we come to vertex 0, we look for all adjacent vertices of it. 2 is also an adjacent vertex of 0. If we don’t mark visited vertices, then 2 will be processed again and it will become a non-terminating process. A Breadth First Traversal of the following graph is 2, 0, 3, 1.

Class Node { Char data; Public Node(char c) { this.data=c; } }

  1. Implement constraint satisfaction through map colouring problem. Graph coloring problem is to assign colors to certain elements of a graph subject to certain constraints. Vertex coloring is the most common graph coloring problem. The problem is, given m colors, find a way of coloring the vertices of a graph such that no two adjacent vertices are colored using same color. The other graph coloring problems like Edge Coloring (No vertex is incident to two edges of same color) and Face Coloring (Geographical Map Coloring) can be transformed into vertex coloring.

Chromatic Number: The smallest number of colors needed to color a graph G is called its chromatic number. Source Code: import java.io.FileNotFoundException; public class MapColoringProblem { Graph graph; int[] color_config_array;//this array holds the color number for each vertex corresponding to the index //For example - if the number of colors is 3. Then then different colours will be repreented by numbers 1,2 and 3. //this method prints which vertex will be of which color public void printConfiguration() { for(int i=0;i<color_config_array.length;i++) { System.out.println("The " +(i+1)+"(th) vertex will be colored in color number "+color_config_array[i]); } } //this method colors the graph using recursive backtracking in all possible combinations and returns true of the graph can //be colored in the given number of colors public boolean colorGraph(int vertex_num,int number_of_colours) { if(vertex_num==graph.adjLists.length)//base condition { return checkGraph(); } for(int i=1;i<=number_of_colours;i++) { color_config_array[vertex_num]=i; if(colorGraph(vertex_num+1,number_of_colours)==true) { return true; } } return false; } //this method returns true of graph is colored properly and false otherwise public boolean checkGraph() { boolean result=true; int no_Of_vertex=graph.adjLists.length;

  1. Write a program to solve 8-Queens problem with reduced number of moves. Refer No.3 for explanation.

Source Code: public class MapColoringProblem { Graph graph; int[] color_config_array;//this array holds the color number for each vertex corresponding to the index //For example - if the number of colors is 3. Then then different colours will be repreented by numbers 1,2 and 3. //this method prints which vertex will be of which color public void printConfiguration() { for(int i=0;i<color_config_array.length;i++) { System.out.println("The " +(i+1)+"(th) vertex will be colored in color number "+color_config_array[i]); } } //this method colors the graph using recursive backtracking in all possible combinations and returns true of the graph can //be colored in the given number of colors public boolean colorGraph(int vertex_num,int number_of_colours) { if(vertex_num==graph.adjLists.length)//base condition { return checkGraph(); } for(int i=1;i<=number_of_colours;i++) { color_config_array[vertex_num]=i; if(colorGraph(vertex_num+1,number_of_colours)==true) { return true; } } return false; } //this method returns true of graph is colored properly and false otherwise public boolean checkGraph() { boolean result=true; int no_Of_vertex=graph.adjLists.length; for(int i=0;i<no_Of_vertex;i++) { int color_of_vertex=color_config_array[i];