def initalize_simulator(self): ''' Initializes the simulator ''' self.vis = uds.PyGameVisualizer((800, 800)) # Create a simple-intersection state, with no agents self.init_state = uds.state.SimpleIntersectionState( ncars=0, nped=0, traffic_lights=True) # Create the world environment initialized to the starting state # Specify the max time the environment will run to 500 # Randomize the environment when env._reset() is called # Specify what types of agents will control cars and traffic lights # Use ray for multiagent parallelism self.env = uds.UrbanDrivingEnv(init_state=self.init_state, visualizer=self.vis, max_time=500, randomize=False, agent_mappings={ Car: NullAgent, TrafficLight: TrafficLightAgent }, use_ray=False) self.env._reset(new_state=self.init_state)
def __init__(self, config_data={}, init_state=None, reward_fn=default_reward_function, randomize=False): self.reward_fn = reward_fn self.max_time = 500 self.observation_type = 'raw' if config_data: self.featurizer = Featurizer( config_data['agents']['state_space_config']) self.init_state = uds.state.PositionState(config_data) if config_data['environment']['visualize']: self.visualizer = uds.PyGameVisualizer(config_data, (800, 800)) else: self.visualizer = None self.max_time = config_data['environment']['max_time'] self.observation_type = config_data['agents']['state_space'] self.visualize_lidar = config_data['environment'][ 'visualize_lidar'] assert (self.observation_type in {'Q-LIDAR', 'raw', 'bmp'}) else: self.init_state = init_state self.visualizer = None self.lidar_points = {} self.randomize = randomize self.statics_rendered = False self.dynamic_collisions, self.static_collisions, self.last_col = [], [], -1 if (self.init_state): self._reset()
def run(): """ Main function to be run to test simple_path_agent with hard coded path. Examples -------- python3 examples/tree_search_train.py """ vis = uds.PyGameVisualizer((800, 800)) init_state = SimpleIntersectionState(ncars=3, nped=2) env = UrbanDrivingEnv(init_state=init_state, visualizer=vis, max_time=500, randomize=True, agent_mappings={Car:AccelAgent, Pedestrian:AccelAgent}, ) state = init_state env._render() agent = KeyboardAgent(agent_num=0) while (True): action = agent.eval_policy(state) state, reward, done, info_dict = env._step(action) env._render() if done: env._reset() state = env.get_state_copy()
def __init__(self, config_data=None, init_state=None, reward_fn=lambda state: 1 if len(state.get_collisions()[2]) == 0 else 0, randomize=False): self.reward_fn = reward_fn self.max_time = 500 self.observation_type = 'raw' if config_data: self.init_state = uds.state.PositionState(config_data) if config_data['environment']['visualize']: self.visualizer = uds.PyGameVisualizer((800, 800)) else: self.visualizer = None self.max_time = config_data['environment']['max_time'] self.observation_type = config_data['recorded_data']['state_space'] assert (self.observation_type in {'Q-LIDAR', 'raw', 'bmp'}) else: self.init_state = init_state self.visualizer = None self.featurizer = Featurizer() self.randomize = randomize self.statics_rendered = False self.dynamic_collisions, self.static_collisions, self.last_col = [], [], -1 if (self.init_state): self._reset()
def test_model(): """ Main function to be run to test imitation learner. Make sure that a model.model file exists in the root directory of the project. Examples -------- python3 examples/test_model.py """ accs = 0 totalticks = 0 start_time = time.time() vis = uds.PyGameVisualizer((800, 800)) init_state = uds.state.SimpleIntersectionState(ncars=2, nped=0) env = uds.UrbanDrivingEnv(init_state=init_state, visualizer=vis, agent_mappings={Car: AccelAgent}, max_time=200, randomize=True, use_ray=True) env._render() state = init_state agent = ModelAgent() reset_counter = 0 action = None while (True): action = agent.eval_policy(state) start_time = time.time() state, reward, done, info_dict = env._step(action) # TODO: fix this line to be consistent with changes in collect_data if info_dict["saved_actions"] == [(0, 1), (0, 1), (0, 1), (0, 1)]: reset_counter += 1 else: reset_counter = 0 totalticks += 1 env._render() if done or reset_counter > 10: reset_counter = -10 print("done") print((time.time() - start_time) / totalticks, totalticks) #accs += info_dict["predict_accuracy"] #print(accs/totalticks) env._reset() state = env.current_state
def f(): # Instantiate a PyGame Visualizer of size 800x800 vis = uds.PyGameVisualizer((800, 800)) # Create a simple-intersection state, with 4 cars, no pedestrians, and traffic lights init_state = uds.state.SimpleIntersectionState(ncars=1, nped=0, traffic_lights=True) # Create the world environment initialized to the starting state # Specify the max time the environment will run to 500 # Randomize the environment when env._reset() is called # Specify what types of agents will control cars and traffic lights # Use ray for multiagent parallelism env = uds.UrbanDrivingEnv(init_state=init_state, visualizer=vis, max_time=500, randomize=True, agent_mappings={ Car: NullAgent, TrafficLight: TrafficLightAgent }, use_ray=True) env._render() state = init_state # Car 0 will be controlled by our KeyboardAgent agent = KeyboardAgent() action = None # Simulation loop while (True): # Determine an action based on the current state. # For KeyboardAgent, this just gets keypresses action = agent.eval_policy(state) start_time = time.time() # Simulate the state state, reward, done, info_dict = env._step(action) env._render() # keep simulator running in spite of collisions or timing out done = False # If we crash, sleep for a moment, then reset if done: print("done") time.sleep(1) env._reset() state = env.current_state
import gym import gym_urbandriving as uds from gym_urbandriving.agents import KeyboardAgent, NullAgent from gym_urbandriving.state import SimpleIntersectionState from gym_urbandriving.assets import Car, Pedestrian from gym_urbandriving import UrbanDrivingEnv vis = uds.PyGameVisualizer((800, 800)) init_state = SimpleIntersectionState(ncars=3, nped=2) env = UrbanDrivingEnv(init_state=init_state, visualizer=vis, max_time=500, randomize=True, agent_mappings={ Car: NullAgent, Pedestrian: NullAgent }) env._render() state = init_state agent = KeyboardAgent(agent_num=0) while (True): action = agent.eval_policy(state) state, reward, done, info_dict = env._step(action) env._render() if done: env._reset() state = env.get_state_copy()
def run_and_collect(): """ Main function to be run to collect driving data. Make sure that a data folder exists in the root directory of the project! Examples -------- python3 examples/collect_data.py """ saved_states = [] saved_actions = [] vis = uds.PyGameVisualizer((800, 800)) init_state = uds.state.SimpleIntersectionState(ncars=2, nped=0) env = uds.UrbanDrivingEnv(init_state=init_state, visualizer=vis, agent_mappings={Car: AccelAgent}, max_time=200, randomize=True, use_ray=True) env._render() state = env.current_state agent = TreeSearchAgent() reset_counter = 0 action = None while (True): action = agent.eval_policy(deepcopy(state)) saved_states.append(vectorize_state(state)) start_time = time.time() state, reward, done, info_dict = env._step(action) saved_actions.append(info_dict["saved_actions"]) # TODO: fix this line, it used to be used to shorten demos by stopping the sim after enough cars came back up to speed. if early_stop_actions(info_dict["saved_actions"]): reset_counter += 1 else: reset_counter = 0 env._render(waypoints=agent.waypoints) if done or reset_counter > 5: # Time to save our current run and reset our env and our saved data reset_counter = 0 print("done") time.sleep(1) env._reset() state = env.current_state # reset agent state agent.waypoints = None agent.actions = None pickle.dump((saved_states, saved_actions), open("data/" + str(np.random.random()) + "dump.data", "wb+")) saved_states = [] saved_actions = []