mirror of https://github.com/OpenTTD/OpenTTD.git
(svn r26166) -Fix: Scale flows only after mapping to avoid rounding errors.
This commit is contained in:
parent
4818b72c75
commit
ec492bfb77
|
@ -18,9 +18,6 @@
|
||||||
*/
|
*/
|
||||||
void FlowMapper::Run(LinkGraphJob &job) const
|
void FlowMapper::Run(LinkGraphJob &job) const
|
||||||
{
|
{
|
||||||
/* Time the graph has been running without being compressed. */
|
|
||||||
uint runtime = job.JoinDate() - job.Settings().recalc_time - job.LastCompression();
|
|
||||||
|
|
||||||
for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
|
for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
|
||||||
Node prev_node = job[node_id];
|
Node prev_node = job[node_id];
|
||||||
StationID prev = prev_node.Station();
|
StationID prev = prev_node.Station();
|
||||||
|
@ -29,8 +26,6 @@ void FlowMapper::Run(LinkGraphJob &job) const
|
||||||
Path *path = *i;
|
Path *path = *i;
|
||||||
uint flow = path->GetFlow();
|
uint flow = path->GetFlow();
|
||||||
if (flow == 0) break;
|
if (flow == 0) break;
|
||||||
/* compress to monthly value */
|
|
||||||
flow = max(1U, flow * 30 / runtime);
|
|
||||||
Node node = job[path->GetNode()];
|
Node node = job[path->GetNode()];
|
||||||
StationID via = node.Station();
|
StationID via = node.Station();
|
||||||
StationID origin = job[path->GetOrigin()].Station();
|
StationID origin = job[path->GetOrigin()].Station();
|
||||||
|
@ -51,7 +46,15 @@ void FlowMapper::Run(LinkGraphJob &job) const
|
||||||
for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
|
for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
|
||||||
/* Remove local consumption shares marked as invalid. */
|
/* Remove local consumption shares marked as invalid. */
|
||||||
Node node = job[node_id];
|
Node node = job[node_id];
|
||||||
node.Flows().FinalizeLocalConsumption(node.Station());
|
FlowStatMap &flows = node.Flows();
|
||||||
|
flows.FinalizeLocalConsumption(node.Station());
|
||||||
|
if (this->scale) {
|
||||||
|
/* Scale by time the graph has been running without being compressed. */
|
||||||
|
uint runtime = job.JoinDate() - job.Settings().recalc_time - job.LastCompression();
|
||||||
|
for (FlowStatMap::iterator i = flows.begin(); i != flows.end(); ++i) {
|
||||||
|
i->second.ScaleToMonthly(runtime);
|
||||||
|
}
|
||||||
|
}
|
||||||
/* Clear paths. */
|
/* Clear paths. */
|
||||||
PathList &paths = node.Paths();
|
PathList &paths = node.Paths();
|
||||||
for (PathList::iterator i = paths.begin(); i != paths.end(); ++i) {
|
for (PathList::iterator i = paths.begin(); i != paths.end(); ++i) {
|
||||||
|
|
|
@ -23,12 +23,25 @@
|
||||||
*/
|
*/
|
||||||
class FlowMapper : public ComponentHandler {
|
class FlowMapper : public ComponentHandler {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a flow mapper.
|
||||||
|
* @param scale Whether the flow mapper should scale all flows to monthly
|
||||||
|
* values. Only do that on the very last flow mapping.
|
||||||
|
*/
|
||||||
|
FlowMapper(bool scale) : scale(scale) {}
|
||||||
virtual void Run(LinkGraphJob &job) const;
|
virtual void Run(LinkGraphJob &job) const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Virtual destructor has to be defined because of virtual Run().
|
* Virtual destructor has to be defined because of virtual Run().
|
||||||
*/
|
*/
|
||||||
virtual ~FlowMapper() {}
|
virtual ~FlowMapper() {}
|
||||||
|
private:
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the flow mapper should scale all flows to monthly values.
|
||||||
|
*/
|
||||||
|
const bool scale;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* FLOWMAPPER_H_ */
|
#endif /* FLOWMAPPER_H_ */
|
||||||
|
|
|
@ -146,9 +146,9 @@ LinkGraphSchedule::LinkGraphSchedule()
|
||||||
this->handlers[0] = new InitHandler;
|
this->handlers[0] = new InitHandler;
|
||||||
this->handlers[1] = new DemandHandler;
|
this->handlers[1] = new DemandHandler;
|
||||||
this->handlers[2] = new MCFHandler<MCF1stPass>;
|
this->handlers[2] = new MCFHandler<MCF1stPass>;
|
||||||
this->handlers[3] = new FlowMapper;
|
this->handlers[3] = new FlowMapper(false);
|
||||||
this->handlers[4] = new MCFHandler<MCF2ndPass>;
|
this->handlers[4] = new MCFHandler<MCF2ndPass>;
|
||||||
this->handlers[5] = new FlowMapper;
|
this->handlers[5] = new FlowMapper(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -79,6 +79,8 @@ public:
|
||||||
|
|
||||||
void ReleaseShare(StationID st);
|
void ReleaseShare(StationID st);
|
||||||
|
|
||||||
|
void ScaleToMonthly(uint runtime);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the actual shares as a const pointer so that they can be iterated
|
* Get the actual shares as a const pointer so that they can be iterated
|
||||||
* over.
|
* over.
|
||||||
|
|
|
@ -4289,6 +4289,22 @@ void FlowStat::ReleaseShare(StationID st)
|
||||||
assert(!this->shares.empty());
|
assert(!this->shares.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scale all shares from link graph's runtime to monthly values.
|
||||||
|
* @param runtime Time the link graph has been running without compression.
|
||||||
|
*/
|
||||||
|
void FlowStat::ScaleToMonthly(uint runtime)
|
||||||
|
{
|
||||||
|
SharesMap new_shares;
|
||||||
|
uint share = 0;
|
||||||
|
for (SharesMap::iterator i = this->shares.begin(); i != this->shares.end(); ++i) {
|
||||||
|
share = max(share + 1, i->first * 30 / runtime);
|
||||||
|
new_shares[share] = i->second;
|
||||||
|
if (this->unrestricted == i->first) this->unrestricted = share;
|
||||||
|
}
|
||||||
|
this->shares.swap(new_shares);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add some flow from "origin", going via "via".
|
* Add some flow from "origin", going via "via".
|
||||||
* @param origin Origin of the flow.
|
* @param origin Origin of the flow.
|
||||||
|
|
Loading…
Reference in New Issue