Skip to content
Snippets Groups Projects
Commit 6c0933c6 authored by Pedro Pablo Cardona Arroyave's avatar Pedro Pablo Cardona Arroyave
Browse files

New implementation of ALT

parent 198531a6
Branches AltNodeClass
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>
\ No newline at end of file
......@@ -156,6 +156,59 @@ class Edge implements Comparable<Edge>{
}
}
class ALTEdge implements Comparable<ALTEdge>{
int start;
int destination;
int drivingTime;
int currentWeight = 0;
int fromLandMark = 0;
int toLandMark = 0;
int destinationDelta;
ALTEdge(int start, int destination, int drivingTime){
this.start = start;
this.destination = destination;
this.drivingTime = drivingTime;
}
public int getStart() {
return start;
}
public int getDestination() {
return destination;
}
public int getDrivingTime() {
return drivingTime;
}
public int getCurrentWeight() {
return currentWeight;
}
public void setCurrentWeight(int currentWeight) {
this.currentWeight = currentWeight;
}
public void setFromLandMark(int fromLandMark) {
this.fromLandMark = fromLandMark;
}
public void setDestinationDelta(int destinationDelta){
this.destinationDelta = destinationDelta;
}
private int getDelta (){
if((destinationDelta - fromLandMark) <0 ) return 0;
else return destinationDelta - fromLandMark;
}
@Override
public int compareTo(ALTEdge o) {
return Integer.compare(drivingTime+currentWeight - getDelta(),o.drivingTime+o.currentWeight-o.getDelta());
}
}
class Graph{
private final HashMap<Integer, List<Edge>> adjacencyList = new HashMap<>();
private final List<Node> nodes = new ArrayList<>();
......@@ -313,6 +366,79 @@ class Graph{
}
}
class ALTGraph{
private final HashMap<Integer, List<ALTEdge>> adjacencyList = new HashMap<>();
private final List<Node> nodes = new ArrayList<>();
public List<Node> getNodes() {
return nodes;
}
public ALTGraph(String inFile) throws IOException {
int numberOfNodes = Utils.readFirstLine(inFile);
for(int i = 0; i < numberOfNodes; ++i) {
adjacencyList.put(i, new ArrayList<>());
}
}
public void loadNodes(String inFile) {
List<String> data = Utils.readFile(inFile);
for(int i = 1; i < data.size(); ++i) {
String[] splitText = Utils.dividedText(data.get(i), 3);
nodes.add(new Node(Integer.parseInt(splitText[0]), Double.parseDouble(splitText[1]), Double.parseDouble(splitText[2])));
}
System.out.println("Nodes loaded.");
}
public void loadALtEdges(String inFileEdges, String distanceFromLandMark) throws Exception{
List<String> data = Utils.readFile(inFileEdges);
int[] distances = Utils.fileToArray(distanceFromLandMark,adjacencyList.size());
for(int i = 1; i < data.size(); ++i) {
String[] splitText = Utils.dividedText(data.get(i), 3);
addEdge(Integer.parseInt(splitText[0]), Integer.parseInt(splitText[1]), Integer.parseInt(splitText[2]));
}
for(int i = 0; i<distances.length;i++){
for(ALTEdge edge: adjacencyList.get(i)){
edge.setDestinationDelta(distances[i]);
}
}
System.out.println("Edges loaded.");
}
public List<ALTEdge> getShortestPath(int startNumber, int destinationNumber){
PriorityQueue<ALTEdge> priorityQueue = new PriorityQueue<>();
int nodeSize = adjacencyList.size();
int[] pathWeight = new int[nodeSize];
Arrays.fill(pathWeight, Integer.MAX_VALUE);
ArrayList<ALTEdge>[] pathToNodes = new ArrayList[nodeSize];
pathWeight[startNumber] = 0;
Arrays.fill(pathToNodes,new ArrayList<>());
priorityQueue.addAll(adjacencyList.get(startNumber));
while (!priorityQueue.isEmpty()){
ALTEdge nextEdge = priorityQueue.poll();
int nextNode = nextEdge.getDestination();
int start = nextEdge.getStart();
int currentSum = pathWeight[start] + nextEdge.getDrivingTime();
if(currentSum<pathWeight[nextNode]) {
pathWeight[nextNode] = currentSum;
pathToNodes[nextNode] = new ArrayList<>(pathToNodes[start]);
pathToNodes[nextNode].add(nextEdge);
for(ALTEdge edge: adjacencyList.get(nextNode)){
edge.setCurrentWeight(pathWeight[start]);
priorityQueue.add(edge);
}
priorityQueue.addAll(adjacencyList.get(nextNode));
}
if(nextNode == destinationNumber) return pathToNodes[destinationNumber];
}
return null;
}
public void addEdge(int start, int destination, int drivingTime) {
adjacencyList.get(start).add(new ALTEdge(start,destination, drivingTime));
}
}
class Utils{
/**
* Static method that return a list that contains all the lines from an extern txt file
......@@ -356,7 +482,7 @@ class Utils{
int[] array = new int[size];
try (BufferedReader reader = Files.newBufferedReader(Paths.get(fileName))) {
String currentLine = null;
int index = 0;
int index = 1;
while((currentLine = reader.readLine()) != null) {
array[index] = Integer.parseInt(currentLine);
++index;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment