Aufgabe bearbeitet
This commit is contained in:
parent
143b6158d5
commit
6584d7e5bd
|
|
@ -1,11 +1,79 @@
|
||||||
package gfn.marc;
|
package gfn.marc;
|
||||||
|
|
||||||
|
import java.sql.SQLOutput;
|
||||||
|
|
||||||
|
import static gfn.marc.Routen.holeStreckeGewicht;
|
||||||
|
import static gfn.marc.Routen.holeStreckePreis;
|
||||||
|
|
||||||
public class Frachtfluege {
|
public class Frachtfluege {
|
||||||
|
|
||||||
|
// Route für Ausgabsort und Zielort
|
||||||
|
static String[][] tabelle = Routen.getRoute("A", "E");
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
// beste Route finden
|
||||||
|
int gewicht = 1300;
|
||||||
|
int besteRoute = findeRoute(gewicht);
|
||||||
|
|
||||||
String[][] route = Routen.getRoute("C", "E");
|
// beste Route ausgeben
|
||||||
|
System.out.print("Beste Route: ");
|
||||||
|
for (int i = 0; i < tabelle[besteRoute].length; i++) {
|
||||||
|
if (tabelle[besteRoute][i] != null) {
|
||||||
|
System.out.print(tabelle[besteRoute][i] + " ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Funktion zur Berechnung der besten Route
|
||||||
|
static int findeRoute(int gewicht) {
|
||||||
|
|
||||||
|
int[] gewichte = new int[tabelle.length];
|
||||||
|
double[] preise = new double[tabelle.length];
|
||||||
|
for (int i = 0; i < tabelle.length; i++) {
|
||||||
|
int gewichtMax = Integer.MAX_VALUE;
|
||||||
|
for (int j = 0; j < tabelle[i].length - 1; j++) {
|
||||||
|
String sb = tabelle[i][j];
|
||||||
|
String se = tabelle[i][j + 1];
|
||||||
|
|
||||||
|
if (tabelle[i][j + 1] != null && j != tabelle[i].length - 1) {
|
||||||
|
if (holeStreckeGewicht(sb, se) < gewichtMax) {
|
||||||
|
gewichtMax = holeStreckeGewicht(sb, se);
|
||||||
|
}
|
||||||
|
gewichte[i] = gewichtMax;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int j = 0; j < tabelle[i].length - 1; j++) {
|
||||||
|
String sb = tabelle[i][j];
|
||||||
|
String se = tabelle[i][j + 1];
|
||||||
|
if (gewichte[i] >= gewicht) {
|
||||||
|
if (tabelle[i][j + 1] != null && j != tabelle[i].length - 1) {
|
||||||
|
preise[i] += gewichte[i] * holeStreckePreis(sb, se);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
double guenstigsterPreis = Integer.MAX_VALUE;
|
||||||
|
int index = 0;
|
||||||
|
for (int i = 0; i < preise.length; i++) {
|
||||||
|
if (preise[i] < guenstigsterPreis && preise[i] != 0) {
|
||||||
|
guenstigsterPreis = preise[i];
|
||||||
|
index = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,12 +6,14 @@ import java.util.*;
|
||||||
|
|
||||||
public class Routen {
|
public class Routen {
|
||||||
|
|
||||||
static Ort a = new Ort("A", 1600, 5.6);
|
// Orte mit von mir erfundenen Frachtkapazitäten und -kosten.
|
||||||
static Ort b = new Ort("B", 1800, 5.2);
|
static Ort a = new Ort("A", 1600, 2.6);
|
||||||
static Ort c = new Ort("C", 1300, 5.5);
|
static Ort b = new Ort("B", 1800, 2.2);
|
||||||
static Ort d = new Ort("D", 1450, 4.9);
|
static Ort c = new Ort("C", 1300, 2.5);
|
||||||
static Ort e = new Ort("E", 1400, 5.1);
|
static Ort d = new Ort("D", 1450, 1.9);
|
||||||
|
static Ort e = new Ort("E", 1400, 2.1);
|
||||||
|
|
||||||
|
// Verknüpfung der Orte miteinander
|
||||||
static {
|
static {
|
||||||
a.setKnoten(b, c, d);
|
a.setKnoten(b, c, d);
|
||||||
b.setKnoten(a, c, e);
|
b.setKnoten(a, c, e);
|
||||||
|
|
@ -19,6 +21,7 @@ public class Routen {
|
||||||
d.setKnoten(a, e);
|
d.setKnoten(a, e);
|
||||||
e.setKnoten(b, d);
|
e.setKnoten(b, d);
|
||||||
|
|
||||||
|
// Berechnung aller möglicher Routen zwischen den einzelnen Orten --> ACHTUNG! BUG!
|
||||||
a.berechneRouten();
|
a.berechneRouten();
|
||||||
b.berechneRouten();
|
b.berechneRouten();
|
||||||
c.berechneRouten();
|
c.berechneRouten();
|
||||||
|
|
@ -26,17 +29,29 @@ public class Routen {
|
||||||
e.berechneRouten();
|
e.berechneRouten();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String[][] getRoute(String SB, String SE) {
|
// gibt alle möglichen Routen für eine Verbindung als zweidimensionales Array zurück
|
||||||
|
public static String[][] getRoute(String sb, String se) {
|
||||||
for (Ort ort : Ort.orte) {
|
for (Ort ort : Ort.orte) {
|
||||||
if (ort.id.matches(SB)) {
|
if (ort.id.matches(sb)) {
|
||||||
return ort.routen.get(SB.concat(SE));
|
return ort.routen.get(sb.concat(se));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// gibt maximale Frachtkapazität zwischen zwei Orten zurück
|
||||||
|
public static int holeStreckeGewicht(String sb, String se) {
|
||||||
|
return Math.min(Ort.orteMap.get(sb).frachtkapazitaet, Ort.orteMap.get(se).frachtkapazitaet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// gibt Frachtkosten (pro Kilo) zwischen zwei Orten zurück
|
||||||
|
public static double holeStreckePreis(String sb, String se) {
|
||||||
|
return Ort.orteMap.get(sb).frachtkosten + Ort.orteMap.get(se).frachtkosten;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Klasse für Orte
|
||||||
class Ort {
|
class Ort {
|
||||||
static ArrayList<Ort> orte = new ArrayList<>();
|
static ArrayList<Ort> orte = new ArrayList<>();
|
||||||
static HashMap<String, Ort> orteMap = new HashMap<>();
|
static HashMap<String, Ort> orteMap = new HashMap<>();
|
||||||
|
|
@ -47,6 +62,7 @@ class Ort {
|
||||||
HashMap<String, Ort> knotenMap;
|
HashMap<String, Ort> knotenMap;
|
||||||
HashMap<String, String[][]> routen;
|
HashMap<String, String[][]> routen;
|
||||||
|
|
||||||
|
// Konstruktor für Orte
|
||||||
public Ort(String id, int frachtkapazitaet, double frachtkosten) {
|
public Ort(String id, int frachtkapazitaet, double frachtkosten) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.frachtkapazitaet = frachtkapazitaet;
|
this.frachtkapazitaet = frachtkapazitaet;
|
||||||
|
|
@ -66,6 +82,7 @@ class Ort {
|
||||||
this.knotenMap = knotenMap;
|
this.knotenMap = knotenMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// berechnet alle möglichen Routen eines Ortes zu allen anderen Orten
|
||||||
void berechneRouten() {
|
void berechneRouten() {
|
||||||
HashMap<String, String[][]> routen = new HashMap<>();
|
HashMap<String, String[][]> routen = new HashMap<>();
|
||||||
Ort start = this;
|
Ort start = this;
|
||||||
|
|
@ -87,7 +104,7 @@ class Ort {
|
||||||
} else if (startKnoten.knoten.contains(ziel)) {
|
} else if (startKnoten.knoten.contains(ziel)) {
|
||||||
tabelle[i][j] = startKnoten.id;
|
tabelle[i][j] = startKnoten.id;
|
||||||
tabelle[i][j + 1] = ziel.id;
|
tabelle[i][j + 1] = ziel.id;
|
||||||
i++;
|
i++; // TODO: Bug beheben! Einige Routen fehlen, wenn die zweite Verzweigung mehr Knoten hat als die erste.
|
||||||
} else {
|
} else {
|
||||||
for (Ort folgeKnoten1 : startKnoten.knoten) {
|
for (Ort folgeKnoten1 : startKnoten.knoten) {
|
||||||
if (folgeKnoten1.knoten.contains(ziel)) {
|
if (folgeKnoten1.knoten.contains(ziel)) {
|
||||||
|
|
@ -136,7 +153,9 @@ class Ort {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println("-----------------------------------");
|
|
||||||
|
// Alle berechneten Routen ausgeben
|
||||||
|
/*System.out.println("-----------------------------------");
|
||||||
System.out.println("Route: " + start.id.concat(ziel.id));
|
System.out.println("Route: " + start.id.concat(ziel.id));
|
||||||
for (int x = 0; x < tabelle.length; x++) {
|
for (int x = 0; x < tabelle.length; x++) {
|
||||||
for (int y = 0; y < tabelle[x].length; y++) {
|
for (int y = 0; y < tabelle[x].length; y++) {
|
||||||
|
|
@ -144,7 +163,13 @@ class Ort {
|
||||||
System.out.print(tabelle[x][y] + " ");
|
System.out.print(tabelle[x][y] + " ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println();
|
System.out.println(); */
|
||||||
|
|
||||||
|
// unvollständige Routen entfernen
|
||||||
|
for (int k = 0; k < tabelle.length; k++) {
|
||||||
|
if (tabelle[k][1] == null) {
|
||||||
|
tabelle[k][0] = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue