forked from rileyalankirk/food-truck-simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeighborhood.java
More file actions
executable file
·463 lines (403 loc) · 18 KB
/
Neighborhood.java
File metadata and controls
executable file
·463 lines (403 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package Simulation;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Neighborhood
{
static final int MAP_HEIGHT = 900;
static final int MAP_WIDTH = 900;
static final int MARKER_SIZE = 5;
static final int BLOCK_WIDTH = 45;
static final double RATIO_ADDRESSES_TO_MAP = MAP_WIDTH / 2000.0;
public static ArrayList<Address> instructions = new ArrayList<>();
public static double truckX = (900 - MARKER_SIZE) * RATIO_ADDRESSES_TO_MAP;
public static double truckY = (910 - MARKER_SIZE) * RATIO_ADDRESSES_TO_MAP;
public static Address currentTarget = null;
public static double targetX = 718.0;
public static double targetY = 722.0;
private static double routeLength = 0;
private static int truckFacing = 0; // direction truck is facing -- 0: any, 1: north, 2: east, 3: south, 4: west
private String[][] grid;
public Neighborhood()
{
grid = new String[201][201];
}
public void generateNeighborhood()
{
// Location of houses, represented as "o"; crossroads as "-"
for (int x = 0; x < 201; x++)
{
for (int y = 0; y < 201; y++)
{
if (x % 10 == 0)
{
if (y % 10 == 0)
grid[x][y] = "- ";
else
grid[x][y] = "o ";
}
if (x % 10 != 0)
{
if (y % 10 == 0)
grid[x][y] = "o ";
else
grid[x][y] = " ";
}
}
}
// Location of the distribution center, represented as "&"
grid[91][90] = "& ";
}
public void generateNeighborhood(String filename)
{
generateNeighborhood();
// Read file, create new address and add location to neighborhood map
try {
File file = new File(filename);
Scanner scanner = new Scanner(file);
while(scanner.hasNextLine())
{
String line = scanner.nextLine();
String address[] = line.split(" ");
int houseNum = Integer.parseInt(address[0]);
int streetNum = Integer.parseInt(address[2]);
int time = Integer.parseInt(address[3]);
add(new Address(houseNum,address[1].compareTo("East") == 0, streetNum, time));
}
}
catch (IOException e)
{
System.out.println("IOException encountered: " + e);
}
// Location of the distribution center, represented as "&"
grid[91][90] = "& ";
}
public void generateNeighborhood(PriorityQueue<Address> addresses)
{
generateNeighborhood();
// Add locations to neighborhood map
Iterator<Address> iterator = addresses.iterator();
while (iterator.hasNext())
add(iterator.next());
// Location of the distribution center, represented as "&"
grid[91][90] = "& ";
}
public void add(Address ad)
{
// Location of an address, represented as "x"
if (!ad.isDirection())
grid[ad.getHouseNum()/10][ad.getStreetNum()*10] = "x ";
else
grid[ad.getStreetNum()*10][ad.getHouseNum()/10] = "x ";
}
public void printNeighborhood()
{
// Print neighborhood
for (int x = 0; x < 201; x++) {
for (int y = 0; y < 201; y++)
System.out.print(grid[x][y]);
System.out.println();
}
}
public static void drawNeighborhood(PriorityQueue<Address> addresses)
{
JFrame map = new JFrame();
JPanel canvas = new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
// draw streets
for (int x = 0; x < 19; x++)
for (int y = 0; y < 19; y++)
g.drawRect(BLOCK_WIDTH * x, BLOCK_WIDTH * y, BLOCK_WIDTH, BLOCK_WIDTH);
// draw distribution center
g.setColor(Color.BLUE);
double x = Address.DISTRIBUTION_STREETNUM * 100.0;
double y = Address.DISTRIBUTION_HOUSENUM;
g.fillRect((int)(RATIO_ADDRESSES_TO_MAP*(x - MARKER_SIZE)), (int)(RATIO_ADDRESSES_TO_MAP*(y - MARKER_SIZE)), MARKER_SIZE, MARKER_SIZE);
// draw deliveries
g.setColor(Color.RED);
Iterator<Address> iterator = addresses.iterator();
while (iterator.hasNext())
{
Address address = iterator.next();
x = (address.isDirection()) ? address.getHouseNum() : address.getStreetNum() * 100;
y = (!address.isDirection()) ? address.getHouseNum() : address.getStreetNum() * 100;
g.fillOval((int)(RATIO_ADDRESSES_TO_MAP*(x - MARKER_SIZE)), (int)(RATIO_ADDRESSES_TO_MAP*(y - MARKER_SIZE)), MARKER_SIZE, MARKER_SIZE);
}
g.setColor(Color.BLACK);
g.fillRect((int)(truckX), (int)(truckY), MARKER_SIZE, MARKER_SIZE);
}
};
map.getContentPane().add(canvas);
map.repaint();
//Getting the first instruction from the list, if there is one there
if(!instructions.isEmpty()) {
currentTarget = instructions.get(0);
instructions.remove(0);
double x = (currentTarget.isDirection()) ? currentTarget.getHouseNum() : currentTarget.getStreetNum() * 100;
double y = (!currentTarget.isDirection()) ? currentTarget.getHouseNum() : currentTarget.getStreetNum() * 100;
targetX = (int)(RATIO_ADDRESSES_TO_MAP*(x - MARKER_SIZE));
targetY = (int)(RATIO_ADDRESSES_TO_MAP*(y - MARKER_SIZE));
System.out.println(currentTarget.toString() + " | " + targetX + ", " + targetY);
}
//Animation handling done here
Timer animationTimer = new Timer(1, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//If we have a target, continue
if(currentTarget != null) {
//If we are not aligned on the y-axis, move along that axis
if(Math.abs(targetY - truckY) > 0.1) {
truckY += Math.signum(targetY - truckY) * 0.1;
//If we are aligned on the y-axis, but are not aligned on the x-axis, mvoe along the x-axis
} else if(Math.abs(targetX - truckX) > 0.1) {
truckX += Math.signum(targetX - truckX) * 0.1;
//If we're at the destination, get a new address from the list of locations
} else if(!instructions.isEmpty()) {
currentTarget = instructions.get(0);
instructions.remove(0);
double x = (currentTarget.isDirection()) ? currentTarget.getHouseNum() : currentTarget.getStreetNum() * 100;
double y = (!currentTarget.isDirection()) ? currentTarget.getHouseNum() : currentTarget.getStreetNum() * 100;
targetX = (int)(RATIO_ADDRESSES_TO_MAP*(x - MARKER_SIZE));
targetY = (int)(RATIO_ADDRESSES_TO_MAP*(y - MARKER_SIZE));
// System.out.println(currentTarget.toString());
} else {
currentTarget = null;
}
map.repaint();
}
}
});
animationTimer.start();
map.setTitle("Neighborhood");
map.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
map.setSize(MAP_WIDTH - 44, MAP_HEIGHT - 10);
map.setResizable(false);
map.setLocationRelativeTo(null); // center the map
map.setVisible(true);
}
public static void drawNeighborhood(String filename)
{
drawNeighborhood(AddressIO.readAddresses(filename));
}
public double getRouteLength()
{
return routeLength;
}
public void addRoute(Collection<Address> addresses) throws UTurnException
{
// Reset the static routeLength class attribute
routeLength = 0;
// start at the distribution center at 10:00 AM
Address truckLocation = new Address(Address.DISTRIBUTION_HOUSENUM, Address.SOUTH, Address.DISTRIBUTION_STREETNUM, 1000);
Iterator<Address> iterator = addresses.iterator();
while (iterator.hasNext())
{
Address address = iterator.next();
int time = address.getTime();
while (!(address.equals(truckLocation)))
{
int x1 = (address.isDirection()) ? address.getHouseNum() : address.getStreetNum() * 100;
int y1 = (!address.isDirection()) ? address.getHouseNum() : address.getStreetNum() * 100;
int x2 = (truckLocation.isDirection()) ? truckLocation.getHouseNum() : truckLocation.getStreetNum() * 100;
int y2 = (!truckLocation.isDirection()) ? truckLocation.getHouseNum() : truckLocation.getStreetNum() * 100;
boolean sameDirection = address.isDirection() == truckLocation.isDirection(); // Same direction streets
boolean sameStreetNum = address.getStreetNum() == truckLocation.getStreetNum(); // Same street number
boolean truckOnSouth = x2 % 100 == 0; // The truck is on a southern street
boolean truckOnEast = y2 % 100 == 0; // The truck is on an eastern street
// The truck can get to the address in two or less moves (excluding the moves necessary to avoid U-turns)
if ((truckOnEast && truckOnSouth) || !sameDirection || sameStreetNum)
{
if (y1 != y2 && (!truckOnEast || truckOnSouth) && ((!truckOnSouth || sameStreetNum) || address.isDirection()))
{
Address handleUturn = handleUturn(x2, y2, x2, y1, x1, y1, time);
if (!handleUturn.equals(new Address(-10, -10, time)))
{
truckLocation = handleUturn;
continue;
}
truckLocation = new Address(x2, y1, time);
routeLength += Math.abs(y1 - y2);
}
else if (x1 != x2)
{
Address handleUturn = handleUturn(x2, y2, x1, y2, x1, y1, time);
if (!handleUturn.equals(new Address(-10, -10, time)))
{
truckLocation = handleUturn;
continue;
}
truckLocation = new Address(x1, y2, time);
routeLength += Math.abs(x1 - x2);
}
}
/*
* If the truck needs to make more than two moves, it must move to a corner first (excluding the moves
* necessary to avoid U-turns)
*/
else
{
if (truckOnEast)
{
int corner = x1 - (x1 % 100); // corner location
if (Math.abs(corner + 100 - x2) < Math.abs(corner - x2))
{
Address handleUturn = handleUturn(x2, y2, corner + 100, y2, x1, y1, time);
if (!handleUturn.equals(new Address(-10, -10, time)))
{
truckLocation = handleUturn;
continue;
}
truckLocation = new Address(corner + 100, y2, time);
routeLength += Math.abs(corner + 100 - x2);
}
else
{
Address handleUturn = handleUturn(x2, y2, corner, y2, x1, y1, time);
if (!handleUturn.equals(new Address(-10, -10, time)))
{
truckLocation = handleUturn;
continue;
}
truckLocation = new Address(corner, y2, time);
routeLength += Math.abs(corner - x2);
}
}
else
{
int corner = y1 - (y1 % 100); // corner location
if (Math.abs(corner + 100 - y2) < Math.abs(corner - y2))
{
Address handleUturn = handleUturn(x2, y2, x2, corner + 100, x1, y1, time);
if (!handleUturn.equals(new Address(-10, -10, time)))
{
truckLocation = handleUturn;
continue;
}
truckLocation = new Address(x2, corner + 100, time);
routeLength += Math.abs(corner + 100 - y2);
}
else
{
Address handleUturn = handleUturn(x2, y2, x2, corner, x1, y1, time);
if (!handleUturn.equals(new Address(-10, -10, time)))
{
truckLocation = handleUturn;
continue;
}
truckLocation = new Address(x2, corner, time);
routeLength += Math.abs(corner - y2);
}
}
}
instructions.add(truckLocation);
}
}
routeLength /= 100.0; // to increase accuracy of measurement, we divide after adding up all of the values
}
// Returns the new address of the truck if a U-turn had to be avoided, Address(-10, -10) otherwise
private Address handleUturn(int truckX, int truckY, int destX, int destY, int finalX, int finalY, int time) throws UTurnException
{
boolean horDelta = truckX != destX; // Has horizontal change
boolean verDelta = truckY != destY; // Has vertical change
// Handle bad arguments
if ((!horDelta && !verDelta) || (horDelta && verDelta)) throw new UTurnException("Bad arguments");
// Truck is in a position to go in any direction and thus does not need to avoid a U-turn
if (truckFacing == 0) return new Address(-10, -10, time);
// Moving north or south
if (!horDelta)
{
// Moving north
if (destY - truckY > 0)
{
// Truck is facing the same direction it is moving
if (truckFacing == 1) return new Address(-10, -10, time);
}
// Moving south
else
{
// Truck is facing the same direction it is moving
if (truckFacing == 3) return new Address(-10, -10, time);
}
}
// Moving east or west
else
{
// Moving east
if (destX - truckX < 0)
{
// Truck is facing the same direction it is moving
if (truckFacing == 2) return new Address(-10, -10, time);
}
// Moving west
else
{
// Truck is facing the same direction it is moving
if (truckFacing == 4) return new Address(-10, -10, time);
}
}
// Truck must avoid a U-turn
Address ret; // New address of truck after move
if (truckX % 100 == 0 && truckY % 100 == 0) // Truck is on a corner
{
if (truckFacing % 2 == 0 && horDelta)
{
// Move north or south a block, whichever is closer to the final destination
routeLength += 1;
ret = new Address(truckX, truckY + ((finalY < truckY) ? -100 : 100), time);
}
else if (truckFacing % 2 == 1 && verDelta)
{
// Move east or west a block, whichever is closer to the final destination
routeLength += 1;
ret = new Address(truckX + ((finalX < truckX) ? -100 : 100), truckY, time);
}
else
{
return new Address(-10, -10, time); // There is no U-turn to be accounted for
}
}
else // Truck will go to the nearest corner
{
if (truckFacing % 2 == 1) // Facing north or south
{
int corner = truckY - (truckY % 100); // nearest corner location
if (truckFacing == 3) corner += 100; // move south a block so the truck moves south
routeLength += Math.abs(corner - truckY);
ret = new Address(truckX, corner, time);
}
else // Facing east or west
{
int corner = truckX - (truckX % 100); // nearest corner location
if (truckFacing == 2) corner += 100; // move east a block so the truck moves east
routeLength += Math.abs(corner - truckX);
ret = new Address(corner, truckY, time);
}
}
instructions.add(ret);
return ret;
}
}
class UTurnException extends Exception
{
UTurnException(String message)
{
super(message);
}
UTurnException()
{
super();
}
}