Skip to content
This repository was archived by the owner on May 8, 2023. It is now read-only.

Commit 928e84e

Browse files
committed
Merge branch 'master' into Lars
# Conflicts: # .gitignore
2 parents 80bd37d + f881ac1 commit 928e84e

File tree

7 files changed

+221
-125
lines changed

7 files changed

+221
-125
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PathfindingProject.iml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,17 @@
2525
<SOURCES />
2626
</library>
2727
</orderEntry>
28+
<orderEntry type="module-library" scope="TEST">
29+
<library name="JUnit5.3">
30+
<CLASSES>
31+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.6.0-M1/junit-jupiter-api-5.6.0-M1.jar!/" />
32+
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.0/apiguardian-api-1.1.0.jar!/" />
33+
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
34+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.6.0-M1/junit-platform-commons-1.6.0-M1.jar!/" />
35+
</CLASSES>
36+
<JAVADOC />
37+
<SOURCES />
38+
</library>
39+
</orderEntry>
2840
</component>
2941
</module>

src/files/Starter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public static void main(String[] args) {
2020
public void start(Stage stage) {
2121
stage.setTitle("Pathfinding Project 2019");
2222
new Controller(new Model(),new View(stage)); // Model-View-Controller Architectural Pattern
23-
stage.show(); // Show Stage
23+
stage.show(); //
2424
}
2525
}

src/files/models/AStar.java

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,115 @@
11
package files.models;
22

3+
import files.models.graph.Edge;
4+
import files.models.graph.Vertex;
5+
6+
import java.util.ArrayList;
7+
import java.util.Comparator;
8+
import java.util.TreeSet;
9+
310
public class AStar {
11+
12+
private Graph graph;
13+
int infinity = (int) Double.POSITIVE_INFINITY;
14+
15+
public static void main(String[] args) {
16+
AStar aStar = new AStar();
17+
aStar.startAStar();
18+
}
19+
20+
public void startAStar() {
21+
// Create graph
22+
//graph = this.makeDijkstraGraph();
23+
graph = this.makeSmallGraphA();
24+
// A
25+
Vertex startNode = graph.getVertex("A");
26+
Vertex endNode = graph.getVertex("F");
27+
28+
// B
29+
//Vertex startNode = graph.getVertex("J");
30+
//Vertex endNode = graph.getVertex("F");
31+
32+
ArrayList<Vertex> result = aStar(startNode, endNode);
33+
34+
}
35+
36+
public Graph makeSmallGraphA() {
37+
Graph myGraph= new Graph();
38+
final Vertex A= myGraph.addVertex("A");
39+
final Vertex B= myGraph.addVertex("B");
40+
final Vertex C = myGraph.addVertex("C");
41+
final Vertex D = myGraph.addVertex("D");
42+
final Vertex E = myGraph.addVertex("E");
43+
44+
myGraph.newEdge(A,B, 5, 3);
45+
myGraph.newEdge(A,C, 10, 3);
46+
myGraph.newEdge(B,C, 3, 3);
47+
myGraph.newEdge(B,D, 2, 3);
48+
myGraph.newEdge(B,E, 9, 3);
49+
myGraph.newEdge(C,B, 2, 3);
50+
myGraph.newEdge(C,E, 1, 3);
51+
myGraph.newEdge(D,E, 6, 3);
52+
myGraph.newEdge(E,D, 4, 3);
53+
54+
return myGraph;
55+
}
56+
57+
public ArrayList<Vertex> aStar(Vertex startNode, Vertex endNode) {
58+
59+
TreeSet<Vertex> openTreeSet = new TreeSet<>(Comparator.comparingLong(Vertex::getF));
60+
ArrayList<Vertex> closedList = new ArrayList<>();
61+
62+
for (Vertex vertex : graph.getVertices()) {
63+
vertex.setDistance(200);
64+
vertex.setPredecessor(null);
65+
//vertex.setHeuristic(endNode);
66+
}
67+
68+
startNode.setDistance(0);
69+
openTreeSet.add(startNode);
70+
Vertex current;
71+
72+
while (openTreeSet.size() != 0) {
73+
current = openTreeSet.first();
74+
System.out.println(current.name + " " + current.distance);
75+
openTreeSet.remove(current);
76+
77+
for (Edge edge : current.edges){
78+
79+
Vertex successor = edge.getToVertex();
80+
int successorG = current.distance + successor.distance;
81+
long successorH = heuristic(current,endNode);
82+
long successorF = successorG + successorH;
83+
84+
if (successor == endNode){
85+
System.out.println("solution found" + current.name + endNode.name);
86+
}
87+
88+
if (openTreeSet.contains(successor) && successorF < successor.f){
89+
System.out.println(successor.name + " " + successorF);
90+
successor.setPredecessor(current);
91+
successor.setDistance(successorG);
92+
successor.setF(successorF);
93+
}
94+
else if (closedList.contains(successor) && successorF < successor.f){
95+
closedList.remove(successor);
96+
openTreeSet.add(successor);
97+
}
98+
else {
99+
openTreeSet.add(successor);
100+
successor.setPredecessor(current);
101+
successor.setDistance(successorG);
102+
successor.setF(successorF);
103+
}
104+
}
105+
closedList.add(current);
106+
}
107+
108+
return null;
109+
}
110+
111+
public long heuristic(Vertex current, Vertex endNode) {
112+
//return sqrt((endNode.x - this.x)*(endNode.x - this.x) + (endNode.y - this.y)*(endNode.y - this.y));
113+
return 0;
114+
}
4115
}

