Skip to content

Commit 349a52d

Browse files
committed
2 parents 836ec0f + 021b10d commit 349a52d

File tree

2 files changed

+14
-202
lines changed

2 files changed

+14
-202
lines changed

src/Optimization/Dijkstra.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,26 @@ public static void relax(int[] current, graphNode[] graph, PriorityQueue pq, int
7171
if (graph[pos].getUp() != null && graph[pos].getVertical() == Directions.UP && indexInCompleted(completed, graph[pos].getUp().getIndex()) == -1) {
7272
int tempWeight = weight + (graph[pos].getY() - graph[pos].getUp().getY())*6/speedVertical;
7373
if (weightUse == 2)tempWeight = 0;
74-
pq.add(tempWeight, graph[pos].getUp().getIndex(), pos);
74+
if ((weightUse == 3 && speedVertical != 3) || weightUse != 3)
75+
pq.add(tempWeight, graph[pos].getUp().getIndex(), pos);
7576
}
7677
if (graph[pos].getRight() != null && graph[pos].getHorizontal() == Directions.RIGHT && indexInCompleted(completed, graph[pos].getRight().getIndex()) == -1) {
7778
int tempWeight = weight + (graph[pos].getRight().getX() - graph[pos].getX())*6/speedHorizontal;
7879
if (weightUse == 2)tempWeight = 0;
79-
pq.add(tempWeight, graph[pos].getRight().getIndex(), pos);
80+
if ((weightUse == 3 && speedHorizontal != 3) || weightUse != 3)
81+
pq.add(tempWeight, graph[pos].getRight().getIndex(), pos);
8082
}
8183
if (graph[pos].getDown() != null && graph[pos].getVertical() == Directions.DOWN && indexInCompleted(completed, graph[pos].getDown().getIndex()) == -1) {
8284
int tempWeight = weight + (graph[pos].getDown().getY() - graph[pos].getY())*6/speedVertical;
8385
if (weightUse == 2)tempWeight = 0;
84-
pq.add(tempWeight, graph[pos].getDown().getIndex(), pos);
86+
if ((weightUse == 3 && speedVertical != 3) || weightUse != 3)
87+
pq.add(tempWeight, graph[pos].getDown().getIndex(), pos);
8588
}
8689
if (graph[pos].getLeft() != null && graph[pos].getHorizontal() == Directions.LEFT && indexInCompleted(completed, graph[pos].getLeft().getIndex()) == -1) {
8790
int tempWeight = weight + (graph[pos].getX() - graph[pos].getLeft().getX())*6/speedHorizontal;
8891
if (weightUse == 2)tempWeight = 0;
89-
pq.add(tempWeight, graph[pos].getLeft().getIndex(), pos);
92+
if ((weightUse == 3 && speedHorizontal != 3) || weightUse != 3)
93+
pq.add(tempWeight, graph[pos].getLeft().getIndex(), pos);
9094
}
9195
}
9296
public static int indexInCompleted(int[][] completed, int item)
@@ -106,13 +110,6 @@ public static int[][] completedItem(int[][] completed, int[] current) {
106110
temp[1][completed[0].length] = current[2];
107111
return temp;
108112
}
109-
110-
// public static boolean finalDestOneDirection(int start, int end) {
111-
// if (start == end || start + 1 == end || start - 1 == end)
112-
// return true;
113-
// return false;
114-
// }
115-
116113
// function to add one direction to the list at a time
117114
public static void moveInDirection(graphNode a, graphNode b, ArrayList<Integer> path) {
118115
if (a.getX() == b.getX()) {

src/Optimization/PathGenerator.java

Lines changed: 6 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -14,197 +14,12 @@ public static ArrayList<Integer> minimizeTime(Map map, Car car){
1414
return Dijkstra.findPath(map, car, 0);
1515
}
1616
public static ArrayList<Integer> minimizeDistance(Map map, Car car){
17-
return Dijkstra.findPath(map, car, 1);
17+
return Dijkstra.findPath(map, car, 1);
1818
}
19-
20-
2119
public static ArrayList<Integer> base(Map map, Car car){
22-
return Dijkstra.findPath(map, car, 2);
23-
24-
25-
}
26-
27-
//this is meant to find a path, by heading until it reaches the correct
28-
//lattitude or longitude.
29-
//only works if the map is a grid with no anomalies.
30-
//a better way to implement this would be to use dijkstra ignoring speed.
31-
//it is probably possible to adapt this to road anomalies, will atempt.
32-
public ArrayList<Integer> baseline(Map map, Car car) {
33-
ArrayList<Integer> path = new ArrayList<Integer>();
34-
int[] position = new int[2];
35-
position[0] = car.getXPos();
36-
position[1] = car.getYPos();
37-
int distX = car.getDestX() - position[0];
38-
int distY = car.getDestY() - position[1];
39-
int dir = car.getDir();
40-
// checks if car is moving horizontally or vertically to start
41-
// then moves the car on that axis towards the destination
42-
while (!(finalXDest(position[0], car.getDestX()) && finalYDest(
43-
position[1], car.getDestY()))) {
44-
if (dir == Directions.LEFT || dir == Directions.RIGHT)
45-
PathGenerator.travelHorizontally(path, car, position, dir);
46-
else if (dir == Directions.UP || dir == Directions.DOWN)
47-
PathGenerator.travelVertically(path, car, position, dir);
48-
49-
if (!(finalXDest(position[0], car.getDestX())
50-
&& finalYDest(position[1], car.getDestY()))) {
51-
// determine if the destination is to the left or right and turns
52-
// that way
53-
if ((distY > 0 && dir == Directions.LEFT)
54-
|| (distY < 0 && dir == Directions.RIGHT))
55-
turnLeft(path, position, dir);
56-
else if ((distY < 0 && dir == Directions.LEFT)
57-
|| (distY > 0 && dir == Directions.RIGHT))
58-
turnRight(path, position, dir);
59-
else if (distX > 0 && dir == Directions.UP || distX < 0
60-
&& dir == Directions.DOWN)
61-
turnRight(path, position, dir);
62-
else if (distX < 0 && dir == Directions.UP || distX > 0
63-
&& dir == Directions.DOWN)
64-
turnLeft(path, position, dir);
65-
}
66-
dir = path.get(path.size() - 1);
67-
}
68-
return path;
69-
}
70-
71-
public static void travelHorizontally(ArrayList<Integer> path, Car car,
72-
int[] position, int dir) {
73-
int distX = car.getDestX() - position[0];
74-
// used to differentiate between moving in negative X or positive X
75-
// direction
76-
short inverter = -1;
77-
if (dir == Directions.RIGHT)
78-
inverter = 1;
79-
boolean cont = true;
80-
// moves the car horizontally until the last turn before destination X
81-
// value
82-
for (int i = 0; cont; i++) {
83-
// checks whether a light is the last one before destination X
84-
if (car.getNextTile(position[0], position[1], dir) instanceof TrafficLight) {
85-
cont = false;
86-
for (int j = 0; i + j > Math.abs(distX) && !cont; j++) {
87-
// checks each square after the turn to see if it's the
88-
// destination or a turn preceding the destination
89-
if ((car.getNextTile(position[0] + inverter * j, position[1],
90-
dir) instanceof TrafficLight)
91-
|| (finalXDest(position[0] + inverter * j, car.getDestX()) && finalYDest(
92-
position[1], car.getDestY())))
93-
cont = true;
94-
}
95-
}
96-
if (finalXDest(position[0], car.getDestX())
97-
&& finalYDest(position[1], car.getDestY()))
98-
cont = false;
99-
if (cont) {
100-
path.add(dir);
101-
position[0] += inverter;
102-
}
103-
}
104-
105-
}
106-
107-
// same idea as the horizontal class just modifying the y direction
108-
public static void travelVertically(ArrayList<Integer> path, Car car,
109-
int[] position, int dir) {
110-
int distY = car.getDestY() - position[1];
111-
short inverter = -1;
112-
if (dir == Directions.UP)
113-
inverter = 1;
114-
boolean cont = true;
115-
for (int i = 0; cont; i++) {
116-
if (car.getNextTile(position[0], position[1], dir) instanceof TrafficLight) {
117-
cont = false;
118-
for (int j = 0; i + j > Math.abs(distY) && !cont; j++) {
119-
if ((car.getNextTile(position[0], position[1] + inverter * j,
120-
dir) instanceof TrafficLight)
121-
|| (finalYDest(position[1] + inverter * j, car.getDestY()) && finalXDest(
122-
position[0], car.getDestX())))
123-
cont = true;
124-
}
125-
}
126-
if (finalXDest(position[0], car.getDestX())
127-
&& finalYDest(position[1], car.getDestY()))
128-
cont = false;
129-
if (cont) {
130-
path.add(dir);
131-
position[1] += inverter;
132-
}
133-
}
134-
135-
}
136-
137-
// checks if the current x is the destination or is on the other side of the
138-
// street (or on the side of the road...)
139-
public static Boolean finalXDest(int xPos, int destX) {
140-
if (xPos == destX || xPos + 1 == destX || xPos - 1 == destX)
141-
return true;
142-
return false;
143-
}
144-
145-
// checks if the current y is the destination or is on the other side of the
146-
// street (or on the side of the road...)
147-
public static Boolean finalYDest(int yPos, int destY) {
148-
if (yPos == destY || yPos + 1 == destY || yPos - 1 == destY)
149-
return true;
150-
return false;
151-
}
152-
153-
public static void turnRight(ArrayList<Integer> path, int[] position,
154-
int currentDir) {
155-
if (currentDir == 0) {
156-
position[0] = position[0] + 1;
157-
position[1] = position[1] - 1;
158-
path.add(0);
159-
path.add(1);
160-
} else if (currentDir == 1) {
161-
position[0] = position[0] + 1;
162-
position[1] = position[1] + 1;
163-
path.add(1);
164-
path.add(2);
165-
} else if (currentDir == 2) {
166-
position[0] = position[0] - 1;
167-
position[1] = position[1] + 1;
168-
path.add(2);
169-
path.add(3);
170-
} else {
171-
position[0] = position[0] - 1;
172-
position[1] = position[1] - 1;
173-
path.add(3);
174-
path.add(0);
175-
}
176-
}
177-
178-
public static void turnLeft(ArrayList<Integer> path, int[] position,
179-
int currentDir) {
180-
if (currentDir == 0) {
181-
position[0] = position[0] - 2;
182-
position[1] = position[1] - 2;
183-
path.add(0);
184-
path.add(0);
185-
path.add(3);
186-
path.add(3);
187-
} else if (currentDir == 1) {
188-
position[0] = position[0] + 2;
189-
position[1] = position[1] - 2;
190-
path.add(1);
191-
path.add(1);
192-
path.add(0);
193-
path.add(0);
194-
} else if (currentDir == 2) {
195-
position[0] = position[0] + 2;
196-
position[1] = position[1] + 2;
197-
path.add(2);
198-
path.add(2);
199-
path.add(1);
200-
path.add(1);
201-
} else {
202-
position[0] = position[0] - 2;
203-
position[1] = position[1] + 2;
204-
path.add(3);
205-
path.add(3);
206-
path.add(2);
207-
path.add(2);
208-
}
20+
return Dijkstra.findPath(map, car, 2);
20921
}
210-
}
22+
public static ArrayList<Integer> avoidHighways(Map map, Car car){
23+
return Dijkstra.findPath(map, car, 3);
24+
}
25+
}

0 commit comments

Comments
 (0)