Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab Manual of ISL

Download as pdf or txt
Download as pdf or txt
You are on page 1of 55

LAB MANUAL

INTELLIGENT SYSTEM LAB


Department of Information Technology
BE(A and B)/ VII semester
Academic Year 2020-21
Ramrao Adik Institute Of Technology, Nerul, Navi Mumbai
Department of Information Technology

SUBJECT CODE: ITL703 SUBJECT NAME: Intelligence System Lab


SEMESTER: VII ACADEMIC YEAR: 2020-21
NAME OF FACULTY: Anita Patil

“Experiment List”
Exp. Lab
Experiments Name
No Outcome
Tutorial exercise for: Design of Intelligent System using PEAS.
1 LO1

Tutorial exercise for: Problem Definition with State Space Representation


2 LO2
To implement application program based on Uninformed (DFS, BFS,
3 Uniformed cost search, Depth limited search, Iterative Deepening) search LO2
algorithm.
To implement application program based on informed (Hill Climbing, simulated
4 LO2
Annealing, BFS, A*)Search Algorithm.
a. To implement Constraint Satisfaction Programming (Cryto Arithmetic,
Map Coloring, N-Queens)
5 OR LO3

aTo implement Game playing algorithms.


Assignment on Predicate Logic, for forward and backward reasoning and
6 resolution LO4

Tutorial Assignment on Design of a Planning system using STRIPS.


7 LO4

To implement the Bayes' Belief Network.


8 LO5
To Implementation of Inference rule(extra)
9 LO4
Mini project

Construction of a domain specific ChatBot using Natural Language Processing


techniques.
10 LO6
( Applications can include : Medical Diagnosis, Personal Shopping Assistant,
Travel Agent , Trouble shooting etc.)

Course Objectives: Students will try:


1. To introduce the concepts of a Rational Intelligent Agent and the different types of Agents that can be designed
to solve problems
2. To impart basic proficiency in representing difficult real life problems in a state space representation so as to
solve them using AI techniques.
3. To make students understand various AI methods like searching and game playing and how to apply them to
solve real applications
4. To explain to students the basic issues of knowledge representation and Logic so as to build inference engines
5. To impart a basic understanding of some of the more advanced topics of AI such as planning. 6. To understand
Bayes networks, natural language processing and introduce concept of cognitive computing.

Course Outcomes: Students will be able to:


1. Design the building blocks of an Intelligent Agent using PEAS representation .
2. Analyze and formalize the problem as a state space, graph, design heuristics and select amongst different search
or game based techniques to solve them.
3. Develop intelligent algorithms for constraint satisfaction problems and also design intelligent systems for Game
Playing
4. Attain the capability to represent various real life problem domains using logic based techniques and use this to
perform inference or planning.
5. Formulate and solve problems with uncertain information using Bayesian approaches.
6. Apply concept Natural Language processing and cognitive computing for creation of domain specific ChatBots

Subject Teacher HOD


Anita Patil Dr. Ashish Jadhav
Jyoti Joshi

Experiment No. 1
Aim: Tutorial exercise for: Design of Intelligent System using PEAS.

Theory:
PEAS stands for Performance Measures, Environment, Actuators, and Sensors.
• Performance Measure: If the objective function to judge the performance of the agent. For example, in case
of pick and place robot, no of correct parts in a bin can be the performance measure.
• Environment: It the real environment where the agent need to deliberate actions.
• Actuators: These are the tools, equipment or organs using which agent performs actions in the environment.
This works as output of the agent.
• Sensors: These are tools, organs using which agent captures the state of the environment. This works as
input to the agent.
Example:
PEAS descriptor for Automated Car Driver:
Performance Measure:
➢ Safety: Automated system should be able to drive the car safely without dashing anywhere.
➢ Optimum speed: Automated system should be able to maintain the optimal speed depending upon
the surroundings.
➢ Comfortable journey: Automated system should be able to give a comfortable journey to the end
user.
Environment:
➢ Roads: Automated car driver should be able to drive on any kind of a road ranging from city roads
to highway.
➢ Traffic conditions: You will find different sort of traffic conditions for different type of roads.
Actuators:
➢ Steering wheel: used to direct car in desired directions.
➢ Accelerator, gear: To increase or decrease speed of the car.
Sensors: To take i/p from environment in car driving.
➢ Cameras
➢ Odometer
➢ GPS
➢ speedometer
Questions:
1. Medical diagnosis system
2. Part-picking robot
3. Crossword puzzle
4. Taxi Driving
5. Refinery Controller
6. Interact. Eng. Tutor
7. Internet Shopping Agent

Experiment No. 2
Aim: Problem Definition with State Space Representation

Theory:

• Formulate a problem as a state space search by showing the legal problem states, the legal operators, and
the initial and goal states .
• A state is defined by the specification of the values of all attributes of interest in the world
• An operator changes one state into the other; it has a precondition which is the value of certain attributes
prior to the application of the operator, and a set of effects, which are the attributes altered by the operator
• The initial state is where you start
• The goal state is the partial description of the solution

Goal Directed Agent:


A goal directed agent needs to achieve certain goals. Such an agent selects its actions based on the goal it has.
Many problems can be represented as a set of states and a set of rules of how one state is transformed to another.
Each state is an abstract representation of the agent's environment. It is an abstraction that denotes a configuration
of the agent. Initial state : The description of the starting configuration of the agent An action/ operator takes the
agent from one state to another state. A state can have a number of successor states. A plan is a sequence of
actions.
A goal is a description of a set of desirable states of the world. Goal states are often specified by a goal test which
any goal state must satisfy.
Let us look at a few examples of goal directed agents.
1. 15-puzzle: The goal of an agent working on a 15-puzzle problem may be to reach a configuration which satisfies
the condition that the top row has the tiles 1, 2 and 3. The details of this problem will be described later.
2. The goal of an agent may be to navigate a maze and reach the HOME position.
The agent must choose a sequence of actions to achieve the desired goal.

State Space Search Notations:


An initial state is the description of the starting configuration of the agent
An action or an operator takes the agent from one state to another state which is called a successor state. A state
can have a number of successor states. A plan is a sequence of actions.
The cost of a plan is referred to as the path cost. The path cost is a positive number, and a common path cost may
be the sum of the costs of the steps in the path.
Problem formulation means choosing a relevant set of states to consider, and a feasible set of operators for moving
from one state to another. Search is the process of considering various possible sequences of operators applied to
the initial state, and finding out a sequence which culminates in a goal state.
For Example:
The eight tile puzzle problem formulation
The eight tile puzzle consist of a 3 by 3 (3*3) square frame board which holds 8 movable tiles numbered 1 to 8.
One square is empty, allowing the adjacent tiles to be shifted. The objective of the puzzle is to find a sequence of
tile movements that leads from a starting configuration to a goal configuration.

The states of 8 tile puzzle are the different permutations of the tiles within frame.
Standard formulation:
States: It specifies the location of each of the 8 tiles and the blank in one of the nice squares.
Initial state : Any state can be designated as the initial state.
Goal : Many goal configurations are possible one such is shown in the figure
Legal moves ( or state) : They generate legal states that result from trying the four actions-
• Blank moves left
• Blank moves right
• Blank moves up
• Blank moves down
Path cost: Each step costs 1, so the path cost is the number of steps in the path.
The tree diagram showing the search space is shown in figure.
Questions:
1. Towers Hanoi
2. Graphs versus Trees
3. Vacuum world
4. Water jug
Extra:
Implement Water Jug Problem Using Problem Formulation

Aim: Implement water jug problem using BFS or DFS (Un-Informed Search).
Theory:

Problem Statement
For further explanation read Section 3.5 of Chapter 3.

In the water jug problem in Artificial Intelligence, we are provided with two jugs: one having the capacity
to hold 3 gallons of water and the other has the capacity to hold 4 gallons of water. There is no other
measuring equipment available and the jugs also do not have any kind of marking on them. So, the agent’s
task here is to fill the 4-gallon jug with 2 gallons of water by using only these two jugs and no other
material. Initially, both our jugs are empty.

So, to solve this problem, following set of rules were proposed:

Production rules for solving the water jug problem

Here, let x denote the 4-gallon jug and y denote the 3-gallon jug.

S.No. Initial State Condition Final state Description of action taken