src/files/models/Dijkstra.java

Lines changed: 61 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,24 @@
22

33
import files.models.graph.Edge;
44
import files.models.graph.Vertex;
5-
import javafx.util.Pair;
65

7-
import java.util.ArrayList;
8-
import java.util.HashMap;
9-
import java.util.Map;
6+
import java.util.*;
107

118
public class Dijkstra {
129

13-
1410
private Graph graph;
1511

16-
int counter = 0;
17-
18-
Map<Vertex,Vertex> predecessorMap= new HashMap<>(); // The key is a vertex, the value is the previous vertex
19-
Map<Vertex,Integer> distanceMap=new HashMap<>(); // The key is a vertex, the value is the shortest distance to it
20-
ArrayList<Vertex> queue = new ArrayList<Vertex>();
21-
int infinity = (int)Double.POSITIVE_INFINITY;
22-
int minimum = infinity;
23-
int weight = infinity;
24-
12+
int infinity = (int) Double.POSITIVE_INFINITY;
2513

2614

2715
public static void main(String[] args) {
2816
Dijkstra dijkstra = new Dijkstra();
2917
dijkstra.startDijkstra();
3018
}
3119

32-
public void startDijkstra(){
20+
public void startDijkstra() {
3321
// Create graph
22+
//graph = this.makeDijkstraGraph();
3423
graph = this.makeSmallGraphB();
3524
// A
3625
//Vertex startNode = graph.getVertex("A");
@@ -40,48 +29,19 @@ public void startDijkstra(){
4029
Vertex startNode = graph.getVertex("J");
4130
Vertex endNode = graph.getVertex("F");
4231

43-
Pair<Integer, Map<Vertex, Vertex>> results = dijkstra(startNode, endNode);
44-
Vertex current =endNode;
45-
ArrayList<Vertex> Path= new ArrayList<>();
46-
Path.add(endNode);
47-
48-
while ((current != startNode) && (results.getValue().get(current)!=null)) {
49-
current=results.getValue().get(current);
50-
Path.add(0,current);
51-
}
32+
ArrayList<Vertex> result = dijkstra(startNode, endNode);
5233

53-
for(Vertex v : Path)
54-
{
55-
System.out.print( v.name);
34+
for(Vertex v : result) {
35+
System.out.print( v.name + " Dist:" + v.distance + " ");
5636
if (v!=endNode)
57-
System.out.print("->");
37+
System.out.print("-> ");
5838
}
5939

6040
}
6141

62-
public Graph makeSmallGraphA() {
63-
Graph myGraph= new Graph();
64-
final Vertex A= myGraph.addVertex("A");
65-
final Vertex B= myGraph.addVertex("B");
66-
final Vertex C = myGraph.addVertex("C");
67-
final Vertex D = myGraph.addVertex("D");
68-
final Vertex E = myGraph.addVertex("E");
69-
70-
myGraph.newEdge(A,B, 5, 3);
71-
myGraph.newEdge(A,C, 10, 3);
72-
myGraph.newEdge(B,C, 3, 3);
73-
myGraph.newEdge(B,D, 2, 3);
74-
myGraph.newEdge(B,E, 9, 3);
75-
myGraph.newEdge(C,B, 2, 3);
76-
myGraph.newEdge(C,E, 1, 3);
77-
myGraph.newEdge(D,E, 6, 3);
78-
myGraph.newEdge(E,D, 4, 3);
79-
80-
return myGraph;
81-
}
8242

8343
public Graph makeSmallGraphB() {
84-
Graph myGraph= new Graph();
44+
Graph myGraph = new Graph();
8545
final Vertex A = myGraph.addVertex("A");
8646
final Vertex B = myGraph.addVertex("B");
8747
final Vertex C = myGraph.addVertex("C");
@@ -93,26 +53,26 @@ public Graph makeSmallGraphB() {
9353
final Vertex I = myGraph.addVertex("I");
9454
final Vertex J = myGraph.addVertex("J");
9555

96-
myGraph.newEdge(A,B,10,0);
97-
myGraph.newEdge(A,D,20,0);
98-
myGraph.newEdge(A,E,20,0);
99-
myGraph.newEdge(A,F,5,0);
100-
myGraph.newEdge(A,G,15,0);
101-
myGraph.newEdge(B,C,7,0);
102-
myGraph.newEdge(B,D,10,0);
103-
myGraph.newEdge(C,B,15,0);
104-
myGraph.newEdge(C,D,5,0);
105-
myGraph.newEdge(D,E,10,0);
106-
myGraph.newEdge(E,F,5,0);
107-
myGraph.newEdge(G,F,10,0);
108-
myGraph.newEdge(H,A,5,0);
109-
myGraph.newEdge(H,B,20,0);
110-
myGraph.newEdge(H,G,5,0);
111-
myGraph.newEdge(I,B,15,0);
112-
myGraph.newEdge(I,H,20,0);
113-
myGraph.newEdge(I,J,10,0);
114-
myGraph.newEdge(J,B,5,0);
115-
myGraph.newEdge(J,C,15,0);
56+
myGraph.newEdge(A, B, 10, 0);
57+
myGraph.newEdge(A, D, 20, 0);
58+
myGraph.newEdge(A, E, 20, 0);
59+
myGraph.newEdge(A, F, 5, 0);
60+
myGraph.newEdge(A, G, 15, 0);
61+
myGraph.newEdge(B, C, 7, 0);
62+
myGraph.newEdge(B, D, 10, 0);
63+
myGraph.newEdge(C, B, 15, 0);
64+
myGraph.newEdge(C, D, 5, 0);
65+
myGraph.newEdge(D, E, 10, 0);
66+
myGraph.newEdge(E, F, 5, 0);
67+
myGraph.newEdge(G, F, 10, 0);
68+
myGraph.newEdge(H, A, 5, 0);
69+
myGraph.newEdge(H, B, 20, 0);
70+
myGraph.newEdge(H, G, 5, 0);
71+
myGraph.newEdge(I, B, 15, 0);
72+
myGraph.newEdge(I, H, 20, 0);
73+
myGraph.newEdge(I, J, 10, 0);
74+
myGraph.newEdge(J, B, 5, 0);
75+
myGraph.newEdge(J, C, 15, 0);
11676

11777
return myGraph;
11878
}
@@ -121,69 +81,47 @@ public Graph makeSmallGraphB() {
12181
///////////////////////////////// TODO: Implement new dijkstra 25-11-2019 //////////////////////////
12282
//////////////////////////////////////////////////////////////////////////////////////
12383

124-
public Pair<Integer, Map<Vertex,Vertex>> dijkstra(Vertex startNode, Vertex endNode) {
84+
public ArrayList<Vertex> dijkstra(Vertex startNode, Vertex endNode) {
85+
86+
TreeSet<Vertex> graphTreeSet = new TreeSet<>(Comparator.comparingInt(Vertex::getDistance));
12587

126-
for(Vertex vertex: graph.getVertices()) {
127-
distanceMap.put(vertex,infinity); // This is the nodes and their weight
128-
predecessorMap.put(vertex, null); // we set the vertex to be null, which is meant to be "not visited"
88+
for (Vertex vertex : graph.getVertices()) {
89+
vertex.setDistance(infinity);
90+
vertex.setPredecessor(null);
12991
}
92+
startNode.setDistance(0);
93+
graphTreeSet.addAll(graph.getVertices());
94+
Vertex current;
13095

131-
Vertex from = startNode; // Set previous to be start node
132-
Vertex next;
133-
134-
distanceMap.put(startNode,0); // Setting previous distance map to 0
135-
predecessorMap.put(startNode,new Vertex("Start")); // Initialize the start as "coming from previous" (which at start is start)
136-
137-
while(predecessorMap.containsValue(null)&&from!=null) {
138-
System.out.println(from.name + " is having the weight of: " + distanceMap.get(from));
139-
System.out.println("For: " + from.name + " {");
140-
for(Edge edge : from.getEdges()) {
141-
if(!edge.getToVertex().equals(from)){
142-
Vertex to = edge.getToVertex();
143-
System.out.print("\t From: " + from.name + " ");
144-
System.out.print("\t To: " + to.name + " ");
145-
System.out.print("\t Distance: " + edge.distance);
146-
System.out.println();
147-
weight = edge.distance+distanceMap.get(from);
148-
if (weight <= distanceMap.get(to)) {
149-
weight = edge.distance+distanceMap.get(from);
150-
distanceMap.put(to, weight); // Set the weight to distMap
151-
System.out.println("\t " + to.name + " is now: " + weight);
152-
} else {
153-
predecessorMap.remove(from); // Removing vertex
154-
System.out.println("\t removed " + from.name);
155-
// we are not setting a new previous here, the reason is that we instead had a loop that runs from the other vertices.
156-
}
157-
}
158-
}
96+
while (graphTreeSet.size() != 0) {
97+
current = graphTreeSet.first();
98+
System.out.println(" CURRENT: " + current.name);
15999

160-
next = findMin(from);
161-
System.out.println("set "+next.name+" to "+from.name);
162-
predecessorMap.put(next,from);
163-
from = next;
100+
for (Edge edge : current.edges) {
164101

165-
if(next.equals(endNode)){
166-
System.out.println("this is the end node!");
167-
from = predecessorMap.get(null);
102+
if (current.distance != infinity && edge.distance + current.distance < edge.getToVertex().distance) {
103+
edge.getToVertex().setDistance(edge.distance + current.distance);
104+
edge.getToVertex().setPredecessor(current);
105+
graphTreeSet.remove(edge.getToVertex());
106+
graphTreeSet.add(edge.getToVertex());
107+
}
108+
109+
System.out.println("Evaluating: " + edge.getFromVertex().name + " -> " + edge.getToVertex().name
110+
+ " dist: " + edge.getToVertex().distance);
168111
}
169-
}
170112

171-
return (new Pair<Integer,Map<Vertex,Vertex>> (distanceMap.get(startNode), predecessorMap));
172-
}
113+
graphTreeSet.remove(current);
114+
System.out.println("removed: " +current.name);
115+
}
173116

174-
private Vertex findMin(Vertex vertex) {
175-
Vertex closestVertex = null;
176-
int lowestDistance = infinity;
177-
for (Edge edge : vertex.getEdges()) {
117+
Vertex resultCurrent = endNode;
118+
ArrayList<Vertex> Path= new ArrayList<>();
119+
Path.add(endNode);
178120

179-
if(!edge.getToVertex().equals(vertex)){
180-
int distance = edge.distance;
181-
if (edge.distance <= lowestDistance) {
182-
lowestDistance = distance;
183-
closestVertex = edge.getToVertex();
184-
}
185-
}
121+
while ((resultCurrent != startNode) && (resultCurrent.predecessor != null)) {
122+
resultCurrent = resultCurrent.predecessor;
123+
Path.add(0,resultCurrent);
186124
}
187-
return closestVertex;
125+
return Path;
188126
}
189127
}

0 commit comments

Comments
 (0)