Skip to content

Commit

Permalink
Added SteeringVel Action
Browse files Browse the repository at this point in the history
  • Loading branch information
jerryz123 committed Sep 20, 2018
1 parent be410dd commit 9efacf7
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 21 deletions.
15 changes: 9 additions & 6 deletions examples/fluids_example.py
Expand Up @@ -25,15 +25,18 @@
# VelocityAction is vel for car to move along trajectory
# SteeringAction is steer, acc control
# KeyboardAction is use keyboard input
# SteeringVelAction is steer, vel control


# actions = simulator.get_supervisor_actions(fluids.SteeringAction, keys=car_keys)
# actions = simulator.get_supervisor_actions(fluids.VelocityAction, keys=car_keys)
# actions = simulator.get_supervisor_actions(fluids.SteeringAccAction, keys=car_keys)
# actions = simulator.get_supervisor_actions(fluids.SteeringAction, keys=car_keys)
# actions = simulator.get_supervisor_actions(fluids.VelocityAction, keys=car_keys)
# actions = simulator.get_supervisor_actions(fluids.SteeringAccAction, keys=car_keys)
actions = simulator.get_supervisor_actions(fluids.SteeringVelAction, keys=car_keys)

# actions = {k:fluids.VelocityAction(1) for k in car_keys}
# actions = {k:fluids.SteeringAction(0, 1) for k in car_keys}
# actions = {k:fluids.KeyboardAction() for k in car_keys}
# actions = {k:fluids.VelocityAction(1) for k in car_keys}
# actions = {k:fluids.SteeringAction(0, 1) for k in car_keys}
# actions = {k:fluids.KeyboardAction() for k in car_keys}
# actions = {k:fluids.SteeringVelAction(0, 1) for k in car_keys}


rew = simulator.step(actions)
Expand Down
20 changes: 19 additions & 1 deletion fluids/actions.py
Expand Up @@ -11,7 +11,25 @@ class KeyboardAction(Action):
This action passes control to keyboard input
"""
pass


class SteeringVelAction(Action):
"""
This action provides steering and velocity control
Parameters
----------
steer: float in range (-1, 1)
vel: float in range (0, 1)
"""
def __init__(self, steer, vel):
self.steer = steer
self.vel = vel
def get_action(self):
return self.steer, self.vel
def get_array(self):
return np.array([self.steer, self.vel])


class SteeringAccAction(Action):
"""
This action provides both steering and acceleration control.
Expand Down
12 changes: 10 additions & 2 deletions fluids/assets/car.py
Expand Up @@ -112,12 +112,18 @@ def step(self, action):
fluids_assert(False, "Cars cannot receive a raw steering action")
elif type(action) == VelocityAction:
steer, acc = self.PIDController(action).get_action()
steer += np.random.randn() * 0.5 * steer
acc += np.random.randn() * 0.5 * acc / 5
#steer += np.random.randn() * 0.5 * steer
#acc += np.random.randn() * 0.5 * acc / 5
self.raw_step(steer, acc)
self.last_action = action
elif type(action) == SteeringVelAction:
steer, vel = action.get_action()
_, acc = self.PIDController(VelocityAction(vel)).get_action()
self.raw_step(steer, acc)
self.last_action = action
elif type(action) == LastValidAction:
self.step(self.last_action)
return
else:
fluids_assert(False, "Car received an illegal action")
while len(self.waypoints) < self.planning_depth and len(self.waypoints) and len(self.waypoints[-1].nxt):
Expand All @@ -144,7 +150,9 @@ def step(self, action):
return

def PIDController(self, target_vel, update=True):

target_vel = target_vel.get_action()

if len(self.waypoints):
target_x = self.waypoints[0].x
target_y = self.waypoints[0].y
Expand Down
11 changes: 8 additions & 3 deletions fluids/sim.py
Expand Up @@ -175,7 +175,7 @@ def step(self, actions={}):
----------
actions : dict of (key -> action)
Keys in dict should correspond to controlled cars.
Action can be of type KeyboardAction, SteeringAction, SteeringAccAction, or VelocityAction
Action can be of type KeyboardAction, SteeringAction, SteeringAccAction, VelocityAction, or SteeringVelAction
Returns
Expand All @@ -188,7 +188,8 @@ def step(self, actions={}):
for k in list(self.next_actions):
if k in car_keys and k in actions:
if type(actions[k]) == SteeringAction:
actions[k] = SteeringAccAction(actions[k].steer, self.state.dynamic_objects[k].PIDController(self.next_actions[k], update=False).acc)
actions[k] = SteeringAccAction(actions[k].steer,
self.state.dynamic_objects[k].PIDController(self.next_actions[k], update=False).acc)
self.next_actions.pop(k)
self.next_actions.update(actions)
for k, v in iteritems(self.next_actions):
Expand Down Expand Up @@ -244,7 +245,7 @@ def get_supervisor_actions(self, action_type=SteeringAccAction, keys={}):
Parameters
----------
action_type: fluids.Action
Type of action to return. VelocityAction, SteeringAccAction, and SteeringAction are currently supported
Type of action to return. VelocityAction, SteeringAccAction, SteeringAction, and SteeringVelAction are currently supported
keys: set
Set of keys for controlled cars or background cars to return actions for
Returns
Expand All @@ -260,6 +261,10 @@ def get_supervisor_actions(self, action_type=SteeringAccAction, keys={}):
elif action_type == SteeringAction:
return {k:self.state.dynamic_objects[k].PIDController(self.next_actions[k], update=False).asSteeringAction()
for k in keys}
elif action_type == SteeringVelAction:
return {k:SteeringVelAction(self.state.dynamic_objects[k].PIDController(self.next_actions[k], update=False).get_action()[0],
self.next_actions[k].get_action())
for k in keys}
else:
fluids_assert(false, "Illegal action type")

Expand Down
22 changes: 13 additions & 9 deletions tests/test_gym_supervisor.py
Expand Up @@ -5,15 +5,19 @@
env.reset()
action = [0, 0]

reward = 0
for i in range(100):
obs, rew, done, info = env.step(action)
reward += rew
assert(obs.shape == (400, 400, 3))
env.render()
action = gym_fluids.agents.fluids_supervisor(obs, info)

assert(reward >= 0)
successes = 0
n = 10
for t in range(n):
reward = 0
for i in range(100):
obs, rew, done, info = env.step(action)
reward += rew
assert(obs.shape == (400, 400, 3))
env.render()
action = gym_fluids.agents.fluids_supervisor(obs, info)
if (reward) >= 0:
successes += 1
assert(successes / n > 0.8)


del(env)
Expand Down

0 comments on commit 9efacf7

Please sign in to comment.