huawei-noah / SMARTS

Scalable Multi-Agent RL Training School for Autonomous Driving
MIT License
922 stars 186 forks source link

[Help Request] Randomize scenario #2108

Closed knightcalvert closed 9 months ago

knightcalvert commented 9 months ago

High Level Description

I have a same demand with #1423, i wish my traffic flow's RandomRoute() are different in each episode instead of a fixing one.

1. Instantitate the smarts env with a list of scenarios, instead of a single scenario, to cycle through various maps on each episode reset call.
2. Create a list of traffic flows in scenario.py, instead of a single traffic flow, to cycle through the various traffic patterns on each episode reset call.

the second point i have made it, but about the first point, I only have a single scenario-4lane, and don't know how to cycle it. so i just tried to copy my scenario a few times to make it a list, obviously it failed.

Version

1.4.0

Operating System

No response

Problems

No response

Gamenot commented 9 months ago

Hello, if you are trying to create a number of traffic flows we do not offer scenarios that are randomly different at runtime. This is because the scenarios are designed to be replicable and deterministic. A possible way around this is to create a series of traffic configurations.

variations = 40
scenario = Scenario(
    traffic={
        f"t{i}": Traffic(
            flows=[
                Flow(
                    route=RandomRoute(),
                    repeat_route=True,
                    rate=3600,
                    actors={TrafficActor(name="car"): 1.0},
                )
            ]
        ) for i in range(variations)
    },
    ...
)
knightcalvert commented 9 months ago

I found that all RandomRoute() are the same direction, but my scenario is 4lane, i need them differently. probably because of the pseudo-random?

knightcalvert commented 9 months ago

i add some time information to make sure generating different rou.xml, but the traffic flow still remain the same direction. it seems like everytime i run the main.py, it random choose a flow route, and keep it still every episode. The route between two runs are different, while between two episode are the same.

Gamenot commented 9 months ago

I found that all RandomRoute() are the same direction, but my scenario is 4lane, i need them differently. probably because of the pseudo-random?

That should not happen. You guessed right that it had to do with pseudo-random. I am putting up a fix now.

knightcalvert commented 9 months ago

hello @Gamenot , i have synchronized the latest modifications. The rou.xml can randomly generate now, but there is still a little distance from what I expected.

The route between two runs are different, while between two episode are the same.

to apply the reinforcement algorithm in SMARTS, I wish the flow route can vary in one run. Have any good solution? Thank you

knightcalvert commented 9 months ago

I tried to build scenario every episode, it worked, but take too much time if my episode step is big.

Gamenot commented 9 months ago

hello @Gamenot , i have synchronized the latest modifications. The rou.xml can randomly generate now, but there is still a little distance from what I expected.

The route between two runs are different, while between two episode are the same.

to apply the reinforcement algorithm in SMARTS, I wish the flow route can vary in one run. Have any good solution? Thank you

I could suggest breaking up the single flow into a bunch of smaller flows.

subflow_count=6
flow_rate=3600 // subflow_count
flows=[
  Flow(
    route=RandomRoute(),
    rate=flow_rate,
    randomly_spaced=True,
    actors={TrafficActor(name="car"): 1.0},
  ) for _ in subflow_count
]

variations = 40
scenario = Scenario(
    traffic={
        f"t{i}": Traffic(
            flows=flows,
        ) for i in range(variations)
    },
    ...
)
Gamenot commented 9 months ago

I tried to build scenario every episode, it worked, but take too much time if my episode step is big.

The build being separated is intentional because it is slow. I do have some thought that the build could be incremental but I would need an explicit request in order to do anything about it.