You are on page 1of 6

MULTIMEDIA UNIVERSITY OF KENYA

FACULTY OF COMPUTING AND INFORMATION TECHNOLOGY


BACHELOR OF SCIENCE IN SOFTWARE ENGINEERING

ASSIGNMENT

NAME: ANSELM MUCHURA OMONDI


REG NO: CIT-227-032/2015
UNIT CODE: CSE 2325
UNIT: ADVANCED ARTIFICIAL INTELLIGENCE
1.
(a)
(i) Uniform cost path: f(n)= g(n)

(ii) Greedy best-first search: f(n)= h(n)

(iii) A* search: f(n)=g(n)+h(n)

(b) F(n) = a1[ g(n) + a2 h(n) / a1]


For optimality, a2/a1 <= 1, since a1 = 1
Therefore, a2 <= 1
A2= 1, 0

2.
(a) For a fixed N, there are (n-1) successor states

(b) Heuristic cost function (h) = the number of adjacent swaps needed to reach the goal

state

(c) It has no local minima since all local minima must also be global

(d)

(i) Hill Climbing


Initial state= (5,9,3,7,2) and let goal state= (9,7,5,3,2) //Descending
Possible Next state= 9,5,3,7,2 (h=2)
Next state = 9,5,7,3,2 (h=1)

(ii) Simulated annealing


Let T=1 and Initial state= (5,9,3,7,2)
4 moves available ie m1=2, m2=4, m3=2, m4=4 (objective functions)
• if any of the moves is picked, move there.
• low T => probability of “locally bad” move is lower
• typically, T is decreased as the algorithm runs longer

(e)
Population Selection Crossover Mutation
53972 59 | 327 59 972 59932
59327 53 | 972 53 327 59327
53972 593 | 27 593 72 59372
59732 539 | 72 539 27 53227
3. Manhattan Distance: The sum of the vertical and horizontal distances from each tile to
their goal position.

8-Puzzle Solution using A* algorithm


import java.util.*;
public class EightPuzzleAStar { //Top Right Bottom Left
public static final int[][] DIR = {{0,1},{1,0},{0,-1},{-1,0}};
public int len;

public void solvePuzzle(int[][] initialState,int[][] finalState) {


int x =0, y = 0;
for(int i=0; i < len; i++) {
for(int j=0; j < len; j++) {
if(initialState[i][j] == 0) {
x = i;
y = j;
}
}
} //find the position of empty space

PriorityQueue< State > pq = new PriorityQueue<>();


State root = new State(initialState, x,y, x,y, 0, null);
root.cost = calculateHammingCost(initialState, finalState);

pq.offer(root);
while(!pq.isEmpty()) {
State current = pq.poll();
if(current.cost == 0) {
System.out.println("SOLUTION FOUND!");
printPath(current);
System.out.println("Total Moves: "+current.step);
System.out.println("PATH: ");
return;
}//reach the goal state
for(int[] dir : DIR) {
int newX = current.x+dir[0];
int newY = current.y+dir[1];

if(isSafe(newX, newY)) {
State child = new State(current.matrix, current.x,current.y, newX, newY, curren-
t.step+1, current);
if(current.parent == null || !
getString(current.parent.matrix).equals(getString(child.matrix))) {
child.cost = calculateHammingCost(child.matrix, finalState);
pq.offer(child);

}
}
}

}
}
public boolean isSafe(int x, int y){
return (x >= 0 && x < len && y >= 0 && y < len);
}
public int calculateHammingCost(int[][] currentState, int[][] finalState) {
int cost = 0;
for(int i=0; i < len;i++) {
for(int j = 0; j < len;j++) {
if(currentState[i][j]!= 0 && currentState[i][j] != finalState[i][j]) {
cost++;
}
}
}
return cost;
}//calculate the cost using Hamming distance

public int calculateManhattanCost(int[][] currentState, int[][] finalState) {


int cost = 0;
String current = getString(currentState);
String goal = getString(finalState);
for(int i=1; i< 9; i++) {
int indexCurrent = current.indexOf(String.valueOf(i));
int indexGoal = goal.indexOf(String.valueOf(i));
cost += Math.abs(indexCurrent / len - indexGoal / len)
+ Math.abs(indexCurrent % len - indexGoal % len);
}
return cost;
}//calculate the cost using Manhattan distance

public String getString(int[][] matrix) {


String result = "";
for(int i=0; i < len; i++) {
for(int j=0; j < len ; j++) {
result += matrix[i][j]+",";
}
}
return result;
}
public void printPath(State root) {
if(root == null) {
return;
}
printPath(root.parent);
printMax(root.matrix);

System.out.println();
}
public void printMax(int[][] matrix) {
for(int i=0; i < len;i++) {
for(int j= 0; j < len; j++) {
System.out.print(matrix[i][j]);
}
System.out.println();
}
}

public static void main(String[] args) {


EightPuzzleAStar eightPuzzle = new EightPuzzleAStar();
int[][] initialState = { {2, 3, 6},
{0, 4, 5},
{8, 1, 7}};
int[][] finalState = {{1, 2, 3},
{8, 0, 4},
{7, 6, 5}};
eightPuzzle.len = 3;
eightPuzzle.solvePuzzle(initialState, finalState);
eightPuzzle.printPath(null);
}
}
class State implements Comparable< State >{
State parent;
int[][] matrix;
int x, y;
int cost;
int step;
State(int[][] matrix, int x, int y, int newX, int newY, int step, State parent){
this.parent = parent;
this.cost = Integer.MAX_VALUE;
this.step = step;
this.x = newX;
this.y = newY;
this.matrix = new int[matrix.length][matrix.length];

for(int i=0; i < matrix.length;i++){


this.matrix[i] = matrix[i].clone();

int temp = this.matrix[x][y];


this.matrix[x][y] = this.matrix[newX][newY];
this.matrix[newX][newY] = temp;

}
@Override
public int compareTo(State that) {
return (this.cost + this.step) - (that.cost+that.step);
}
}
OUTPUT
236 203 123
236 084 186 864
045 175 754 705
817
236 023 123
236 184 186 804
845 075 754 765
017
236 123 SOLUTION FOUND!
236 184 086 PATH: DRRULLDR-
845 705 754 RUULLDRDDLU
107
Total Moves: 19
236 123
236 184 806
845 750 754
170
236 123
236 180 860
840 754 754
175
230 123
236 186 864
804 754 750
175

You might also like