def calculate_moving_costs(self, acceleration, deceleration, velocity): # Loading in the spatial lengths self.load_length_from_db() # Defining sum_time as a list sum_time = [] # Finding the time which it takes to achieve average speed acc_time = formulas.time_acc(velocity, 0, acceleration) # Now finding the distance it takes to accelerate acc_distance = formulas.dist_acc(acceleration, acc_time) # Finding the time in which it takes to stop dec_time = formulas.time_acc(0, velocity, deceleration) # Now finding the distance it takes to decelerate dec_distance = formulas.dist_acc(deceleration, dec_time) # Finding the remaining distance left for i in range(len(self.spatial_length)): if self.spatial_length[i] >= (acc_distance + dec_distance): distance = self.spatial_length[i] - acc_distance - dec_distance # Finding the time that the trains is driving at its average speed drive_time = distance / velocity # Summarizing all the times sum_time.append(acc_time + dec_time + drive_time) else: # Iterating to see what the absolute max speed can be reached and still being able to brake sum_time.append( self.__cost_acc_dec_times(acceleration, deceleration, self.spatial_length[i])) return sum_time
def __cost_acc_dec_times(acceleration, deceleration, spatial_length): # Iterating through velocities to find the maximum value for k in range(1, 10000): velocity = k * 0.025 # Finding the time which it takes to achieve average speed acc_time = formulas.time_acc(velocity, 0, acceleration) # Now finding the distance it takes to accelerate acc_distance = formulas.dist_acc(acceleration, acc_time) # Finding the time in which it takes to stop dec_time = formulas.time_acc(0, velocity, deceleration) # Now finding the distance it takes to decelerate dec_distance = formulas.dist_acc(deceleration, dec_time) # The braking distance has to be equal to the remaining distance after accelerating if dec_distance >= spatial_length - acc_distance - 1 and \ dec_distance <= spatial_length - acc_distance + 1: # Returning the time it takes to get from start to destination sum_time = acc_time + dec_time return sum_time