Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajmal1990 committed Aug 30, 2014
1 parent 0b59b79 commit f799141
Show file tree
Hide file tree
Showing 17 changed files with 560 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Input File/nodeconfig.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<Nodes>
<Size> 9 </Size>
<Labels> A,B,C,D,E,F,G,H,I </Labels>
<Values> 5,3,4,4,1,3,2,4,5 </Values>
<Edges>
<Edge> A,B </Edge>
<Edge> A,C </Edge>
<Edge> B,E </Edge>
<Edge> B,F </Edge>
<Edge> C,G </Edge>
<Edge> C,H </Edge>
<Edge> G,I </Edge>
</Edges>
<StartNode> A </StartNode>
<EndNode> I </EndNode>
<BeamSize> 2 </BeamSize>
</Nodes>


6 changes: 6 additions & 0 deletions SearchAlogorithms/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions SearchAlogorithms/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>SearchAlogorithms</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
12 changes: 12 additions & 0 deletions SearchAlogorithms/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#Sat Aug 02 18:00:04 IST 2014
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
55 changes: 55 additions & 0 deletions SearchAlogorithms/src/com/cusat/abc/search/BFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.cusat.abc.search;

import java.util.Stack;

/**
* BFS : Best First search implementation
* @author Ajmal
*
*/
public class BFS {
/**
*
* @param graph
* @param start
* @param end
*/
public void bfs(Graph graph, Node start, Node end){
graph.setCurrentNode(start);
graph.addNodeToCurrentPath(start);
System.out.println("----------------- Started BFS ----------------- ");
//System.out.println(start.getLabel());
System.out.println("Push " + start.getLabel() +" into stack");

int currentIndex = graph.getNodes().indexOf(start);
graph.getNodes().get(currentIndex).setVisited(true);

Stack stack=new Stack();
stack.push(start);
while(!stack.isEmpty()) {
Node n=(Node)stack.peek();
Node currentNode=graph.getMinUnvisitedNeighbour(n);

if (end.equals(currentNode)) {
//System.out.print("--->" + currentNode.getLabel());
System.out
.println("Reached the destination.." + currentNode.getLabel());
stack.clear();
break;
} else if(currentNode!=null) {
System.out.println("Push " + currentNode.getLabel()+" into stack");
stack.push(currentNode);
} else {
System.out.println("Pop " + n.getLabel() +" into stack");
graph.getCurrentPath().remove(graph.getCurrentPath().indexOf(n));
stack.pop();
}
}
System.out.print("The BFS PATH " );
for(Node n: graph.getCurrentPath()){
System.out.print("---> "+n.getLabel());
}
}
}


96 changes: 96 additions & 0 deletions SearchAlogorithms/src/com/cusat/abc/search/BeamSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.cusat.abc.search;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
* BeamSearch base class
*
* @author Ajmal
*
*/
public class BeamSearch {
/**
*
* @param garph
* @param start
* @param end
*/
public void beamserach(Graph graph, Node start, Node end, int beamSize) {

System.out
.println("----------------- Started Beam Search ----------------");
start.setPath(start.getLabel());
Queue openNodeQueue = new LinkedList();
openNodeQueue.add(start);
int currentIndex = graph.getNodes().indexOf(start);
graph.getNodes().get(currentIndex).setVisited(true);


while (true) {
List<Node> expandedNodes = new ArrayList<Node>();

while (!openNodeQueue.isEmpty()) {
Node currentExapndedNode = graph
.getMinUnvisitedNeighbour((Node) openNodeQueue.peek());
if (end.equals(currentExapndedNode)) {
System.out.println("Expanding... "
+ ((Node) openNodeQueue.peek()).getLabel() + "===>"
+ currentExapndedNode.getLabel());
currentExapndedNode.setPath(((Node) openNodeQueue.peek()).getPath()+" ==> "+currentExapndedNode.getLabel());
System.out.println("Reached destination......... :) ");
System.out.println("Path === " + currentExapndedNode.getPath());

return;
} else if (currentExapndedNode != null) {
System.out.println("Expanding... "
+ ((Node) openNodeQueue.peek()).getLabel() + "===>"
+ currentExapndedNode.getLabel());
currentExapndedNode.setPath(((Node) openNodeQueue.peek()).getPath()+" ==> "+currentExapndedNode.getLabel());
System.out.println("Path === " + currentExapndedNode.getPath());
expandedNodes.add(currentExapndedNode);
} else {
openNodeQueue.remove();
}
}
//Sorting the expanded nodes based on the heuristic value
if (expandedNodes.size() > 1) {
Collections.sort(expandedNodes, new Comparator<Node>() {
@Override
public int compare(Node node1, Node node2) {
return Double.compare(node1.getHeuristic(),
node2.getHeuristic());
}
});
}
//Reduce the expanded node list to beam size
if (expandedNodes.size() > beamSize) {
System.out.println("The expanded nodes size "
+ expandedNodes.size() + " Grater than beam size "
+ beamSize);
for (int i = 0; i < beamSize; i++) {
System.out.println("Selecting "
+ expandedNodes.get(i).getLabel() + " To exapnd");
openNodeQueue.add(expandedNodes.get(i));
}
} else if (expandedNodes.size() == 0) {
System.out.println("reached dead end ... ");
break;
} else {
System.out.println("The expanded nodes size "
+ expandedNodes.size() + " Less than beam size "
+ beamSize);
for (int i = 0; i < expandedNodes.size(); i++) {
System.out.println("Selecting "
+ expandedNodes.get(i).getLabel() + " To exapnd");
openNodeQueue.add(expandedNodes.get(i));
}
}

}
}
}
136 changes: 136 additions & 0 deletions SearchAlogorithms/src/com/cusat/abc/search/Graph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.cusat.abc.search;

