You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

310 lines
7.7KB

  1. package de.hems.trafficsim;
  2. import java.util.ArrayList;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.Observable;
  6. import java.util.concurrent.Semaphore;
  7. /**
  8. * Main model class of TrafficSim. Represents a round course containing vehicles.
  9. */
  10. public class Track extends Observable {
  11. /**
  12. * list a vehicles on the track
  13. */
  14. protected List<Vehicle> vehicles;
  15. /**
  16. * list of resulting time records of the simulation
  17. */
  18. protected List<List<VehicleTimeRecord>> vtrList;
  19. /**
  20. * length of the track
  21. */
  22. protected float trackLength;
  23. /**
  24. * sum of all vehicle speeds during the simulation, used for average calculation
  25. */
  26. protected float sumAvgMemory;
  27. /**
  28. * sum of all vehicle speeds during the simulation ignoring the first ten steps,
  29. * used for average calculation
  30. */
  31. protected float sumDelAvgMemory;
  32. /**
  33. * length of the history kept
  34. */
  35. protected int historyLength;
  36. /**
  37. * average over all velocities in the simulation
  38. */
  39. protected float overallAvg;
  40. /**
  41. * average over all velocities in the simulation, ignoring the first ten steps
  42. */
  43. protected float delayedAvg;
  44. /**
  45. * average over all velocities in the last step
  46. */
  47. protected float lastAvg;
  48. /**
  49. * current configured wait time between two simulation steps
  50. */
  51. protected int waitTime;
  52. /**
  53. * currently configured max velocity for all vehicles
  54. */
  55. protected float maxVelocity;
  56. /**
  57. * currently configured brake probability for all vehicles
  58. */
  59. protected float brakeProb;
  60. /**
  61. * counter for executed simulation steps
  62. */
  63. protected long steps;
  64. /**
  65. * semaphore protecting the vtrlist
  66. */
  67. protected Semaphore listSemaphore;
  68. /**
  69. * Constructor for a new Track.
  70. *
  71. * @param numberVehicles number of vehicles on the Track
  72. * @param trackLength length of the new Track
  73. * @param brakeProb probability of a vehicle to suddenly brake without reason
  74. * @param maxVelocity maxmimum velocity of the vehicles
  75. * @param waitTime time between two simulation steps to slow down the simulation artificially
  76. * @param historyLength length of the history to keep
  77. */
  78. public Track(int numberVehicles, float trackLength, float brakeProb, float maxVelocity, int waitTime, int historyLength) {
  79. this.trackLength = trackLength;
  80. this.brakeProb = brakeProb;
  81. this.maxVelocity = maxVelocity;
  82. this.vehicles = createVehiclesList(numberVehicles);
  83. this.vtrList = new LinkedList<>();
  84. this.sumAvgMemory = 0;
  85. this.sumDelAvgMemory = 0;
  86. this.overallAvg = 0;
  87. this.delayedAvg = 0;
  88. this.lastAvg = 0;
  89. this.historyLength = historyLength;
  90. this.waitTime = waitTime;
  91. this.steps = 0;
  92. this.listSemaphore = new Semaphore(1);
  93. }
  94. /**
  95. * Getter for vtrList
  96. *
  97. * @return vtrList
  98. */
  99. public List<List<VehicleTimeRecord>> getVtrList() {
  100. return vtrList;
  101. }
  102. /**
  103. * Getter for overallAvg
  104. *
  105. * @return overallAvg
  106. */
  107. public float getOverallAvg() {
  108. return overallAvg;
  109. }
  110. /**
  111. * Getter for lastAvg
  112. *
  113. * @return lastAvg
  114. */
  115. public float getLastAvg() {
  116. return lastAvg;
  117. }
  118. /**
  119. * Getter for delayedAvg
  120. *
  121. * @return delayedAvg
  122. */
  123. public float getDelayedAvg() {
  124. return delayedAvg;
  125. }
  126. /**
  127. * Getter for vehicles
  128. *
  129. * @return vehicles
  130. */
  131. public List<Vehicle> getVehicles() {
  132. return vehicles;
  133. }
  134. /**
  135. * Getter for trackLength
  136. *
  137. * @return trackLength
  138. */
  139. public float getTrackLength() {
  140. return trackLength;
  141. }
  142. /**
  143. * Getter for steps
  144. *
  145. * @return steps
  146. */
  147. public long getSteps() {
  148. return steps;
  149. }
  150. /**
  151. * Getter for listSemaphore
  152. *
  153. * @return listSemaphore
  154. */
  155. public Semaphore getListSemaphore() {
  156. return listSemaphore;
  157. }
  158. /**
  159. * Getter for historyLength
  160. *
  161. * @return historyLength
  162. */
  163. public int getHistoryLength() {
  164. return historyLength;
  165. }
  166. /**
  167. * Utility function to add vehicles to the Track.
  168. *
  169. * @param numberVehicles number of vehicles to add
  170. * @return filled list with vehicles
  171. */
  172. protected List<Vehicle> createVehiclesList(int numberVehicles) {
  173. List<Vehicle> result = new ArrayList<>();
  174. for (int i = 0; i < numberVehicles; i++) {
  175. Vehicle vehicle = new Vehicle(i, i, this.maxVelocity, this.brakeProb, this.trackLength);
  176. result.add(vehicle);
  177. }
  178. return result;
  179. }
  180. /**
  181. * Update the wait time of the simulation.
  182. *
  183. * @param waitTime new wait time in ms
  184. */
  185. public void setWaitTime(int waitTime) {
  186. this.waitTime = waitTime;
  187. }
  188. /**
  189. * Update the brake probability of all vehicles.
  190. *
  191. * @param brakeProb new brake probability
  192. */
  193. public void setBrakeProb(float brakeProb) {
  194. for (Vehicle v : this.vehicles) {
  195. v.setBrakeProb(brakeProb);
  196. }
  197. }
  198. /**
  199. * Update the maximum velocity of all vehicles.
  200. *
  201. * @param maxVelocity new maximum velocity
  202. */
  203. public void setMaxVelocity(float maxVelocity) {
  204. for (Vehicle v : this.vehicles) {
  205. v.setMaxVelocity(maxVelocity);
  206. }
  207. }
  208. /**
  209. * Calculates on simulation step ahead and then waits for the configured wait time.
  210. */
  211. public void timeElapse() {
  212. for (int i = 0; i < vehicles.size(); i++) {
  213. Vehicle v = vehicles.get(i);
  214. int forerunnerIndex = i + 1;
  215. if (forerunnerIndex >= vehicles.size()) {
  216. forerunnerIndex -= vehicles.size();
  217. }
  218. Vehicle forerunner = vehicles.get(forerunnerIndex);
  219. float distanceForerunner = forerunner.getPosition() - v.getPosition() - 1;
  220. if (distanceForerunner < 0.0) {
  221. distanceForerunner += this.trackLength;
  222. }
  223. v.updateVelocity(distanceForerunner);
  224. }
  225. try {
  226. this.listSemaphore.acquire();
  227. } catch (InterruptedException ex) {return;}
  228. List <VehicleTimeRecord> records = new ArrayList<>(vehicles.size());
  229. this.vtrList.add(records);
  230. if (this.vtrList.size() > this.historyLength) {
  231. this.vtrList.remove(0);
  232. }
  233. for(Vehicle v: vehicles){
  234. v.timeElapse();
  235. VehicleTimeRecord vtr = new VehicleTimeRecord(v.id, v.position, v.curVelocity, v.maxVelocity);
  236. records.add(vtr);
  237. }
  238. steps++;
  239. this.listSemaphore.release();
  240. update_avg();
  241. try {
  242. Thread.sleep(waitTime);
  243. } catch (InterruptedException ex) {
  244. }
  245. }
  246. /**
  247. * Returns the average velocity of the given simulation step.
  248. *
  249. * @param step index of the step in the history list
  250. * @return average velocity
  251. */
  252. public float avg_step(int step) {
  253. float sum_step = 0;
  254. for (VehicleTimeRecord r : vtrList.get(step)) {
  255. sum_step += r.velocity;
  256. }
  257. return sum_step / vehicles.size();
  258. }
  259. /**
  260. * Utility function which updates the averages values with the results from the last simulation
  261. * step.
  262. */
  263. protected void update_avg() {
  264. lastAvg = avg_step(vtrList.size() - 1);
  265. if (this.steps > 10) {
  266. sumDelAvgMemory += lastAvg;
  267. delayedAvg = sumDelAvgMemory / (this.steps - 10);
  268. }
  269. sumAvgMemory += lastAvg;
  270. overallAvg = sumAvgMemory / this.steps;
  271. }
  272. }