
Abstreet: Traffic simulation game written in Rust - adamnemecek
https://github.com/dabreegster/abstreet
======
dabreegster
Hi, author here! Didn't intend for this to wind up on HN quite yet, but I'm
excited to answer the good questions coming up here.

~~~
malpighien
Question from a completely clueless perspective. The points you mention I
always have and I have been wondering where to simulate it and get numerical
answers to all those behaviors that can be infuriating. In your seattle full
map you say you heuristically got the time for the lights, but where is that
coming from exactly, like would it be possible to infer that for another city
from some kind of public data. Is it in the domain of science fiction, there
are so many parameters to take in account so it probably is impossible, that
you could model the usual behavior of commuters in a city and then simulate
whether the current traffic lights are set for the right length of time. I
suppose some are updated live to react to traffic jams but in these
circumstances there could also be way to simulate how shifting times would be
the best to refluidify the traffic, instead of just going with brute force and
empirical intuition which I assume is how it works most of the time.

~~~
dabreegster
I don't have real traffic signal timing for Seattle. I'm waiting on a reply
from some folks at SDOT and King County GIS.

In the meantime, I have heuristics like
[https://github.com/dabreegster/abstreet/blob/ed2f703e859d57d...](https://github.com/dabreegster/abstreet/blob/ed2f703e859d57d9333738d3fd97942fce725ae7/map_model/src/traffic_signals.rs#L200).
They're not great at all. If you're interested in improving these,
[https://github.com/dabreegster/abstreet/issues/8](https://github.com/dabreegster/abstreet/issues/8)
is a great bug. ;)

Some traffic signals are adaptive (they'll switch phases sooner/later based on
real-time vehicle detection, bus priority, etc) or even centrally
controllable. I'm not modeling that yet.

------
ken
This is pretty cool! One thing that bothers me about every traffic simulation
I've ever seen, though: all of the vehicles and pedestrians here seem to be
obeying the law.

That's just not realistic -- especially on Mercer, which is blocked (both
directions) every few minutes in rush hour by some jerk trying to make an
illegal turn onto a packed street. I'd say the _main_ causes of traffic
blockages that I encounter during rush hour are drivers blocking the box, and
pedestrians entering the crosswalk against a red hand. I also see a lot of
lane sweeping, too, which I assume is from the influx of drivers from
California (where it's legal).

I hope traffic engineers aren't making real-world decisions based on
simulations that follow all state laws perfectly, but I fear they are.

~~~
blincoln
I agree with you. However, this may be obvious, but some of that could be due
to people trying to follow Google Maps, Waze, or other similar navigation
system directions where the routes are generated based on what other users
have done.

I drove in downtown Seattle rush "hour" traffic for the first time in almost a
year earlier this week, and Maps was giving me all kinds of dangerous,
traffic-jam-causing directions. Things like "make a left turn at an
uncontrolled intersection, from a side street onto an arterial that's
gridlocked for about a kilometer in either direction". Apparently it had been
happening for awhile, because the city had installed a barrier across the
median, and I couldn't have followed the directions even if I'd wanted to.

IOW, it's not just drivers ignoring the law, it's tech companies explicitly
instructing drivers to break the law and cause traffic problems that the
simulations need to account for now.

~~~
nkrisc
> it's not just drivers ignoring the law

It is. If a driver comes across the situation you described, any action other
than a right-hand turn means they should probably take a driver's ed course
again. Regardless of what a robotic voice on their phone is telling them.

Google maps once instructed my wife to do a U-turn on Lakeshore Drive. Nope.

~~~
blincoln
That's certainly the ideal approach, but I don't think it's realistic to plan
for normal people behaving that way.

By the time I realized that Google had given me a route that would not work,
breaking out of it cost me 10 minutes of sitting in traffic going the wrong
way. I think most people would take the "drive like a jerk" alternative at
that point, because the sunk-cost illusion is hard to let go of.

A self-driving car is basically just going to do what the mapping software
tells it to as well, so the "bad instructions from tech companies" factor is a
problem that needs to be addressed specifically anyway.

------
brylie
A/B Street would fit really well into the Citybound project, as they are both
striving to produce realistic simulations.

[https://github.com/citybound/citybound](https://github.com/citybound/citybound)

I really hope A/B Street and Citybound developers will work together.

[https://github.com/dabreegster/abstreet/issues/20](https://github.com/dabreegster/abstreet/issues/20)

[https://github.com/citybound/citybound/issues/361](https://github.com/citybound/citybound/issues/361)

------
farhanhubble
I swear I was discussing exactly this sort of simulation with my brother as
both of us wondered how many city planners model the changes they are making
to the road networks before pumping millions into projects that dump traffic
from one point to another. Additionally, it'd be interesting to simulate the
cascading effect of traffic violations like bad parkings, straddling lanes.

Kudos to Dustin for working on this.

~~~
natestemen
in nyc it's quite common (maybe even required) to run simulations of changes
of infrastructure before making them to see how they will effect traffic.

source: my partner helps run these simulations as part of their job/their
company gets contract work from the city all the time to check changes of bus
routes/etc.

~~~
dabreegster
I've had some trouble getting in touch with the engineers who did this around
Seattle. Any details they can share about the software they use, what metrics
they base decisions on, where they pull demand data? Any thoughts on making
the input and results of these simulations public so anybody can reproduce the
analysis, experiment on their own, etc?

------
leppr
Nice work. I'm wondering since this project seems to use its own nicely
modularized GUI library, what motivated you in particular to write your own
instead of using an existing Rust GUI library/game engine ? And would you make
the same decision if you started now (vs 2 years ago) ?

~~~
erlend_sh
Iced has emerged as a strong favorite for game GUIs:
[https://github.com/hecrj/iced](https://github.com/hecrj/iced)

~~~
Waterluvian
Looks promising. But I don't see any sort of XML-like component layout/config
so I assume it's all done by calling methods? That sounds kind of kludgy. Or
am I missing an example?

~~~
setr
What makes you think xml isn't kludgy

~~~
Waterluvian
Oh it is. And I don't want the XML-style solution.

But I don't see any configuration option in the examples other than chaining
method calls to set each config option. Is there something else?

------
BelleOfTheBall
While I'm not overly optimistic about his goal of getting solutions from the
game to be enacted in real life Seattle, it's a really admirable idea and an
impressive execution to boot.

I'm wondering if, with a big enough budget and a team of skilled devs like
Dustin here, the local government could create more refined simulations to
tackle these planning issues. Or would that inevitably run into the problem
mentioned in his Project Mission: 'People are local experts on the small slice
of the city they interact with daily -- the one left turn lane that always
backs up or a certain set of poorly timed walk signals'?

Perhaps, with a large enough data pool, even these small quirks could be
worked out via simulation?

~~~
ken
> Project Mission: 'People are local experts on the small slice of the city
> they interact with daily -- the one left turn lane that always backs up or a
> certain set of poorly timed walk signals'

100% of the intersections I see that get backed up are backed up because
someone blocks the box. Can we get more enforcement for that? They don't even
need a patrol car. Just have an officer stand at the corner of Mercer and
Westlake from 4pm to 7pm, and each time the light changes, walk out into the
intersection and ticket the guy who's blocking it. You could write $1000 in
tickets an hour, every night of the week.

Many intersections have threatening signs to this effect, but they don't do
any good. I've never seen or heard of anyone getting ticketed for that.

~~~
jeffbarr
Seconded!

I would also like to see some pedestrian bridges or tunnels that would allow
walking commuters like me to cross our major streets (Mercer, Denny, Westlake,
and Dexters) at any time. This, I strongly believe, could reduce traffic, make
residential neighborhoods effectively closer to commercial areas (due to
reduced walking time), and improve both vehicle and pedestrian safety. Bridges
should be far less expensive than roads, and could be built very quickly.

~~~
dabreegster
I would love this too. [https://lidi5.org](https://lidi5.org) is the only
proposal I know of right now to do some of this

------
pmontra
The next step would be writing a program to modify intersections, monitor how
long cars take to get a journey done and optimize the streets for total
transit time, or emissions, or whatever. That's probably faster than letting
people discover optima along their usual routes.

~~~
bluGill
I'm pretty sure that is a NP-complete problem (or worse). Which means on the
scale of a city all the computers in the world couldn't solve it in a human
lifetime. A small town is easy, but in a large city is out of control.

Ignoring that, cities change. You can come up with an optimal system, but
people get new jobs, move to new houses, have kids, die, just to name a few
obvious things that affect traffic - it might seem minor but they all add up
to your optimal system today being wrong for tomorrow.

Even ignoring that, budgets are finite. The ideal solution probably is to tear
down all buildings, move the mountain a few dozen miles east, re-reoute the
river, change the shoreline, and then rebuild. Any one of them is possible on
a small scale, but not on the whole thing all at once (you wouldn't move a
whole mountain, but we have created passes).

Really the best we can do is come up with ideas that are affordable and then
test them. One thing this can do well (if the simulation is good!) is let us
see if fixing one are makes a different area worse - which helps as talk about
the trade offs in the projects we can afford to do.

------
arjie
This is really fucking cool. And I love your ultimate objective: to allow
informed public participation in transport planning. Nice developer info, too.
Looking forward to playing with this.

~~~
w0utert
Yes it's awesome! Calling it just a 'traffic simulation game' is probably a
disservice to the potential applications of something like this. Very
inspiring!

------
chme
Very cool!

That reminds me of Sumo[1] a open source traffic simulator I used at
university to import a OSM map of town, populate it with activity based
traffic and simulated the traffic under different conditions.

While that isn't quite as 'gameified' as this, it was fun!

[1] [https://sumo.dlr.de/index.html](https://sumo.dlr.de/index.html)

------
jackhack
Very impressive simulation! For others unfamiliar with the term "lane
sweeping", it means crossing/changing a lane during a turn. You must turn into
the lane closest to you. [https://cherinlawfirm.com/2018/01/what-is-lane-
sweeping-and-...](https://cherinlawfirm.com/2018/01/what-is-lane-sweeping-and-
is-it-illegal/)

(It's scary thinking a half-mile traffic jam starts at 4am... and I can't help
but wonder what it looks like at more sane hours such as 8:30am or 5:15pm. Is
everything just red for miles around?)

~~~
dabreegster
The jam is probably not realistic. Because that area is a divided set of one-
way roads, there are a few intersections each tagged with a traffic signal in
OpenStreetMap. I'm simulating each signal independently right now. In reality,
it's one signal with more reasonable timing.

(Also sorry for the slow replies, HN is rate-limiting comments because this is
a new account.)

------
gtycomb
Link to a slide set in this distribution -- the overall approach.

[https://docs.google.com/presentation/d/1cF7qFtjAzkXL_r62CjxB...](https://docs.google.com/presentation/d/1cF7qFtjAzkXL_r62CjxBvgQnLvuQ9I2WTE2iX_5tMCY/edit#slide=id.g6201f22714_0_144)

A beautiful piece of work. What else to say.

------
jmpman
I have a theory that restricting an on-ramp/off-ramp for one street during
peak rush hour will significantly improve traffic flow on my freeway commute.
I’ve taken the time to propose the change to my department of transportation,
suggesting they simulate the change. Expect that suggestion went straight into
the trash can.

Realistically, modeling this section of the freeway would be difficult, and
proving there would be a net benefit - even harder, because there are
accidents due to this on-ramp merge which cause the delays. The simulation has
to model accidents to be valid.

If anyone knows of a way to simulate traffic, including changing accident
probability based upon traffic flow (quick stops caused by merging traffic
likely increase the accident probability), I’d like to model my commute and
provide that to the department of transportation.

------
noobermin
Is this is a one person show? If so bravo! It looks like there's a mirror at
google though, is this like a mini-google project?

~~~
dabreegster
I started the project while I was working at Google, went through the open
source process, and then left a short while after to work on it full-time.
There's no affiliation with Google.

~~~
Tempest1981
I'm super-curious to hear more about this, in that it's hard to imagine
walking away from a significant stable money-stream like Google.

~~~
dabreegster
It's a bit more of a story to tell offline. In short, the stable money stream
quickly leads to a comfortable savings, giving me time to try an experiment
like this. Right now I'm motivated by technically interesting projects that
might lead towards good things in the real world, and once I was in a position
to realize that, there was no way I wouldn't be working on A/B St.

------
pjc50
Amazing! This is basically a live action more detailed version of
Cities:Skylines. That game taught me how much of a blocker traffic turning
across other traffic is, and the benefits of one-way systems in avoiding that.

------
crimsonalucard
Looking to learn rust. Anybody know how/what library was used to build those
graphics?

I'm looking for 3D or 2D graphics libraries that are good for prototyping
stuff in Rust.

~~~
mastax
Looking at the Cargo.toml for ezgui this uses glium which is an attempt at
making OpenGL safe using the typesystem. I don't think it's worth the hassle.

For 2D I'd recommend ggez, it's easily the simplest and most polished rust
game toolkit.

There's a good wrapper for SDL2, but I forget which one it is.

wgpu-rs is a really cool next-gen-but-easier-to-use cross platform graphics
API. It's early days though so you have to be willing to do some legwork.

Amethyst is taking a shot at a Godot or Unity style modern integrated game
engine. It'll be a while before they get to that level but it's still useful
and reasonably well documented.

------
brimstedt
Nice work! How would I go about to use it for my own city?

~~~
dabreegster
In theory,
[https://github.com/dabreegster/abstreet/blob/master/docs/new...](https://github.com/dabreegster/abstreet/blob/master/docs/new_city.md).
There are a bunch of unsolved problems -- for example, most places in OSM
don't map on-street parking, so there won't be any. For Seattle, I'm inferring
from an extra shapefile. Have to figure out how to generalize that.

