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

AI ASSIGNMENT

NOTE: Some classes are common and required throughout


the entire assignment. They are shown and described
below.

Node class
This class is responsible to generate nodes. It has data members like
data,index,colour, start_time,end_time etc as shown in the below class.It
holds all the properties of a node.
import java.util.*;

public class Node {


Data data;
int index;
int colour;
int start_time;
int end_time;
int path_cost;
int goal_cost;
int total_cost;
Node parentNode;
ArrayList<Integer> adjNodes;
ArrayList<Integer> edgeValues;

public Node(int index) {


this.index = index;
colour = 0;
start_time = end_time = -1;
parentNode = null;
path_cost = Integer.MAX_VALUE;
goal_cost = total_cost = Integer.MAX_VALUE;
adjNodes = new ArrayList<Integer>();
edgeValues = new ArrayList<Integer>();
}
public String toString() {
return ""+data;
}
}

Graph class
This class is responsible for creating nodes in a graph, adding edges in a
graph. Graph class also have functions to define heuristic for a graph and
generateNode function which is used to generate new states based on a
problem. The graph has reinitialize function to reinitialize the parameters of
a node used during execution of a search technique without disturbing the
weights of edges and data of the nodes.

import java.util.*;

public class Graph {


int no_nodes;
ArrayList<Node> list;
boolean undirected;
boolean searchFlag;
ArrayList<Node> solutionPath;
Node solutionNode;
Data goalNode;

public Graph(int noNodes, boolean flag) {


list = new ArrayList<Node>();
undirected = flag;
no_nodes = noNodes;
solutionPath = new ArrayList<Node>();
for (int i = 0; i < noNodes; i++) {
list.add(new Node(i));
}
}

public void reinitializeGraph() {


solutionPath = new ArrayList<Node>();
solutionNode = null;
for(Node m: list) {
m.colour = 0;
m.start_time = m.end_time = -1;
m.path_cost = m.goal_cost = Integer.MAX_VALUE;
}
}

public void createGoalNode(int data) {


goalNode = new Data(data);
}

public void enterData() {


Scanner sc =new Scanner(System.in);
for(Node i: list) {
System.out.println("Enter the data for node "+i.index);
i.data = new Data(sc.nextInt());
}
}

public void displaySolutionPath() {


Node m = solutionNode;
while (m != null) {
System.out.print(m.index + "<----");
m = m.parentNode;
}
System.out.println("null");
}

public void addEdge(int u, int v) {


Node m = list.get(u);
m.adjNodes.add(v);
m.edgeValues.add(1);
if (undirected) {
m = list.get(v);
m.adjNodes.add(u);
m.edgeValues.add(1);
}
}

public void addEdge(int u, int v, int value) {


Node m = list.get(u);
m.adjNodes.add(v);
m.edgeValues.add(value);
if (undirected) {
m = list.get(v);
m.adjNodes.add(u);
m.edgeValues.add(value);
}
}

public int heuristicFunc(Node node) {


return 0;
}

public void generateNodes(Node node) {


return;
}
}

Data class
This class defines the data that a node wants to store in it.User defined
datatype is intentionally used to define any type and format of data in a
node so that it gives the flexibility to store information in any type of
problem.

public class Data {


public Data(int data){
this.data = data;
}
public boolean validate(Data obj) {
if(obj.data == data) {
return true;
}
else
return false;
}
public String toString() {
return ""+data;
}
}

Search Class
This class implements all the search techniques namely:-
a) Breadth-First Search
b) Depth First Search
c) Iterative Broadening Search
d) Iterative Deepening Search
e) Uniform Cost Search
f) Best first greedy search
g) A star Search technique
h) import java.util.*;

public class Search {

static int globalTime = 0;


static boolean solutionFlag = false;
private static Stack<Node> stack;

public static void dfs(Graph graph,int index) {


//System.out.println("Starting DFS");
globalTime = 0;
solutionFlag = false;
ArrayList<Node> list = graph.list;
Node m = list.get(index);
stack = new Stack<Node>();
if (m.colour == 0)
dfs(m, list, graph);
}

private static void dfs(Node m, ArrayList<Node> list, Graph


graph) {
if (m.colour != 0)
return;
stack.add(m);
System.out.println(stack);
m.start_time = ++globalTime;
m.colour = 1;
graph.generateNodes(m);
if(m.data.validate(graph.goalNode)) {
solutionFlag = true;
m.end_time = ++globalTime;
m.colour = 2;
graph.solutionNode = m;
return;
}
for (int i : m.adjNodes) {
Node ind = list.get(i);
if(ind.colour == 0) {
ind.parentNode = m;
dfs(ind, list, graph);
}
if(solutionFlag) {
return;
}
}
m.end_time = ++globalTime;
m.colour = 2;
stack.pop();
System.out.println(stack);
}

public static void bfs(Graph graph, int index) {


//System.out.println("Starting BFS");
globalTime = 0;
solutionFlag = false;
ArrayList<Node> list = graph.list;
Queue<Node> queue = new LinkedList<>();
Node m = list.get(index);
queue.add(m);
System.out.println(queue);
m.colour = 1;
m.start_time = ++globalTime;
if(m.data.validate(graph.goalNode)) {
System.out.println("Goal Found");
solutionFlag = true;
m.end_time = ++globalTime;
m.colour = 2;
graph.solutionNode = m;
return;
}
while (!queue.isEmpty()) {
Node i = queue.poll();
Node node = i;
System.out.println(queue);
graph.generateNodes(node);
for (int p : node.adjNodes) {
if( list.get(p).colour == 0) {
list.get(p).colour = 1;
list.get(p).start_time = ++globalTime;
list.get(p).parentNode = node;
queue.add(list.get(p));
System.out.println(queue);
if(list.get(p).data.validate(graph.goalNode)) {
solutionFlag = true;
list.get(p).end_time = ++globalTime;
list.get(p).colour = 2;
graph.solutionNode = list.get(p);
return;
}
}
}
node.end_time = ++globalTime;
node.colour = 2;
}
}

public static void dls(Graph graph, int depth, int index) {


//System.out.println("Starting DLS");
globalTime = 0;
solutionFlag = false;
ArrayList<Node> list = graph.list;
Node m = list.get(index);
stack = new Stack<Node>();
if (m.colour == 0)
dls(m, list,graph, 0, depth);
}

private static void dls(Node m, ArrayList<Node> list, Graph


graph, int currentDepth, int depthLimit) {
currentDepth++;
if (m.colour != 0)
return;
if (currentDepth > depthLimit) {
m.colour = 2;
return;
}
stack.add(m);
System.out.println(stack);
m.start_time = ++globalTime;
m.colour = 1;
graph.generateNodes(m);
if(m.data.validate(graph.goalNode)) {
solutionFlag = true;
m.end_time = ++globalTime;
m.colour = 2;
graph.solutionNode = m;
return;
}
for (int i : m.adjNodes) {
Node ind = list.get(i);
if(ind.colour == 0) {
ind.parentNode = m;
dls(ind, list, graph,currentDepth, depthLimit);
}
if(solutionFlag) {
return;
}
}
m.end_time = ++globalTime;
m.colour = 2;
stack.pop();
System.out.println(stack);
}

public static void ids(Graph graph,int index) {


//System.out.println("Starting IDS");
globalTime = 0;
solutionFlag = false;
ArrayList<Node> list = graph.list;
for(int i=1;i<20;i++) {
System.out.println("Iteration "+i);
Search.dls(graph, i, index);
if(graph.solutionNode == null) {
graph.reinitializeGraph();
}
else
return;
}
}

public static void ibs(Graph graph,int index) {


globalTime = 0;
solutionFlag = false;
ArrayList<Node> list = graph.list;
for (int i = 1; i <= 20; i++) {
stack = new Stack<Node>();
System.out.println("Iteration "+i);
Node m = list.get(index);
if (m.colour == 0)
ibs(m, list, graph,i);
if(graph.solutionNode == null) {
graph.reinitializeGraph();
}
else
return;
}
}
private static void ibs(Node m, ArrayList<Node> list, Graph
graph,int breadthLimit) {
//System.out.println(m.index);
if (m.colour != 0)
return;
stack.add(m);
System.out.println(stack);
m.start_time = ++globalTime;
m.colour = 1;
graph.generateNodes(m);
if(m.data.validate(graph.goalNode)) {
solutionFlag = true;
m.end_time = ++globalTime;
m.colour = 2;
graph.solutionNode = m;
return;
}
int c=0;
for (int i : m.adjNodes) {
Node ind = list.get(i);
if(ind.colour == 0)
c++;
//System.out.println(c+" "+breadthLimit);
if(c>breadthLimit)
break;
//System.out.println(c+" "+breadthLimit+" "+ind.index+"
"+ind.colour);
if(ind.colour == 0) {
ind.parentNode = m;
ibs(ind, list, graph,breadthLimit);
}
if(solutionFlag) {
return;
}
}
m.end_time = ++globalTime;
m.colour = 2;
stack.pop();
System.out.println(stack);
}

public static void uniformCostSearch(Graph graph, int index) {


//System.out.println("Starting Uniform Cost Search");
globalTime = 0;
solutionFlag = false;
ArrayList<Node> list = graph.list;
PriorityQueue<Node> queue = new PriorityQueue<Node>(new
CompareNodeByPath());
Node m = list.get(index);
m.path_cost=0;
queue.add(m);
m.colour = 1;
m.start_time = ++globalTime;
if(m.data.validate(graph.goalNode)) {
solutionFlag = true;
m.end_time = ++globalTime;
m.colour = 2;
graph.solutionNode = m;
return;
}
while (!queue.isEmpty()) {
Node i= queue.poll();
System.out.println( queue);
if(i.data.validate(graph.goalNode)) {
solutionFlag = true;
i.end_time = ++globalTime;
i.colour = 2;
graph.solutionNode = i;
return;
}
int count = -1;
for (int p : i.adjNodes) {
count++;
Node node = list.get(p);
int edgeValue = i.edgeValues.get(count);
int pathCost = i.path_cost+ edgeValue;
if( node.colour == 0) {
node.colour = 1;
node.start_time = ++globalTime;
node.parentNode = i;
node.path_cost = pathCost;
node.parentNode = i;
queue.add(node);
System.out.println(queue);

}
if(node.path_cost > pathCost) {
//System.out.println(node +" "+node.path_cost+"
"+pathCost);
node.path_cost = pathCost;
node.parentNode = i;
}

}
i.end_time = ++globalTime;
i.colour = 2;
}
}
public static void aStar(Graph graph,int index) {
globalTime = 0;
solutionFlag = false;
ArrayList<Node> list = graph.list;
PriorityQueue<Node> queue = new PriorityQueue<Node>(new
CompareNodeByTotal());
Node m = list.get(index);
m.path_cost = 0;
m.goal_cost = graph.heuristicFunc(m);
m.total_cost = m.path_cost + m.goal_cost;
queue.add(m);
System.out.println( queue);
m.colour = 1;
m.start_time = ++globalTime;
if(m.data.validate(graph.goalNode)) {
solutionFlag = true;
m.end_time = ++globalTime;
m.colour = 2;
graph.solutionNode = m;
return;
}
while (!queue.isEmpty()) {
Node i= queue.poll();
System.out.println( queue);
graph.generateNodes(i);
if(i.data.validate(graph.goalNode)) {
solutionFlag = true;
i.end_time = ++globalTime;
i.colour = 2;
graph.solutionNode = i;
return;
}
int count = -1;
for (int p : i.adjNodes) {
count++;
Node node = list.get(p);
int edgeValue = i.edgeValues.get(count);
int pathCost = i.path_cost+ edgeValue;
int goalCost = graph.heuristicFunc(node);
int totalCost = pathCost + graph.heuristicFunc(node);
if( node.colour == 0) {
node.colour = 1;
node.start_time = ++globalTime;
node.parentNode = i;
node.path_cost = pathCost;
node.goal_cost = goalCost;
node.total_cost = totalCost;
node.parentNode = i;
queue.add(node);
System.out.println(queue);
}
if(node.total_cost > totalCost) {
//System.out.println(node +" "+node.path_cost+"
"+pathCost);
node.path_cost = pathCost;
node.goal_cost = goalCost;
node.total_cost = totalCost;
node.parentNode = i;
}
}
i.end_time = ++globalTime;
i.colour = 2;
}
}
public static void bfgs(Graph graph,int index) {
globalTime = 0;
solutionFlag = false;
ArrayList<Node> list = graph.list;
PriorityQueue<Node> queue = new PriorityQueue<Node>(new
CompareNodeByHeuristic());
Node m = list.get(index);
m.path_cost=0;
queue.add(m);
m.colour = 1;
m.start_time = ++globalTime;
System.out.println( queue);
if(m.data.validate(graph.goalNode)) {
solutionFlag = true;
m.end_time = ++globalTime;
m.colour = 2;
graph.solutionNode = m;
return;
}
while (!queue.isEmpty()) {
Node i= queue.poll();
System.out.println( queue);
graph.generateNodes(i);
if(i.data.validate(graph.goalNode)) {
solutionFlag = true;
i.end_time = ++globalTime;
i.colour = 2;
graph.solutionNode = i;
return;
}
int count = -1;
for (int p : i.adjNodes) {
count++;
Node node = list.get(p);
int edgeValue = i.edgeValues.get(count);
int pathCost = i.path_cost+ edgeValue;
int goalCost = graph.heuristicFunc(node);
if( node.colour == 0) {
node.colour = 1;
node.start_time = ++globalTime;
node.parentNode = i;
node.path_cost = pathCost;
node.goal_cost = goalCost;
node.total_cost = node.path_cost +
node.goal_cost;
queue.add(node);
System.out.println(queue);

}
i.end_time = ++globalTime;
i.colour = 2;
}
}
}

CompareNodesByPath class
This class is a comparator class whose object is use to compare Nodes by
path cost.This class has been used for the implementation of Uniform cost
search algorithm to sort the nodes based on path cost.
import java.util.Comparator;

public class CompareNodeByPath implements Comparator<Node> {


public int compare(Node o1,Node o2) {
return (o1.path_cost - o2.path_cost);
}
}

CompareNodesByHeuristic class
This class is a comparator class whose object is use to compare Nodes by
heuristic value.This class has been used for the implementation of Best first
greedy search algorithm to sort the nodes based on heuristic value.

import java.util.Comparator;

public class CompareNodeByHeuristic implements Comparator<Node> {


public int compare(Node o1,Node o2) {
return (o1.goal_cost - o2.goal_cost);
}
}
CompareNodesByTotal class
This class is a comparator class whose object is use to compare Nodes by
path cost.This class has been used for the implementation of A star search
algorithm to sort the nodes based on total cost i.e path cost + goal cost.

import java.util.Comparator;

public class CompareNodeByTotal implements Comparator<Node> {


@Override
public int compare(Node o1, Node o2) {
return (o1.total_cost - o2.total_cost);
}
}

WaterJugData class
Describes the data format for water jug problem.Here x denotes the amount
of water in jar 1 and y denotes amount of water in jar 2.
public class WaterJugData extends Data {
int x;
int y;

public WaterJugData(int x, int y) {


super(0);
this.x = x;
this.y = y;
}

public boolean validate(Data obj) {


WaterJugData obj1 = (WaterJugData) obj;
if (obj1.x == x && obj1.y == y) {
return true;
} else
return false;
}
public String toString(){
return "("+this.x+", "+this.y+")";
}
}
QUESTION 1

a)Implementation of i) BFS ii) DFS iii) DLS iii) IDS iv) IBS

Solution: Done in Search class

b) Application of BFS,DFS,DLS,IDS and IBS on Tree

Solution:

TestOnTree class
public class TestOnTree {
public static void main(String args[]) {
Graph graph = new Graph(5, true);
graph.createGoalNode(10);
graph.enterData();
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 3);
graph.addEdge(1, 4);
System.out.println("Starting BFS");
Search.bfs(graph, 0);
graph.displaySolutionPath();
graph.reinitializeGraph();

System.out.println("Starting DFS");
Search.dfs(graph, 0);
graph.displaySolutionPath();
graph.reinitializeGraph();

System.out.println("Starting DLS with depth limit 5");


Search.dls(graph, 5,0);
graph.displaySolutionPath();
graph.reinitializeGraph();

System.out.println("Starting IDS ");


Search.ids(graph, 0);
graph.displaySolutionPath();
graph.reinitializeGraph();

System.out.println("Starting IBS");
Search.ibs(graph, 0);
graph.displaySolutionPath();
}
}

Output:---

Enter the data for node 0


1
Enter the data for node 1
2

Enter the data for node 2


3
Enter the data for node 3
4
Enter the data for node 4
10
Starting BFS
[0]
[]
[1]

[1, 2]
[2]
[2, 3]
[2, 3, 4]
4<----1<----0<----null
Starting DFS
[0]
[0, 1]
[0, 1, 3]

[0, 1]
[0, 1, 4]
4<----1<----0<----null
Starting DLS with depth limit 5
[0]

[0, 1]
[0, 1, 3]
[0, 1]
[0, 1, 4]
4<----1<----0<----null

Starting IDS
Iteration 1
[0]
[]
Iteration 2

[0]
[0, 1]
[0]
[0, 2]
[0]

[]
Iteration 3
[0]
[0, 1]
[0, 1, 3]
[0, 1]
[0, 1, 4]
4<----1<----0<----null
Starting IBS

Iteration 1
[0]
[0, 1]
[0, 1, 3]
[0, 1]

[0]
[]
Iteration 2
[0]
[0, 1]

[0, 1, 3]
[0, 1]
[0, 1, 4]
4<----1<----0<----null

Process finished with exit code 0

c) Application of BFS,DFS,DLS,IDS and IBS on Graph

TestOnGraph Class
public class TestOnGraph {
public static void main(String args[]) {
Graph graph = new Graph(5, true);
graph.createGoalNode(10);
graph.enterData();
graph.addEdge(0, 1);
graph.addEdge(2, 3);
graph.addEdge(1, 3);
graph.addEdge(0, 4);
System.out.println("Starting BFS");
Search.bfs(graph, 0);
graph.displaySolutionPath();
graph.reinitializeGraph();

System.out.println("Starting DFS");
Search.dfs(graph, 0);
graph.displaySolutionPath();
graph.reinitializeGraph();

System.out.println("Starting DLS with depth limit 5");


Search.dls(graph, 5,0);
graph.displaySolutionPath();
graph.reinitializeGraph();

System.out.println("Starting IDS ");


Search.ids(graph, 0);
graph.displaySolutionPath();
graph.reinitializeGraph();

System.out.println("Starting IBS");
Search.ibs(graph, 0);
graph.displaySolutionPath();

}
}

Output
Enter the data for node 0
1
Enter the data for node 1
2
Enter the data for node 2
3
Enter the data for node 3
10
Enter the data for node 4
4
Starting BFS
[0]
[]
[1]
[1, 4]
[4]
[4, 3]
3<----1<----0<----null
Starting DFS
[0]
[0, 1]
[0, 1, 3]
3<----1<----0<----null
Starting DLS with depth limit 5
[0]
[0, 1]
[0, 1, 3]
3<----1<----0<----null
Starting IDS
Iteration 1
[0]
[]
Iteration 2
[0]
[0, 1]
[0]
[0, 4]
[0]
[]
Iteration 3
[0]
[0, 1]
[0, 1, 3]
3<----1<----0<----null
Starting IBS
Iteration 1
[0]
[0, 1]
[0, 1, 3]
3<----1<----0<----null

Process finished with exit code 0

c) Application of BFS,DFS,DLS,IDS and IBS on WaterJug

BFS
WaterJug2 class
import java.util.HashMap;

