-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulation.java
129 lines (102 loc) · 3.99 KB
/
Simulation.java
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
package a3;
import java.util.Random;
import java.util.List;
import java.util.ArrayList;
/*
* Simulation
* A ride simulator.
*
* This class simulates a random sequence of ride requests on
* a grid of a certain size. A number of random drivers with
* vehicles are created. A number of "hotspot" positions are
* randomly selected. Requests are generated from/to a hotspot
* to/from a random position on the grid. A Dispatcher object is
* employed to choose for each request which driver to use.
*
* The long value "seed" is used to initialize the random number
* generator used by the simulation. Using the same seed will
* result in the same sequence of random drivers, hotspots, and
* ride requests. This makes it possible to repeat a simulation
* and get the same results each time.
*
* getRideLog()
* Returns an array of CompletedRide objects representing the
* results of the simulation.
*/
public class Simulation {
/* Named constants for defaults */
private static final int DEFAULT_GRID_WIDTH = 100;
private static final int DEFAULT_GRID_HEIGHT = 100;
private static final int DEFAULT_HOTSPOT_COUNT = 10;
private static final int DEFAULT_DRIVER_COUNT = 10;
private int _gridWidth;
private int _gridHeight;
private Position[] _hotSpots;
private List<CompletedRide> _rideLog;
private Random _rng;
private Dispatcher _dispatcher;
public Simulation(long seed, int request_count, Dispatcher dispatcher) {
_gridWidth = DEFAULT_GRID_WIDTH;
_gridHeight = DEFAULT_GRID_HEIGHT;
_hotSpots = new Position[DEFAULT_HOTSPOT_COUNT];
_rideLog = new ArrayList<CompletedRide>();
_rng = new Random(seed);
_dispatcher = dispatcher;
for (int i=0; i<_hotSpots.length; i++) {
_hotSpots[i] = createRandomPosition();
}
Driver[] drivers = new Driver[DEFAULT_DRIVER_COUNT];
for (int i=0; i<drivers.length; i++) {
drivers[i] = createRandomDriver(i);
}
for (int r=0; r<request_count; r++) {
RideRequest request = createRandomRequest();
CompletedRide ride = request.complete(_dispatcher.chooseDriver(drivers, request));
_rideLog.add(ride);
}
}
public Simulation(long seed, int request_count, Dispatcher dispatcher,
int grid_width, int grid_height, int hotspot_count, int driver_count) {
_gridWidth = grid_width;
_gridHeight = grid_height;
_hotSpots = new Position[hotspot_count];
_rideLog = new ArrayList<CompletedRide>();
_rng = new Random(seed);
_dispatcher = dispatcher;
for (int i=0; i<_hotSpots.length; i++) {
_hotSpots[i] = createRandomPosition();
}
Driver[] drivers = new Driver[driver_count];
for (int i=0; i<drivers.length; i++) {
drivers[i] = createRandomDriver(i);
}
for (int r=0; r<request_count; r++) {
RideRequest request = createRandomRequest();
CompletedRide ride = request.complete(_dispatcher.chooseDriver(drivers, request));
_rideLog.add(ride);
}
}
public CompletedRide[] getRideLog() {
return _rideLog.toArray(new CompletedRide[_rideLog.size()]);
}
private Position createRandomPosition() {
return new PositionImpl(_rng.nextInt(_gridWidth), _rng.nextInt(_gridHeight));
}
private Driver createRandomDriver(int id) {
String[] first_names = {"Alice", "Bob", "Carol", "Daveon", "Ebony",
"Fatima", "Geraldo", "Hannah", "Ito", "Javier"};
String[] last_names = {"Smith", "Patel", "Kim", "Garcia", "Okafor"};
String first = first_names[_rng.nextInt(first_names.length)];
String last = last_names[_rng.nextInt(last_names.length)];
Vehicle vehicle = new VehicleImpl("Toyota", "Prius", "DRV-" + String.format("%03d", id), createRandomPosition());
return new DriverImpl(first, last, id, vehicle);
}
private RideRequest createRandomRequest() {
Position hotspot = _hotSpots[_rng.nextInt(_hotSpots.length)];
Position rand_pos = createRandomPosition();
// Half the time create a request to a hotspot,
// half the time create a request from a hotspot.
boolean to_hotspot = _rng.nextBoolean();
return (to_hotspot) ? new RideRequestImpl(rand_pos, hotspot) : new RideRequestImpl(hotspot, rand_pos);
}
}