Skip to content

Commit

Permalink
Merge pull request #101 from huawei-noah/vehicle-index-diff-poc
Browse files Browse the repository at this point in the history
  • Loading branch information
JVillella authored Nov 10, 2020
2 parents d732b38 + 75cb7be commit 8632127
Show file tree
Hide file tree
Showing 12 changed files with 649 additions and 501 deletions.
30 changes: 13 additions & 17 deletions smarts/core/agent_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@
from envision.types import format_actor_id

from .mission_planner import MissionPlanner
from .scenario import Scenario
from .sensors import Sensors
from .vehicle import VehicleState, Vehicle
from .controllers import ActionSpaceType
from .remote_agent_buffer import RemoteAgentBuffer
from .sensors import Sensors
from .vehicle import VehicleState


class AgentManager:
Expand Down Expand Up @@ -137,17 +135,15 @@ def observe(self, sim):
}

for agent_id in self.active_agents:
agent_interface = self._agent_interfaces[agent_id]
# An agent may be pointing to its own vehicle or observing a social vehicle

indices = sim.vehicle_index.vehicle_indices_by_actor_id(
agent_id, columns=["vehicle_id", "shadow_actor_id"],
vehicle_ids = sim.vehicle_index.vehicle_ids_by_actor_id(
agent_id, include_shadowers=True
)

if self.is_boid_agent(sim, agent_id):
vehicles = [
sim.vehicle_index.vehicle_by_id(vehicle_id)
for vehicle_id in indices["vehicle_id"]
for vehicle_id in vehicle_ids
]
# returns format of {<agent_id>: {<vehicle_id>: {...}}}
sensor_states = {
Expand All @@ -174,26 +170,26 @@ def observe(self, sim):
for (vehicle_id, sensor_state) in sensor_states.items()
}
else:
assert len(indices["vehicle_id"]) == 1, (
"Unless this vehicle is part of a boid then we should only have a"
assert len(vehicle_ids) == 1, (
"Unless this vehicle is part of a boid then we should only have a "
f"single vehicle under agent_id={agent_id}\n "
f"(vehicle_ids={indices['vehicle_id']})"
f"(vehicle_ids={vehicle_ids})"
)

vehicle = sim.vehicle_index.vehicle_by_id(indices["vehicle_id"][0])
vehicle = sim.vehicle_index.vehicle_by_id(vehicle_ids[0])
sensor_state = sim.vehicle_index.sensor_state_for_vehicle_id(vehicle.id)
observations[agent_id], dones[agent_id] = Sensors.observe(
sim, agent_id, sensor_state, vehicle
)

# It is not a shadow agent's fault if it is done
if indices["shadow_actor_id"][0] == agent_id:
dones[agent_id] = False
else:
if sim.vehicle_index.vehicle_is_hijacked(vehicle.id):
logging.log(
logging.DEBUG,
f"Agent `{agent_id}` has raised done with {observations[agent_id].events}",
)
else:
# It is not a shadowing agent's fault if it is done
dones[agent_id] = False

rewards[agent_id] = vehicle.trip_meter_sensor(increment=True)
scores[agent_id] = vehicle.trip_meter_sensor()
Expand Down
Loading

0 comments on commit 8632127

Please sign in to comment.