demands.h

Go to the documentation of this file.
00001 
00003 #ifndef DEMANDS_H_
00004 #define DEMANDS_H_
00005 
00006 #include "linkgraph.h"
00007 #include "../cargo_type.h"
00008 #include "../map_func.h"
00009 
00013 class Scaler {
00014 public:
00015   Scaler() : demand_per_node(0) {}
00016 
00017   void SetDemands(LinkGraphComponent * graph, NodeID from, NodeID to, uint demand_forw);
00018 protected:
00019   uint demand_per_node; 
00020 };
00021 
00025 class SymmetricScaler : public Scaler {
00026 public:
00027   FORCEINLINE SymmetricScaler(uint mod_size) : mod_size(mod_size), supply_sum(0) {}
00028 
00033   FORCEINLINE void AddNode(const Node &node)
00034   {
00035     this->supply_sum += node.supply;
00036   }
00037 
00042   FORCEINLINE void SetDemandPerNode(uint num_demands)
00043   {
00044     this->demand_per_node = max(this->supply_sum / num_demands, 1U);
00045   }
00046 
00054   FORCEINLINE uint EffectiveSupply(const Node &from, const Node &to)
00055   {
00056     return max(from.supply * max(1U, to.supply) * this->mod_size / 100 / this->demand_per_node, 1U);
00057   }
00058 
00065   FORCEINLINE bool DemandLeft(Node &to)
00066   {
00067     return (to.supply == 0 || to.undelivered_supply > 0) && to.demand > 0;
00068   }
00069 
00070   void SetDemands(LinkGraphComponent *graph, NodeID from, NodeID to, uint demand_forw);
00071 
00072 private:
00073   uint mod_size;   
00074   uint supply_sum; 
00075 };
00076 
00080 class AsymmetricScaler : public Scaler {
00081 public:
00082   AsymmetricScaler() : demand_sum(0) {}
00083 
00088   FORCEINLINE void AddNode(const Node &node)
00089   {
00090     this->demand_sum += node.demand;
00091   }
00092 
00097   FORCEINLINE void SetDemandPerNode(uint num_demands)
00098   {
00099     this->demand_per_node = max(this->demand_sum / num_demands, (uint)1);
00100   }
00101 
00108   FORCEINLINE uint EffectiveSupply(const Node &from, const Node &to)
00109   {
00110     return max(from.supply * to.demand / this->demand_per_node, (uint)1);
00111   }
00112 
00118   FORCEINLINE bool DemandLeft(Node &to) { return to.demand > 0; }
00119 
00120 private:
00121   uint demand_sum; 
00122 };
00123 
00128 class DemandCalculator {
00129 public:
00130   DemandCalculator(LinkGraphComponent *graph);
00131 
00132 private:
00133   int32 max_distance; 
00134   int32 mod_dist;     
00135   int32 accuracy;     
00136 
00137   template<class Tscaler>
00138   void CalcDemand(LinkGraphComponent *graph, Tscaler scaler);
00139 };
00140 
00144 class DemandHandler : public ComponentHandler {
00145 public:
00146 
00151   virtual void Run(LinkGraphComponent *graph) { DemandCalculator c(graph); }
00152 
00156   virtual ~DemandHandler() {}
00157 };
00158 
00159 #endif /* DEMANDS_H_ */