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.

124 lines
3.6KB

  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. public class Track extends Observable {
  7. protected List<Vehicle> vehicles;
  8. protected List<List<VehicleTimeRecord>> vtrList;
  9. protected float trackLength;
  10. protected float sumAvgMemory;
  11. protected int historyLength;
  12. protected float overallAvg;
  13. protected float lastAvg;
  14. protected int waitTime;
  15. protected float maxVelocity;
  16. protected float brakeProb;
  17. public List<List<VehicleTimeRecord>> getVtrList() {
  18. return vtrList;
  19. }
  20. public float getOverallAvg() {
  21. return overallAvg;
  22. }
  23. public float getLastAvg() {
  24. return lastAvg;
  25. }
  26. public List<Vehicle> getVehicles() {
  27. return vehicles;
  28. }
  29. public float getTrackLength() {
  30. return trackLength;
  31. }
  32. public Track(int numberVehicles, float trackLength, float brakeProb, float maxVelocity) {
  33. this.trackLength = trackLength;
  34. this.brakeProb = brakeProb;
  35. this.maxVelocity = maxVelocity;
  36. this.vehicles = createVehiclesList(numberVehicles);
  37. this.vtrList = new LinkedList<>();
  38. this.sumAvgMemory = 0;
  39. this.overallAvg = 0;
  40. this.lastAvg = 0;
  41. this.historyLength = 25;
  42. }
  43. protected List<Vehicle> createVehiclesList(int numberVehicles){
  44. List<Vehicle> result = new ArrayList<>();
  45. for(int i=0;i<numberVehicles;i++){
  46. Vehicle vehicle = new Vehicle(i, i, this.maxVelocity, this.brakeProb, this.trackLength);
  47. result.add(vehicle);
  48. }
  49. return result;
  50. }
  51. public void setBrakeProb(float brakeProb) {
  52. for (Vehicle v : this.vehicles) {
  53. v.setBrakeProb(brakeProb);
  54. }
  55. }
  56. public void setMaxVelocity(float maxVelocity) {
  57. for (Vehicle v : this.vehicles) {
  58. v.setMaxVelocity(maxVelocity);
  59. }
  60. }
  61. public void timeElapse() {
  62. for(int i=0; i<vehicles.size();i++) {
  63. Vehicle v = vehicles.get(i);
  64. int forerunnerIndex = i + 1;
  65. if (forerunnerIndex >= vehicles.size()) {
  66. forerunnerIndex -= vehicles.size();
  67. }
  68. Vehicle forerunner = vehicles.get(forerunnerIndex);
  69. float distanceForerunner = forerunner.getPosition() - v.getPosition() - 1;
  70. if(distanceForerunner < 0.0){
  71. distanceForerunner += this.trackLength;
  72. }
  73. v.updateVelocity(distanceForerunner);
  74. }
  75. List <VehicleTimeRecord> records = new ArrayList<>(vehicles.size());
  76. this.vtrList.add(records);
  77. for(Vehicle v: vehicles){
  78. v.timeElapse();
  79. VehicleTimeRecord vtr = new VehicleTimeRecord(v.id, v.position, v.curVelocity, v.maxVelocity);
  80. records.add(vtr);
  81. }
  82. update_avg();
  83. this.setChanged();
  84. this.notifyObservers();
  85. this.clearChanged();
  86. try {
  87. Thread.sleep(waitTime);
  88. } catch (InterruptedException ex) { }
  89. }
  90. public float avg_step(int step){
  91. float sum_step = 0;
  92. for (VehicleTimeRecord r : vtrList.get(step)) {
  93. sum_step += r.velocity;
  94. }
  95. return sum_step / vehicles.size();
  96. }
  97. protected void update_avg(){
  98. lastAvg = avg_step(vtrList.size()-1);
  99. sumAvgMemory += lastAvg;
  100. overallAvg = sumAvgMemory / vtrList.size();
  101. }
  102. public float avg_span(int start, int end){
  103. float sum_span = 0;
  104. for (int i=start; i <end;i++){
  105. sum_span += avg_step(i);
  106. }
  107. return sum_span / (end-start+1);
  108. }
  109. }