1. (x,y) If x<4 (4,y) Fill the 4 gallon jug completely
2. (x,y) if y<3 (x,3) Fill the 3 gallon jug completely
3. (x,y) If x>0 (x-d,y) Pour some part from the 4 gallon jug
4. (x,y) If y>0 (x,y-d) Pour some part from the 3 gallon jug
5. (x,y) If x>0 (0,y) Empty the 4 gallon jug
6. (x,y) If y>0 (x,0) Empty the 3 gallon jug
7. (x,y) If (x+y)<7 (4, y-[4-x]) Pour some water from the 3 gallon jug to fill the four gallon
jug

8.
(x,y)
If (x+y)<7
(x-[3-y],y)
Pour some water from the 4 gallon jug to fill the 3 gallon jug.

9. (x,y) If (x+y)<4 (x+y,0)Pour all water from 3 gallon jug to the 4 gallon jug
10. (x,y) if (x+y)<3 (0, x+y) Pour all water from the 4 gallon jug to the 3 gallon jug

The listed production rules contain all the actions that could be performed by the agent in transferring the
contents of jugs. But, to solve the water jug problem in a minimum number of moves, following set of rules
in the given sequence should be performed:

Solution of water jug problem according to the production rules:

S.No. 4 gallon jug contents 3 gallon jug contents Rule followed


1. 0 gallon 0 gallon Initial state
2. 0 gallon 3 gallons Rule no.2
3. 3 gallons 0 gallon Rule no. 9
4. 3 gallons 3 gallons Rule no. 2
5. 4 gallons 2 gallons Rule no. 7
6. 0 gallon 2 gallons Rule no. 5
7. 2 gallons 0 gallon Rule no. 9

On reaching the 7th attempt, we reach a state which is our goal state. Therefore, at this state, our problem is
solved.

Conclusion: Thus, we have successfully implemented water jug problem.

Program:
Problem Statement: There are two jugs (suppose capacity of 3 and 5) and we need to fill the jug in such a
way that 5 liters capacity jug should contain 4 liters of water.

import java.util.*; class WaterJug{


public static void main(String sap[]){ Scanner sc = new Scanner(System.in);

// j1 is capacity of small tank


System.out.print("\nEnter odd capacity of small tank: "); int j1 = sc.nextInt();

// j2 is capacity of large tank


System.out.print("\nEnter odd capacity of large tank: "); int j2 = sc.nextInt();

// count takes care of number of iterations int count = j1 + j2;

/* jug1 array would hold the values for smaller tank and jug2 array would hold the values for larger
tank */
int jug1[] = new int[count]; int jug2[] = new int[count];

int i=0;

// initialzing jug1 and jug2 array jug1[i] = j1;


jug2[i] = 0; i++;

jug1[i] = 0; jug2[i] = j1; i++;

while(i < count){ if(jug1[i-1] > 0){


// if jug1 has any amount of water i.e. it is not empty jug1[i] = jug1[i-1];
jug2[i] = 0;
}
else{
// jug1 is fully empty jug1[i] = j1;
jug2[i] = jug2[i-1];
} i++;

if(jug2[i-1] > 0){


// if jug2 has any amount of water i.e. it is not empty if(jug1[i-1] + jug2[i-1] < j2){
// final result obtained
jug2[i] = jug1[i-1] + jug2[i-1]; jug1[i] = 0;
}
else{
int temp = jug2[i-1]; temp = j2 - temp;
jug2[i] = temp + jug2[i-1];

jug1[i] = jug1[i-1] - temp;


}
}
else{
// jug2 is fully empty

jug2[i] = jug1[i-1]; jug1[i] = 0;


}
i++;
}

// display final result for(i=0; i<count; i++){


System.out.print("\nJUG1: "+jug1[i]+"\tJUG2: "+jug2[i]);
}

System.out.println();
}
}

Output:
Enter odd capacity of small tank: 3 Enter odd capacity of large tank: 5 JUG1: 3 JUG2: 0
JUG1: 0 JUG2: 3
JUG1: 3 JUG2: 3
JUG1: 1 JUG2: 5
JUG1: 1 JUG2: 0
JUG1: 0 JUG2: 1
JUG1: 3 JUG2: 1
JUG1: 0 JUG2: 4

Experiment No 3
Uniformed Search Techniques
Aim: Path finding in maze using depth-first search (DFS).

Theory:
1. Maze generation algorithms are automated methods for the creation of mazes.
2. A maze can be generated by starting with a predetermined arrangement of cells (most
commonly a rectangular grid but other arrangements are possible) with wall sites between
them.
3. This predetermined arrangement can be considered as a connected graph with the edges
representing possible wall sites and the nodes representing cells.
4. The purpose of the maze generation algorithm can then be considered to be making a sub
graph, where it is challenging to find a route between two particular nodes.

Depth-First Search:
1. Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data
structures.
2. One starts at the root (selecting some arbitrary node as the root in the case of a graph) and
explores as far as possible along each branch before backtracking.

Program:
Depth-first search is an algorithm that can be used to generate a maze. The idea is really simple
and easy to implement using recursive method or stack.

Basically, you start from a random point and keep digging paths in one of 4 directions(up, right,
down, left) until you can’t go any further. Once you are stuck, you take a step back until you find
an open path. You would continue digging from there. It’s just the repetition of these.First of all,
I would like to explain the general idea a little deeper which you can apply using your choice of
programming language. After you have the picture in your mind, you can take a look at the
sample code and applet in java.

Explanation

Create a 2-dimensional int array with odd row and column size. 0 represents paths(orange cell)
and 1 would be walls(black cell).

Set all cells to 1(wall). There are no paths right now.

Next, let’s set the starting point. Generate odd numbers for row and col. Set that cell to 0. Use row
and col variables to keep track of current location. On the picture above, it would be row = 3, col
= 5. For clarity, I will be filling the current cell with red.
Now, choose a random direction(up, right, down, or left) you are moving to. You will always be
moving by 2 cells. The picture above illustrates the current cell moving down. There are couple
things you need to check when you move. First, you need to check if 2 cells ahead of that direction
is outside of the maze. Then, you check if 2 cells ahead is a path(0) or wall(1). If it’s a wall, you
can move by setting these 2 cells to 0(path). Update your current location which is row=5, col=5
at this moment.

As you keep digging as above, you notice that you get to a dead end. In this case, keep moving
your current cell to previous cells until you are able to move to a new direction. This is called
backtracking. Current location is at row=7, col=7, so you would be moving back to row=7, col=5
on the picture above. You can implement this logic using recursive method or stack.

So you keep digging as the picture demonstrates. For better visual, I changed the color of arrow
every time it hits a dead end.

Lastly, this is the final result. With this size, it’s just natural that the maze gets too simple. The
bigger the maze, the more complicated it will get.

Sample Applet
Using Recursive Method

After you choose your starting point, pass that information to the recursive method. In the recursive
method, you can do the following…

1. Generate an int array with 4 random numbers to represent directions.


2. Start a for loop to go for 4 times.
3. Set up a switch statement to take care of 4 directions.
4. For that direction, check if the new cell will be out of maze or if it’s a path already open. If
so, do nothing.
5. If the cell in that direction is a wall, set that cell to path and call recursive method passing the
new current row and column.
6. Done.
Sample code
public int[][] generateMaze() {
int[][] maze = new int[height][width];
// Initialize
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
maze[i][j] = 1;

Random rand = new Random();


// r for row、c for column
// Generate random r
int r = rand.nextInt(height);
while (r % 2 == 0) {
r = rand.nextInt(height);
}
// Generate random c
int c = rand.nextInt(width);
while (c % 2 == 0) {
c = rand.nextInt(width);
}
// Starting cell
maze[r][c] = 0;

// Allocate the maze with recursive method


recursion(r, c);

return maze;
}