import java.util.ArrayList;
import java.util.List;
/**
* Base Graph class
* @author Ajmal
*
*/
public class Graph {

private List<Node> nodes = new ArrayList<Node>();
private int[][] adjesancyMatrix;
private int size;
private Node currentNode;
private List<Node> currentPath = new ArrayList<Node>();

public Graph() {

}
/**
*
* @param nodes : list of nodes in graph
*
*/
public Graph(List<Node> nodes) {
this.nodes = nodes;
}

public List<Node> getNodes() {
return nodes;
}

public void setNodes(List<Node> nodes) {
this.nodes = nodes;
}

public int[][] getAdjesancyMatrix() {
return adjesancyMatrix;
}

public void setAdjesancyMatrix(int[][] adjesancyMatrix) {
this.adjesancyMatrix = adjesancyMatrix;
}

public int getSize() {
return size;
}

public void setSize(int size) {
this.size = size;
}

public Node getCurrentNode() {
return currentNode;
}

public void setCurrentNode(Node currentNode) {
this.currentNode = currentNode;
}

public List<Node> getCurrentPath() {
return currentPath;
}

public void setCurrentPath(List<Node> currentPath) {
this.currentPath = currentPath;
}

public void addNodeToCurrentPath(Node node){
currentPath.add(node);
}

/**
* Create a path between 2 nodes
* @param start
* @param end
*/
public void connectNode(Node start,Node end) {
if(adjesancyMatrix==null) {
size=nodes.size();
adjesancyMatrix=new int[size][size];
}

int startIndex=nodes.indexOf(start);
int endIndex=nodes.indexOf(end);
adjesancyMatrix[startIndex][endIndex]=1;
adjesancyMatrix[endIndex][startIndex]=1;
}

public void clearVisitedNodes(){
int i=0;
while(i<size){
nodes.get(i).setVisited(false);
i++;
}
}

/**
*
* @param currentNode
* @return Unvisited neighbour node with minimum heuristic value
*
*/
public Node getMinUnvisitedNeighbour(Node currentNode){
Node minNode = null;
double minHuristcValue = Double.MAX_VALUE;
int currentNodeIndex = nodes.indexOf(currentNode);

for (int j = 0; j < nodes.size() ; j ++ ) {
if( adjesancyMatrix[currentNodeIndex][j] == 1 && !nodes.get(j).isVisited() && nodes.get(j).getHeuristic() <= minHuristcValue ){
minNode = nodes.get(j);
minHuristcValue = nodes.get(j).getHeuristic();
}
}
if(minNode != null ) {
nodes.get(nodes.indexOf(minNode)).setVisited(true);
setCurrentNode(minNode);
addNodeToCurrentPath(minNode);
}

return minNode;
}

public Node getNodeByLabelName(String label){
Node node = null;
for(int i =0; i <size; i++){
if(nodes.get(i).getLabel().equals(label)){
node = nodes.get(i);
break;
}
}
return node;
}

}
45 changes: 45 additions & 0 deletions SearchAlogorithms/src/com/cusat/abc/search/HillClimb.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.cusat.abc.search;

/**
* Hill climb search implementation
*
* @author Ajmal
*
*/
public class HillClimb {
/**
*
* @param graph
* @param start
* @param end
*/
public void hillClimb(Graph graph, Node start, Node end) {
graph.setCurrentNode(start);
graph.addNodeToCurrentPath(start);
int currentIndex = graph.getNodes().indexOf(start);
graph.getNodes().get(currentIndex).setVisited(true);
if (start.equals(end)) {
System.out.println("Start and end nodes are equal");
} else {
System.out.println("------------------- Started Hill climb ----------------- ");
System.out.print(start.getLabel());
Node currentNode;
do {
currentNode = graph.getMinUnvisitedNeighbour(graph
.getCurrentNode());
if (currentNode == null) {
System.out
.println("\nReached dead end... Not able to continuee.. ");
} else if (currentNode.equals(end)) {
System.out.print("--->" + currentNode.getLabel());
System.out
.println("\nReached the destination.. Thank you..");
} else {
System.out.print("--->" + currentNode.getLabel());
}
} while (currentNode != null && !currentNode.equals(end));

}

}
}
Loading

0 comments on commit f799141

Please sign in to comment.