Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Simulator {
- 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;
- public static final int RIDE_COUNT = 100000;
- private List<Station> stations;
- private List<University> universities = new LinkedList<>();
- private List<School> schools = new LinkedList<>();
- private List<Line> lines = new LinkedList<>();
- private List<Ride> rides = new LinkedList<>();
- private HashMap<String, Statistic> lineSections = new HashMap<>();
- private static Random random = new Random();
- private boolean isSimulating = false;
- private final Object isSimulatingLock = new Object();
- /**
- * 0700-2200
- */
- public GregorianCalendar calendar = new GregorianCalendar();
- /**
- * Generates random lines and adds them to the {@link Simulator#lines} list
- *
- * @param i Contains a set of stations and lines containing every station min. 1 time
- */
- public Simulator (Infrastructure i, int maxAmountOfLines) {
- this.stations = i.getStations();
- this.universities = i.getUniversities();
- this.stations.addAll(universities);
- this.schools = i.getSchools();
- this.stations.addAll(schools);
- HashSet<Station> set = new HashSet<>();//set to keep track of all stations which have (not) been added to a line
- List<Station> tempStations = new LinkedList<>();
- for (int j = 0; j < maxAmountOfLines - 1; j++) {
- tempStations.clear();
- Collections.shuffle(stations);
- for (int k = 0; k < stations.size() / maxAmountOfLines + 2; k++) {
- tempStations.add(stations.get(k));
- set.add(stations.get(k));
- }
- Line line = new Line(tempStations, this);
- this.lines.add(line);
- }
- tempStations.clear();
- for (Station s : this.stations) {
- if (! set.contains(s)) {
- tempStations.add(s);
- }
- }
- for (Line line : lines) {
- tempStations.add(line.getStations().get(0));
- }
- lines.add(new Line(tempStations, this));
- generateLineSections();
- Date d = new Date();
- d.setTime(0);
- calendar.setTime(d);
- calendar.set(Calendar.HOUR_OF_DAY, SIMULATION_START_HOUR);
- }
- public List<Ride> getRides () {
- return rides;
- }
- public HashMap<String, Statistic> getLineSections () {
- return lineSections;
- }
- /**
- * Generates {@link LineSection} for each {@link Station} pair which is connected through a {@link Line} in both directions
- */
- private void generateLineSections () {
- for (Line l : lines) {
- List<Station> stations = l.getStations();
- for (int i = 0; i < stations.size() - 1; i++) {
- LineSection ls = new LineSection(l, stations.get(i), stations.get(i + 1));
- lineSections.put(ls.getStringRepresentationForHashMap(), new Statistic(ls));
- }
- for (int i = stations.size() - 1; i > 0; i--) {
- LineSection ls = new LineSection(l, stations.get(i), stations.get(i - 1));
- lineSections.put(ls.getStringRepresentationForHashMap(), new Statistic(ls));
- }
- }
- }
- public Statistic getLineSectionStatistics (Line l, Station start, Station end) {
- return getLineSectionStatistics(new LineSection(l, start, end));
- }
- public Statistic getLineSectionStatistics (LineSection l) {
- Statistic s = lineSections.get(l.getStringRepresentationForHashMap());
- if (s == null) {
- throw new IllegalArgumentException("No such (Line, Station, Station) combination exists! " + l.toString() + "\nRepresentation: " + l.getStringRepresentationForHashMap());
- } else {
- return s;
- }
- }
- /**
- * generate an random {@link Ride}. The start and end stations will be different iff more than one stations exist
- */
- public void generateRide () {
- Ride r = Ride.getRandomRide(this, calendar);
- r.findRoute(lines);
- rides.add(r);
- }
- /**
- * 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!
- *
- * @param nullableStation
- * @return
- */
- public Station getRandomStationExcluding (Station nullableStation) {
- Station toRet;
- int ind;
- do {
- ind = random.nextInt(stations.size());
- toRet = stations.get(ind);
- } while (toRet.equals(nullableStation));
- return toRet;
- }
- private void setIsSimulating (boolean val) {
- synchronized (isSimulatingLock) {
- isSimulating = val;
- }
- }
- public void simulate () {
- setIsSimulating(true);
- LinkedList<Ride> toDelete = new LinkedList<>();
- for (int i = 0; i < TIME_STEPS; i++) {
- for (int j = 0; j < RIDE_COUNT / TIME_STEPS; j++) {
- generateRide();
- }
- for (Ride r : rides) {
- if (! r.tick()) {
- toDelete.add(r);
- }
- }
- if (! toDelete.isEmpty()) {
- rides.removeAll(toDelete);
- toDelete.clear();
- }
- for (Statistic stat : lineSections.values()) {
- stat.resetCurrent();
- }
- calendar.add(Calendar.MINUTE, MINUTES_PER_SIMULATION_TICK);
- }
- setIsSimulating(false);
- }
- public boolean isSimulating () {
- synchronized (isSimulatingLock) {
- return isSimulating;
- }
- }
- /**
- * prints the statistics of the simulation. Should not be called while the simulation is running!
- */
- public void printStatistics () {
- System.out.println("Statistics of the line sections:");
- for (Statistic stat : lineSections.values()) {
- System.out.println("\t" + stat);
- }
- System.out.println("Statistics of the stations:");
- for (Station s : stations) {
- System.out.println("\t" + s);
- }
- System.out.println("Statistics of the lines");
- for (Line l : lines) {
- System.out.println("\t" + l.getStatistics());
- }
- }
- @Override
- public String toString () {
- return "Simulator{\n" +
- "stations=\n" + stations +
- ",\n lines=\n" + lines +
- ",\n lineSections=\n" + lineSections +
- '}';
- }
- public Station getRandomUniversity () {
- return universities.get(random.nextInt(universities.size()));
- }
- public Station getRandomSchool () {
- return schools.get(random.nextInt(schools.size()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement