OpenTTD
flowmapper.cpp
Go to the documentation of this file.
1 /* $Id: flowmapper.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #include "../stdafx.h"
13 #include "flowmapper.h"
14 
15 #include "../safeguards.h"
16 
21 void FlowMapper::Run(LinkGraphJob &job) const
22 {
23  for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
24  Node prev_node = job[node_id];
25  StationID prev = prev_node.Station();
26  PathList &paths = prev_node.Paths();
27  for (PathList::iterator i = paths.begin(); i != paths.end(); ++i) {
28  Path *path = *i;
29  uint flow = path->GetFlow();
30  if (flow == 0) break;
31  Node node = job[path->GetNode()];
32  StationID via = node.Station();
33  StationID origin = job[path->GetOrigin()].Station();
34  assert(prev != via && via != origin);
35  /* Mark all of the flow for local consumption at "first". */
36  node.Flows().AddFlow(origin, via, flow);
37  if (prev != origin) {
38  /* Pass some of the flow marked for local consumption at "prev" on
39  * to this node. */
40  prev_node.Flows().PassOnFlow(origin, via, flow);
41  } else {
42  /* Prev node is origin. Simply add flow. */
43  prev_node.Flows().AddFlow(origin, via, flow);
44  }
45  }
46  }
47 
48  for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
49  /* Remove local consumption shares marked as invalid. */
50  Node node = job[node_id];
51  FlowStatMap &flows = node.Flows();
52  flows.FinalizeLocalConsumption(node.Station());
53  if (this->scale) {
54  /* Scale by time the graph has been running without being compressed. Add 1 to avoid
55  * division by 0 if spawn date == last compression date. This matches
56  * LinkGraph::Monthly(). */
57  uint runtime = job.JoinDate() - job.Settings().recalc_time - job.LastCompression() + 1;
58  for (FlowStatMap::iterator i = flows.begin(); i != flows.end(); ++i) {
59  i->second.ScaleToMonthly(runtime);
60  }
61  }
62  /* Clear paths. */
63  PathList &paths = node.Paths();
64  for (PathList::iterator i = paths.begin(); i != paths.end(); ++i) {
65  delete *i;
66  }
67  paths.clear();
68  }
69 }
const bool scale
Whether the flow mapper should scale all flows to monthly values.
Definition: flowmapper.h:44
void FinalizeLocalConsumption(StationID self)
Subtract invalid flows from locally consumed flow.
Date LastCompression() const
Get the date when the underlying link graph was last compressed.
Definition: linkgraphjob.h:324
StationID Station() const
Get ID of station belonging to wrapped node.
Definition: linkgraph.h:159
virtual void Run(LinkGraphJob &job) const
Map the paths generated by the MCF solver into flows associated with nodes.
Definition: flowmapper.cpp:21
NodeID GetOrigin() const
Get the overall origin of the path.
Definition: linkgraphjob.h:354
const LinkGraphSettings & Settings() const
Get the link graph settings for this component.
Definition: linkgraphjob.h:299
Declaration of flow mapper; maps paths into flows at nodes.
Updatable node class.
Definition: linkgraph.h:374
uint GetFlow() const
Get the flow on this leg.
Definition: linkgraphjob.h:396
NodeID GetNode() const
Get the node this leg passes.
Definition: linkgraphjob.h:351
A leg of a path in the link graph.
Definition: linkgraphjob.h:344
Date JoinDate() const
Get the date when the job should be finished.
Definition: linkgraphjob.h:287
uint Size() const
Get the size of the underlying link graph.
Definition: linkgraphjob.h:312
Flow descriptions by origin stations.
Definition: station_base.h:152
uint16 recalc_time
time (in days) for recalculating each link graph component.
Class for calculation jobs to be run on link graphs.
Definition: linkgraphjob.h:31