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(LinkGraphJob *job, NodeID from, NodeID to, uint demand_forw);
00018 protected:
00019   uint demand_per_node; 
00020 };
00021 
00025 class SymmetricScaler : public Scaler {
00026 public:
00027   inline SymmetricScaler(uint mod_size) : mod_size(mod_size), supply_sum(0) {}
00028 
00033   inline void AddNode(const Node &node)
00034   {
00035     this->supply_sum += node.supply;
00036   }
00037 
00042   inline void SetDemandPerNode(uint num_demands)
00043   {
00044     this->demand_per_node = max(this->supply_sum / num_demands, 1U);
00045   }
00046 
00054   inline 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 
00066   inline bool HasDemandLeft(Node &to, NodeAnnotation &to_anno)
00067   {
00068     return (to.supply == 0 || to_anno.undelivered_supply > 0) && to.demand > 0;
00069   }
00070 
00071   void SetDemands(LinkGraphJob *job, NodeID from, NodeID to, uint demand_forw);
00072 
00073 private:
00074   uint mod_size;   
00075   uint supply_sum; 
00076 };
00077 
00081 class AsymmetricScaler : public Scaler {
00082 public:
00083   AsymmetricScaler() : demand_sum(0) {}
00084 
00089   inline void AddNode(const Node &node)
00090   {
00091     this->demand_sum += node.demand;
00092   }
00093 
00098   inline void SetDemandPerNode(uint num_demands)
00099   {
00100     this->demand_per_node = max(this->demand_sum / num_demands, (uint)1);
00101   }
00102 
00109   inline uint EffectiveSupply(const Node &from, const Node &to)
00110   {
00111     return max(from.supply * to.demand / this->demand_per_node, (uint)1);
00112   }
00113 
00120   inline bool HasDemandLeft(Node &to, NodeAnnotation &to_anno)
00121   {
00122     return to.demand > 0;
00123   }
00124 
00125 private:
00126   uint demand_sum; 
00127 };
00128 
00133 class DemandCalculator {
00134 public:
00135   DemandCalculator(LinkGraphJob *job);
00136 
00137 private:
00138   int32 max_distance; 
00139   int32 mod_dist;     
00140   int32 accuracy;     
00141 
00142   template<class Tscaler>
00143   void CalcDemand(LinkGraphJob *job, Tscaler scaler);
00144 };
00145 
00149 class DemandHandler : public ComponentHandler {
00150 public:
00151 
00156   virtual void Run(LinkGraphJob *job) { DemandCalculator c(job); }
00157 
00161   virtual ~DemandHandler() {}
00162 };
00163 
00164 #endif /* DEMANDS_H */