def __init__(self, row_start, col_start, row_end, col_end, earliest_start, latest_finish): self.start = Location(row_start, col_start) self.end = Location(row_end, col_end) self.earliest_start = int(earliest_start) self.latest_finish = int(latest_finish) self.was_processed = False
def the_real_tester(self, vehicle, ride, bonus): # data before calculation takes place vehicle.print() ride.print() print("Bonus: {0}".format(bonus)) vehicle_position_before_ride = vehicle.position vehicle_step_time_before_ride = vehicle.step_time score = Solver.calculate_points(vehicle, ride, bonus) self.assertEqual(vehicle.position, ride.end, "The position of the vehicle is not at the end of the ride") self.assertEqual(ride.was_processed, True, "The ride should have been processed") step_time_total = vehicle_step_time_before_ride distance_to_car = Location.distance_calculator(vehicle_position_before_ride, ride.start) distance_to_car = ride.earliest_start if distance_to_car < ride.earliest_start else distance_to_car if distance_to_car < ride.earliest_start: step_time_total += ride.earliest_start else: step_time_total += distance_to_car distance_to_destination = Location.distance_calculator(ride.start, ride.end) step_time_total += distance_to_destination self.assertEqual(vehicle.step_time, step_time_total, "The step time is not the same as the calculated distance") if vehicle.step_time >= ride.latest_finish: self.assertEqual(score, 0, "Score should be 0 if the car was not taken to finish in time") else: # checking bonus expected_score = distance_to_destination if vehicle_step_time_before_ride + distance_to_car <= ride.earliest_start: expected_score += bonus self.assertEqual(expected_score, score, "The scores are not calculated properly!")
class Ride: def __init__(self, row_start, col_start, row_end, col_end, earliest_start, latest_finish): self.start = Location(row_start, col_start) self.end = Location(row_end, col_end) self.earliest_start = int(earliest_start) self.latest_finish = int(latest_finish) self.was_processed = False start = Location end = Location earliest_start = 0 latest_finish = 0 was_processed = False json_result = -1 # ride not taken def get_distance_to_finish(self): return Location.distance_calculator(self.start, self.end) def print(self): print("Start location: ") self.start.print() print("End location: ") self.end.print() #distance = self.get_distance() #print("Distance: {0}".format(distance)) print("Earliest start: {0}".format(self.earliest_start)) print("Latest finish: {0}".format(self.latest_finish))
def calculate_points(vehicle, ride, bonus): # if the ride was already processed, the output is not good if ride.was_processed: raise Exception("Output error: Attempting to take a ride already processed. Ride") # vehicle.print() # ride.print() ride_score = 0 ride.was_processed = True # getting distance until the ride distance_to_ride = Location.distance_calculator(vehicle.position, ride.start) vehicle.step_time += distance_to_ride print("Distance until ride: {0}".format(distance_to_ride)) # updating step time if we have to wait for earliest start, or give bonus if we arrive on time if vehicle.step_time < ride.earliest_start: print("Earliest start {0} < step time {1} , waiting for {2} duration".format( vehicle.step_time, ride.earliest_start, ride.earliest_start - vehicle.step_time)) vehicle.step_time = ride.earliest_start # if we arrived on time or if we waited for client for earliest start: still giving bonus if vehicle.step_time == ride.earliest_start: ride_score += bonus ride.json_result = 1000 print("Bonus received for this ride! + {0}".format(bonus)) else: ride.json_result = 1 # getting ride distance (from start to end) ride_distance = Location.distance_calculator(ride.start, ride.end) vehicle.step_time += ride_distance print("Moving {0} distance".format(ride_distance)) # no money / bonus if we go over the finish time if vehicle.step_time >= int(ride.latest_finish): ride_score = 0 ride.json_result = 0 print("Ride score set to 0, time {0} > finish {1}".format(vehicle.step_time, ride.latest_finish)) else: ride_score += ride_distance print("Received money ! {0}".format(ride_score)) # updating vehicle location to be in the finish position vehicle.position = ride.end return ride_score
def generate_vehicle(min=DEFAULT_MIN_VALUE, max=DEFAULT_MAX_VALUE): new_vehicle = Vehicle(1, 0) new_vehicle.position = Location(rnd(min, max), rnd(min, max)) return new_vehicle
def get_distance_to_finish(self): return Location.distance_calculator(self.start, self.end)
def __init__(self, index, rides): self.rides_indexes = rides self.index = index self.position = Location(0, 0)