def test_update_in_at_end(): road = Road([ [ [None, None, None], [None, None, None], [None, None, None] ], [ [None, None, Car(Directions.BOTTOM, TurnDirection.LEFT)], [None, None, None], [None, None, Car(Directions.BOTTOM, TurnDirection.LEFT)] ] ]) road.update_in(0) assert road.in_lanes[0][2] is not None assert isinstance(road.in_lanes[0][2], Car) assert road.in_lanes[0][2].waiting_time == 1 road.update_in(1) assert road.in_lanes[1][2] is None road.update_in(2) assert road.in_lanes[2][2] is not None assert isinstance(road.in_lanes[2][2], Car) assert road.in_lanes[2][2].waiting_time == 1 for _ in range(4): road.update_in(0) road.update_in(2) assert road.in_lanes[0][2].waiting_time == 5 assert road.in_lanes[2][2].waiting_time == 5
def test_update_in_two(): road = Road([ [ [None, None, None], [None, None, None], [None, None, None] ], [ [Car(Directions.BOTTOM, TurnDirection.LEFT), Car(Directions.BOTTOM, TurnDirection.LEFT), None], [None, None, None], [Car(Directions.BOTTOM, TurnDirection.LEFT), Car(Directions.BOTTOM, TurnDirection.LEFT), None] ] ]) road.update_in(0) assert road.in_lanes[0][1] is not None assert isinstance(road.in_lanes[0][1], Car) assert road.in_lanes[0][2] is not None assert isinstance(road.in_lanes[0][2], Car) road.update_in(1) assert road.in_lanes[1][1] is None assert road.in_lanes[1][2] is None road.update_in(2) assert road.in_lanes[2][1] is not None assert isinstance(road.in_lanes[2][1], Car) assert road.in_lanes[2][2] is not None assert isinstance(road.in_lanes[2][2], Car)
def test_pull_car(): intersection = get_intersection() # top for i in range(5): intersection.push_car(Directions.TOP, 5 + i, Car(Directions.LEFT, TurnDirection.LEFT)) # bottom for i in range(5): intersection.push_car(Directions.BOTTOM, 5 + i, Car(Directions.LEFT, TurnDirection.RIGHT)) # left for i in range(5): intersection.push_car(Directions.LEFT, 5 + i, Car(Directions.BOTTOM, TurnDirection.LEFT)) # right for i in range(5): intersection.push_car(Directions.RIGHT, 5 + i, Car(Directions.TOP, TurnDirection.LEFT)) for i in range(5): assert intersection.pull_car(Directions.LEFT, i) is not None assert intersection.pull_car(Directions.LEFT, i) is None assert intersection.pull_car(Directions.TOP, i) is not None assert intersection.pull_car(Directions.TOP, i) is None assert intersection.pull_car(Directions.BOTTOM, i) is not None assert intersection.pull_car(Directions.BOTTOM, i) is None assert intersection.pull_car(Directions.RIGHT, i) is not None assert intersection.pull_car(Directions.RIGHT, i) is None
def test_avoid_obstacle(self): road = self.twoLaneRoad car = Car(road, (89, 0), self.maxSpeed) road.placeObject(car) self.assertTrue(car.willingToChangeDown()) self.assertFalse(car.willingToChangeUp())
def test_avoid_obstacle(self): road = self.twoLaneRoad car = Car(road, (89, 0), self.maxSpeed) road.placeObject(car) self.assertTrue( car.willingToChangeDown() ) self.assertFalse( car.willingToChangeUp() )
def test_change_for_speed(self): road = self.twoLaneRoad car = Car(road, (50, 0)) road.placeObject(car) self.assertTrue( car.willingToChangeDown() ) self.assertFalse( car.willingToChangeUp() )
def test_dont_change_because_car_is_near(self): road = self.threeLaneRoad carBlocked1, carBlocking1 = Car(road, (50, 0)), Car(road, (50, 1)) road.placeObjects([carBlocked1, carBlocking1]) self.assertNotWillingToChange(carBlocked1) self.assertNotWillingToChange(carBlocking1)
def test_change_because_car_ahead(self): road = self.threeLaneRoad car1, car2 = Car(road, (0, 1), 5), Car(road, (2, 1), 5) road.placeObjects( [car1, car2] ) self.assertTrue( car1.willingToChangeUp() and car1.willingToChangeDown() ) self.assertNotWillingToChange(car2)
def test_change_for_speed(self): road = self.twoLaneRoad car = Car(road, (50, 0)) road.placeObject(car) self.assertTrue(car.willingToChangeDown()) self.assertFalse(car.willingToChangeUp())
def init(self): self.start_node = TraversableNode(node_id=1, lat=50.06238262767, long=19.92439693544) self.end_node = TraversableNode(node_id=2, lat=50.0622555, long=19.9237373) self.long_way = Way(1, self.start_node, self.end_node, lanes=1, intermediate_nodes=[]) self.long_way.distance = 100 self.way_dict = {1: self.long_way} self.car = Car(current_way=self.long_way, current_lane=0, directions_map={1: 1}, way_position=0, destination_node_id=123, v=0, v_max=10, acc=10) self.positioner = Positioner(self.way_dict)
def test_change_because_car_ahead(self): road = self.threeLaneRoad car1, car2 = Car(road, (0, 1), 5), Car(road, (2, 1), 5) road.placeObjects([car1, car2]) self.assertTrue(car1.willingToChangeUp() and car1.willingToChangeDown()) self.assertNotWillingToChange(car2)
def gt__pushCars(self, inflow, lanes): #if car pushed then return 1, if not then 0 #totalCars, avgSpeed = self.getAvgCarSpeed() lane = lanes.pop() avnum = self.gaussianprob(inflow) rvnum = inflow - avnum # print("RV: "+ str(rvnum)) for cars in range(1,avnum+1): car = Car(self, (0, lane), self.speedLimits.maxSpeed, 2) # print("AV num " + str(cars)) #First AV car 1 for carse in range(avnum+1,inflow+1): car = Car(self, (0, lane), self.speedLimits.maxSpeed, 1)
def setUp(self): speedLimits = SpeedLimits([ SpeedLimit(range=((25, 0), (30, 0)), limit=1, ticks=0), SpeedLimit.createObstacle((80, 2)) ], 5) self.road = Road(3, 100, speedLimits) self.freeCar = Car(self.road, (50, 0)) self.carFacingObstacle = Car(self.road, (79, 2)) self.car1 = Car(self.road, (75, 2)) self.road.placeObjects( [self.freeCar, self.carFacingObstacle, self.car1])
def test_placeObject(self): r = Road(3, 40, None) car1, car2 = Car(r, (20, 0)), Car(r, (30, 0)) self.assertTrue(r.placeObjects([car1, car2])) self.assertEqual(2, r.carCount()) self.assertEqual(9, r.distanceToNextThing(car1.pos)) self.assertTrue(r.distanceToNextThing(car2.pos) >= r.getLength()) car3 = Car(r, (21, 0)) self.assertTrue(r.placeObject(car3)) self.assertEqual(3, r.carCount()) self.assertEqual(0, r.distanceToNextThing(car1.pos)) self.assertEqual(8, r.distanceToNextThing(car3.pos))
def test_pull_no_car(): road = Road([ [ [None, None, None], [None, None, None], ], [ [Car(Directions.BOTTOM, TurnDirection.LEFT), Car(Directions.BOTTOM, TurnDirection.LEFT), None], [Car(Directions.BOTTOM, TurnDirection.LEFT), Car(Directions.BOTTOM, TurnDirection.LEFT), None], ] ]) assert road.pull_car(0) is None assert road.pull_car(1) is None
def setUp(self): random.seed(None) self.maxSpeed = 5 speedLimits = SpeedLimits( [SpeedLimit(range=((50, 0), (99, 0)), limit=2, ticks=0)], self.maxSpeed) road = self.road = Road(1, 100, speedLimits) self.car1 = Car(road, (0, 0)) self.carA = Car(road, (25, 0)) self.carB = Car(road, (26, 0)) self.carC = Car(road, (28, 0)) self.car2 = Car(road, (50, 0)) self.allCars = [self.car1, self.carA, self.carB, self.carC, self.car2] self.road.placeObjects(self.allCars)
def init(self): self.first_node_start = TraversableNode(node_id=1, lat=50.06238262767, long=19.92439693544) self.first_node_end = TraversableNode(node_id=2, lat=50.0622555, long=19.9237373) self.first_way = Way(1, self.first_node_start, self.first_node_end, lanes=1, intermediate_nodes=[]) self.first_node_start.add_outgoing_way(self.first_way) self.first_way.distance = 5 self.second_node_end = TraversableNode(node_id=3, lat=50.06238262767, long=19.92439693544) self.second_way = Way(2, self.first_node_end, self.second_node_end, lanes=1, intermediate_nodes=[]) self.first_node_end.add_outgoing_way(self.second_way) self.second_way.distance = 2 self.third_node_end = TraversableNode(node_id=4, lat=50.0622555, long=19.9237373) self.third_way = Way(3, self.second_node_end, self.third_node_end, lanes=1, intermediate_nodes=[]) self.second_node_end.add_outgoing_way(self.third_way) self.way_dict = { 1: self.first_way, 2: self.second_way, 3: self.third_way } self.directions_map = {1: 2, 2: 3, 3: 3} self.car = Car(current_way=self.first_way, current_lane=0, directions_map=self.directions_map, way_position=0, destination_node_id=123, v=0, v_max=10, acc=10) self.positioner = Positioner(self.way_dict)
def add_car(self, lane: "AbstractLane"): direction = 'upstream' if lane.upstream else 'downstream' if self.queues[direction][lane.index] > 0: self.queues[direction][lane.index] += 1 else: fwd = self.first_car_on_lane(lane) speed = lane.speed_limit if fwd: gap = fwd.s_path if gap < MIN_INSERTION_GAP: self.queues[direction][lane.index] += 1 return else: speed = IDM.calc_max_initial_speed(gap, fwd.speed, lane.speed_limit) car = Car.make_on_lane(road=self, lane=lane, speed=speed) self.cars.add(car) if lane.upstream: self.upstream_cars.add(car) else: self.downstream_cars.add(car)
def test_car_pull_push_in(): road = Road([[[None, None, None], [None, None, None], [None, None, None]], [[None, None, None], [None, None, None], [None, None, None]]]) road.push_car_in(0, Car(Directions.BOTTOM, TurnDirection.LEFT)) arr = np.array(road.in_lanes) assert len(arr[arr != np.array(None)]) == 1 assert arr[0][0] is not None and isinstance(arr[0][0], Car) for _ in range(10): road.push_car_in(0, Car(Directions.BOTTOM, TurnDirection.LEFT)) arr = np.array(road.in_lanes) assert len(arr[arr != np.array(None)]) == 1 assert arr[0][0] is not None and isinstance(arr[0][0], Car) road.push_car_in(1, Car(Directions.BOTTOM, TurnDirection.LEFT)) arr = np.array(road.in_lanes) assert len(arr[arr != np.array(None)]) == 2 assert arr[0][0] is not None and isinstance(arr[0][0], Car) and arr[1][0] is not None and isinstance(arr[1][0], Car)
def addCar(self): #doesn't seemed to have been used. Boolean Results! if self.lanes[0][0] == None: #true initially self.lanes[0][0] = Car( self, (0, 0) ) #initiates the identity car at with road being self, and position being (0,0) return True else: return False
def test_update_out_at_end(): road = Road([ [ [None, None, Car(Directions.BOTTOM, TurnDirection.LEFT)], [None, None, None], [None, None, Car(Directions.BOTTOM, TurnDirection.LEFT)] ], [ [None, None, None], [None, None, None], [None, None, None] ] ]) road.update_out() assert road.out_lanes[0][2] is None assert road.out_lanes[1][2] is None assert road.out_lanes[2][2] is None
def __pushCars(self, amount, lanes): if not amount or not lanes: return 0 else: lane = lanes.pop() car = Car(self, (0, lane), self.speedLimits.maxSpeed) if (self.placeObject(car)): return 1 + self.__pushCars(amount - 1, lanes) else: return self.__pushCars(amount, lanes)
def test_pull_car(): road = Road([ [ [None, None, None], [None, None, None], ], [ [None, None, Car(Directions.BOTTOM, TurnDirection.LEFT)], [None, None, Car(Directions.TOP, TurnDirection.LEFT)], ] ]) car1 = road.pull_car(1) car0 = road.pull_car(0) assert car0 is not None assert isinstance(car0, Car) assert car0.source == Directions.BOTTOM assert car1 is not None assert isinstance(car1, Car) assert car1.source == Directions.TOP
def __pushCars(self, amount, lanes): #if car pushed then return 1, if not then 0 -->HETERO if not amount or not lanes: return 0 else: lane = random.randint(0,2) #lane = lanes.pop() car = Car(self, (random.randint(0,self.getLength()-1), lane), self.speedLimits.maxSpeed, stackAcars.pop()) if(self.placeObject(car)): #if true --> object in desired position is not blocked return 1 + self.__pushCars(amount - 1, lanes) #recursive condition that counts number of cars enterring else: return self.__pushCars(amount, lanes)
def test_update_out_middle(): road = Road([ [ [None, Car(Directions.BOTTOM, TurnDirection.LEFT), None], [None, None, None], [None, Car(Directions.BOTTOM, TurnDirection.LEFT), None] ], [ [None, None, None], [None, None, None], [None, None, None] ] ]) road.update_out() assert road.out_lanes[0][2] is not None assert isinstance(road.out_lanes[0][2], Car) assert road.out_lanes[1][2] is None assert road.out_lanes[2][2] is not None assert isinstance(road.out_lanes[2][2], Car)
def __dpushCars(self, amount): #if car pushed then return 1, if not then 0 -->HETERO if not amount: return 0 else: for index in range(amount): lane = random.randint(0,2) car = Car(self, (random.randint(0,self.getLength()-1), lane), self.speedLimits.maxSpeed, self.assigntype()) if(self.placeObject(car)): #if true --> object in desired position is not blocked return 1 + self.__dpushCars(amount - 1) else: return 0 + self.__dpushCars( amount )
def test_tunneling(self): # obstacle speedLimits = SpeedLimits([SpeedLimit.createObstacle((10, 0))], 5) r = Road(1, 20, speedLimits) car = Car(r, (0, 0)) self.assertTrue(r.placeObject(car)) for x in range(100): r.update() self.assertEqual(0, car.velocity) self.assertEqual((9, 0), car.pos)
def test_multiple_bottom_forward_update(): intersection = get_intersection() for i in range(5): intersection.push_car(Directions.BOTTOM, i, Car(Directions.BOTTOM, TurnDirection.STRAIGHT)) for i in range(9): for q in range(9, 4, -1): assert intersection[9 - i][q] == 0 intersection.update() for q in range(9, 5, -1): assert intersection[0][q] == 0
def test_one_top_left_update(): intersection = get_intersection() intersection.push_car(Directions.TOP, 0, Car(Directions.TOP, TurnDirection.LEFT)) for q in range(10): assert intersection[q][0] == 3 intersection.update() assert intersection[9][0] != 1 for q in range(8): assert intersection[9][q + 1] == 3 intersection.update()
def test_multiple_top_forward_update(): intersection = get_intersection() for i in range(5): intersection.push_car(Directions.TOP, i, Car(Directions.TOP, TurnDirection.STRAIGHT)) for i in range(9): for j in range(5): assert intersection[i][j] == 2 intersection.update() for j in range(5): assert intersection[9][j] == 2
def test_multiple_left_forward_update(): intersection = get_intersection() for i in range(5): intersection.push_car(Directions.LEFT, i, Car(Directions.LEFT, TurnDirection.STRAIGHT)) for i in range(9): for j in range(5, 10): assert intersection[j][i] == 3 intersection.update() for j in range(5, 10): assert intersection[j][9] == 3
def test_multiple_right_forward_update(): intersection = get_intersection() for i in range(5): intersection.push_car(Directions.RIGHT, i, Car(Directions.RIGHT, TurnDirection.STRAIGHT)) for i in range(9, 0, -1): for j in range(5): assert intersection[j][i] == 1 intersection.update() for j in range(5): assert intersection[j][0] == 1
def t__pushCars(self, amount, lanes): #if car pushed then return 1, if not then 0 if not amount or not lanes: return 0 else: lane = lanes.pop() #decides which lane to select # print("lane popped " +str(lane)) car = Car(self, (0, lane), self.speedLimits.maxSpeed, self.assigntype()) #point of generation of object Car at a random lane if(self.placeObject(car)): #if true --> object is at starting position if car.vtype == 2: self.avcount += 1 return 1 + self.t__pushCars(amount - 1, lanes) #recursive condition that counts number of cars enterring else: return self.t__pushCars(amount, lanes)