Improved Algortihms and A Star Heuristik
This commit is contained in:
parent
8de3c54775
commit
5bab9b5992
@ -247,16 +247,16 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
|
||||
// Den nächsten Knoten, der am wenigsten kostet, besuchen
|
||||
WrapperElement<T> nextVertex = queue.poll();
|
||||
|
||||
// Knoten als besucht makieren
|
||||
visited.put(nextVertex.getElement(), true);
|
||||
|
||||
|
||||
// Logging
|
||||
textDescription = "Visit " + nextVertex.getElement().getName();
|
||||
System.out.println(textDescription);
|
||||
nextVertex.getElement().getScreenVertex().setColor(Color.BLUE);
|
||||
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
|
||||
|
||||
// Falls Knoten schon besucht
|
||||
if(!visited.get(nextVertex.getElement())){
|
||||
// Knoten als besucht makieren
|
||||
visited.put(nextVertex.getElement(), true);
|
||||
textDescription = "Visit " + nextVertex.getElement().getName();
|
||||
// Logging
|
||||
System.out.println(textDescription);
|
||||
nextVertex.getElement().getScreenVertex().setColor(Color.BLUE);
|
||||
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
|
||||
}
|
||||
|
||||
// Wenn Weg gefunden, brich ab
|
||||
if (nextVertex.getElement() == n2) {
|
||||
@ -333,7 +333,7 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
|
||||
|
||||
|
||||
|
||||
public int getShortestPathAStar(MarkedVertex<T> n1, MarkedVertex<T> n2) {
|
||||
public double getShortestPathAStar(MarkedVertex<T> n1, MarkedVertex<T> n2) {
|
||||
|
||||
// Erstellt Hashmap um Distanz von Startnoten zu jedem Knoten auf dem Graph zu tracken
|
||||
// Erstellt Hashmap um zu tracken welche Knoten schon besucht wurden
|
||||
@ -341,11 +341,11 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
|
||||
// Initialisierung aller Distanzen auf UNENDLICH (= -1)
|
||||
// Initialisierung, dass kein Knoten besucht wurde
|
||||
// Initialisierung aller Vorgänger auf null
|
||||
HashMap<MarkedVertex<T>, Integer> distance = new HashMap<>();
|
||||
HashMap<MarkedVertex<T>, Double> distance = new HashMap<>();
|
||||
HashMap<MarkedVertex<T>, Boolean> visited = new HashMap<>();
|
||||
HashMap<MarkedVertex<T>, MarkedVertex<T>> predecessors = new HashMap<>();
|
||||
for (MarkedVertex<T> i: this.getAllVertexes()) {
|
||||
distance.put(i, -1);
|
||||
distance.put(i, -1.0);
|
||||
visited.put(i, false);
|
||||
predecessors.put(i, null);
|
||||
}
|
||||
@ -355,15 +355,15 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
|
||||
|
||||
// Distanz zu Startknoten auf 0
|
||||
// Weg zu Startknoten in die Schlange aufnehmen
|
||||
distance.put(n1, 0);
|
||||
distance.put(n1, 0.0);
|
||||
queue.add(new WrapperElement<>(n1, 0));
|
||||
|
||||
// Variable, die Distanz zwischen aktuellem Knoten und Nachfolger speichert
|
||||
int dist = 0;
|
||||
double dist = 0;
|
||||
// Variable, die Distanz zwischen dem potenziell nächsten Knoten und dem Zielknoten speichert
|
||||
int airDist = 0;
|
||||
double airDist = 0;
|
||||
// Variable, die Distanz zwischen dem aktuellen Knoten bis zum Endknoten speichert
|
||||
int distToFinish = 0;
|
||||
double distToFinish = 0;
|
||||
// Zähler für LogList
|
||||
int step = 0;
|
||||
// String für den Description Inhalt
|
||||
@ -380,15 +380,17 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
|
||||
// Den nächsten Knoten, der am wenigsten kostet, besuchen
|
||||
WrapperElement<T> nextVertex = queue.poll();
|
||||
|
||||
// Knoten als besucht makieren
|
||||
visited.put(nextVertex.getElement(), true);
|
||||
|
||||
|
||||
// Logging
|
||||
textDescription = "Visit " + nextVertex.getElement().getName();
|
||||
System.out.println(textDescription);
|
||||
nextVertex.getElement().getScreenVertex().setColor(Color.BLUE);
|
||||
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
|
||||
// Falls Knoten schon besucht
|
||||
if(!visited.get(nextVertex.getElement())){
|
||||
// Knoten als besucht makieren
|
||||
visited.put(nextVertex.getElement(), true);
|
||||
textDescription = "Visit " + nextVertex.getElement().getName();
|
||||
// Logging
|
||||
System.out.println(textDescription);
|
||||
nextVertex.getElement().getScreenVertex().setColor(Color.BLUE);
|
||||
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
|
||||
}
|
||||
|
||||
// Wenn Weg gefunden, brich ab
|
||||
if (nextVertex.getElement() == n2) {
|
||||
@ -417,8 +419,8 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
|
||||
if (j.getSource() == nextVertex.getElement() && j.getDestination() == i) {
|
||||
|
||||
//Berechnung der Heuristik über die Luftdistanz des nächsten Knoten zum Zielknoten
|
||||
airDist = (int) Math.sqrt(Math.pow((i.getCords()[0] - n2.getCords()[0]), 2)
|
||||
+ Math.pow((i.getCords()[1] - n2.getCords()[1]), 2));
|
||||
airDist = Math.sqrt(Math.pow((i.getCords()[0] - n2.getCords()[0]), 2)
|
||||
+ Math.pow((i.getCords()[1] - n2.getCords()[1]), 2)) / 100;
|
||||
|
||||
// Berechne Distanz zu nächstem Knoten
|
||||
EdgeWeightMarking marking = (EdgeWeightMarking) j.getMarking();
|
||||
@ -440,7 +442,7 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
|
||||
distance.put(i, dist);
|
||||
|
||||
// Logging
|
||||
textDescription = "Add " + i.getName() + " with " + dist + " weight to queue.";
|
||||
textDescription = "Add " + i.getName() + " with " + distToFinish + " weight to queue.";
|
||||
System.out.println(textDescription);
|
||||
i.getScreenVertex().setColor(Color.YELLOW);
|
||||
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
|
||||
|
@ -276,5 +276,5 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
|
||||
|
||||
public abstract int getShortestPathDijkstra(MarkedVertex<T> n1, MarkedVertex<T> n2);
|
||||
|
||||
public abstract int getShortestPathAStar(MarkedVertex<T> n1, MarkedVertex<T> n2);
|
||||
public abstract double getShortestPathAStar(MarkedVertex<T> n1, MarkedVertex<T> n2);
|
||||
}
|
||||
|
@ -28,12 +28,12 @@ class WrapperElement<T extends VertexMarking> {
|
||||
// ATTRIBUTE
|
||||
|
||||
private MarkedVertex<T> n1;
|
||||
private int prio;
|
||||
private double prio;
|
||||
|
||||
|
||||
// KONSTRUKTOR
|
||||
|
||||
public WrapperElement(MarkedVertex<T> n1, int prio) {
|
||||
public WrapperElement(MarkedVertex<T> n1, double prio) {
|
||||
this.n1 = n1;
|
||||
this.prio = prio;
|
||||
}
|
||||
@ -46,7 +46,7 @@ class WrapperElement<T extends VertexMarking> {
|
||||
}
|
||||
|
||||
|
||||
public int getPrio() {
|
||||
public double getPrio() {
|
||||
return this.prio;
|
||||
}
|
||||
|
||||
@ -62,6 +62,6 @@ class WrapperElement<T extends VertexMarking> {
|
||||
class WrapperComparator<T extends VertexMarking> implements Comparator<WrapperElement<T>> {
|
||||
|
||||
public int compare(WrapperElement<T> element1, WrapperElement<T> element2) {
|
||||
return Integer.compare(element1.getPrio(), element2.getPrio());
|
||||
return Double.compare(element1.getPrio(), element2.getPrio());
|
||||
}
|
||||
}
|
||||
|
@ -182,15 +182,16 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
|
||||
// Den nächsten Knoten, der am wenigsten kostet, besuchen
|
||||
WrapperElement<T> nextVertex = queue.poll();
|
||||
|
||||
// Knoten als besucht makieren
|
||||
visited.put(nextVertex.getElement(), true);
|
||||
|
||||
|
||||
// Logging
|
||||
textDescription = "Visit " + nextVertex.getElement().getName();
|
||||
System.out.println(textDescription);
|
||||
nextVertex.getElement().getScreenVertex().setColor(Color.BLUE);
|
||||
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
|
||||
// Falls Knoten schon besucht
|
||||
if(!visited.get(nextVertex.getElement())){
|
||||
// Knoten als besucht makieren
|
||||
visited.put(nextVertex.getElement(), true);
|
||||
textDescription = "Visit " + nextVertex.getElement().getName();
|
||||
// Logging
|
||||
System.out.println(textDescription);
|
||||
nextVertex.getElement().getScreenVertex().setColor(Color.BLUE);
|
||||
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
|
||||
}
|
||||
|
||||
|
||||
// Wenn Weg gefunden, brich ab
|
||||
@ -267,7 +268,7 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
|
||||
}
|
||||
|
||||
|
||||
public int getShortestPathAStar(MarkedVertex<T> n1, MarkedVertex<T> n2) {
|
||||
public double getShortestPathAStar(MarkedVertex<T> n1, MarkedVertex<T> n2) {
|
||||
|
||||
// Erstellt Hashmap um Distanz von Startnoten zu jedem Knoten auf dem Graph zu tracken
|
||||
// Erstellt Hashmap um zu tracken welche Knoten schon besucht wurden
|
||||
@ -275,11 +276,11 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
|
||||
// Initialisierung aller Distanzen auf UNENDLICH (= -1)
|
||||
// Initialisierung, dass kein Knoten besucht wurde
|
||||
// Initialisierung aller Vorgänger auf null
|
||||
HashMap<MarkedVertex<T>, Integer> distance = new HashMap<>();
|
||||
HashMap<MarkedVertex<T>, Double> distance = new HashMap<>();
|
||||
HashMap<MarkedVertex<T>, Boolean> visited = new HashMap<>();
|
||||
HashMap<MarkedVertex<T>, MarkedVertex<T>> predecessors = new HashMap<>();
|
||||
for (MarkedVertex<T> i: this.getAllVertexes()) {
|
||||
distance.put(i, -1);
|
||||
distance.put(i, -1.0);
|
||||
visited.put(i, false);
|
||||
predecessors.put(i, null);
|
||||
}
|
||||
@ -289,15 +290,15 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
|
||||
|
||||
// Distanz zu Startknoten auf 0
|
||||
// Weg zu Startknoten in die Schlange aufnehmen
|
||||
distance.put(n1, 0);
|
||||
distance.put(n1, 0.0);
|
||||
queue.add(new WrapperElement<>(n1, 0));
|
||||
|
||||
// Variable, die Distanz zwischen aktuellem Knoten und Nachfolger speichert
|
||||
int dist = 0;
|
||||
double dist = 0;
|
||||
// Variable, die Distanz zwischen dem potenziell nächsten Knoten und dem Zielknoten speichert
|
||||
int airDist = 0;
|
||||
double airDist = 0;
|
||||
// Variable, die Distanz zwischen dem aktuellen Knoten bis zum Endknoten speichert
|
||||
int distToFinish = 0;
|
||||
double distToFinish = 0;
|
||||
// Zähler für LogList
|
||||
int step = 0;
|
||||
// String für den Description Inhalt
|
||||
@ -314,16 +315,16 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
|
||||
// Den nächsten Knoten, der am wenigsten kostet, besuchen
|
||||
WrapperElement<T> nextVertex = queue.poll();
|
||||
|
||||
// Knoten als besucht makieren
|
||||
visited.put(nextVertex.getElement(), true);
|
||||
|
||||
|
||||
// Logging
|
||||
textDescription = "Visit " + nextVertex.getElement().getName();
|
||||
System.out.println(textDescription);
|
||||
nextVertex.getElement().getScreenVertex().setColor(Color.BLUE);
|
||||
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
|
||||
|
||||
// Falls Knoten schon besucht
|
||||
if(!visited.get(nextVertex.getElement())){
|
||||
// Knoten als besucht makieren
|
||||
visited.put(nextVertex.getElement(), true);
|
||||
textDescription = "Visit " + nextVertex.getElement().getName();
|
||||
// Logging
|
||||
System.out.println(textDescription);
|
||||
nextVertex.getElement().getScreenVertex().setColor(Color.BLUE);
|
||||
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
|
||||
}
|
||||
|
||||
// Wenn Weg gefunden, brich ab
|
||||
if (nextVertex.getElement() == n2) {
|
||||
@ -352,8 +353,8 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
|
||||
if (j.getSource() == nextVertex.getElement() && j.getDestination() == i) {
|
||||
|
||||
//Berechnung der Heuristik über die Luftdistanz des nächsten Knoten zum Zielknoten
|
||||
airDist = (int) Math.sqrt(Math.pow((i.getCords()[0] - n2.getCords()[0]), 2)
|
||||
+ Math.pow((i.getCords()[1] - n2.getCords()[1]), 2));
|
||||
airDist = Math.sqrt(Math.pow((i.getCords()[0] - n2.getCords()[0]), 2)
|
||||
+ Math.pow((i.getCords()[1] - n2.getCords()[1]), 2)) / 100;
|
||||
|
||||
// Berechne Distanz zu nächstem Knoten
|
||||
EdgeWeightMarking marking = (EdgeWeightMarking) j.getMarking();
|
||||
@ -375,7 +376,7 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
|
||||
distance.put(i, dist);
|
||||
|
||||
// Logging
|
||||
textDescription = "Add " + i.getName() + " with " + dist + " weight to queue.";
|
||||
textDescription = "Add " + i.getName() + " with " + distToFinish + " weight to queue.";
|
||||
System.out.println(textDescription);
|
||||
i.getScreenVertex().setColor(Color.YELLOW);
|
||||
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user