Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Simulator {
  4.  
  5. public static final int TIME_STEPS = 900, SIMULATION_START_HOUR = 7, SIMULATION_END_HOUR = 22, MINUTES_PER_SIMULATION_TICK = (SIMULATION_END_HOUR - SIMULATION_START_HOUR) * 60 / TIME_STEPS;
  6. public static final int RIDE_COUNT = 100000;
  7.  
  8.  
  9. private List<Station> stations;
  10. private List<University> universities = new LinkedList<>();
  11. private List<School> schools = new LinkedList<>();
  12.  
  13. private List<Line> lines = new LinkedList<>();
  14. private List<Ride> rides = new LinkedList<>();
  15.  
  16. private HashMap<String, Statistic> lineSections = new HashMap<>();
  17. private static Random random = new Random();
  18.  
  19.  
  20. private boolean isSimulating = false;
  21. private final Object isSimulatingLock = new Object();
  22. /**
  23. * 0700-2200
  24. */
  25. public GregorianCalendar calendar = new GregorianCalendar();
  26.  
  27. /**
  28. * Generates random lines and adds them to the {@link Simulator#lines} list
  29. *
  30. * @param i Contains a set of stations and lines containing every station min. 1 time
  31. */
  32. public Simulator (Infrastructure i, int maxAmountOfLines) {
  33. this.stations = i.getStations();
  34. this.universities = i.getUniversities();
  35. this.stations.addAll(universities);
  36. this.schools = i.getSchools();
  37. this.stations.addAll(schools);
  38.  
  39. HashSet<Station> set = new HashSet<>();//set to keep track of all stations which have (not) been added to a line
  40.  
  41. List<Station> tempStations = new LinkedList<>();
  42.  
  43. for (int j = 0; j < maxAmountOfLines - 1; j++) {
  44. tempStations.clear();
  45. Collections.shuffle(stations);
  46. for (int k = 0; k < stations.size() / maxAmountOfLines + 2; k++) {
  47. tempStations.add(stations.get(k));
  48. set.add(stations.get(k));
  49. }
  50. Line line = new Line(tempStations, this);
  51. this.lines.add(line);
  52. }
  53. tempStations.clear();
  54.  
  55. for (Station s : this.stations) {
  56. if (! set.contains(s)) {
  57. tempStations.add(s);
  58. }
  59. }
  60.  
  61. for (Line line : lines) {
  62. tempStations.add(line.getStations().get(0));
  63. }
  64.  
  65. lines.add(new Line(tempStations, this));
  66.  
  67. generateLineSections();
  68.  
  69. Date d = new Date();
  70. d.setTime(0);
  71.  
  72. calendar.setTime(d);
  73. calendar.set(Calendar.HOUR_OF_DAY, SIMULATION_START_HOUR);
  74. }
  75.  
  76. public List<Ride> getRides () {
  77. return rides;
  78. }
  79.  
  80. public HashMap<String, Statistic> getLineSections () {
  81. return lineSections;
  82. }
  83.  
  84. /**
  85. * Generates {@link LineSection} for each {@link Station} pair which is connected through a {@link Line} in both directions
  86. */
  87. private void generateLineSections () {
  88. for (Line l : lines) {
  89. List<Station> stations = l.getStations();
  90. for (int i = 0; i < stations.size() - 1; i++) {
  91. LineSection ls = new LineSection(l, stations.get(i), stations.get(i + 1));
  92. lineSections.put(ls.getStringRepresentationForHashMap(), new Statistic(ls));
  93. }
  94. for (int i = stations.size() - 1; i > 0; i--) {
  95. LineSection ls = new LineSection(l, stations.get(i), stations.get(i - 1));
  96. lineSections.put(ls.getStringRepresentationForHashMap(), new Statistic(ls));
  97. }
  98.  
  99. }
  100. }
  101.  
  102. public Statistic getLineSectionStatistics (Line l, Station start, Station end) {
  103. return getLineSectionStatistics(new LineSection(l, start, end));
  104. }
  105.  
  106. public Statistic getLineSectionStatistics (LineSection l) {
  107. Statistic s = lineSections.get(l.getStringRepresentationForHashMap());
  108. if (s == null) {
  109. throw new IllegalArgumentException("No such (Line, Station, Station) combination exists! " + l.toString() + "\nRepresentation: " + l.getStringRepresentationForHashMap());
  110. } else {
  111. return s;
  112. }
  113. }
  114.  
  115. /**
  116. * generate an random {@link Ride}. The start and end stations will be different iff more than one stations exist
  117. */
  118. public void generateRide () {
  119.  
  120.  
  121. Ride r = Ride.getRandomRide(this, calendar);
  122. r.findRoute(lines);
  123. rides.add(r);
  124.  
  125. }
  126.  
  127. /**
  128. * returns a random, not null, {@link Station} from all stations (no universities, ...). It is guaranteed that iff nullableStation is not null, nullableStation will not be returned!
  129. *
  130. * @param nullableStation
  131. * @return
  132. */
  133. public Station getRandomStationExcluding (Station nullableStation) {
  134. Station toRet;
  135. int ind;
  136.  
  137. do {
  138. ind = random.nextInt(stations.size());
  139. toRet = stations.get(ind);
  140. } while (toRet.equals(nullableStation));
  141. return toRet;
  142. }
  143.  
  144. private void setIsSimulating (boolean val) {
  145. synchronized (isSimulatingLock) {
  146. isSimulating = val;
  147. }
  148. }
  149.  
  150. public void simulate () {
  151. setIsSimulating(true);
  152. LinkedList<Ride> toDelete = new LinkedList<>();
  153.  
  154. for (int i = 0; i < TIME_STEPS; i++) {
  155. for (int j = 0; j < RIDE_COUNT / TIME_STEPS; j++) {
  156. generateRide();
  157. }
  158. for (Ride r : rides) {
  159. if (! r.tick()) {
  160. toDelete.add(r);
  161. }
  162. }
  163. if (! toDelete.isEmpty()) {
  164. rides.removeAll(toDelete);
  165. toDelete.clear();
  166. }
  167. for (Statistic stat : lineSections.values()) {
  168. stat.resetCurrent();
  169. }
  170.  
  171. calendar.add(Calendar.MINUTE, MINUTES_PER_SIMULATION_TICK);
  172. }
  173. setIsSimulating(false);
  174. }
  175.  
  176. public boolean isSimulating () {
  177. synchronized (isSimulatingLock) {
  178. return isSimulating;
  179. }
  180. }
  181.  
  182. /**
  183. * prints the statistics of the simulation. Should not be called while the simulation is running!
  184. */
  185. public void printStatistics () {
  186. System.out.println("Statistics of the line sections:");
  187. for (Statistic stat : lineSections.values()) {
  188. System.out.println("\t" + stat);
  189. }
  190. System.out.println("Statistics of the stations:");
  191. for (Station s : stations) {
  192. System.out.println("\t" + s);
  193. }
  194. System.out.println("Statistics of the lines");
  195. for (Line l : lines) {
  196. System.out.println("\t" + l.getStatistics());
  197. }
  198. }
  199.  
  200. @Override
  201. public String toString () {
  202. return "Simulator{\n" +
  203. "stations=\n" + stations +
  204. ",\n lines=\n" + lines +
  205. ",\n lineSections=\n" + lineSections +
  206. '}';
  207. }
  208.  
  209. public Station getRandomUniversity () {
  210. return universities.get(random.nextInt(universities.size()));
  211. }
  212.  
  213. public Station getRandomSchool () {
  214. return schools.get(random.nextInt(schools.size()));
  215. }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement