Commit af430148 authored by wansari's avatar wansari

W16KKAT001 added for Dijkstra Algorithm Active Picking location wise map


git-svn-id: http://15.206.35.175/svn/proteus/business-java/trunk@104839 ce508802-f39f-4f6c-b175-0d175dae99d5
parent 9598a139
package ibase.webitm.bean.wms;
public class Edge {
private final String id;
private final Vertex source;
private final Vertex destination;
private final int weight;
public Edge(String id, Vertex source, Vertex destination, int weight) {
this.id = id;
this.source = source;
this.destination = destination;
this.weight = weight;
}
public String getId() {
return id;
}
public Vertex getDestination() {
return destination;
}
public Vertex getSource() {
return source;
}
public int getWeight() {
return weight;
}
@Override
public String toString() {
return source + " " + destination;
}
}
\ No newline at end of file
package ibase.webitm.bean.wms;
import java.util.HashMap;
import java.util.List;
public class Graph {
private final HashMap<String,Vertex> vertexes;
private final List<Edge> edges;
public Graph(HashMap<String,Vertex> vertexes, List<Edge> edges) {
this.vertexes = vertexes;
this.edges = edges;
}
public HashMap<String,Vertex> getVertexes() {
return vertexes;
}
public List<Edge> getEdges() {
return edges;
}
}
package ibase.webitm.bean.wms;
public class Vertex {
final private String id;
final private String name;
public Vertex(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Vertex other = (Vertex) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
@Override
public String toString() {
return name;
}
}
\ No newline at end of file
package ibase.webitm.utility.wms;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/*import de.vogella.algorithms.dijkstra.model.Edge;
import de.vogella.algorithms.dijkstra.model.Graph;
import de.vogella.algorithms.dijkstra.model.Vertex;*/
import ibase.webitm.bean.wms.Edge;
import ibase.webitm.bean.wms.Graph;
import ibase.webitm.bean.wms.Vertex;
public class DijkstraAlgorithm {
// private final List<Vertex> nodes;
private final HashMap<String,Vertex> nodes;
private final List<Edge> edges;
private Set<Vertex> settledNodes;
private Set<Vertex> unSettledNodes;
private Map<Vertex, Vertex> predecessors;
private Map<Vertex, Integer> distance;
public DijkstraAlgorithm(Graph graph) {
// create a copy of the array so that we can operate on this array
this.nodes = new HashMap<String,Vertex>(graph.getVertexes());
this.edges = new ArrayList<Edge>(graph.getEdges());
}
public void execute(Vertex source) {
settledNodes = new HashSet<Vertex>();
unSettledNodes = new HashSet<Vertex>();
distance = new HashMap<Vertex, Integer>();
predecessors = new HashMap<Vertex, Vertex>();
distance.put(source, 0);
unSettledNodes.add(source);
while (unSettledNodes.size() > 0) {
Vertex node = getMinimum(unSettledNodes);
settledNodes.add(node);
unSettledNodes.remove(node);
findMinimalDistances(node);
}
}
private void findMinimalDistances(Vertex node) {
List<Vertex> adjacentNodes = getNeighbors(node);
for (Vertex target : adjacentNodes) {
if (getShortestDistance(target) > getShortestDistance(node)
+ getDistance(node, target)) {
distance.put(target, getShortestDistance(node)
+ getDistance(node, target));
predecessors.put(target, node);
unSettledNodes.add(target);
}
}
}
private int getDistance(Vertex node, Vertex target) {
for (Edge edge : edges) {
if (edge.getSource().equals(node)
&& edge.getDestination().equals(target)) {
return edge.getWeight();
}
}
throw new RuntimeException("Should not happen");
}
private List<Vertex> getNeighbors(Vertex node) {
List<Vertex> neighbors = new ArrayList<Vertex>();
for (Edge edge : edges) {
if (edge.getSource().equals(node)
&& !isSettled(edge.getDestination())) {
neighbors.add(edge.getDestination());
}
}
return neighbors;
}
private Vertex getMinimum(Set<Vertex> vertexes) {
Vertex minimum = null;
for (Vertex vertex : vertexes) {
if (minimum == null) {
minimum = vertex;
} else {
if (getShortestDistance(vertex) < getShortestDistance(minimum)) {
minimum = vertex;
}
}
}
return minimum;
}
private boolean isSettled(Vertex vertex) {
return settledNodes.contains(vertex);
}
private int getShortestDistance(Vertex destination) {
Integer d = distance.get(destination);
if (d == null) {
return Integer.MAX_VALUE;
} else {
return d;
}
}
/*
* This method returns the path from the source to the selected target and
* NULL if no path exists
*/
public LinkedList<Vertex> getPath(Vertex target) {
LinkedList<Vertex> path = new LinkedList<Vertex>();
Vertex step = target;
// check if a path exists
if (predecessors.get(step) == null) {
return null;
}
path.add(step);
while (predecessors.get(step) != null) {
step = predecessors.get(step);
path.add(step);
}
// Put it into the correct order
Collections.reverse(path);
return path;
}
}
package ibase.webitm.utility.wms;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import org.junit.Test;
//import de.vogella.algorithms.dijkstra.engine.DijkstraAlgorithm;
/*import de.vogella.algorithms.dijkstra.model.Edge;
import de.vogella.algorithms.dijkstra.model.Graph;
import de.vogella.algorithms.dijkstra.model.Vertex;*/
import ibase.webitm.utility.wms.DijkstraAlgorithm;
import ibase.webitm.bean.wms.Edge;
import ibase.webitm.bean.wms.Graph;
import ibase.webitm.bean.wms.Vertex;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
//public class TestDijkstraAlgorithm {
public class DijkstraPickPath {
private HashMap<String,Vertex> nodes;
private List<Edge> edges;
private DijkstraAlgorithm dijkstra = null;
//@Test
//Changed by wasim on 27-02-2017 for passing column and rows numbers.
//public void testExcute() {
public void getLocationPickPath(int row,int col)
{
//nodes = new ArrayList<Vertex>();
nodes = new HashMap<String,Vertex>();
edges = new ArrayList<Edge>();
//Original Code. Start
/* for (int i = 0; i < 11; i++) {
//for (int i = 0; i <= 16; i++) {
Vertex location = new Vertex("Node_" + i, "Node_" + i);
nodes.add(location);
}*/
//int row =7;
//int col = 6;
String[][] locArr = new String[row][col];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
//Vertex location = new Vertex("Node_" + i+""+j, "Node_"+ i+""+j);
//Vertex location = new Vertex( i+""+j,i+""+j);
Vertex location = new Vertex( i+""+j,"["+i+","+j+"]");
locArr[i][j] = i+""+j;
nodes.put(locArr[i][j],location);
}
}
System.out.println("Data contains["+locArr.length+"]");
System.out.println("Nodes["+nodes.toString()+"]");
int locArrLength = locArr.length;
for (int i = 0; i < locArr.length; i++)
{
System.out.println("Value["+i+"]");
String[] strings = locArr[i];
for (int j = 0; j < strings.length; j++)
{
String string = strings[j];
System.out.println("string["+string+"]");
if((j+1) < strings.length)
{
System.out.println("strings[j+1]["+strings[j+1]+"]");
if(i==0 || i == (row-1))
{
System.out.println("Only Rows ["+i+"]");
addLane("Edge"+j, string,strings[j+1] , 10);
}
}
if((i+1) < locArrLength)
{
System.out.println("locArr[i+1][j]["+locArr[i+1][j]+"]");
addLane("Edge"+(i+j), string,locArr[i+1][j], 10);
addLane("Edge"+(j+i), locArr[i+1][j],string, 10);
}
}
}
System.out.println("Edges["+edges.toString()+"]");
Graph graph = new Graph(nodes, edges);
dijkstra = new DijkstraAlgorithm(graph);
/*dijkstra.execute(nodes.get("11"));
LinkedList<Vertex> path = dijkstra.getPath(nodes.get("64"));
assertNotNull(path);
assertTrue(path.size() > 0);
for (Vertex vertex : path) {
System.out.println(vertex);
}*/
}
private void addLane(String laneId, String sourceLocNo, String destLocNo,
int duration) {
Edge lane = new Edge(laneId,nodes.get(sourceLocNo), nodes.get(destLocNo), duration );
edges.add(lane);
}
//public String getPickPathStr(Graph graph,String locPhyRow,String locPhyCol)
public String getPickPathStr(String fromLoction,String toLocation)
{
String pickPathStr = "";
try
{
System.out.println("@@Inside getPickPath DijkstraAlgorithm");
System.out.println("nodes.get(fromLoction)["+nodes.get(fromLoction)+"]");
//DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(graph);
dijkstra.execute(nodes.get(fromLoction));
LinkedList<Vertex> path = dijkstra.getPath(nodes.get(toLocation));
//assertNotNull(path);
//assertTrue(path.size() > 0);
System.out.println("path["+path+"]");
if(path != null && path.size() > 0)
{
/* for (Vertex vertex : path) {
System.out.println(vertex);
pickPathStr = pickPathStr + "," +vertex.toString();
}*/
pickPathStr = path.toString();
//pickPathStr = pickPathStr.substring(pickPathStr.indexOf("[")+1,pickPathStr.indexOf("]"));
//pickPathStr = pickPathStr.substring(pickPathStr.indexOf("[")+1,pickPathStr.lastIndexOf("]"));
}
System.out.println("pickPathStr["+pickPathStr+"]");
}
catch(Exception e)
{
e.printStackTrace();
}
return pickPathStr;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment