CitiesSkylinesMods / TMPE

Cities: Skylines Traffic Manager: President Edition
https://steamcommunity.com/sharedfiles/filedetails/?id=1637663252
MIT License
571 stars 85 forks source link

Cargo trains (or the cargo) should choose destination station dynamically #1536

Open Kelvin-Ng opened 2 years ago

Kelvin-Ng commented 2 years ago

The Problem

In the screenshots below, you can see that all trains are going to one station. However, the two stations are actually load balanced. Just that it is like a consecutive of 5 trains going to one, and then a consecutive of 5 trains going to another one. In fact, there are more trains that are pending to go to the empty one, but they are just blocked by the trains going to the busy one.

I think this is because the choice is determined by the cargo trucks instead of the trains. The cargo trucks choose whichever destination station that is more free at the time when it enters the drop-off station, and the choice will not change later even if dynamic lane selection is used. At the time I took the first 3 screenshots, the north station is free, so all trucks are choosing the north one as destination (compare the 2nd and 3rd screenshots). Later, since all trains carrying the cargo from these trucks go to the north station, it will congest and the south one will be free (i.e., the opposite of the original situation), as shwon in the 4th screenshot. The amount of traffic in the two station will just flip periodically, with one of them being almost free.

EDIT

I realize that the situation is more general than that. For example, the road traffic on the drop-off station also shows a similar flipping effect. Also, even warehouse has a similar effect: if there are two warehouses that take the same resource (say, Paper), the trucks dropping off paper will all choose one, and then all choose another one.

All these effects are caused by the fact that all vehicles (train, trucks, or whatever) will never change destination dynamically even if there is a different destination that can serve the exact same purpose.

A simple but weird solution

The easy way is to just allow trains to choose destination station dynamically, regardless of the requested station of the cargo. The dynamic choice takes into account of the time it takes to transport the cargo to the station through train track, and to transport the cargo from the station to the final destination. However, that is weird because, think about if you are a customer, and the train does not transfer your cargo to your designated station. But I think that will work.

However, I think this simple solution should work perfectly well for drop-off traffic and for warehouse traffic.

A more sophisticated solution

TMPE can add a feature to allow us to group stations. So, when a cargo truck drops cargo, it should not designate the exact destination station, but instead should designate which group of stations it is going to. Then, the train will destinate to the group of stations, and dynamically choose which exact one to go to at track junctions.

Similarly, when a cargo truck chooses which station to drop-off, it can also designate the group of stations first, and then choose the exact one at road junctions.

The same mechanism also work for warehouses or even other buildings.

Screenshots?

My setup: My setup

Among of cargo going to the south station: Among of cargo going to the south station

Among of cargo going to the north station: Among of cargo going to the north station

After the situation flips: After the situation flips

krzychu124 commented 2 years ago

It's not a new idea, I already thought about that and I've already implemented similar, still experimental feature for Cargo Airplanes in #1458 draft so... doable but only for destination stations (due to how cargo transfers and pathfinding works).

Basically when cargo airplane is waiting too long either in front of the destination stand(other aircraft is still loading) or blocked because of other airplane is also waiting it will try to find better cargo aircraft stand (moving forward down the taxiways, taking into account traffic flow, vehicle restrictions etc.). Once better aircraft stand has been found then hopefully the vehicle will start moving so all transported cargo (attached invisible cargo trucks) starts redirecting their original route taking new aircraft stand as a new, future start position once aircraft finally reach the stand building. It works surprisingly well, can handle a ton of aircraft if taxiways are built correctly (many intersections == multiple points for re-routing/DLS-similar behavior)

Speaking of DLS(dynamic lane selection) - it can only change lanes, cannot the segment and here, as in experimental dynamic Cargo airplane selection it needs to traverse network going forward and as you noticed require something to mark stations as a logical group. In #1458 it was fairly simple. Aircraft stand needs to be built in the Airport park so I can easily find all stands or match if they belong to the same area/park or not (kind of wishful thinking that redirected cargo trucks can still reach their original target building). I think that district paint will be the easiest and most flexibly way to do something like grouping. Obviously it won't be limited to station buildings but will also support swapping station track so if one is occupied then it could use other free if reachable, assuming multi-track stations.