public void recursion(int r, int c) {


// 4 random directions
int[] randDirs = generateRandomDirections();
// Examine each direction
for (int i = 0; i < randDirs.length; i++) {
switch(randDirs[i]){
case 1: // Up
// Whether 2 cells up is out or not
if (r - 2 <= 0)
continue;
if (maze[r - 2][c] != 0) {
maze[r-2][c] = 0;
maze[r-1][c] = 0;
recursion(r - 2, c);
}
break;
case 2: // Right
// Whether 2 cells to the right is out or not
if (c + 2 >= width - 1)
continue;
if (maze[r][c + 2] != 0) {
maze[r][c + 2] = 0;
maze[r][c + 1] = 0;
recursion(r, c + 2);
}
break;
case 3: // Down
// Whether 2 cells down is out or not
if (r + 2 >= height - 1)
continue;
if (maze[r + 2][c] != 0) {
maze[r+2][c] = 0;
maze[r+1][c] = 0;
recursion(r + 2, c);
}
break;
case 4: // Left
// Whether 2 cells to the left is out or not
if (c - 2 <= 0)
continue;
if (maze[r][c - 2] != 0) {
maze[r][c - 2] = 0;
maze[r][c - 1] = 0;
recursion(r, c - 2);
}
break;
}
}

}
/**
* Generate an array with random directions 1-4

* @return Array containing 4 directions in random order


*/
public Integer[] generateRandomDirections() {
ArrayList<Integer> randoms = new ArrayList<Integer>();
for (int i = 0; i < 4; i++)
randoms.add(i + 1);
Collections.shuffle(randoms);

return randoms.toArray(new Integer[4]);


}
Experiment No 4

Informed Search Technique

Aim: Implement 8-puzzle problem with heuristic function using hill climbing (informed search)

Theory:
In an 8-puzzle game, we need to rearrange some tiles to reach a predefined goal state. Consider
the following 8-puzzle board.

This is the goal state where each tile is in correct place. In this game, you will be given a board
where the tiles aren’t in the correct places. You need to move the tiles using the gap to reach the
goal state.

Suppose f (n) can be defined as: the number of misplaced tiles.

In the above figure, tiles 6, 7 and 8 are misplaced. So f (n) = 3 for this case.

For solving this problem with hill climbing search, we need to set a value for the heuristic. Suppose
the heuristic function h (n) is the lowest possible f (n) from a given state. First, we need to know
all the possible moves from the current state. Then we have to calculate f(n) (number of misplaced
tiles) for each possible move. Finally we need to choose the path with lowest possible f (n) (which
is our h (n) or heuristic).

Consider the figure above. Here, 3 moves are possible from the current state. For each state we
have calculated f (n). From the current state, it is optimal to move to the state with f (n) = 3 as it is
closer to the goal state. So we have our h (n) = 3.

However, do you really think we can guarantee that it will reach the goal state? What will you do
if you reach on a state(Not the goal state) from which there are no better neighbour states! This
condition can be called a local maxima and this is the problem of hill climbing search. Therefore,
we may get stuck in local maxima. In this scenario, you need to backtrack to a previous state to
perform the search again to get rid of the path having local maxima.

What will happen if we reach to a state where all the f (n) values are equal? This condition is called
a plateau. You need to select a state at random and perform the hill climbing search again!
Input:
You will be given the initial state of the board. The input will be given by row by row of the 3x3
grid of the 8-puzzle game. A digit from 1 to 9 will denote a tiles number. A 0 will denote the gap
of the board.

For the above board, the input will be:

123

784

605
Task 1: Print the total cost (number of steps) needed to reach the goal state (if possible). Report if
you reach a local maxima and get stuck. Print the board state in this scenario.

Task 2: You need get rid of any local maxima and reach the goal state anyway. Print the total cost
needed to reach the goal state. Mention where you have backtracked to avoid local maxima (if
any).

Program:

Main.java

public
class
Main
{
public static void main(String[] args) {
Eight_Puzzle eight_Puzzle = new Eight_Puzzle();
eight_Puzzle.initializations();
}
}

Priority.java

import java.util.Arrays;

Public
class
Priority

static int[][] preState;//keeps the previous state


static Node neighbors_nodeArray[];
//takes an node array, sort nodes based on distance of fn
//and return lowest fn node
public static Node sort(Node[] nodeArray) {

if(preState!=null){//parent exists
nodeArray = getParentRemovedNodeArray(nodeArray, preState);//remove
parent
}

//sorting nodes based on fn


for (int i = 0; i < nodeArray.length; i++) {
for (int j = nodeArray.length - 1; j > i; j--) {
if (nodeArray[j].fn < nodeArray[j - 1].fn) {
Node temp = nodeArray[j];
nodeArray[j] = nodeArray[j - 1];
nodeArray[j - 1] = temp;
}
}
}
Priority.neighbors_nodeArray = nodeArray;
return nodeArray[0];
}
//takes node array and prestate
//remove the neighbor which same as prestate and return parent removed node
array
public static Node[] getParentRemovedNodeArray(Node []nodeArray, int[][]
preState) {
Node[] parentRemovedNodeArray = new Node[nodeArray.length - 1];
int j = 0;
for (int i = 0; i < nodeArray.length; i++) {
if (Arrays.deepEquals(nodeArray[i].state, preState)) {
//System.out.println("removed parent");
} else {
parentRemovedNodeArray[j] = nodeArray[i];
j++;
}
}
return parentRemovedNodeArray;
}
}
//Node class
class Node {
int fn;//fn value
int[][] state;//states
int [][] parent;
public Node(int fn, int[][] state, int[][]parent) {
this.fn = fn;
this.state = state;
this.parent = parent;
}
}

Eight_Puzzle.java

