class CustomState(PositionState): static_objects = [Lane(175, 550, 350, 100, angle=-180), Lane(175, 650, 350, 100, angle=-180), Lane(175, 750, 350, 100), Lane(175, 850, 350, 100), Lane(825, 550, 350, 100, angle=-180), Lane(825, 650, 350, 100, angle=-180), Lane(825, 750, 350, 100), Lane(825, 850, 350, 100), Street(500, 700, 300, 400), Lane(450, 250, 500, 100, angle=-90), Lane(550, 250, 500, 100, angle=90), Sidewalk(200, 475, 400, 50), Sidewalk(800, 475, 400, 50), Terrain(200, 225, 400, 450), Terrain(800, 225, 400, 450), Terrain(500, 950, 1000, 100)] def randomize(self): self.dynamic_objects = [] lane_objs = [obj for obj in self.static_objects if type(obj) == Lane] sidewalk_objs = [obj for obj in self.static_objects if type(obj) == Sidewalk] for i in range(3): car = random.choice(lane_objs).generate_car() if not any([car.collides(obj) for obj in self.static_objects + self.dynamic_objects]): self.dynamic_objects.append(car) for i in range(2): man = random.choice(sidewalk_objs).generate_man() if not any([man.collides(obj) for obj in self.static_objects + self.dynamic_objects]): self.dynamic_objects.append(man)
class SimpleTIntersectionState(PositionState): static_objects = [ Terrain(175, 175, 350, 350), Terrain(825, 175, 350, 350), Terrain(500, 825, 1000, 350), Lane(200, 450, 400, 100, angle=-180), Lane(200, 550, 400, 100), Lane(800, 450, 400, 100, angle=-180), Lane(800, 550, 400, 100), Lane(450, 200, 400, 100, angle=-90), Lane(550, 200, 400, 100, angle=90), Street(500, 500, 200, 200), Sidewalk(200, 375, 400, 50), Sidewalk(800, 375, 400, 50), Sidewalk(500, 625, 1000, 50), Sidewalk(375, 175, 50, 350), Sidewalk(625, 175, 50, 350), ] def __init__(self, ncars=4, nped=2): self.ncars = ncars self.nped = nped PositionState.__init__(self) self.randomize() def randomize(self): self.dynamic_objects = [] while len(self.dynamic_objects) < self.ncars: lane = [ Lane(200, 550, 400, 100), Lane(800, 450, 400, 100, angle=-180), Lane(450, 200, 400, 100, angle=-90), ][np.random.random_integers(0, 2)] car = lane.generate_car() car.vel = 0 if not any([ car.collides(obj) for obj in self.static_objects + self.dynamic_objects ]): self.dynamic_objects.append(car) while len(self.dynamic_objects) < self.ncars + self.nped: sidewalk = [ Sidewalk(200, 375, 400, 50), Sidewalk(200, 625, 400, 50), Sidewalk(800, 375, 400, 50), Sidewalk(800, 625, 400, 50), Sidewalk(375, 175, 50, 350), Sidewalk(625, 175, 50, 350), Sidewalk(375, 825, 50, 350), Sidewalk(625, 825, 50, 350), ][np.random.random_integers(0, 1)] man = sidewalk.generate_man() man.vel = 2 if not any([ man.collides(obj) for obj in self.static_objects + self.dynamic_objects ]): self.dynamic_objects.append(man)
def randomize(self): self.dynamic_objects = [] while len(self.dynamic_objects) < self.ncars: lane = [Lane(150, 550, 300, 100), Lane(850, 450, 300, 100, angle=-180), Lane(450, 150, 300, 100, angle=-90), Lane(550, 850, 300, 100, angle=90), Lane(150, 650, 300, 100), Lane(850, 350, 300, 100, angle=-180), Lane(350, 150, 300, 100, angle=-90), Lane(650, 850, 300, 100, angle=90), ][np.random.random_integers(0, 7)] car = lane.generate_car() car.vel = 0 if not any([car.collides(obj) for obj in self.static_objects+self.dynamic_objects]): self.dynamic_objects.append(car) while len(self.dynamic_objects) < self.ncars+self.nped: sidewalk = [Sidewalk(200, 375, 400, 50), Sidewalk(200, 625, 400, 50), Sidewalk(800, 375, 400, 50), Sidewalk(800, 625, 400, 50), Sidewalk(375, 175, 50, 350), Sidewalk(625, 175, 50, 350), Sidewalk(375, 825, 50, 350), Sidewalk(625, 825, 50, 350), ][np.random.random_integers(0, 1)] man = sidewalk.generate_man() man.vel = 2 if not any([man.collides(obj) for obj in self.static_objects+self.dynamic_objects]): self.dynamic_objects.append(man)
def randomize(self): """ Randomly generates car and pedestrian positions """ self.dynamic_objects = [] while len(self.dynamic_objects) < self.ncars: i = np.random.random_integers(0, 3) if len( self.dynamic_objects) else np.random.random_integers(0, 2) lane = [ Lane(200, 550, 400, 100), Lane(800, 450, 400, 100, angle=-180), Lane(450, 200, 400, 100, angle=-90), Lane(550, 800, 400, 100, angle=90) ][np.random.random_integers(0, 2)] car = lane.generate_car() car.vel = 0 if not any([ car.collides(obj) for obj in self.static_objects + self.dynamic_objects ]): self.dynamic_objects.append(car) while len(self.dynamic_objects) < self.ncars + self.nped: sidewalk = [ Sidewalk(200, 375, 400, 50), Sidewalk(200, 625, 400, 50), Sidewalk(800, 375, 400, 50, angle=-180), Sidewalk(800, 625, 400, 50, angle=-180), Sidewalk(375, 175, 350, 50, angle=-90), Sidewalk(625, 175, 350, 50, angle=-90), Sidewalk(375, 825, 350, 50, angle=90), Sidewalk(625, 825, 350, 50, angle=90) ][np.random.random_integers(0, 7)] man = sidewalk.generate_man() man.vel = 2 if not any([ man.collides(obj) for obj in self.static_objects + self.dynamic_objects ]): self.dynamic_objects.append(man) if self.traffic_lights: self.dynamic_objects.append(TrafficLight(600, 440, 0)) self.dynamic_objects.append(TrafficLight(400, 560, -180)) self.dynamic_objects.append( TrafficLight(560, 600, -90, initial_color="red")) self.dynamic_objects.append( TrafficLight(440, 400, 90, initial_color="red"))
class SimpleIntersectionState(PositionState): """ Instance of a :class:`PositionState` describing a four-way intersection Parameters ---------- ncars : int Number of cars to generate nped : int Number of pedestrians to generate traffic_lights : bool Whether or not to generate traffic lights """ static_objects = [ Terrain(175, 175, 350, 350), Terrain(825, 175, 350, 350), Terrain(175, 825, 350, 350), Terrain(825, 825, 350, 350), Lane(200, 450, 400, 100, angle=-180), Lane(200, 550, 400, 100), Lane(800, 450, 400, 100, angle=-180), Lane(800, 550, 400, 100), Lane(450, 200, 400, 100, angle=-90), Lane(550, 200, 400, 100, angle=90), Lane(450, 800, 400, 100, angle=-90), Lane(550, 800, 400, 100, angle=90), Street(500, 500, 200, 200), Sidewalk(200, 375, 400, 50), Sidewalk(200, 625, 400, 50), Sidewalk(800, 375, 400, 50, angle=-180), Sidewalk(800, 625, 400, 50, angle=-180), Sidewalk(375, 175, 350, 50, angle=-90), Sidewalk(625, 175, 350, 50, angle=-90), Sidewalk(375, 825, 350, 50, angle=90), Sidewalk(625, 825, 350, 50, angle=90) ] def __init__(self, ncars=4, nped=2, traffic_lights=False): self.ncars = ncars self.nped = nped self.traffic_lights = traffic_lights PositionState.__init__(self) self.randomize() def randomize(self): """ Randomly generates car and pedestrian positions """ self.dynamic_objects = [] while len(self.dynamic_objects) < self.ncars: i = np.random.random_integers(0, 3) if len( self.dynamic_objects) else np.random.random_integers(0, 2) lane = [ Lane(200, 550, 400, 100), Lane(800, 450, 400, 100, angle=-180), Lane(450, 200, 400, 100, angle=-90), Lane(550, 800, 400, 100, angle=90) ][np.random.random_integers(0, 2)] car = lane.generate_car() car.vel = 0 if not any([ car.collides(obj) for obj in self.static_objects + self.dynamic_objects ]): self.dynamic_objects.append(car) while len(self.dynamic_objects) < self.ncars + self.nped: sidewalk = [ Sidewalk(200, 375, 400, 50), Sidewalk(200, 625, 400, 50), Sidewalk(800, 375, 400, 50, angle=-180), Sidewalk(800, 625, 400, 50, angle=-180), Sidewalk(375, 175, 350, 50, angle=-90), Sidewalk(625, 175, 350, 50, angle=-90), Sidewalk(375, 825, 350, 50, angle=90), Sidewalk(625, 825, 350, 50, angle=90) ][np.random.random_integers(0, 7)] man = sidewalk.generate_man() man.vel = 2 if not any([ man.collides(obj) for obj in self.static_objects + self.dynamic_objects ]): self.dynamic_objects.append(man) if self.traffic_lights: self.dynamic_objects.append(TrafficLight(600, 440, 0)) self.dynamic_objects.append(TrafficLight(400, 560, -180)) self.dynamic_objects.append( TrafficLight(560, 600, -90, initial_color="red")) self.dynamic_objects.append( TrafficLight(440, 400, 90, initial_color="red"))
class WideIntersectionState(PositionState): static_objects = [ Terrain(125, 125, 250, 250), Terrain(875, 125, 250, 250), Terrain(125, 875, 250, 250), Terrain(875, 875, 250, 250), Lane(150, 450, 300, 100, angle=-np.pi), Lane(150, 550, 300, 100), Lane(850, 450, 300, 100, angle=-np.pi), Lane(850, 550, 300, 100), Lane(450, 150, 300, 100, angle=-(np.pi / 2)), Lane(550, 150, 300, 100, angle=(np.pi / 2)), Lane(450, 850, 300, 100, angle=-(np.pi / 2)), Lane(550, 850, 300, 100, angle=(np.pi / 2)), Lane(150, 350, 300, 100, angle=-np.pi), Lane(150, 650, 300, 100), Lane(850, 350, 300, 100, angle=-np.pi), Lane(850, 650, 300, 100), Lane(350, 150, 300, 100, angle=-(np.pi / 2)), Lane(650, 150, 300, 100, angle=(np.pi / 2)), Lane(350, 850, 300, 100, angle=-(np.pi / 2)), Lane(650, 850, 300, 100, angle=(np.pi / 2)), Street(500, 500, 400, 400), Sidewalk(150, 275, 300, 50), Sidewalk(150, 725, 300, 50), Sidewalk(850, 275, 300, 50), Sidewalk(850, 725, 300, 50), Sidewalk(275, 125, 50, 250), Sidewalk(725, 125, 50, 250), Sidewalk(275, 875, 50, 250), Sidewalk(725, 875, 50, 250), ] def randomize(self): self.dynamic_objects = [] while len(self.dynamic_objects) < self.ncars: lane = [ Lane(150, 550, 300, 100), Lane(850, 450, 300, 100, angle=-np.pi), Lane(450, 150, 300, 100, angle=-(np.pi / 2)), Lane(550, 850, 300, 100, angle=(np.pi / 2)), Lane(150, 650, 300, 100), Lane(850, 350, 300, 100, angle=-np.pi), Lane(350, 150, 300, 100, angle=-(np.pi / 2)), Lane(650, 850, 300, 100, angle=(np.pi / 2)), ][np.random.random_integers(0, 7)] car = lane.generate_car() car.vel = 0 if not any([ car.collides(obj) for obj in self.static_objects + self.dynamic_objects ]): self.dynamic_objects.append(car) while len(self.dynamic_objects) < self.ncars + self.nped: sidewalk = [ Sidewalk(200, 375, 400, 50), Sidewalk(200, 625, 400, 50), Sidewalk(800, 375, 400, 50), Sidewalk(800, 625, 400, 50), Sidewalk(375, 175, 50, 350), Sidewalk(625, 175, 50, 350), Sidewalk(375, 825, 50, 350), Sidewalk(625, 825, 50, 350), ][np.random.random_integers(0, 1)] man = sidewalk.generate_man() man.vel = 2 if not any([ man.collides(obj) for obj in self.static_objects + self.dynamic_objects ]): self.dynamic_objects.append(man) if self.traffic_lights: self.dynamic_objects.append(TrafficLight(700, 450, 0)) self.dynamic_objects.append(TrafficLight(700, 350, 0)) self.dynamic_objects.append(TrafficLight(300, 550, -np.pi)) self.dynamic_objects.append(TrafficLight(300, 650, -np.pi)) self.dynamic_objects.append( TrafficLight(550, 700, -(np.pi / 2), initial_color="red")) self.dynamic_objects.append( TrafficLight(650, 700, -(np.pi / 2), initial_color="red")) self.dynamic_objects.append( TrafficLight(450, 300, (np.pi / 2), initial_color="red")) self.dynamic_objects.append( TrafficLight(350, 300, (np.pi / 2), initial_color="red")) self.dynamic_objects.append( CrosswalkLight(275, 290, -np.pi / 2, initial_color="red", time_in_color=80)) self.dynamic_objects.append( CrosswalkLight(290, 275, 0, initial_color="white")) self.dynamic_objects.append( CrosswalkLight(710, 725, -np.pi, initial_color="white")) self.dynamic_objects.append( CrosswalkLight(725, 710, np.pi / 2, initial_color="red", time_in_color=80)) self.dynamic_objects.append( CrosswalkLight(275, 710, np.pi / 2, initial_color="red", time_in_color=80)) self.dynamic_objects.append( CrosswalkLight(290, 725, 0, initial_color="white")) self.dynamic_objects.append( CrosswalkLight(725, 290, -np.pi / 2, initial_color="red", time_in_color=80)) self.dynamic_objects.append( CrosswalkLight(710, 275, -np.pi, initial_color="white"))
class AngledIntersectionState(PositionState): static_objects = [ Terrain(0, 0, 0, 0, points=[(0, 0), (200, 0), (400, 400), (0, 400)]), Terrain(0, 0, 0, 0, points=[(200 + 400 / np.sqrt(3), 0), (1000, 0), (1000, 400), (400 + 400 / np.sqrt(3), 400)]), Terrain(0, 0, 0, 0, points=[(0, 600), (750 - 400 / np.sqrt(3), 600), (950 - 400 / np.sqrt(3), 1000), (0, 1000)]), Terrain(0, 0, 0, 0, points=[(750, 600), (1000, 600), (1000, 1000), (950, 1000)]), Lane(200, 450, 400, 100, angle=-np.pi), Lane(200, 550, 400, 100), Lane(875, 450, 250, 100, angle=-np.pi), Lane(875, 550, 250, 100), Lane(0, 0, 0, 0, angle=-np.radians(60), points=[(200, 0), (200 + 200 / np.sqrt(3), 0), (400 + 200 / np.sqrt(3), 400), (400, 400)]), Lane(0, 0, 0, 0, angle=np.radians(120), points=[(200 + 200 / np.sqrt(3), 0), (200 + 400 / np.sqrt(3), 0), (400 + 400 / np.sqrt(3), 400), (400 + 200 / np.sqrt(3), 400)]), Lane(0, 0, 0, 0, angle=-np.radians(60), points=[(750 - 400 / np.sqrt(3), 600), (750 - 200 / np.sqrt(3), 600), (950 - 200 / np.sqrt(3), 1000), (950 - 400 / np.sqrt(3), 1000)]), Lane(0, 0, 0, 0, angle=np.radians(120), points=[(750 - 200 / np.sqrt(3), 600), (750, 600), (950, 1000), (950 - 200 / np.sqrt(3), 1000)]), Street(575, 500, 350, 200), #Sidewalk(200, 400, 400, 50, angle=-np.pi), #Sidewalk(200, 650, 400, 50), #Sidewalk(875, 400, 250, 50, angle=-np.pi), Sidewalk(0, 0, 0, 0, angle=-np.radians(60), points=[(200 - 100 / np.sqrt(3), 0), (200, 0), (400, 400), (400 - 100 / np.sqrt(3), 400)]), Sidewalk(0, 0, 0, 0, angle=-np.pi, points=[(0, 350), (400 - 50 / np.sqrt(3), 400 - 50), (400, 400), (0, 400)]), Sidewalk(0, 0, 0, 0, angle=np.radians(120), points=[(200 + 400 / np.sqrt(3), 0), (200 + 500 / np.sqrt(3), 0), (400 + 500 / np.sqrt(3), 400), (400 + 400 / np.sqrt(3), 400)]), Sidewalk(0, 0, 0, 0, points=[(400 + 350 / np.sqrt(3), 400 - 50), (1000, 350), (1000, 400), (400 + 400 / np.sqrt(3), 400)]), Sidewalk(0, 0, 0, 0, angle=-np.radians(60), points=[(750 - 500 / np.sqrt(3), 600), (750 - 400 / np.sqrt(3), 600), (950 - 400 / np.sqrt(3), 1000), (950 - 500 / np.sqrt(3), 1000)]), Sidewalk(0, 0, 0, 0, points=[(0, 600), (750 - 400 / np.sqrt(3), 600), (750 - 350 / np.sqrt(3), 650), (0, 650)]), Sidewalk(0, 0, 0, 0, angle=np.radians(120), points=[(750, 600), (750 + 100 / np.sqrt(3), 600), (950 + 100 / np.sqrt(3), 1000), (950, 1000)]), Sidewalk(0, 0, 0, 0, angle=-np.pi, points=[(750, 600), (1000, 600), (1000, 650), (750 + 50 / np.sqrt(3), 650)]) ] def randomize(self): self.dynamic_objects = [] while len(self.dynamic_objects) < self.ncars: lane = [ Lane(200, 550, 400, 100), Lane(875, 450, 250, 100, angle=-np.pi), Lane(0, 0, 0, 0, angle=-np.radians(60), points=[(200, 0), (200 + 200 / np.sqrt(3), 0), (400 + 200 / np.sqrt(3), 400), (400, 400)]), Lane(0, 0, 0, 0, angle=np.radians(120), points=[(750 - 200 / np.sqrt(3), 600), (750, 600), (950, 1000), (950 - 200 / np.sqrt(3), 1000)]), ][np.random.random_integers(0, 3)] car = lane.generate_car() car.vel = 0 if not any([ car.collides(obj) for obj in self.static_objects + self.dynamic_objects ]): self.dynamic_objects.append(car) if self.traffic_lights: self.dynamic_objects.append(TrafficLight(750, 440, 0)) self.dynamic_objects.append(TrafficLight(400, 560, -np.pi)) self.dynamic_objects.append( TrafficLight(580, 600, -(np.pi / 2), initial_color="red")) self.dynamic_objects.append( TrafficLight(460, 400, (np.pi / 2), initial_color="red")) self.dynamic_objects.append( CrosswalkLight(365, 395, -np.pi / 2, initial_color="red", time_in_color=80)) self.dynamic_objects.append( CrosswalkLight(375, 375, 0, initial_color="white")) self.dynamic_objects.append( CrosswalkLight(770, 625, -np.pi, initial_color="white")) self.dynamic_objects.append( CrosswalkLight(790, 610, np.pi / 2, initial_color="red", time_in_color=80)) self.dynamic_objects.append( CrosswalkLight(495, 610, np.pi / 2, initial_color="red", time_in_color=80)) self.dynamic_objects.append( CrosswalkLight(520, 625, 0, initial_color="white")) self.dynamic_objects.append( CrosswalkLight(660, 395, -np.pi / 2, initial_color="red", time_in_color=80)) self.dynamic_objects.append( CrosswalkLight(625, 375, -np.pi, initial_color="white"))