Anyways, it will be slightly less weird if you'll think about cargo train as a train loaded with vehicles with cargo, not just that cargo alone 😄

svnolte commented 2 years ago

Maybe instead of dynamic pathfinding, the demand/spawn/pathfinding mechanism could be modified: Cargo transfer location selection might take into account how many vehicles are sent/on their way to the alternative transfer locations already (and not having reached their destination, yet). Means, the simulation would notice that it is going to jam the transfer location.

originalfoo commented 2 years ago

Maybe instead of dynamic pathfinding, the demand/spawn/routing mechanism could be modified: Destination selection might take into account how many vehicles are sent/on their way to the alternative destinations already (and not having reached their destination, yet). Meaning, the simulation would notice that it is going to jam a destination.

At the initial pathfinding stage it's not possible to determine when the vehicle will arrive at destination, or what other stuff will be arriving at the same time. For exmaple, there could be slower trains on track, or trains leaving/joining at junctions, all causing delays that can't be predicted in advance. So that's why the additional bit of pathfinding is required at end of journey, and it only needs to happen if the train gets stuck in a queue.

@krzychu124 maybe uploading that vid of the cargo aircraft rerouting would help illustrate?

svnolte commented 2 years ago

Oh ok, you're right about cargo train stations. (Sorry, I had cargo harbors in mind, C:S ships not caring about traffic)

lokpro commented 2 years ago

Steam Workshop::Adjust Pathfinding https://steamcommunity.com/sharedfiles/filedetails/?id=1829496988

is this mod for your need? (I have not used it) (I wish some of its functions getting added to TMPE)

Kelvin-Ng commented 2 years ago

@lokpro

I tried Adjust Pathfinding and it somehow works. If you start with no traffic (clear traffic using TMPE), then at the beginning the traffic to the two stations are strictly balanced. But if there is any temporal unbalance, it will take a long time to balance out due to its static nature. So, a dynamic solution is still very helpful.

Kelvin-Ng commented 2 years ago

@krzychu124

Do you think it is easy to port the logic for finding another aircraft stand to finding another cargo station / hub? I know programming but this is the first time I look into CS mods and your pull request is too big...

To mark stations as a group, instead of using districts, maybe using names of the stations is also a good idea. The problem of district is that, a district may be too large and have two stations that are far away and that you don't want to group. You can definitely remove the station from an existing district and make one specifically for the stations, but that sounds weird.

To use names to mark a group, we can specify the names to be things like "Group Name | Specific Name". All stations with the same Group Name are considered as one group. On the menu of a station, we can add a button to enable/disable grouping, to avoid messing up the existing stations that we are not going to group.

krzychu124 commented 2 years ago

It shouldn't be too hard but I'm not 100% sure yet.

The main reason to use district/park area is I can query district manager to get district ID based on building position and then search for other buildings using that id (district id, as many other things is not stored in the building data). Using names requires custom solution for tracking buildings. If building is built in a district it will register itself as service building but if not then.. how can I find it? Area search? How far? There might be thousands buildings in vicinity and no guarantee there is another one with the same name.

I was thinking about alternative tool similar to areas (just create new virtual park type) as a helper for tracking parking spaces/buildings. I could reuse area tool but that tool is available only when you own at least one DLC that use "park" areas (Parklife/Industries/Campus/Airports)

krzychu124 commented 2 years ago

@aubergine10 I've updated PR description with short example how dynamic cargo airplane selection: works https://github.com/CitiesSkylinesMods/TMPE/pull/1458

SnapperTT commented 1 year ago

Another simple idea is mark a node as a "load balancing node" and attach an atomic counter to it. If the counter has value 0 then only the first path is allowed. If the counter is 1 then only the second path is allowed, etc. Every time a vehicle is successfully routed through it the counter increments.