import java.util.Rando m;
import java.util.Stack;
public class Eight_Puzzle {
//solution state of the 8-puzzle game
int goal_state[][] = {
{1, 2, 3},
{8, 0, 4},
{7, 6, 5}
};
//problem board of 8-puzzle game
int game_board[][] = {
{2, 6, 3},
{1, 0, 4},
{8, 7, 5}
};
/* one local maxima input example input
{2, 8, 3},
{1, 6, 4},
{7, 0, 5}
*/
/* one solved input example
{1, 3, 4},
{8, 2, 5},
{0, 7, 6}

{2, 0, 6},
{1, 4, 3},
{8, 7, 5}
//nice backtrack not solved in local maxima
{2, 6, 3},
{1, 0, 4},
{8, 7, 5}
*/
/* one no backtrack local maxima test input example
{1, 4, 0},
{8, 3, 2},
{7, 6, 5}
*/
/* one impossible local maxima test input example
{1, 2, 0},
{8, 3, 4},
{7, 6, 5}

using best solved


{8, 2, 3},
{0, 6, 4},
{7, 1, 5}

not using best


{1, 0, 2},
{8, 3, 6},
{7, 4, 5}
*/
//initial empty tile position
int emptyTile_row = 0;
int emptyTile_col = 0;
int stepCounter = 0;
int min_fn;
Node min_fn_node;
Random random = new Random();
Stack<Node> stack_state = new Stack<>();//for backtracking
//initializations
public void initializations() {
locateEmptyTilePosition();//set empty tile position
min_fn = get_fn(game_board);//-? min fn

System.out.println("================================
=========");
printState(game_board, "initial problem state");
System.out.println("initial empty tile position: " + emptyTile_row + ",
" + emptyTile_col);
System.out.println("initial fn (number of misplaced tiles): " + min_fn);

System.out.println("================================
=========");
//start hill climbing search
try {
hill_climbing_search();
} catch (Exception e) {
System.out.println("Goal can not be reached, found closest solution
state");
printState(min_fn_node.state, "---------solution state------with min
fn " + min_fn);
}
}
//start hill climbing search for 8-puzzle problem
public void hill_climbing_search() throws Exception {
while (true) {
System.out.println(">=============================
==
=========<");
System.out.println("cost/steps: " + (++stepCounter));
System.out.println(" ");
//Priority.preState = game_board;//change pre state
Node lowestPossible_fn_node = getLowestPossible_fn_node();
addToStackState(Priority.neighbors_nodeArray);//add neighbors
to stack in high to low order fn
printState(lowestPossible_fn_node.state, " new state");
//print all fn values
// System.out.print("all sorted fn of current state: ");
// for (int i = 0; i < Priority.neighbors_nodeArray.length; i++) {
// System.out.print(Priority.neighbors_nodeArray[i].fn + " ");
// }
// System.out.println();
//check for local maxima
int fnCounter = 1;
for (int i = 1; i < Priority.neighbors_nodeArray.length; i++) {
if (Priority.neighbors_nodeArray[i - 1].fn ==
Priority.neighbors_nodeArray[i].fn) {//fns are equal
fnCounter++;
}
}
if (Priority.neighbors_nodeArray.length != 1 && fnCounter ==
Priority.neighbors_nodeArray.length) {//all fns are equal, equal
chances to choose
System.out.println("---fn's are equal, found in local maxima---");
//backtracking
for (int i = 0; i < Priority.neighbors_nodeArray.length; i++) {
if (stack_state != null) {
System.out.println("pop " + (i + 1));

stack_state.pop();
} else {
System.out.println("empty stack inside loop");
}
}
if (stack_state != null) {
Node gameNode = stack_state.pop();
game_board = gameNode.state;//update game board
Priority.preState = gameNode.parent;//update prestate
locateEmptyTilePosition();//locate empty tile for updated state
printState(game_board, "popped state from all equal fn");
System.out.println("empty tile position: " + emptyTile_row + ",
" + emptyTile_col);
} else {
System.out.println("stack empty inside first lm check");
}
} else {//for backtracking
System.out.println("lowest fn: " + lowestPossible_fn_node.fn);
if (lowestPossible_fn_node.fn == 0) {//no misplaced found
System.out.println(" ");
System.out.println("8-Puzzle has been solved!");
System.out.println(" ");
System.out.println("Total cost/steps to reach the goal: " +
stepCounter);
System.out.println(" ");
break;
}
if (lowestPossible_fn_node.fn <= min_fn) {
min_fn = lowestPossible_fn_node.fn;
min_fn_node = lowestPossible_fn_node;//store lowest fn
solution
if (stack_state != null) {
Node gameNode = stack_state.pop();
game_board = gameNode.state;//update game board

Priority.preState = gameNode.parent;//update prestate


locateEmptyTilePosition();//locate empty tile for updated
state
printState(game_board, " ------ new state as going deeper");
System.out.println("empty tile position: " + emptyTile_row
+ ", " + emptyTile_col);
} else {
System.out.println("stack empty");
}
} else {
System.out.println("---stuck in local maxima---");
System.out.println("getting higher, not possible");
//break;
//backtracking
for (int i = 0; i < Priority.neighbors_nodeArray.length; i++) {
if (stack_state != null) {
//System.out.println("pop " + (i + 1));
stack_state.pop();
} else {
System.out.println("empty stack inside loop");
}
}
if (stack_state != null) {
Node gameNode = stack_state.pop();
game_board = gameNode.state;//update game board
Priority.preState = gameNode.parent;//update prestate
locateEmptyTilePosition();//locate empty tile for updated
state
printState(game_board, "popped state from getting
higher");
System.out.println("empty tile position: " + emptyTile_row
+ ", " + emptyTile_col);
} else {
System.out.println("stack empty inside second lm check");
}
}//end of if cond: new fn<=pre min fn
}//end of if cond: all fn equal
}//while end
}
private Node getLowestPossible_fn_node() {
if (emptyTile_row == 0 && emptyTile_col == 0) {//0,0 position is
empty tile
//System.out.println("Empty 0,0");
Node fn_array[] = {get_fn_down(), get_fn_right()};
Node lowest_fn_node = Priority.sort(fn_array);
return lowest_fn_node;
} else if (emptyTile_row == 0 && emptyTile_col == 1) {//0,1 position
is empty tile
//System.out.println("Empty 0,1");
Node fn_array[] = {get_fn_left(), get_fn_down(), get_fn_right()};
Node lowest_fn_node = Priority.sort(fn_array);
return lowest_fn_node;
} else if (emptyTile_row == 0 && emptyTile_col == 2) {//0,2 position
is empty tile
//System.out.println("Empty 0,2");
Node fn_array[] = {get_fn_left(), get_fn_down()};
Node lowest_fn_node = Priority.sort(fn_array);
return lowest_fn_node;
} else if (emptyTile_row == 1 && emptyTile_col == 0) {//1,0 position
is empty tile
//System.out.println("Empty 1,0");
Node fn_array[] = {get_fn_down(), get_fn_right(), get_fn_up()};
Node lowest_fn_node = Priority.sort(fn_array);
return lowest_fn_node;
} else if (emptyTile_row == 1 && emptyTile_col == 1) {//1,1 position
is empty tile
//System.out.println("Empty 1,1");
Node fn_array[] = {get_fn_left(), get_fn_down(), get_fn_right(),
get_fn_up()};
Node lowest_fn_node = Priority.sort(fn_array);
return lowest_fn_node;
} else if (emptyTile_row == 1 && emptyTile_col == 2) {//1,2 position
is empty tile
//System.out.println("Empty 1,2");
Node fn_array[] = {get_fn_left(), get_fn_down(), get_fn_up()};
Node lowest_fn_node = Priority.sort(fn_array);
return lowest_fn_node;
} else if (emptyTile_row == 2 && emptyTile_col == 0) {//2,0 position
is empty tile
//System.out.println("Empty 2,0");
Node fn_array[] = {get_fn_right(), get_fn_up()};
Node lowest_fn_node = Priority.sort(fn_array);
return lowest_fn_node;
} else if (emptyTile_row == 2 && emptyTile_col == 1) {//2,1 position
is empty tile
//System.out.println("Empty 2,1");
Node fn_array[] = {get_fn_left(), get_fn_right(), get_fn_up()};
Node lowest_fn_node = Priority.sort(fn_array);
return lowest_fn_node;
} else if (emptyTile_row == 2 && emptyTile_col == 2) {//2,2 position
is empty tile
//System.out.println("Empty 2,2");
Node fn_array[] = {get_fn_left(), get_fn_up()};
Node lowest_fn_node = Priority.sort(fn_array);
return lowest_fn_node;
}
return null;
}
//
//return number of misplaced tiles for left state
private Node get_fn_left() {
int left_state[][] = new
int[game_board.length][game_board[0].length];
for (int i = 0; i < game_board.length; i++) {
for (int j = 0; j < game_board[0].length; j++) {
if (i == emptyTile_row && j == emptyTile_col) {//empty tile,
swap left
left_state[i][j] = game_board[i][j - 1];
left_state[i][j - 1] = game_board[i][j];
} else {//normal copy
left_state[i][j] = game_board[i][j];
}
}
}
printState(left_state, "left state");//print left state
Node node = new Node(get_fn(left_state), left_state, game_board);
return node;
}
//return number of misplaced tiles for right state
private Node get_fn_right() {
int right_state[][] = new
int[game_board.length][game_board[0].length];
for (int i = 0; i < game_board.length; i++) {
for (int j = 0; j < game_board[0].length; j++) {
if (i == emptyTile_row && j == emptyTile_col) {//empty tile,
swap right
right_state[i][j] = game_board[i][j + 1];
right_state[i][j + 1] = game_board[i][j];
j++;//as j++ position already copied/updated
} else {//normal copy
right_state[i][j] = game_board[i][j];
}
}
}
printState(right_state, "right state");//print right state
Node node = new Node(get_fn(right_state), right_state,
game_board);
return node;
}
//return number of misplaced tiles for up state
private Node get_fn_up() {
int up_state[][] = new
int[game_board.length][game_board[0].length];
for (int i = 0; i < game_board.length; i++) {
for (int j = 0; j < game_board[0].length; j++) {
if (i == emptyTile_row && j == emptyTile_col) {//empty tile,
swap up
up_state[i][j] = game_board[i - 1][j];
up_state[i - 1][j] = game_board[i][j];
} else {//normal copy
up_state[i][j] = game_board[i][j];
}
}
}
printState(up_state, "up state");//print up state
Node node = new Node(get_fn(up_state), up_state, game_board);
return node;
}
//return number of misplaced tiles for down state
private Node get_fn_down() {
int down_state[][] = new
int[game_board.length][game_board[0].length];
for (int i = 0; i < game_board.length; i++) {
for (int j = 0; j < game_board[0].length; j++) {
if ((i - 1) == emptyTile_row && j == emptyTile_col) {//down pos
of empty tile, swap down
down_state[i][j] = game_board[i - 1][j];
down_state[i - 1][j] = game_board[i][j];
} else {//normal copy
down_state[i][j] = game_board[i][j];
}
}
}
printState(down_state, "down state");//print down state
Node node = new Node(get_fn(down_state), down_state,
game_board);
return node;
}
//takes a game state and returns number of misplaced tiles
private int get_fn(int[][] game_state) {
int fn_count = 0;
for (int i = 0; i < game_state.length; i++) {
for (int j = 0; j < game_state[0].length; j++) {
if (game_state[i][j] != goal_state[i][j] && game_state[i][j] !=
0) {//found misplaced tiles
fn_count++;
}
}
}
return fn_count;
}
//takes parent removed, sorted node array and add states to stack in high
to low order
private void addToStackState(Node nodeArray[]) {
for (int i = nodeArray.length - 1; i >= 0; i--) {
stack_state.add(nodeArray[i]);//highest fn to lowest fn
}
}
//find out the new empty tile position for current state
private void locateEmptyTilePosition() {
nestedloop://to break inner and outer loop
for (int i = 0; i < game_board.length; i++) {
for (int j = 0; j < game_board[0].length; j++) {
if (game_board[i][j] == 0) {
emptyTile_row = i;
emptyTile_col = j;
break nestedloop;
}
}
}
}
//print the current state of the game board
private void printState(int[][] state, String message) {
System.out.println(message);
for (int i = 0; i < state.length; i++) {
for (int j = 0; j < state[0].length; j++) {
System.out.print(state[i][j] + " ");
}
System.out.println();
}
System.out.println("--------");
}
}
Experiment No 5

Adversarial Search

Aim: Adversarial search minimax algorithm with α-β pruning.


Theory: Minimax may be a decision rule utilised in decision theory, game theory, statistics
and philosophy for minimizing the possible loss for a worst case (maximum loss) scenario. as
an alternative, it will be thought of as maximizing the minimum gain (maximin). Originally
developed for two-player zero-sum game theory, covering both the cases wherever players take
alternate moves and people where they create simultaneous moves, it is additionally been
extended to additional complicated games and to general decision making within the presence
of uncertainty.
In the theory of simultaneous games, a minimax strategy may be a mixed strategy that
is part of the answer to a zero-sum game. In zero-sum games, the minimax solution is that the
same as the nash equilibrium.
Minimax Theorem: The minimax theorem states the following:

For every two-person, zero-sum game with finitely many strategies, there exists a value V and
a mixed strategy for each player, such that
1. Given player 2's strategy, the best payoff possible for player 1 is V, and
2. Given player 1's strategy, the best payoff possible for player 2 is −V.
Equivalently, Player 1's strategy guarantees him a payoff of V regardless of Player 2's strategy,
and similarly Player 2 can guarantee himself a payoff of −V. The name minimax arises because
each player minimizes the maximum payoff possible for the other—since the game is zero-
sum, he also minimizes his own maximum loss (i.e. maximize his minimum payoff).
Example:
B chooses B1 B chooses B2 B chooses B3
A chooses A1 +3 −2 +2
A chooses A2 −1  0 +4
A chooses A3 −4 −3 +1

The following example of a game, wherever A and B create simultaneous moves,


illustrates minimax solutions. Suppose every player has 3 selections and think about the payoff
matrix for A displayed at right. Assume the payoff matrix for B is that the same matrix with
the signs reversed (i.e. if the alternatives are A1 and B1 then B pays three to A). Then, the
minimax selection for A is A2 since the worst possible result then needs to pay one, whereas
the simple minimax selection for B is B2 since the worst attainable result is then no payment.
However, this solution is not stable, since if B believes A can opt for A2 then B can opt for B1
to gain 1; then if A believes B can opt for B1 then A can opt for A1 to achieve 3; then B can
opt for B2; and eventually each players can realize the difficulty of making a selection. Thus,
an additional stable strategy is required.
Some decisions are dominated by others and may be eliminated: A won't select A3
since either A1 or A2 can turn out a stronger result, no matter what B selects; B won't choose
B3 since some mixtures of B1 and B2 can turn out a stronger result, no matter what A chooses.
A can avoid having to make an expected payment of more than 1/3 by selecting A1
with probability 1/6 and A2 with probability 5/6, despite what B chooses. B will ensure an
expected gain of a minimum of 1/3 by using an irregular strategy of choosing B1 with
probability 1/3 and B2 with probability 2/3, no matter what A chooses. These mixed minimax
strategies are currently stable and can't be improved.
Minimax-algorithm

Properties of minimax

• Complete? Yes (if tree is finite)


• Optimal? Yes (against an optimal opponent)
• Time complexity? O (bm)
• Space complexity? O (bm) (depth- first exploration)
The α-β algorithm
Properties of α-β

• Pruning does not affect final result


• Good move ordering improves effectiveness of pruning
• With "perfect ordering," time complexity = O (bm/2)
doubles depth of search
• A simple example of the value of reasoning about which
Computations are relevant (a form of meta-reasoning)
Intelligence Methods – WS 2005/2006 – Marc Erich Latoschik
Conclusion: Thus, we have enforced a program using alpha-beta pruning in Min-Max
algorithm. Thus, Min-Max algorithm prunes the information, thereby using alpha-beta pruning,
we reduce the iteration steps.

Program:

import java.util.Scanner;
public class minmax {
public static int min(int a[][],int n,intsetIndex) {
int smallest = a[setIndex][0];

for(int i=1; i<n; i++) {


if(smallest > a[setIndex][i])

smallest = a[setIndex][i];

return smallest;

public static int max(int a[][],int n,intsetIndex) {


int greatest = a[setIndex][0];

for(int i=1; i<n; i++) {


if(greatest < a[setIndex][i])

greatest = a[setIndex][i];

return greatest;

public static void main(String[] args) {


Scanner s = new Scanner(System.in);

System.out.println("Enter the no. of nodes in each subtree");


int n = s.nextInt();

int set[][] = new int[n][n];


System.out.println("Enter the utility values: ");

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


for(int j=0; j<n; j++) {

set[i][j] = s.nextInt();

int max[][] = new int[1][n];


System.out.print("The min values retured are: ");

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


max[0][i] = min(set, n, i);

System.out.print(" " +max[0][i]);


}

System.out.println("");

int maxValue = max(max, n, 0);


System.out.println("The Max Value is " + maxValue);

}
OUTPUT:

Enter the no. of nodes in each subtree


3

Enter the utility values:

-2

3
4

The min values retured are: -2 1 3


The Max Value is 3

Process completed.

Experiment No 6
Constraint Satisfaction Problem

Aim: To solve 8-queen problem using constraint satisfaction.


Theory:
Introduction: The problem is to search out all ways that of placing N non-attacking queens on
an N by N board. A queen attacks all cells in its same row, column, and either diagonal.
Therefore, the target is to put N queens on an n by n board in such the simplest way that no 2
queens are on identical row, column or diagonal.
In chess, a queen will move as so much as she pleases, horizontally, vertically or
diagonally. A chess board has eight rows and eight columns. The quality eight by eight Queens’
drawback asks the way to place eight queens on a standard chess board so none of them will
hit the other in one move.
The following constraints need to be satisfied:
To place N queens on an N N chessboard so that no two queens are attacking one another: i.e.
1. They are not on the same row.
2. They are not on the same column.
3. They are not on the same diagonal.
Algorithm:
1. Backtracking Algorithm: The idea is to place queens one by one in different columns,
starting from the left-most column. When we place a queen in a column, we check for
clashes with already placed queens. In the current column, if we find a row for which there
is no clash, we mark this row and column as part of the solution. If we do not find such a
row due to clashes, then we backtrack and return false.
1. Start
2. Start in the leftmost column
3. If all queens are placed, return true
4. Try all rows in the current column. Do following for every tried row.
(a) If the queen can be placed safely in this row then mark this [row, column] as part of
the solution and recursively check if placing queen here leads to a solution.
(b) If placing queen in [row, column] leads to a solution then return true.
(c) If placing queen does not lead to a solution then unmark this [row, column]
(Backtrack) and go to step (a) to try other rows.
5. If all rows have been tried and nothing worked, return false to trigger backtracking.
6.Stop

Conclusion: The constraint satisfaction problem (CSP) consists in finding a solution for a constraint
network. This has numerous applications including, e.g. scheduling and timetabling.

Program:
import java.util.*;

public class EightQueens {

public static void main(String args[]) {


Scanner src=new Scanner (System.in);
System.out.println ("Enter the number of queens you want to place :");
int N=src.nextInt();

int[][] board = new int[N][N];


solve(0, board, N);

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


for (int j = 0; j < N; j++) {
if (board[i][j]==1)

System.out.print ("Q ");


else System.out.print("* ");

System.out.println();

} }

static boolean solve(int row, int[][] board, int N) {


if(row>=N) return true;

for(int position = 0; position < N; position++) {


if(isValid(board, row, position, N)) {
board[row][position] = 1;

if(!solve(row+1, board, N)) {


board[row][position] = 0;

} else

return true;

return false;
}

static booleanisValid(int[][] board, int x, int y, int N) {


int i, j;

for(i = 0; i< x; i++) if(board[i][y]==1) return false;

i = x - 1;

j = y - 1;
while((i>=0)&&(j>=0))

if(board[i--][j--]==1) return false;


i = x - 1;

j = y + 1;
while((i>=0)&&(j<N))

if(board[i--][j++]==1) return false;


return true;

Output: Enter the number of queens you want to place:8.


Q*******

****Q***

*******Q

*****Q**

**Q*****

******Q*

*Q******

***Q****

Experiment No 7
Design of a Planning System Using STRIPS (Block World Problem )

Aim: Study of planning agent.


Theory:

Language of Planning Problem:

What is STRIPS?

The Stanford Research Institute Problem Solver (STRIPS) is an automated planning technique
that works by executing a domain and problem to find a goal. With STRIPS, you first describe
the world. You do this by providing objects, actions, preconditions, and effects. These are all
the types of things you can do in the game world.

Once the world is described, you then provide a problem set. A problem consists of an initial
state and a goal condition. STRIPS can then search all possible states, starting from the initial
one, executing various actions, until it reaches the goal.

A common language for writing STRIPS domain and problem sets is the Planning Domain
Definition Language (PDDL). PDDL lets you write most of the code with English words, so
that it can be clearly read and (hopefully) well understood. It’s a relatively easy approach to
writing simple AI planning problems.

Problem statement
Design a planning agent for a Blocks World problem. Assume suitable initial state and
final state for the problem.

Designing the Agent


Idea is to give an agent:
• Representation of goal/intention to achieve
• Representation of actions it can perform; and
• Representation of the environment;
Then have the agent generate a plan to achieve the goal.
The plan is generated entirely by the planning system, without human intervention.
Assume start & goal states as below:

a. STRIPS : A planning system – Has rules with precondition deletion list and addition list
Sequence of actions :
b. Grab C
c. Pickup C
d. Place on table C
e. Grab B
f. Pickup B
g. Stack B on C
h. Grab A
i. Pickup A
j. Stack A on B
Rules:
k. R1 : pickup(x)

1. Precondition & Deletion List : hand empty, on(x,table), clear(x)


2. Add List : holding(x)

l. R2 : putdown(x)

1. Precondition & Deletion List : holding(x)


2. Add List : hand empty, on(x,table), clear(x)

m. R3 : stack(x,y)

1. Precondition & Deletion List :holding(x), clear(y)


2. Add List : on(x,y), clear(x)

n. R4 : unstack(x,y)

1. Precondition & Deletion List : on(x,y), clear(x)


2. Add List : holding(x), clear(y)

Plan for the assumed blocks world problem


For the given problem, Start $\rightarrow$ Goal can be achieved by the following sequence:

1. Unstack(C,A)
2. Putdown(C)
3. Pickup(B)
4. Stack(B,C)
5. Pickup(A)
6. Stack(A,B)
Experiment No 8

Implementation of Bayes' belief network (probabilistic reasoning in an uncertain


domain)

Aim:To implementation of Bayes' belief network (probabilistic reasoning in an uncertain


domain).

Theory:
Probabilistic reasoning

The aim of a reasoning is to combine the capacity of probability theory to handle uncertainty
with the capacity of deductive logic to exploit structure. The result is a richer and more
expressive formalism with a broad range of possible application areas. Probabilistic logics
attempt to find a natural extension of traditional logic truth tables: the results they define are
derived through probabilistic expressions instead. A difficulty with probabilistic logics is that
they tend to multiply the computational complexities of their probabilistic and logical
components. Other difficulties include the possibility of counter-intuitive results, such as those
of Dempster-Shafer theory. The need to deal with a broad variety of contexts and issues has
led to many different proposals.
Probabilistic Reasoning Using Bayesian Learning: The idea of Bayesian learning is to
compute the posterior probability distribution of the target features of a new example
conditioned on its input features and all of the training examples.

Suppose a new case has inputs X=x and has target features, Y; the aim is to compute
P(Y|X=x∧e), where e is the set of training examples. This is the probability distribution of the
target variables given the particular inputs and the examples. The role of a model is to be the
assumed generator of the examples. If we let M be a set of disjoint and covering models, then
reasoning by cases and the chain rule give

P(Y|x∧e) = ∑m∈M P(Y ∧m |x∧e)


= ∑m∈M P(Y | m ∧x∧e) ×P(m|x∧e)
= ∑m∈M P(Y | m ∧x) ×P(m|e) .

The first two equalities are theorems from the definition of probability. The last equality
makes two assumptions: the model includes all of the information about the examples that is
necessary for a particular prediction [i.e., P(Y | m ∧x∧e)= P(Y | m ∧x) ], and the model does
not change depending on the inputs of the new example [i.e., P(m|x∧e)= P(m|e)]. This formula
says that we average over the prediction of all of the models, where each model is weighted by
its posterior probability given the examples.

P(m|e) can be computed using Bayes' rule:

P(m|e) = (P(e|m)×P(m))/(P(e)) .

Thus, the weight of each model depends on how well it predicts the data (the likelihood)
and its prior probability. The denominator, P(e), is a normalizing constant to make sure the
posterior probabilities of the models sum to 1. Computing P(e) can be very difficult when there
are many models.

A set {e1,...,ek} of examples are IID (independent and identically distributed), where the
distribution is given by model m if, for all i and j, examples ei and ej are independent given m,
which means P(ei∧ej|m)=P(ei|m)×P(ej|m). We usually assume that the examples are i.i.d.

Suppose the set of training examples e is {e1,...,ek}. That is, e is the conjunction of the
ei, because all of the examples have been observed to be true. The assumption that the examples
are IID implies

P(e|m) = ∏i=1k P(ei|m)

The set of models may include structurally different models in addition to models that
differ in the values of the parameters. One of the techniques of Bayesian learning is to make
the parameters of the model explicit and to determine the distribution over the parameters.

Example: Consider the simplest learning task under uncertainty. Suppose there is a single
Boolean random variable, Y. One of two outcomes, a and ¬a, occurs for each example. We
want to learn the probability distribution of Y given some examples.

There is a single parameter, φ, that determines the set of all models. Suppose that φ
represents the probability of Y=true. We treat this parameter as a real-valued random variable
on the interval [0,1]. Thus, by definition of φ, P(a|φ)=φ and P(¬a|φ)=1-φ.

Suppose an agent has no prior information about the probability of Boolean variable Y
and no knowledge beyond the training examples. This ignorance can be modelled by having
the prior probability distribution of the variable φ as a uniform distribution over the interval
[0,1]. This is the probability density function labeledn0=0, n1=0 in.

We can update the probability distribution of φ given some examples. Assume that the
examples, obtained by running a number of independent experiments, are a particular sequence
of outcomes that consists of n0 cases where Y is false and n1 cases where Y is true.
Figure Beta distribution based on different samples.

The posterior distribution for φ given the training examples can be derived by Bayes'
rule. Let the examples e be the particular sequence of observation that resulted in n1 occurrences
of Y=true and n0 occurrences of Y=false. Bayes' rule gives us

P(φ|e)=(P(e|φ)×P(φ))/(P(e)) .

The denominator is a normalizing constant to make sure the area under the curve is 1.

Given that the examples are IID,

P(e|φ)=φn1×(1-φ)n0

Because there are n0 cases where Y=false, each with a probability of 1-φ, and n1 cases
where Y=true, each with a probability of φ.

One possible prior probability, P(φ), is a uniform distribution on the interval [0,1]. This
would be reasonable when the agent has no prior information about the probability.

The figure on “Beta distribution based on different samples” gives some posterior
distributions of the variable φ based on different sample sizes, and given a uniform prior. The
cases are (n0=1, n1=2), (n0=2, n1=4), and (n0=4, n1=8). Each of these peak at the same place,
namely at (2)/(3). More training examples make the curve sharper.

The distribution of this example is known as beta distribution; it is parametrized by two


counts, α0 and α1, and a probability p. Traditionally, the αi parameters for the beta distribution
are one more than the counts; thus, αi=ni+1. The beta distribution is

Betaα0,α 1(p)=(1)/(K) pα 1-1×(1-p)α 0-1

where K is a normalizing constant that ensures the integral over all values is 1. Thus, the
uniform distribution on [0,1] is the beta distribution Beta1,1.

The generalization of the beta distribution to more than two parameters is known as the
Dirichlet distribution. The Dirichlet distribution with two sorts of parameters, the "counts"
α1,...,αk, and the probability parameters p1,...,pk, is

Dirichletα1,...,α k(p 1,...,p k) = (1)/(K) ∏j=1 kj pj α -1

where K is a normalizing constant that ensures the integral over all values is 1; pi is the
probability of the ith outcome (and so 0 ≤ pi ≤ 1) and αi is one more than the count of the ith
outcome. That is, αi=ni+1. The Dirichlet distribution looks like as in the figure along each
dimension (i.e. as each pj varies between 0 and 1).

For many cases, summing over all models weighted by their posterior distribution is
difficult, because the models may be complicated (e.g., if they are decision trees or even belief
networks). However, for the Dirichlet distribution, the expected value for outcome i (averaging
over all pj's) is
(αi)/(∑j αj) .

The reason that the αi parameters are one more than the counts is to make this formula
simple. This fraction is well defined only when the αj are all non-negative and not all are zero.

Example: Consider Example, which determines the value of φ based on a sequence of


observations made up of n0 cases where Y is false and n1 cases where Y is true. Consider the
posterior. What is interesting about this is that, whereas the most likely posterior value of φ is
(n1)/(n0+n1), the expected value of this distribution is (n1+1)/(n0+n1+2).

Thus, the expected value of the n0=1, n1=2 curve is (3)/(5), for the n0=2, n1=4 case the
expected value is (5)/(8), and for the n0=4, n1=8 case it is (9)/(14). As the learner gets more
training examples, this value approaches (n)/(m).

This estimate is better than (n)/(m) for a number of reasons. First, it tells us what to do
if the learning agent has no examples: Use the uniform prior of (1)/(2). This is the expected
value of the n=0, m=0 case. Second, consider the case where n=0 and m=3. The agent should
not use P(y)=0, because this says that Y is impossible, and it certainly does not have evidence
for this! The expected value of this curve with a uniform prior is (1)/(5).

An agent does not have to start with a uniform prior; it can start with any prior
distribution. If the agent starts with a prior that is a Dirichlet distribution, its posterior will be
a Dirichlet distribution. The posterior distribution can be obtained by adding the observed
counts to the αi parameters of the prior distribution.

The IID assumption can be represented as a belief network, where each of the ei are
independent given model m. This independence assumption can be represented by the belief
network.

Figure Belief network and plate models of Bayesian learning.

If m is made into a discrete variable, any of the inference methods of the previous
chapter can be used for inference in this network. A standard reasoning technique in such a
network is to condition on all of the observed ei and to query the model variable or an
unobserved ei variable.

The problem with specifying a belief network for a learning problem is that the model
grows with the number of observations. Such a network can be specified before any
observations have been received by using a plate model. A plate model specifies what variables
will be used in the model and what will be repeated in the observations. The plate is drawn as
a rectangle that contains some nodes, and an index (drawn on the bottom right of the plate).
The nodes in the plate are indexed by the index. In the plate model, there are multiple copies
of the variables in the plate, one for each value of the index. The intuition is that there is a pile
of plates, one for each value of the index. The number of plates can be varied depending on the
number of observations and what is queried. In this figure, all of the nodes in the plate share a
common parent. The probability of each copy of a variable in a plate given the parents is the
same for each index.

A plate model lets us specify more complex relationships between the variables. In a
hierarchical Bayesian model, the parameters of the model can depend on other parameters.
Such a model is hierarchical in the sense that some parameters can depend on other parameters.

Example: Suppose a diagnostic assistant agent wants to model the probability that a particular
patient in a hospital is sick with the flu before symptoms have been observed for this patient.
This prior information about the patient can be combined with the observed symptoms of the
patient. The agent wants to learn this probability, based on the statistics about other patients in
the same hospital and about patients at different hospitals. This problem can range from the
cases where a lot of data exists about the current hospital (in which case, presumably, that data
should be used) to the case where there is no data about the particular hospital that the patient
is in. A hierarchical Bayesian model can be used to combine the statistics about the particular
hospital the patient is in with the statistics about the other hospitals.

Suppose that for patient X in hospital H there is a random variable SHX that is true when
the patient is sick with the flu. (Assume that the patient identification number and the hospital
uniquely determine the patient.) There is a value φH for each hospital H that will be used for
the prior probability of being sick with the flu for each patient in H. In a Bayesian model, φH
is treated as a real-valued random variable with domain [0,1]. SHX depends on φH, with
P(SHX|φH)=φH. Assume that φH is distributed according to a beta distribution. We don't assume
that φhi and φh2 are independent of each other, but depend on hyperparameters. The
hyperparameters
hyperparameters incan be of
terms thetheprior
conditional α0 and αP(φ
counts probability 1. The parametersα ,α
|α ,α )= Beta depend
(φ ); α on
andthe
α
hi 0 1 hi 0 1

01

are real-valued random variables, which require some prior distribution.

Figure Hierarchical Bayesian model.


Part (a) shows the plate model, where there is a copy of the outside plate for each
hospital and a copy of the inside plate for each patient in the hospital. Part of the resulting belief
network is shown in part (b). Observing some of the SHX will affect the φH and so α0 and α1,
which will in turn affect the other φH variables and the unobserved SHX variables.

Sophisticated methods exist to evaluate such networks. However, if the variables are
made discrete, any of the methods of the previous chapter can be used.

In addition to using the posterior distribution of φ to derive the expected value, we can
use it to answer other questions such as: What is the probability that the posterior probability
of φ is in the range [a,b]? In other words, derive P((φ ≥ a ∧φ ≤ b) | e).

(∫abpn×(1-p)m-n)/(∫01pn×(1-p)m-n)

This kind of knowledge is used in surveys when it may be reported that a survey is
correct with an error of at most 5%, 19 times out of 20. It is also the same type of information
that is used by probably approximately correct (PAC) learning, which guarantees an error at
most ε at least 1-δ of the time. If an agent chooses the midpoint of the range [a,b], namely
(a+b)/(2), as its hypothesis, it will have error less than or equal to (b-a)/(2), just when the
hypothesis is in [a,b]. The value 1-δ corresponds to P(φ ≥ a ∧φ ≤ b | e). If ε=(b-a)/(2) and δ=1-
P(φ ≥ a ∧φ ≤ b | e), choosing the midpoint will result in an error at most ε in 1-δ of the time.
PAC learning gives worst-case results, whereas Bayesian learning gives the expected number.
Typically, the Bayesian estimate is more accurate, but the PAC results give a guarantee of the
error. The sample complexity required for Bayesian learning is typically much less than that of
PAC learning – many fewer examples are required to expect to achieve the desired accuracy
than are needed to guarantee the desired accuracy.
Experiment No 9

Implement Resolution Inference Rule Using Prolog

Aim: To study resolution inference rule.


Theory: Inference is the act or method of deriving logical conclusions from premises known
or assumed to be true. The conclusion drawn is additionally known as an idiomatic. The laws
of valid inference are studied within the field of logic.
Human inference (i.e. how humans draw conclusions) is historically studied inside the
sphere of cognitive psychology; artificial intelligence researchers develop machine-driven
inference systems to emulate human inference. statistical inference permits for inference from
quantitative data.
The process by which a conclusion is inferred from multiple observations is named
inductive reasoning. The conclusion is also correct or incorrect, or correct to within a certain
degree of accuracy, or correct in certain situations. Conclusions inferred from multiple
observations is also tested by additional observations.
A conclusion reached on the basis of proof and reasoning.
The process of reaching such a conclusion: "order, health, and by inference
cleanliness".
The validity of an inference depends on the shape of the inference. That is, the word
"valid" does not refer to the reality of the premises or the conclusion, but rather to the form of
the inference. an inference may be valid though the elements are false, and may be invalid
though the elements are true. However, a valid form with true premises can always have a real
conclusion.
For example,
All fruits are sweet.
A banana is a fruit.
Therefore, a banana is sweet.
For the conclusion to be necessarily true, the premises need to be true.
To show that this form is invalid, we demonstrate how it can lead from true premises to a false
conclusion.
All apples are fruit. (Correct)
Bananas are fruit. (Correct)
Therefore, bananas are apples. (Wrong)
A valid argument with false premises may lead to a false conclusion:
All tall people are Greek.
John Lennon was tall.
Therefore, John Lennon was Greek.
When a valid argument is used to derive a false conclusion from false premises, the inference
is valid because it follows the form of a correct inference. A valid argument can also be used
to derive a true conclusion from false premises:
All tall people are musicians.
John Lennon was tall.
Therefore, John Lennon was a musician.
In this case we have two false premises that imply a true conclusion.
In mathematical logic and automated theorem proving, resolution could be a rule of inference
resulting in a refutation theorem-proving technique for sentences in propositional logic and
first-order logic. In alternative words, iteratively applying the resolution rule in an acceptable
method allows for telling whether a propositional formula is satisfiable and for proving that a
first-order formula is unsatisfiable; this methodology could prove the satisfiability of a first-
order satisfiable formula, however not always, because it is the case for all ways for first-order
logic. Resolution was introduced by John Alan Robinson in 1965.
Resolution Rule: The resolution rule in propositional logic is a single valid inference rule that
produces a new clause implied by two clauses containing complementary literals. A literal is a
propositional variable or the negation of a propositional variable. Two literals are said to be
complements if one is the negation of the other (in the following, is taken to be the
complement to ). The resulting clause contains all the literals that do not have complements.
Formally:

where
all s and s are literals,
is the complement to , and the dividing line stands for entails
The clause produced by the resolution rule is called the resolvent of the two input clauses.
When the two clauses contain more than one pair of complementary literals, the
resolution rule can be applied (independently) for each such pair; however, the result is always
a tautology.
Modus ponens can be seen as a special case of resolution of a one-literal clause and a
two-literal clause.
A Resolution Technique: When coupled with a complete search algorithm, the resolution rule
yields a sound and complete algorithm for deciding the satisfiability of a propositional formula,
and, by extension, the validity of a sentence under a set of axioms.
This resolution technique uses proof by contradiction and is based on the fact that any
sentence in propositional logic can be transformed into an equivalent sentence in conjunctive
normal form. The steps are as follows.
All sentences in the knowledge base and the negation of the sentence to be proved
(the conjecture) are conjunctively connected.
The resulting sentence is transformed into a conjunctive normal form with the conjuncts
viewed as elements in a set, S, of clauses.
For example,

gives rise to the set


.

Algorithm: The resolution rule is applied to all possible pairs of clauses that contain
complementary literals. After each application of the resolution rule, the resulting sentence is
simplified by removing repeated literals. If the sentence contains complementary literals, it is
discarded (as a tautology). If not, and if it is not yet present in the clause set S, it is added to S,
and is considered for further resolution inferences.
If after applying a resolution rule the empty clause is derived, the original formula is
unsatisfiable (or contradictory), and hence, it can be concluded that the initial conjecture
follows from the axioms.
If, on the other hand, the empty clause cannot be derived, and the resolution rule cannot
be applied to derive any more new clauses, the conjecture is not a theorem of the original
knowledge base.
One instance of this algorithm is the original Davis–Putnam algorithm that was later
refined into the DPLL algorithm that removed the need for explicit representation of the
resolvents.
This description of the resolution technique uses a set S as the underlying data-structure
to represent resolution derivations. Lists, Trees and Directed Acyclic Graphs are other possible
and common alternatives. Tree representations are more faithful to the fact that the resolution
rule is binary. Together with a sequent notation for clauses, a tree representation also makes it
clear to see how the resolution rule is related to a special case of the cut-rule, restricted to
atomic cut-formulas. However, tree representations are not as compact as set or list
representations, because they explicitly show redundant subderivations of clauses that are used
more than once in the derivation of the empty clause. Graph representations can be as compact
in the number of clauses as list representations and they also store structural information
regarding which clauses were resolved to derive each resolvent.
A simple example

In plain language: Suppose is false. In order for the premise to be true, must be
true. Alternatively, suppose is true. In order for the premise to be true, must
be true. Therefore, regardless of falsehood or veracity of , if both premises hold, then the
conclusion is true.
Resolution in First-Order Logic: In first-order logic, resolution condenses the
traditional syllogisms of logical inference down to a single rule.

To understand how resolution works, consider the following example syllogism of term
logic:
All Greeks are Europeans.
Homer is a Greek.
Therefore, Homer is a European.

Or, more generally:

Therefore,
To recast the reasoning using the resolution technique, first the clauses must be
converted to conjunctive normal form. In this form, all quantification becomes
implicit: universal quantifiers on variables (X, Y, …) are simply omitted as understood,
while existentially quantified variables are replaced by Skolem functions.

Therefore,

So, the question is, how does the resolution technique derive the last clause from the
first two? The rule is simple:
Find two clauses containing the same predicate, where it is negated in one clause but
not in the other.
Perform unification on the two predicates. (If the unification fails, you made a bad
choice of predicates. Go back to the previous step and try again.)
If any unbound variables which were bound in the unified predicates also occur in other
predicates in the two clauses, replace them with their bound values (terms) there as well.
Discard the unified predicates, and combine the remaining ones from the two clauses
into a new clause, also joined by the "∨" operator.
To apply this rule to the above example, we find the predicate P occurs in negated form
¬P(X)

in the first clause, and in non-negated form


P(a)
in the second clause. X is an unbound variable, while a is bound value (term). Unifying the two
produces the substitution
X↦a

Discarding the unified predicates, and applying this substitution to the remaining
predicates (just Q(X), in this case), produces the conclusion:
Q(a)

For another example, consider the syllogistic form


All Cretans are islanders.
All islanders are liars.
Therefore, all Cretans are liars.
Or more generally,
∀X P(X) → Q(X)

∀X Q(X) → R(X)

Therefore, ∀X P(X) → R(X)


In CNF, the antecedents become:
¬P(X) ∨Q(X)

¬Q(Y) ∨ R(Y)

(Note that the variable in the second clause was renamed to make it clear that variables in
different clauses are distinct.)
Now, unifying Q(X) in the first clause with ¬Q(Y) in the second clause means
that X and Y become the same variable anyway. Substituting this into the remaining clauses
and combining them gives the conclusion:
¬P(X) ∨ R(X)
The resolution rule, as defined by Robinson, also incorporated factoring, which unifies
two literals in the same clause, before or during the application of resolution as defined above.
The resulting inference rule is refutation complete, in that a set of clauses is unsatisfiable if and
only if there exists a derivation of the empty clause using resolution alone.

Program:
%% Sam's likes and dislikes in food

%% Considering the following will give some practice

%% in thinking about backtracking.

%% ?- likes(sam,dahl).

%% ?- likes(sam,chop_suey).

%% ?- likes(sam,pizza).

%% ?- likes(sam,chips).

%% ?- likes(sam,curry). likes(sam,Food) :- indian(Food), mild(Food). likes(sam,Food) :-


chinese(Food). likes(sam,Food) :- italian(Food). likes(sam,chips). indian(curry).
indian(dahl). indian(tandoori). indian(kurma). mild(dahl). mild(tandoori). mild(kurma).
chinese(chow_mein). chinese(chop_suey). chinese(sweet_and_sour). italian(pizza).
italian(spaghetti).

Output:

Welcome to SWI-Prolog (Multi-threaded, 32 bits, Version 6.6.6)


Copyright (c) 1990-2013 University of Amsterdam, VU Amsterdam

SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,


and you are welcome to redistribute it under certain conditions.

Please visit http://www.swi-prolog.org for details.


For help, use ?- help(Topic). or ?- apropos(Word).
1 ?- chdir('C:/Program Files (x86)/swipl/demo/').
true.

2 ?- consult('C:/Program Files (x86)/swipl/demo/likes.pl').


% C:/Program Files (x86)/swipl/demo/likes.pl compiled 0.02 sec, 17 clauses
true.

3 ?- likes(sam,pizza).
true.
4 ?- likes(sam,idle).
false.
5 ?-

You might also like