Skip to content

Commit

Permalink
bugfix timeoveflow
Browse files Browse the repository at this point in the history
  • Loading branch information
JMVCoelho committed May 21, 2020
1 parent 714c544 commit 05dc306
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 34 deletions.
58 changes: 30 additions & 28 deletions src/Environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ def __init__(self):
Line('blue', 2, self.reporter, self.gui),
Line('green', 2, self.reporter, self.gui)]
self.orchestrator = Orchestrator(self.lines)
self.start_day()

def start_day(self):
self.time = datetime.time(6,15)

#TIME MEASURE
def tik(self):
self.time = (datetime.datetime.combine(datetime.date.min, self.time) + datetime.timedelta(minutes = 1)).time()
if self.time == datetime.time(0,1):
Expand All @@ -38,6 +37,29 @@ def tik(self):
#may be todo: dont generate more trains at 0h01, dont generate more people, but let the remaining trains finish the trip
#people not picked up by the final trip are then deleted


#EVENTS
def move_trains(self):
for line in self.lines:
passengers_to_exchange = line.move_trains(self.time)
if passengers_to_exchange != {}:
self.change_passengers_line(passengers_to_exchange)

def generate_people(self):
for line in self.lines:
self.populate_stations(line)

def reset_passangers_and_trains(self):
for line in self.lines:
line.new_day()

def update_lines(self, decisions):
for line in self.lines:
line.update_line_info(decisions[line.color])

#AUXILIAR

#not being used?
def add_change_passengers_to_line(self, current_station, line, passengers_to_exchange):
for station in line.get_stations():
print(station.get_name(), current_station.get_name())
Expand Down Expand Up @@ -77,36 +99,16 @@ def change_passengers_line(self, passengers_to_exchange):
person.update_way(line, station)
self.add_person_to_station(station, line, person)

def move_trains(self):
for line in self.lines:
passengers_to_exchange = line.move_trains(self.time)
if passengers_to_exchange != {}:
self.change_passengers_line(passengers_to_exchange)

def generate_people(self):
for line in self.lines:
self.populate_stations(line)


def reset_passangers_and_trains(self):
for line in self.lines:
line.new_day()

# receives a Line object
def populate_stations(self, line):
stations_distribution = estimate_number_of_people_per_station(line, self.time.hour, self.time.minute)
stations_distribution = estimate_number_of_people_per_station(line, self.time)
#{oriente: 2, encarnacao: 3, .... }
for station in stations_distribution:
for number_of_persons in range(stations_distribution[station]):
final, way = estimate_final_station(station, self.time.hour, self.time.minute)
final, way = estimate_final_station(station, self.time)
person = Person(station, final, self.time, way)
line.add_person_to_station(person, station)


def update_lines(self, decisions):
for line in self.lines:
line.update_line_info(decisions[line.color])

def run(self):
try:
while True:
Expand All @@ -130,14 +132,14 @@ def run(self):
pid = 0
forecaster = Forecasting()

def estimate_final_station(station, hours, minutes):
final, way = forecaster.predict_final_station(station, hours, minutes)
def estimate_final_station(station, time):
final, way = forecaster.predict_final_station(station, time)
return final, way

def estimate_number_of_people_per_station(line, hours, minutes):
def estimate_number_of_people_per_station(line, time):
estimative = dict()
for station in line.stations:
estimative[station.name] = forecaster.predict_number_of_people(station.name, hours, minutes)
estimative[station.name] = forecaster.predict_number_of_people(station.name, time)
return estimative

def get_unique_id():
Expand Down
12 changes: 6 additions & 6 deletions src/Forecasting.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ def __init__(self):
self.model_number_of_people = create_model_number_of_people(self.files_path_count)
self.model_final_station = create_model_final_station(self.files_path_exit)

def predict_number_of_people(self, station, hour, minutes):
def predict_number_of_people(self, station, p_time):
global date_values

time = get_closest_15_min_time(hour, minutes)
time = get_closest_15_min_time(p_time)
time_index = date_values[time]

base_number = int(round(self.model_number_of_people[mapping[station]][0][time_index]))
deviation = int(round(self.model_number_of_people[mapping[station]][1][time_index]))

return round(random.randint(max(0, base_number-deviation), base_number+deviation)/15)

def predict_final_station(self, station, hour, minutes):
time = get_closest_15_min_time(hour, minutes)
def predict_final_station(self, station, p_time):
time = get_closest_15_min_time(p_time)

stations = self.model_final_station[mapping[station]][time][0]
probabilities = self.model_final_station[mapping[station]][time][1]
Expand Down Expand Up @@ -172,8 +172,8 @@ def create_model_final_station(files_path):
return result


def get_closest_15_min_time(hour, minute):
date_time = datetime.datetime(1, 1, 1, hour, minute)
def get_closest_15_min_time(time):
date_time = datetime.datetime.combine(datetime.date.min, time)
delta = datetime.timedelta(minutes=15)
ceil = date_time + (datetime.datetime.min - date_time) % delta
return str(ceil.time())
Expand Down

0 comments on commit 05dc306

Please sign in to comment.