public class WaterJug2 extends Graph{


static boolean baseFlag = true;
static HashMap<String,Integer> map = new HashMap<String,Integer>();
public WaterJug2(int noNodes, boolean flag) {
super(noNodes, flag);
}
public static void main(String args[]) {
WaterJug2 graph = new WaterJug2(1,false);
graph.list.get(0).data = new WaterJugData(0,0);
map.put("00",0);
graph.goalNode = new WaterJugData(0,4);
Search.bfs(graph,0);
graph.displaySolutionPath();
}

// Fill a jar from tap

// Empty a jar

// Fill a jar from another jar until it gets filled or other one gets
empty

@Override
public void generateNodes(Node node) {
final int JAR1 = 3;
final int JAR2 = 5;
final int MEASURE = 4;
WaterJugData data = (WaterJugData) node.data;
int jar1 = data.x;
int jar2 = data.y;
WaterJugData data1,data2;
if(jar1 == 0 && jar2 == 0) {
if(!baseFlag) {
baseFlag = false;
return;
}
data1 = new WaterJugData(JAR1,0);
addToList(data1,node);
data1 = new WaterJugData(0, JAR2);
addToList(data1,node);
return;
}
if(jar1 == JAR1 && jar2 == JAR2)
return;
int rem_jar1 = JAR1 - jar1;
int rem_jar2 = JAR2 - jar2;
if(rem_jar1 != 0) {
data1 = new WaterJugData(JAR1,jar2);
addToList(data1,node);
int fill = Math.min(jar1+jar2,JAR1);
int updateJar2 = Math.max(jar2-fill+jar1,0);
data1 = new WaterJugData(fill,updateJar2);
addToList(data1,node);
}
if(rem_jar2 != 0){
data1 = new WaterJugData(jar1,JAR2);
addToList(data1,node);
int fill = Math.min(JAR2,jar1+jar2);
int updateJar1 = Math.max(jar2-fill+jar1,0);
data1 = new WaterJugData(updateJar1,fill);
addToList(data1,node);
}
if(jar1 != 0) {
data1 = new WaterJugData(0,jar2);
addToList(data1,node);
}
if(jar2 != 0) {
data1 = new WaterJugData(jar1,0);
addToList(data1,node);
}
}

public void addToList(WaterJugData data,Node node){


if(data.x == 0 && data.y == 0)
return;
//System.out.println(data);
if(!map.containsKey(data.x+""+data.y)) {
//System.out.println(data);
Node node1 = new Node(no_nodes);
no_nodes++;
node1.data = (Data)data;
list.add(node1);
addEdge(node.index,no_nodes-1);
map.put(data.x+""+data.y,no_nodes-1);
}
else
addEdge(node.index,map.get(data.x+""+data.y));

Output:
[(0, 0)]
[]
[(3, 0)]
[(3, 0), (0, 5)]
[(0, 5)]
[(0, 5), (3, 5)]
[(0, 5), (3, 5), (0, 3)]
[(3, 5), (0, 3)]
[(3, 5), (0, 3), (3, 2)]
[(0, 3), (3, 2)]
[(3, 2)]
[(3, 2), (3, 3)]
[(3, 3)]
[(3, 3), (0, 2)]
[(0, 2)]
[(0, 2), (1, 5)]
[(1, 5)]
[(1, 5), (2, 0)]
[(2, 0)]
[(2, 0), (1, 0)]
[(1, 0)]
[(1, 0), (2, 5)]
[(2, 5)]
[(2, 5), (0, 1)]
[(0, 1)]
[(0, 1), (3, 4)]
[(3, 4)]
[(3, 4), (3, 1)]
[(3, 1)]
[(3, 1), (0, 4)]
(0, 4)<----(3, 4)<----(2, 5)<----(2, 0)<----(0, 2)<----(3, 2)<----(0, 5)<----(0, 0)<----null

Process finished with exit code 0

DFS

WaterJug2 class
import java.util.HashMap;

public class WaterJug2 extends Graph{


static boolean baseFlag = true;
static HashMap<String,Integer> map = new HashMap<String,Integer>();
public WaterJug2(int noNodes, boolean flag) {
super(noNodes, flag);
}
public static void main(String args[]) {
WaterJug2 graph = new WaterJug2(1,false);
graph.list.get(0).data = new WaterJugData(0,0);
map.put("00",0);
graph.goalNode = new WaterJugData(0,4);
Search.dfs(graph,0);
graph.displaySolutionPath();
}

// Fill a jar from tap

// Empty a jar

// Fill a jar from another jar until it gets filled or other one gets
empty

@Override
public void generateNodes(Node node) {
final int JAR1 = 3;
final int JAR2 = 5;
final int MEASURE = 4;
WaterJugData data = (WaterJugData) node.data;
int jar1 = data.x;
int jar2 = data.y;
WaterJugData data1,data2;
if(jar1 == 0 && jar2 == 0) {
if(!baseFlag) {
baseFlag = false;
return;
}
data1 = new WaterJugData(JAR1,0);
addToList(data1,node);
data1 = new WaterJugData(0, JAR2);
addToList(data1,node);
return;
}
if(jar1 == JAR1 && jar2 == JAR2)
return;
int rem_jar1 = JAR1 - jar1;
int rem_jar2 = JAR2 - jar2;
if(rem_jar1 != 0) {
data1 = new WaterJugData(JAR1,jar2);
addToList(data1,node);
int fill = Math.min(jar1+jar2,JAR1);
int updateJar2 = Math.max(jar2-fill+jar1,0);
data1 = new WaterJugData(fill,updateJar2);
addToList(data1,node);
}
if(rem_jar2 != 0){
data1 = new WaterJugData(jar1,JAR2);
addToList(data1,node);
int fill = Math.min(JAR2,jar1+jar2);
int updateJar1 = Math.max(jar2-fill+jar1,0);
data1 = new WaterJugData(updateJar1,fill);
addToList(data1,node);
}
if(jar1 != 0) {
data1 = new WaterJugData(0,jar2);
addToList(data1,node);
}
if(jar2 != 0) {
data1 = new WaterJugData(jar1,0);
addToList(data1,node);
}
}

public void addToList(WaterJugData data,Node node){


if(data.x == 0 && data.y == 0)
return;
//System.out.println(data);
if(!map.containsKey(data.x+""+data.y)) {
//System.out.println(data);
Node node1 = new Node(no_nodes);
no_nodes++;
node1.data = (Data)data;
list.add(node1);
addEdge(node.index,no_nodes-1);
map.put(data.x+""+data.y,no_nodes-1);
}
else
addEdge(node.index,map.get(data.x+""+data.y));

Output:

[(0, 0)]
[(0, 0), (3, 0)]
[(0, 0), (3, 0), (3, 5)]
[(0, 0), (3, 0)]

[(0, 0), (3, 0), (0, 3)]


[(0, 0), (3, 0), (0, 3), (3, 3)]
[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5)]
[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5)]
[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2)]
[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2), (0, 2)]
[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2), (0, 2), (2, 0)]
[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2), (0, 2), (2, 0), (2, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2), (0, 2), (2, 0), (2, 5), (3, 4)]
[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2), (0, 2), (2, 0), (2, 5), (3, 4), (0, 4)]

(0, 4)<----(3, 4)<----(2, 5)<----(2, 0)<----(0, 2)<----(3, 2)<----(0, 5)<----(1, 5)<----(3, 3)<----(0, 3)<--
--(3, 0)<----(0, 0)<----null

DLS
WaterJug2 class
import java.util.HashMap;

public class WaterJug2 extends Graph{


static 25oolean baseFlag = true;
static HashMap<String,Integer> map = new HashMap<String,Integer>();
public WaterJug2(int noNodes, 25oolean flag) {
super(noNodes, flag);
}
public static void main(String args[]) {
WaterJug2 graph = new WaterJug2(1,false);
graph.list.get(0).data = new WaterJugData(0,0);
map.put(“00”,0);
graph.goalNode = new WaterJugData(0,4);
Search.dls(graph,20,0);
graph.displaySolutionPath();
}

// Fill a jar from tap

// Empty a jar

// Fill a jar from another jar until it gets filled or other one gets
empty

@Override
public void generateNodes(Node node) {
final int JAR1 = 3;
final int JAR2 = 5;
final int MEASURE = 4;
WaterJugData data = (WaterJugData) node.data;
int jar1 = data.x;
int jar2 = data.y;
WaterJugData data1,data2;
if(jar1 == 0 && jar2 == 0) {
if(!baseFlag) {
baseFlag = false;
return;
}
data1 = new WaterJugData(JAR1,0);
addToList(data1,node);
data1 = new WaterJugData(0, JAR2);
addToList(data1,node);
return;
}
if(jar1 == JAR1 && jar2 == JAR2)
return;
int rem_jar1 = JAR1 – jar1;
int rem_jar2 = JAR2 – jar2;
if(rem_jar1 != 0) {
data1 = new WaterJugData(JAR1,jar2);
addToList(data1,node);
int fill = Math.min(jar1+jar2,JAR1);
int updateJar2 = Math.max(jar2-fill+jar1,0);
data1 = new WaterJugData(fill,updateJar2);
addToList(data1,node);
}
if(rem_jar2 != 0){
data1 = new WaterJugData(jar1,JAR2);
addToList(data1,node);
int fill = Math.min(JAR2,jar1+jar2);
int updateJar1 = Math.max(jar2-fill+jar1,0);
data1 = new WaterJugData(updateJar1,fill);
addToList(data1,node);
}
if(jar1 != 0) {
data1 = new WaterJugData(0,jar2);
addToList(data1,node);
}
if(jar2 != 0) {
data1 = new WaterJugData(jar1,0);
addToList(data1,node);
}
}

public void addToList(WaterJugData data,Node node){


if(data.x == 0 && data.y == 0)
return;
//System.out.println(data);
if(!map.containsKey(data.x+””+data.y)) {
//System.out.println(data);
Node node1 = new Node(no_nodes);
no_nodes++;
node1.data = (Data)data;
list.add(node1);
addEdge(node.index,no_nodes-1);
map.put(data.x+””+data.y,no_nodes-1);
}
else
addEdge(node.index,map.get(data.x+””+data.y));

}
Output:
[(0, 0)]

[(0, 0), (3, 0)]

[(0, 0), (3, 0), (3, 5)]

[(0, 0), (3, 0)]

[(0, 0), (3, 0), (0, 3)]

[(0, 0), (3, 0), (0, 3), (3, 3)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2), (0, 2)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2), (0, 2), (2, 0)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2), (0, 2), (2, 0), (2, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2), (0, 2), (2, 0), (2, 5), (3, 4)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2), (0, 2), (2, 0), (2, 5), (3, 4), (0, 4)]

(0, 4)--(3, 4)--(2, 5)--(2, 0)--(0, 2)--(3, 2)--(0, 5)--(1, 5)--(3, 3)--(0, 3)--(3, 0)--
(0, 0)--null

Process finished with exit code 0

IDS
WaterJug2 class
import java.util.HashMap;

public class WaterJug2 extends Graph{


static boolean baseFlag = true;
static HashMap<String,Integer> map = new HashMap<String,Integer>();
public WaterJug2(int noNodes, boolean flag) {
super(noNodes, flag);
}
public static void main(String args[]) {
WaterJug2 graph = new WaterJug2(1,false);
graph.list.get(0).data = new WaterJugData(0,0);
map.put("00",0);
graph.goalNode = new WaterJugData(0,4);
Search.ids(graph,0);
graph.displaySolutionPath();
}

// Fill a jar from tap

// Empty a jar

// Fill a jar from another jar until it gets filled or other one gets
empty

@Override
public void generateNodes(Node node) {
final int JAR1 = 3;
final int JAR2 = 5;
final int MEASURE = 4;
WaterJugData data = (WaterJugData) node.data;
int jar1 = data.x;
int jar2 = data.y;
WaterJugData data1,data2;
if(jar1 == 0 && jar2 == 0) {
if(!baseFlag) {
baseFlag = false;
return;
}
data1 = new WaterJugData(JAR1,0);
addToList(data1,node);
data1 = new WaterJugData(0, JAR2);
addToList(data1,node);
return;
}
if(jar1 == JAR1 && jar2 == JAR2)
return;
int rem_jar1 = JAR1 - jar1;
int rem_jar2 = JAR2 - jar2;
if(rem_jar1 != 0) {
data1 = new WaterJugData(JAR1,jar2);
addToList(data1,node);
int fill = Math.min(jar1+jar2,JAR1);
int updateJar2 = Math.max(jar2-fill+jar1,0);
data1 = new WaterJugData(fill,updateJar2);
addToList(data1,node);
}
if(rem_jar2 != 0){
data1 = new WaterJugData(jar1,JAR2);
addToList(data1,node);
int fill = Math.min(JAR2,jar1+jar2);
int updateJar1 = Math.max(jar2-fill+jar1,0);
data1 = new WaterJugData(updateJar1,fill);
addToList(data1,node);
}
if(jar1 != 0) {
data1 = new WaterJugData(0,jar2);
addToList(data1,node);
}
if(jar2 != 0) {
data1 = new WaterJugData(jar1,0);
addToList(data1,node);
}
}

public void addToList(WaterJugData data,Node node){


if(data.x == 0 && data.y == 0)
return;
//System.out.println(data);
if(!map.containsKey(data.x+""+data.y)) {
//System.out.println(data);
Node node1 = new Node(no_nodes);
no_nodes++;
node1.data = (Data)data;
list.add(node1);
addEdge(node.index,no_nodes-1);
map.put(data.x+""+data.y,no_nodes-1);
}
else
addEdge(node.index,map.get(data.x+""+data.y));

Output:
Iteration 1

[(0, 0)]

[]

Iteration 2

[(0, 0)]

[(0, 0), (3, 0)]

[(0, 0)]

[(0, 0), (0, 5)]

[(0, 0)]

[]

Iteration 3

[(0, 0)]

[(0, 0), (3, 0)]

[(0, 0), (3, 0), (3, 5)]

[(0, 0), (3, 0)]

[(0, 0), (3, 0), (0, 3)]

[(0, 0), (3, 0)]

[(0, 0)]

[]

Iteration 4
[(0, 0)]

[(0, 0), (3, 0)]

[(0, 0), (3, 0), (3, 5)]

[(0, 0), (3, 0)]

[(0, 0), (3, 0), (0, 3)]

[(0, 0), (3, 0), (0, 3), (3, 3)]

[(0, 0), (3, 0), (0, 3)]

[(0, 0), (3, 0), (0, 3), (0, 5)]

[(0, 0), (3, 0), (0, 3)]

[(0, 0), (3, 0)]

[(0, 0)]

[]

Iteration 5

[(0, 0)]

[(0, 0), (3, 0)]

[(0, 0), (3, 0), (3, 5)]

[(0, 0), (3, 0)]

[(0, 0), (3, 0), (0, 3)]

[(0, 0), (3, 0), (0, 3), (3, 3)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3)]

[(0, 0), (3, 0), (0, 3)]

[(0, 0), (3, 0)]

[(0, 0)]

[]

Iteration 6

[(0, 0)]

[(0, 0), (3, 0)]

[(0, 0), (3, 0), (3, 5)]

[(0, 0), (3, 0)]

[(0, 0), (3, 0), (0, 3)]

[(0, 0), (3, 0), (0, 3), (3, 3)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5)]
[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (1, 0)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3)]

[(0, 0), (3, 0), (0, 3)]

[(0, 0), (3, 0)]

[(0, 0)]

[]

Iteration 7

[(0, 0)]

[(0, 0), (3, 0)]

[(0, 0), (3, 0), (3, 5)]

[(0, 0), (3, 0)]

[(0, 0), (3, 0), (0, 3)]

[(0, 0), (3, 0), (0, 3), (3, 3)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (1, 0)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (1, 0), (0, 1)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (1, 0)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3)]

[(0, 0), (3, 0), (0, 3)]

[(0, 0), (3, 0)]

[(0, 0)]

[]

Iteration 8

[(0, 0)]

[(0, 0), (3, 0)]


[(0, 0), (3, 0), (3, 5)]

[(0, 0), (3, 0)]

[(0, 0), (3, 0), (0, 3)]

[(0, 0), (3, 0), (0, 3), (3, 3)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2), (0, 2)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (1, 0)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (1, 0), (0, 1)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (1, 0), (0, 1), (3, 1)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (1, 0), (0, 1)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (1, 0)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3)]

[(0, 0), (3, 0), (0, 3)]

[(0, 0), (3, 0)]

[(0, 0)]

[]

Iteration 9

[(0, 0)]

[(0, 0), (3, 0)]

[(0, 0), (3, 0), (3, 5)]

[(0, 0), (3, 0)]

[(0, 0), (3, 0), (0, 3)]

[(0, 0), (3, 0), (0, 3), (3, 3)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2), (0, 2)]
[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2), (0, 2), (2, 0)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2), (0, 2)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (1, 0)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (1, 0), (0, 1)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (1, 0), (0, 1), (3, 1)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (1, 0), (0, 1), (3, 1), (0, 4)]

(0, 4)<----(3, 1)<----(0, 1)<----(1, 0)<----(1, 5)<----(3, 3)<----(0, 3)<----(3, 0)<----(0, 0)<----null

Process finished with exit code 0

IBS
import java.util.HashMap;

public class WaterJug2 extends Graph{


static boolean baseFlag = true;
static HashMap<String,Integer> map = new HashMap<String,Integer>();
public WaterJug2(int noNodes, boolean flag) {
super(noNodes, flag);
}
public static void main(String args[]) {
WaterJug2 graph = new WaterJug2(1,false);
graph.list.get(0).data = new WaterJugData(0,0);
map.put("00",0);
graph.goalNode = new WaterJugData(0,4);
Search.ibs(graph,0);
graph.displaySolutionPath();
}

// Fill a jar from tap

// Empty a jar

// Fill a jar from another jar until it gets filled or other one gets
empty

@Override
public void generateNodes(Node node) {
final int JAR1 = 3;
final int JAR2 = 5;
final int MEASURE = 4;
WaterJugData data = (WaterJugData) node.data;
int jar1 = data.x;
int jar2 = data.y;
WaterJugData data1,data2;
if(jar1 == 0 && jar2 == 0) {
if(!baseFlag) {
baseFlag = false;
return;
}
data1 = new WaterJugData(JAR1,0);
addToList(data1,node);
data1 = new WaterJugData(0, JAR2);
addToList(data1,node);
return;
}
if(jar1 == JAR1 && jar2 == JAR2)
return;
int rem_jar1 = JAR1 - jar1;
int rem_jar2 = JAR2 - jar2;
if(rem_jar1 != 0) {
data1 = new WaterJugData(JAR1,jar2);
addToList(data1,node);
int fill = Math.min(jar1+jar2,JAR1);
int updateJar2 = Math.max(jar2-fill+jar1,0);
data1 = new WaterJugData(fill,updateJar2);
addToList(data1,node);
}
if(rem_jar2 != 0){
data1 = new WaterJugData(jar1,JAR2);
addToList(data1,node);
int fill = Math.min(JAR2,jar1+jar2);
int updateJar1 = Math.max(jar2-fill+jar1,0);
data1 = new WaterJugData(updateJar1,fill);
addToList(data1,node);
}
if(jar1 != 0) {
data1 = new WaterJugData(0,jar2);
addToList(data1,node);
}
if(jar2 != 0) {
data1 = new WaterJugData(jar1,0);
addToList(data1,node);
}
}

public void addToList(WaterJugData data,Node node){


if(data.x == 0 && data.y == 0)
return;
//System.out.println(data);
if(!map.containsKey(data.x+""+data.y)) {
//System.out.println(data);
Node node1 = new Node(no_nodes);
no_nodes++;
node1.data = (Data)data;
list.add(node1);
addEdge(node.index,no_nodes-1);
map.put(data.x+""+data.y,no_nodes-1);
}
else
addEdge(node.index,map.get(data.x+""+data.y));

}
Output
Iteration 1

[(0, 0)]

[(0, 0), (3, 0)]

[(0, 0), (3, 0), (3, 5)]

[(0, 0), (3, 0)]

[(0, 0)]

[]

Iteration 2

[(0, 0)]

[(0, 0), (3, 0)]

[(0, 0), (3, 0), (3, 5)]

[(0, 0), (3, 0)]

[(0, 0), (3, 0), (0, 3)]

[(0, 0), (3, 0), (0, 3), (3, 3)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2), (0, 2)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2), (0, 2), (2, 0)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2), (0, 2), (2, 0), (2, 5)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2), (0, 2), (2, 0), (2, 5), (3, 4)]

[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (0, 5), (3, 2), (0, 2), (2, 0), (2, 5), (3, 4), (0, 4)]

(0, 4)<----(3, 4)<----(2, 5)<----(2, 0)<----(0, 2)<----(3, 2)<----(0, 5)<----(1, 5)<----(3, 3)<----(0, 3)<----(3, 0)<----(0, 0)<-
---null

Process finished with exit code 0


QUESTION 2

2. Implement Uniform Cost Search method using a Graph. Report Order


of nodes visited, Solution Path, and Solution Cost.

Uniform cost search implementation done in Search class

Test2 class here is used to show the uniform cost search on a graph
public class Test2 {
public static void main(String args[]) {
Graph graph = new Graph(4, true);
graph.createGoalNode(10);
graph.enterData();
graph.addEdge(0, 1,4);
graph.addEdge(0, 3,1);
graph.addEdge(3, 2,1);
graph.addEdge(2, 1, 1);
Search.uniformCostSearch(graph, 0);
graph.displaySolutionPath();
System.out.println("Solution cost---
>>"+graph.solutionNode.path_cost);
}
}

Output:
Enter the data for node 0

Enter the data for node 1

10

Enter the data for node 2

Enter the data for node 3

[]

[1]
[3, 1]

[1]

[2, 1]

[1]

[]

1<----2<----3<----0<----null

Solution cost--->>3

Process finished with exit code 0

QUESTION 3

3. Implement following Uninformed Search Processes:


a) Best First Greedy Search and b) A* Search
and for each of the search techniques mentioned above, consider the
following scenario/ problem
i) Tree, ii) Graph, iii) 8-Puzzle Problem, iv) Maze Problem
As an output, in each case, report Order of nodes visited and Solution Path

Solution:

BFGS and A* Search algorithm implementation done in Search class


above.

Test of BFGS and A* on Tree


TestOnTree2 class

public class TestOnTree2 extends Graph {


public TestOnTree2(int noNodes, boolean flag) {
super(noNodes, flag);
}
public static void main(String args[]) {
TestOnTree2 graph = new TestOnTree2(10, true);
graph.createGoalNode(10);
graph.enterData();
graph.addEdge(0, 1);
graph.addEdge(0, 7);
graph.addEdge(1, 2);
graph.addEdge(2, 3);
graph.addEdge(3, 4);
graph.addEdge(4, 5);
graph.addEdge(5, 6);
graph.addEdge(7, 8);
graph.addEdge(8, 9);
System.out.println("Starting Best First Greedy Search");
Search.bfgs(graph,0);
graph.displaySolutionPath();
graph.reinitializeGraph();
System.out.println("Starting A* Search");
Search.aStar(graph,0);
graph.displaySolutionPath();
}
@Override
public int heuristicFunc(Node node) {
int index = node.index;
if(index == 0)
return 3;
else if(index == 1)
return 2;
else if(index == 2 || index == 3 || index == 4 || index == 5 ||
index == 8)
return 1;
else if(index == 7)
return 4;
else if(index == 6 || index == 9)
return 0;
else
return 1000;
}
}

OUTPUT:
Enter the data for node 0

Enter the data for node 1

Enter the data for node 2

Enter the data for node 3

Enter the data for node 4

Enter the data for node 5

Enter the data for node 6

10

Enter the data for node 7

Enter the data for node 8

Enter the data for node 9

10

Starting Best First Greedy Search

[0]

[]

[1]

[1, 7]
[7]

[2, 7]

[7]

[3, 7]

[7]

[4, 7]

[7]

[5, 7]

[7]

[6, 7]

[7]

6<----5<----4<----3<----2<----1<----0<----null

Starting A* Search

[0]

[]

[1]

[1, 7]

[7]

[2, 7]

[7]

[3, 7]

[7]

[7, 4]

[4]

[8, 4]

[4]

[9, 4]

[4]

9<----8<----7<----0<----null
Test of BFGS and A* on Graph

public class TestOnGraph2 extends Graph {


public TestOnGraph2(int noNodes, boolean flag) {
super(noNodes, flag);
}
public static void main(String args[]) {
TestOnGraph2 graph = new TestOnGraph2(9, true);
graph.createGoalNode(10);
graph.enterData();
graph.addEdge(0, 1);
graph.addEdge(0, 7);
graph.addEdge(1, 2);
graph.addEdge(2, 3);
graph.addEdge(3, 4);
graph.addEdge(4, 5);
graph.addEdge(5, 6);
graph.addEdge(7, 8);
graph.addEdge(8, 6);
System.out.println("Starting Best First Greedy Search");
Search.bfgs(graph,0);
graph.displaySolutionPath();
graph.reinitializeGraph();
System.out.println("Starting A* Search");
Search.aStar(graph,0);
graph.displaySolutionPath();
}
@Override
public int heuristicFunc(Node node) {
int index = node.index;
if(index == 0)
return 3;
else if(index == 1)
return 2;
else if(index == 2 || index == 3 || index == 4 || index == 5 ||
index == 8)
return 1;
else if(index == 7)
return 4;
else if(index == 6 || index == 9)
return 0;
else
return 1000;
}
}

OUTPUT
Enter the data for node 0

Enter the data for node 1

Enter the data for node 2

Enter the data for node 3

Enter the data for node 4

Enter the data for node 5

Enter the data for node 6

10

Enter the data for node 7

Enter the data for node 8

Starting Best First Greedy Search

[0]

[]

[1]

[1, 7]

[7]

[2, 7]

[7]

[3, 7]

[7]
[4, 7]

[7]

[5, 7]

[7]

[6, 7]

[7]

6<----5<----4<----3<----2<----1<----0<----null

Starting A* Search

[0]

[]

[1]

[1, 7]

[7]

[2, 7]

[7]

[3, 7]

[7]

[7, 4]

[4]

[8, 4]

[4]

[6, 4]

[4]

6<----8<----7<----0<----null

Process finished with exit code 0

Test of BFGS and A* on Maze Problem

BEST FIRST GREEDY SEARCH

MazeProblemData class
This class contains the data format for Maze problem. x and y denotes the
coordinates of a cell.
public class MazeProblemData extends Data {
int x,y;
public MazeProblemData(int x, int y) {
super(0);
this.x = x;
this.y = y;
}
public String toString() {
return "("+x+","+y+")";
}
public boolean validate(Data obj) {
MazeProblemData obj1 = (MazeProblemData) obj;
if (obj1.x == x && obj1.y == y) {
return true;
} else
return false;
}

MazeProblem class

import java.util.HashMap;

public class MazeProblem extends Graph {


static HashMap<String,Integer> map = new HashMap<String,Integer>();
static MazeProblemData goalData;
static int matrix[][] ={{1, 0, 0, 0},
{1, 1, 0, 1},
{0, 1, 0, 0},
{1, 1, 1, 1}};

public MazeProblem(int noNodes, boolean flag) {


super(noNodes, flag);
}
public static void main(String args[]) {
MazeProblem graph = new MazeProblem(1,false);
graph.list.get(0).data = new MazeProblemData(0,0);
map.put("0 0",1);
goalData = new MazeProblemData(matrix.length-1, matrix.length-1);
// System.out.println(goalData);
graph.goalNode = goalData;
Search.bfgs(graph,0);
graph.displaySolutionPath();
}

@Override
public int heuristicFunc(Node node) {
MazeProblemData data = (MazeProblemData) node.data;
return (Math.abs(goalData.x-data.x)+Math.abs(goalData.y-data.y));
}

@Override
public void generateNodes(Node node) {
MazeProblemData data = (MazeProblemData) node.data;
int x = data.x;
int y = data.y;
if(validateCoordinate(x-1,y)) {
addToList(new MazeProblemData(x-1,y),node);
}
if(validateCoordinate(x+1,y)) {
addToList(new MazeProblemData(x+1,y),node);
}
if(validateCoordinate(x,y+1)) {
addToList(new MazeProblemData(x,y+1),node);
}
if(validateCoordinate(x,y-1)) {
addToList(new MazeProblemData(x,y-1),node);
}
}

public boolean validateCoordinate(int x, int y) {


if((x>=0 && x<matrix.length) && (y>=0 && y<matrix.length) &&
matrix[x][y] == 1)
return true;
else
return false;
}
public void addToList(MazeProblemData data,Node node){
if(data.x == 0 && data.y == 0)
return;
if(!map.containsKey(data.x+" "+data.y)) {
//System.out.println(data);
Node node1 = new Node(no_nodes);
no_nodes++;
node1.data = (Data)data;
list.add(node1);
addEdge(node.index,no_nodes-1);
map.put(data.x+" "+data.y,no_nodes-1);
}
else
addEdge(node.index,map.get(data.x+" "+data.y));

}
}

Output:
[(0,0)]
[]
[(1,0)]
[]
[(1,1)]
[]
[(2,1)]
[]
[(3,1)]
[]
[(3,2)]
[(3,2), (3,0)]
[(3,0)]
[(3,3), (3,0)]
[(3,0)]
(3,3)<----(3,2)<----(3,1)<----(2,1)<----(1,1)<----(1,0)<----(0,0)<----null

Process finished with exit code 0

ASTAR SEARCH

MazeProblemData class

public class MazeProblemData extends Data {


int x,y;
public MazeProblemData(int x, int y) {
super(0);
this.x = x;
this.y = y;
}
public String toString() {
return "("+x+","+y+")";
}
public boolean validate(Data obj) {
MazeProblemData obj1 = (MazeProblemData) obj;
if (obj1.x == x && obj1.y == y) {
return true;
} else
return false;
}

MazeProblem class
import java.util.HashMap;

public class MazeProblem extends Graph {


static HashMap<String,Integer> map = new HashMap<String,Integer>();
static MazeProblemData goalData;
static int matrix[][] ={{1, 0, 0, 0},
{1, 1, 0, 1},
{0, 1, 0, 0},
{1, 1, 1, 1}};

public MazeProblem(int noNodes, boolean flag) {


super(noNodes, flag);
}
public static void main(String args[]) {
MazeProblem graph = new MazeProblem(1,false);
graph.list.get(0).data = new MazeProblemData(0,0);
map.put("0 0",1);
goalData = new MazeProblemData(matrix.length-1, matrix.length-1);
graph.goalNode = goalData;
Search.aStar(graph,0);
graph.displaySolutionPath();
}

@Override
public int heuristicFunc(Node node) {
MazeProblemData data = (MazeProblemData) node.data;
return (Math.abs(goalData.x-data.x)+Math.abs(goalData.y-data.y));
}

@Override
public void generateNodes(Node node) {
MazeProblemData data = (MazeProblemData) node.data;
int x = data.x;
int y = data.y;
if(validateCoordinate(x-1,y)) {
addToList(new MazeProblemData(x-1,y),node);
}
if(validateCoordinate(x+1,y)) {
addToList(new MazeProblemData(x+1,y),node);
}
if(validateCoordinate(x,y+1)) {
addToList(new MazeProblemData(x,y+1),node);
}
if(validateCoordinate(x,y-1)) {
addToList(new MazeProblemData(x,y-1),node);
}
}

public boolean validateCoordinate(int x, int y) {


if((x>=0 && x<matrix.length) && (y>=0 && y<matrix.length) &&
matrix[x][y] == 1)
return true;
else
return false;
}
public void addToList(MazeProblemData data,Node node){
if(data.x == 0 && data.y == 0)
return;
if(!map.containsKey(data.x+" "+data.y)) {
//System.out.println(data);
Node node1 = new Node(no_nodes);
no_nodes++;
node1.data = (Data)data;
list.add(node1);
addEdge(node.index,no_nodes-1);
map.put(data.x+" "+data.y,no_nodes-1);
}
else
addEdge(node.index,map.get(data.x+" "+data.y));

}
}
Output:
[(0,0)]

[]

[(1,0)]

[]

[(1,1)]

[]

[(2,1)]

[]

[(3,1)]

[]

[(3,2)]

[(3,2), (3,0)]

[(3,0)]

[(3,3), (3,0)]

[(3,0)]

(3,3)<----(3,2)<----(3,1)<----(2,1)<----(1,1)<----(1,0)<----(0,0)<----null

Process finished with exit code 0

4. Implement single solution based search algorithm (e.g., Hill Climbing


Algorithm) for Function Optimization.

Solution:

StateNode class

public class StateNode {


double x,y;
public StateNode(double x) {
this.x = x;
this.y = func(x);
}
public double func(double x) {
y = Math.pow(x,2);
return y;
}

@Override
public String toString() {
return ""+x;
}
}

HillClimbing class
import java.util.ArrayList;

import java.util.Random;
public class HillClimbing {

static ArrayList<StateNode> list = new ArrayList<StateNode>();


static CompareStateNode compare = new CompareStateNode();
static StateNode prevNode = null;

public static void generateStates(StateNode obj) {


Random rand = new Random();
int d = rand.nextInt(1,1000);
double delta = d/1000.0;
System.out.println("Delta--->"+delta);
double x1 = obj.x + delta;
double x2 = obj.x - delta;
StateNode node1 = new StateNode(x1);
StateNode node2 = new StateNode(x2);
list.add(node1);
list.add(node2);
}
public static void hillclimbing(StateNode node) {
while(true) {
list.add(node);
generateStates(node);
list.sort(compare);
StateNode newNode = list.get(0);
System.out.println(list);
list = new ArrayList<StateNode>();
if (prevNode.y == newNode.y)
return;
node = newNode;
prevNode = newNode;
}
}
public static void main(String args[]) {
StateNode node = new StateNode(21);
prevNode = node;
hillclimbing(node);
System.out.println("Minimum value of the function x2---
>"+prevNode.y);
}
}

CompareStateNode class
import java.util.Comparator;

public class CompareStateNode implements Comparator<StateNode> {


@Override
public int compare(StateNode o1, StateNode o2) {
if(o1.y > o2.y)
return 1;
else if(o1.y == o2.y)
return 0;
else
return -1;
}
}

Output:
Delta--->0.442
[20.558, 21.0, 21.442]
Delta--->0.638
[19.919999999999998, 20.558, 21.196]
Delta--->0.722
[19.197999999999997, 19.919999999999998, 20.642]
Delta--->0.128
[19.069999999999997, 19.197999999999997, 19.325999999999997]
Delta--->0.579
[18.490999999999996, 19.069999999999997, 19.648999999999997]
Delta--->0.644
[17.846999999999998, 18.490999999999996, 19.134999999999994]
Delta--->0.302
[17.544999999999998, 17.846999999999998, 18.148999999999997]
Delta--->0.135
[17.409999999999997, 17.544999999999998, 17.68]
Delta--->0.232
[17.177999999999997, 17.409999999999997, 17.641999999999996]
Delta--->0.946
[16.231999999999996, 17.177999999999997, 18.124]
Delta--->0.815
[15.416999999999996, 16.231999999999996, 17.046999999999997]
Delta--->0.236
[15.180999999999996, 15.416999999999996, 15.652999999999997]
Delta--->0.238
[14.942999999999996, 15.180999999999996, 15.418999999999995]
Delta--->0.853
[14.089999999999996, 14.942999999999996, 15.795999999999996]
Delta--->0.092
[13.997999999999996, 14.089999999999996, 14.181999999999997]
Delta--->0.19
[13.807999999999996, 13.997999999999996, 14.187999999999995]
Delta--->0.881
[12.926999999999996, 13.807999999999996, 14.688999999999997]
Delta--->0.828
[12.098999999999997, 12.926999999999996, 13.754999999999995]
Delta--->0.892
[11.206999999999997, 12.098999999999997, 12.990999999999996]
Delta--->0.248
[10.958999999999998, 11.206999999999997, 11.454999999999997]
Delta--->0.405
[10.553999999999998, 10.958999999999998, 11.363999999999997]
Delta--->0.609
[9.944999999999999, 10.553999999999998, 11.162999999999998]
Delta--->0.804
[9.140999999999998, 9.944999999999999, 10.748999999999999]
Delta--->0.876
[8.264999999999999, 9.140999999999998, 10.016999999999998]
Delta--->0.866
[7.398999999999999, 8.264999999999999, 9.130999999999998]
Delta--->0.57
[6.828999999999999, 7.398999999999999, 7.968999999999999]
Delta--->0.36
[6.4689999999999985, 6.828999999999999, 7.188999999999999]
Delta--->0.694
[5.774999999999999, 6.4689999999999985, 7.1629999999999985]
Delta--->0.004
[5.770999999999999, 5.774999999999999, 5.778999999999998]
Delta--->0.672
[5.098999999999999, 5.770999999999999, 6.442999999999999]
Delta--->0.901
[4.1979999999999995, 5.098999999999999, 5.999999999999999]
Delta--->0.572
[3.6259999999999994, 4.1979999999999995, 4.77]
Delta--->0.185
[3.4409999999999994, 3.6259999999999994, 3.8109999999999995]
Delta--->0.343
[3.0979999999999994, 3.4409999999999994, 3.7839999999999994]
Delta--->0.567
[2.5309999999999997, 3.0979999999999994, 3.664999999999999]
Delta--->0.032
[2.4989999999999997, 2.5309999999999997, 2.5629999999999997]
Delta--->0.431
[2.0679999999999996, 2.4989999999999997, 2.9299999999999997]
Delta--->0.278
[1.7899999999999996, 2.0679999999999996, 2.3459999999999996]
Delta--->0.727
[1.0629999999999997, 1.7899999999999996, 2.5169999999999995]
Delta--->0.144
[0.9189999999999997, 1.0629999999999997, 1.2069999999999996]
Delta--->0.532
[0.3869999999999997, 0.9189999999999997, 1.4509999999999996]
Delta--->0.253
[0.13399999999999967, 0.3869999999999997, 0.6399999999999997]
Delta--->0.341
[0.13399999999999967, -0.20700000000000035, 0.4749999999999997]
Minimum value of the function x2--->0.017955999999999913

Process finished with exit code 0

You might also like