def __init__(self, agent_num=0, target_loc=[450, 900], collect_radius=15, vis=None): self.agent_num = agent_num self.target_loc = target_loc self.waypoints = None self.actions = None self.current_action_index = 0 self.collect_radius = collect_radius self.vis = vis self.action_space = [(0, 1), (3, 0), (-3, 0)] def reward_function(state, dest, wayp): pos = state.dynamic_objects[0].get_pos() distance = np.linalg.norm(pos - dest) wayp = sum([max(-50, -np.linalg.norm(pos - w)) for w in wayp]) log_coll = np.log(state.min_dist_to_coll(self.agent_num)) return -distance + 25 * wayp + 100 * log_coll self.reward_fn = reward_function from gym_urbandriving import UrbanDrivingEnv self.planning_env = uds.UrbanDrivingEnv( init_state=None, visualizer=vis, agent_mappings={Car: AccelAgent}) self.planning_threshold = 500 # If solution isn't found within threshold number of steps, give up return
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 f(): config = json.load(open('configs/default_config.json')) config['environment']['visualize'] = True env = uds.UrbanDrivingEnv(config_data=config) env._reset() env._render() state = env.current_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
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 sample_state(self): self.num_cars = np.random.randint(self.il_config['min_cars'], high=self.il_config['max_cars']) self.fluids_config['agents']['controlled_cars'] = self.num_cars env = fluids.UrbanDrivingEnv(self.fluids_config) return env
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
def plan(self, state, agent_num, type_of_agent="background_cars"): state_copy = deepcopy(state) testing_env = uds.UrbanDrivingEnv(init_state=state_copy, randomize=False) num_controlled_cars = state_copy.agent_config["controlled_cars"] empty_actions = [None] * num_controlled_cars state_copy = testing_env.current_state if state_copy.dynamic_objects[type_of_agent][str( agent_num)].trajectory.stopped: state_copy.dynamic_objects[type_of_agent][str( agent_num)].trajectory.set_vel(VelocityAction(4.0)) for t in range(self.lookahead): state_copy, reward, done, info_dict = testing_env._step( empty_actions, background_simplified=True) state_copy = testing_env.current_state done = state_copy.collides_any_dynamic( agent_num, type_of_agent=type_of_agent) if done: break if done: return VelocityAction(0.0) return VelocityAction(4.0) elif not state_copy.dynamic_objects[type_of_agent][str( agent_num)].trajectory.stopped: for t in range(self.lookahead): state_copy, reward, done, info_dict = testing_env._step( empty_actions, background_simplified=True) state_copy = testing_env.current_state done = state_copy.collides_any_dynamic( agent_num, type_of_agent=type_of_agent) if done: break if done: return VelocityAction(0.0) return VelocityAction(4.0)
config['environment']['visualize'] = False n_trials = 20 while (True): f = open("sensitivity_out.txt", "a+") config['agents']['background_cars'] = np.random.randint(2, 10) config['agents']['use_traffic_lights'] = np.random.random() < 0.5 config['agents']['use_pedestrians'] = True config['agents']['number_of_pedestrians'] = np.random.randint(0, 6) config['agents']['bg_state_space_config']['noise'] = np.random.random() config['agents']['bg_state_space_config'][ 'omission_prob'] = np.random.random() print(config) env = uds.UrbanDrivingEnv(config_data=config, randomize=True) env._reset() env._render() z = 0 n_successes = 0 while (True): if (z == n_trials): break state, _, done, _ = env._step([]) env._render() state = env.current_state done = False success = True # for k in state.dynamic_objects['background_cars']:
import json import gym import gym_urbandriving as uds from gym_urbandriving import * from gym_urbandriving.agents import * from gym_urbandriving.assets import * from gym_urbandriving.utils import Trajectory import numpy as np with open('configs/default_config.json') as json_data_file: data = json.load(json_data_file) action = [np.array([0.0, 0.0])] env = uds.UrbanDrivingEnv(data) obs, reward, done, info_dict = env.step(action)
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) vis = uds.PyGameVisualizer((800, 800)) init_state = CustomState() env = uds.UrbanDrivingEnv(init_state=init_state, visualizer=vis, randomize=True) env._render() while(True): env._render()
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 = []
config = json.load(open('configs/example_config.json')) config['environment']['visualize'] = True #COMMENT OUT TO ADD PEDESTIRANS #config['agents']["number_of_pedestrians"] = 4 #COMMENT OUT TO USE NEURAL BASED PLANNER config['agents']['agent_mappings']['Car'] = 'NeuralPursuitAgent' #COMMENT OUT FOR RAW STATE SPACE config['agents']['state_space'] = 'raw' #COMMENT OUT TO CHANGE ACTION SPACE # config['agents']['action_space'] = 'velocity' env = uds.UrbanDrivingEnv(config_data=config) env._reset() env._render() obs = env.get_initial_observations() # Car 0 will be controlled by our KeyboardAgent agent = KeyboardAgent() #COMMENT OUT TO CHANGE SUPERVISOR #agent = SteeringSupervisor() # agent = VelocitySupervisor() # Simulation loop while (True):