CitiesSkylinesMods / TMPE

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

Unify traffic measurement methods #66

Open Strdate opened 5 years ago

Strdate commented 5 years ago

Currently, there are two methods of measuring traffic flow/density that run at the same time. The algorithm root is located at this method:

https://github.com/krzychu124/Cities-Skylines-Traffic-Manager-President-Edition/blob/139bcbaf47a057c00bc92ff6e56ce3b07012dc37/TLM/TLM/Custom/AI/CustomRoadAI.cs#L50

One of them is the vanilla traffic measurement, which is happening at the original method (which is called by the new implementation), second is a technique implemented by TrafficMeasurementManager. Issues: 1) The custom traffic measurement manager should report its data back to vanilla traffic density report tool (NetSegment.m_trafficDensity) 2) When the custom implementation is running, the vanilla one should be turned off

3) Moreover, this all can be somehow unified with the Advanced Traffic Congestion Report by pcfantasy It measures traffic per segment, not per lane, but it could be implemented. I don't know its advantages compared to TMPE implementation.

4) All custom measurements are performance hungry, they can be done once per second or even at longer intervals, not every simulation step as now.

I am looking at it myself a bit...

pcfantasy commented 5 years ago

@Strdate

Both TrafficMeasurementManager and vanilla traffic density caculate is used for better pathfind, to aviod congestion route, so they only consider traffic amout, not traffic speed.

For example, if a highway contains lots of traffic, but vehicle flow is not at a standstill, instead they are at a certain speed. We should consider this road is clear.

But in current game, this road with be consider as high density. So in my mod, I use vehicle speed / Land speed limit as a factor to caculate trafficDensity.

trafficDensity will only affect vanilla traffic density report tool, trafficBuffer will affect pathfinding, I think TMPE can also do some change to caculate trafficBuffer with (vehicle speed / Land speed limit) to choose better way for pathfinding

originalfoo commented 5 years ago

While the vanilla traffic density measurements might seem overkill on their own, I suspect there will be some linkage with the *noise pollution system, so be sure to check that too. It would make sense that while scanning all the roads and vehicles that the game calculates both traffic density and noise pollution from same data?

Strdate commented 5 years ago

@aubergine10 Yes, it both happens within the same method. int noisePollution = (int)(100 - (data.m_trafficDensity - 100) * (data.m_trafficDensity - 100) / 100); (this is not all) You are right that the noise pollution must be recalculated as well depending on the amount of cars, not the traffic density. The link between these two scalars wound not be as direct as it is today.

@pcfantasy Actually it seems that pathfinding is not at all affected by traffic situation. It would be nice if it was, but for that we definitely need better density measurement which your mod provides. By the way, I think that TMPE measurement calculates the density from the flow too, not only from the amount of cars.

originalfoo commented 5 years ago

These seem to be prime areas where TMPE uses the road utilisation metrics:

See also: https://tmpe.viathinksoft.com/wiki/index.php?title=Vehicle_routing

pcfantasy commented 5 years ago

@Strdate

where is blow codes? in which method?

int noisePollution = (int)(100 - (data.m_trafficDensity - 100) * (data.m_trafficDensity - 100) / 100);

Strdate commented 5 years ago

@aubergine10 I forgot about the dynamic lane changing. But the fact that traffic density is used directly for pathfinding is new to me, I don't know where in the code does that happen.

@pcfantasy https://github.com/krzychu124/Cities-Skylines-Traffic-Manager-President-Edition/blob/139bcbaf47a057c00bc92ff6e56ce3b07012dc37/TLM/TLM/Custom/AI/CustomRoadAI.cs#L862

This is the same piece of code as in the vanilla game. It is in the RoadAI class. Apparently your mod has an unexpected side effect of lowering noise pollution.

pcfantasy commented 5 years ago

@Strdate

In vanilla game, this logic is blow: int num8 = (int)(100 - (data.m_noiseDensity - 100) * (data.m_noiseDensity - 100) / 100);

In Addtraffic logic, vanilla code will also add data.m_noiseDensity public void AddTraffic(int amount, int noise) { this.m_trafficBuffer = (ushort)Mathf.Min((int)this.m_trafficBuffer + amount, 65535); this.m_noiseBuffer = (ushort)Mathf.Min((int)this.m_noiseBuffer + noise, 65535); }

In TMPE,

public void AddTraffic(ushort segmentId, byte laneIndex, ushort speed) { if (this.laneTrafficData[(int)segmentId] == null || (int)laneIndex >= this.laneTrafficData[(int)segmentId].Length) { return; } this.laneTrafficData[(int)segmentId][(int)laneIndex].trafficBuffer = (ushort)Math.Min(65535u, (uint)(this.laneTrafficData[(int)segmentId][(int)laneIndex].trafficBuffer + 1)); TrafficMeasurementManager.LaneTrafficData[] expr_5E_cp_0_cp_0 = this.laneTrafficData[(int)segmentId]; expr_5E_cp_0_cp_0[(int)laneIndex].accumulatedSpeeds = expr_5E_cp_0_cp_0[(int)laneIndex].accumulatedSpeeds + (uint)speed; }

So there must be some mismatch bettween TMPE and vanilla game , I think we need to correct that.

Strdate commented 5 years ago

It is possible. But I don't even know when the AddTraffic method is called.

pcfantasy commented 5 years ago

@Strdate I will try to fix this in TMPE and create a pull

Strdate commented 5 years ago

@pcfantasy As I looked through the TMPE measurement, it seems that it is almost the same as yours, but doesn't show the data on the map. I don't know if the units are the same and if it can be directly loaded into NetSegment.m_trafficDensity (you could know). In any case, we can let the vanilla measurement method run in background and calculate the noise according to it.

originalfoo commented 5 years ago

It is possible. But I don't even know when the AddTraffic method is called.

You could find it via the dnSpy debugger: https://github.com/krzychu124/Cities-Skylines-Traffic-Manager-President-Edition/wiki/Attaching-Debugger-to-Cities-Skylines

Once that's up and running, right-click a method definition and choose Analyze to find what uses it.

originalfoo commented 5 years ago

Would it be possible to get both traffic views (congestion, and density)?

Maybe tabs could be added to the info view panel (like the Routes or Education info views have). One tab would show density, the other congestion.

pcfantasy commented 5 years ago

@Strdate

No I do not think TMPE measurement is the same as mine, you can see TMPE measurement and vannila game will add num into trafficBuffer for every vehicle. In my mod, only those slow speed vehicle will increase trafficBuffer.

Then trafficBuffer will impact on trafficDensity and show on traffic congestion report.