OpenTTD
demands.cpp
Go to the documentation of this file.
1 
3 #include "../stdafx.h"
4 #include "demands.h"
5 #include <queue>
6 
7 #include "../safeguards.h"
8 
9 typedef std::queue<NodeID> NodeList;
10 
14 class Scaler {
15 public:
16  void SetDemands(LinkGraphJob &job, NodeID from, NodeID to, uint demand_forw);
17 };
18 
22 class SymmetricScaler : public Scaler {
23 public:
29  inline SymmetricScaler(uint mod_size) : mod_size(mod_size), supply_sum(0),
30  demand_per_node(0)
31  {}
32 
37  inline void AddNode(const Node &node)
38  {
39  this->supply_sum += node.Supply();
40  }
41 
46  inline void SetDemandPerNode(uint num_demands)
47  {
48  this->demand_per_node = max(this->supply_sum / num_demands, 1U);
49  }
50 
58  inline uint EffectiveSupply(const Node &from, const Node &to)
59  {
60  return max(from.Supply() * max(1U, to.Supply()) * this->mod_size / 100 / this->demand_per_node, 1U);
61  }
62 
70  inline bool HasDemandLeft(const Node &to)
71  {
72  return (to.Supply() == 0 || to.UndeliveredSupply() > 0) && to.Demand() > 0;
73  }
74 
75  void SetDemands(LinkGraphJob &job, NodeID from, NodeID to, uint demand_forw);
76 
77 private:
78  uint mod_size;
79  uint supply_sum;
81 };
82 
86 class AsymmetricScaler : public Scaler {
87 public:
92  inline void AddNode(const Node &)
93  {
94  }
95 
100  inline void SetDemandPerNode(uint)
101  {
102  }
103 
109  inline uint EffectiveSupply(const Node &from, const Node &)
110  {
111  return from.Supply();
112  }
113 
120  inline bool HasDemandLeft(const Node &to) { return to.Demand() > 0; }
121 };
122 
131 void SymmetricScaler::SetDemands(LinkGraphJob &job, NodeID from_id, NodeID to_id, uint demand_forw)
132 {
133  if (job[from_id].Demand() > 0) {
134  uint demand_back = demand_forw * this->mod_size / 100;
135  uint undelivered = job[to_id].UndeliveredSupply();
136  if (demand_back > undelivered) {
137  demand_back = undelivered;
138  demand_forw = max(1U, demand_back * 100 / this->mod_size);
139  }
140  this->Scaler::SetDemands(job, to_id, from_id, demand_back);
141  }
142 
143  this->Scaler::SetDemands(job, from_id, to_id, demand_forw);
144 }
145 
154 inline void Scaler::SetDemands(LinkGraphJob &job, NodeID from_id, NodeID to_id, uint demand_forw)
155 {
156  job[from_id].DeliverSupply(to_id, demand_forw);
157 }
158 
164 template<class Tscaler>
165 void DemandCalculator::CalcDemand(LinkGraphJob &job, Tscaler scaler)
166 {
167  NodeList supplies;
168  NodeList demands;
169  uint num_supplies = 0;
170  uint num_demands = 0;
171 
172  for (NodeID node = 0; node < job.Size(); node++) {
173  scaler.AddNode(job[node]);
174  if (job[node].Supply() > 0) {
175  supplies.push(node);
176  num_supplies++;
177  }
178  if (job[node].Demand() > 0) {
179  demands.push(node);
180  num_demands++;
181  }
182  }
183 
184  if (num_supplies == 0 || num_demands == 0) return;
185 
186  /* Mean acceptance attributed to each node. If the distribution is
187  * symmetric this is relative to remote supply, otherwise it is
188  * relative to remote demand. */
189  scaler.SetDemandPerNode(num_demands);
190  uint chance = 0;
191 
192  while (!supplies.empty() && !demands.empty()) {
193  NodeID from_id = supplies.front();
194  supplies.pop();
195 
196  for (uint i = 0; i < num_demands; ++i) {
197  assert(!demands.empty());
198  NodeID to_id = demands.front();
199  demands.pop();
200  if (from_id == to_id) {
201  /* Only one node with supply and demand left */
202  if (demands.empty() && supplies.empty()) return;
203 
204  demands.push(to_id);
205  continue;
206  }
207 
208  int32 supply = scaler.EffectiveSupply(job[from_id], job[to_id]);
209  assert(supply > 0);
210 
211  /* Scale the distance by mod_dist around max_distance */
212  int32 distance = this->max_distance - (this->max_distance -
213  (int32)DistanceMaxPlusManhattan(job[from_id].XY(), job[to_id].XY())) *
214  this->mod_dist / 100;
215 
216  /* Scale the accuracy by distance around accuracy / 2 */
217  int32 divisor = this->accuracy * (this->mod_dist - 50) / 100 +
218  this->accuracy * distance / this->max_distance + 1;
219 
220  assert(divisor > 0);
221 
222  uint demand_forw = 0;
223  if (divisor <= supply) {
224  /* At first only distribute demand if
225  * effective supply / accuracy divisor >= 1
226  * Others are too small or too far away to be considered. */
227  demand_forw = supply / divisor;
228  } else if (++chance > this->accuracy * num_demands * num_supplies) {
229  /* After some trying, if there is still supply left, distribute
230  * demand also to other nodes. */
231  demand_forw = 1;
232  }
233 
234  demand_forw = min(demand_forw, job[from_id].UndeliveredSupply());
235 
236  scaler.SetDemands(job, from_id, to_id, demand_forw);
237 
238  if (scaler.HasDemandLeft(job[to_id])) {
239  demands.push(to_id);
240  } else {
241  num_demands--;
242  }
243 
244  if (job[from_id].UndeliveredSupply() == 0) break;
245  }
246 
247  if (job[from_id].UndeliveredSupply() != 0) {
248  supplies.push(from_id);
249  } else {
250  num_supplies--;
251  }
252  }
253 }
254 
260  max_distance(DistanceMaxPlusManhattan(TileXY(0,0), TileXY(MapMaxX(), MapMaxY())))
261 {
262  const LinkGraphSettings &settings = job.Settings();
263  CargoID cargo = job.Cargo();
264 
265  this->accuracy = settings.accuracy;
266  this->mod_dist = settings.demand_distance;
267  if (this->mod_dist > 100) {
268  /* Increase effect of mod_dist > 100 */
269  int over100 = this->mod_dist - 100;
270  this->mod_dist = 100 + over100 * over100;
271  }
272 
273  switch (settings.GetDistributionType(cargo)) {
274  case DT_SYMMETRIC:
275  this->CalcDemand<SymmetricScaler>(job, SymmetricScaler(settings.demand_size));
276  break;
277  case DT_ASYMMETRIC:
278  this->CalcDemand<AsymmetricScaler>(job, AsymmetricScaler());
279  break;
280  default:
281  /* Nothing to do. */
282  break;
283  }
284 }
uint mod_size
Size modifier. Determines how much demands increase with the supply of the remote station...
Definition: demands.cpp:78
uint Demand() const
Get demand of wrapped node.
Definition: linkgraph.h:153
uint supply_sum
Sum of all supplies in the component.
Definition: demands.cpp:79
bool HasDemandLeft(const Node &to)
Check if there is any acceptance left for this node.
Definition: demands.cpp:120
void SetDemandPerNode(uint num_demands)
Calculate the mean demand per node using the sum of supplies.
Definition: demands.cpp:46
uint Supply() const
Get supply of wrapped node.
Definition: linkgraph.h:147
uint EffectiveSupply(const Node &from, const Node &to)
Get the effective supply of one node towards another one.
Definition: demands.cpp:58
uint EffectiveSupply(const Node &from, const Node &)
Get the effective supply of one node towards another one.
Definition: demands.cpp:109
void SetDemands(LinkGraphJob &job, NodeID from, NodeID to, uint demand_forw)
Set the demands between two nodes using the given base demand.
Definition: demands.cpp:131
void SetDemands(LinkGraphJob &job, NodeID from, NodeID to, uint demand_forw)
Set the demands between two nodes using the given base demand.
Definition: demands.cpp:154
uint demand_per_node
Mean demand associated with each node.
Definition: demands.cpp:80
Symmetric distribution. The same amount of cargo travels in each direction between each pair of nodes...
void CalcDemand(LinkGraphJob &job, Tscaler scaler)
Do the actual demand calculation, called from constructor.
Definition: demands.cpp:165
Asymmetric distribution. Usually cargo will only travel in one direction.
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
int32 mod_dist
Distance modifier, determines how much demands decrease with distance.
Definition: demands.h:18
DemandCalculator(LinkGraphJob &job)
Create the DemandCalculator and immediately do the calculation.
Definition: demands.cpp:259
const LinkGraphSettings & Settings() const
Get the link graph settings for this component.
Definition: linkgraphjob.h:299
bool HasDemandLeft(const Node &to)
Check if there is any acceptance left for this node.
Definition: demands.cpp:70
Updatable node class.
Definition: linkgraph.h:374
void SetDemandPerNode(uint)
Nothing to do here.
Definition: demands.cpp:100
SymmetricScaler(uint mod_size)
Constructor.
Definition: demands.cpp:29
Declaration of demand calculating link graph handler.
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
uint8 accuracy
accuracy when calculating things on the link graph. low accuracy => low running time ...
Scale various things according to symmetric/asymmetric distribution.
Definition: demands.cpp:14
A scaler for asymmetric distribution.
Definition: demands.cpp:86
CargoID Cargo() const
Get the cargo of the underlying link graph.
Definition: linkgraphjob.h:318
void AddNode(const Node &node)
Count a node&#39;s supply into the sum of supplies.
Definition: demands.cpp:37
int32 accuracy
Accuracy of the calculation.
Definition: demands.h:19
uint DistanceMaxPlusManhattan(TileIndex t0, TileIndex t1)
Gets the biggest distance component (x or y) between the two given tiles plus the Manhattan distance...
Definition: map.cpp:207
uint Size() const
Get the size of the underlying link graph.
Definition: linkgraphjob.h:312
static uint MapMaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition: map_func.h:113
uint8 demand_size
influence of supply ("station size") on the demand function
void AddNode(const Node &)
Nothing to do here.
Definition: demands.cpp:92
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
static uint MapMaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition: map_func.h:104
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:165
Class for calculation jobs to be run on link graphs.
Definition: linkgraphjob.h:31
uint8 demand_distance
influence of distance between stations on the demand function
Scaler for symmetric distribution.
Definition: demands.cpp:22