linkgraphjob.cpp

Go to the documentation of this file.
00001 /* $Id: linkgraphjob.cpp 26286 2014-01-29 19:55:29Z fonsinchen $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "../stdafx.h"
00013 #include "../core/pool_func.hpp"
00014 #include "../window_func.h"
00015 #include "linkgraphjob.h"
00016 
00017 /* Initialize the link-graph-job-pool */
00018 LinkGraphJobPool _link_graph_job_pool("LinkGraphJob");
00019 INSTANTIATE_POOL_METHODS(LinkGraphJob)
00020 
00021 
00027 LinkGraphJob::LinkGraphJob(const LinkGraph &orig) :
00028     /* Copying the link graph here also copies its index member.
00029      * This is on purpose. */
00030     link_graph(orig),
00031     settings(_settings_game.linkgraph),
00032     thread(NULL),
00033     join_date(_date + _settings_game.linkgraph.recalc_time)
00034 {
00035 }
00036 
00041 void LinkGraphJob::EraseFlows(NodeID from)
00042 {
00043   for (NodeID node_id = 0; node_id < this->Size(); ++node_id) {
00044     (*this)[node_id].Flows().erase(from);
00045   }
00046 }
00047 
00051 LinkGraphJob::~LinkGraphJob()
00052 {
00053   assert(this->thread == NULL);
00054 
00055   /* Don't update stuff from other pools, when everything is being removed.
00056    * Accessing other pools may be invalid. */
00057   if (CleaningPool()) return;
00058 
00059   /* Link graph has been merged into another one. */
00060   if (!LinkGraph::IsValidID(this->link_graph.index)) return;
00061 
00062   uint size = this->Size();
00063   for (NodeID node_id = 0; node_id < size; ++node_id) {
00064     Node from = (*this)[node_id];
00065 
00066     /* The station can have been deleted. Remove all flows originating from it then. */
00067     Station *st = Station::GetIfValid(from.Station());
00068     if (st == NULL) {
00069       this->EraseFlows(node_id);
00070       continue;
00071     }
00072 
00073     /* Link graph merging and station deletion may change around IDs. Make
00074      * sure that everything is still consistent or ignore it otherwise. */
00075     GoodsEntry &ge = st->goods[this->Cargo()];
00076     if (ge.link_graph != this->link_graph.index || ge.node != node_id) {
00077       this->EraseFlows(node_id);
00078       continue;
00079     }
00080 
00081     LinkGraph *lg = LinkGraph::Get(ge.link_graph);
00082     FlowStatMap &flows = from.Flows();
00083 
00084     for (EdgeIterator it(from.Begin()); it != from.End(); ++it) {
00085       if (from[it->first].Flow() == 0) continue;
00086       StationID to = (*this)[it->first].Station();
00087       Station *st2 = Station::GetIfValid(to);
00088       if (st2 == NULL || st2->goods[this->Cargo()].link_graph != this->link_graph.index ||
00089           st2->goods[this->Cargo()].node != it->first ||
00090           (*lg)[node_id][it->first].LastUpdate() == INVALID_DATE) {
00091         /* Edge has been removed. Delete flows. */
00092         StationIDStack erased = flows.DeleteFlows(to);
00093         /* Delete old flows for source stations which have been deleted
00094          * from the new flows. This avoids flow cycles between old and
00095          * new flows. */
00096         while (!erased.IsEmpty()) ge.flows.erase(erased.Pop());
00097       } else if ((*lg)[node_id][it->first].LastUnrestrictedUpdate() == INVALID_DATE) {
00098         /* Edge is fully restricted. */
00099         flows.RestrictFlows(to);
00100       }
00101     }
00102 
00103     /* Swap shares and invalidate ones that are completely deleted. Don't
00104      * really delete them as we could then end up with unroutable cargo
00105      * somewhere. Do delete them if automatic distribution has been turned
00106      * off for that cargo, though. */
00107     for (FlowStatMap::iterator it(ge.flows.begin()); it != ge.flows.end();) {
00108       FlowStatMap::iterator new_it = flows.find(it->first);
00109       if (new_it == flows.end()) {
00110         if (_settings_game.linkgraph.GetDistributionType(this->Cargo()) != DT_MANUAL) {
00111           it->second.Invalidate();
00112           ++it;
00113         } else {
00114           ge.flows.erase(it++);
00115         }
00116       } else {
00117         it->second.SwapShares(new_it->second);
00118         flows.erase(new_it);
00119         ++it;
00120       }
00121     }
00122     ge.flows.insert(flows.begin(), flows.end());
00123     InvalidateWindowData(WC_STATION_VIEW, st->index, this->Cargo());
00124   }
00125 }
00126 
00132 void LinkGraphJob::Init()
00133 {
00134   uint size = this->Size();
00135   this->nodes.Resize(size);
00136   this->edges.Resize(size, size);
00137   for (uint i = 0; i < size; ++i) {
00138     this->nodes[i].Init(this->link_graph[i].Supply());
00139     EdgeAnnotation *node_edges = this->edges[i];
00140     for (uint j = 0; j < size; ++j) {
00141       node_edges[j].Init();
00142     }
00143   }
00144 }
00145 
00149 void LinkGraphJob::EdgeAnnotation::Init()
00150 {
00151   this->demand = 0;
00152   this->flow = 0;
00153   this->unsatisfied_demand = 0;
00154 }
00155 
00161 void LinkGraphJob::NodeAnnotation::Init(uint supply)
00162 {
00163   this->undelivered_supply = supply;
00164   new (&this->flows) FlowStatMap;
00165   new (&this->paths) PathList;
00166 }
00167 
00176 void Path::Fork(Path *base, uint cap, int free_cap, uint dist)
00177 {
00178   this->capacity = min(base->capacity, cap);
00179   this->free_capacity = min(base->free_capacity, free_cap);
00180   this->distance = base->distance + dist;
00181   assert(this->distance > 0);
00182   if (this->parent != base) {
00183     this->Detach();
00184     this->parent = base;
00185     this->parent->num_children++;
00186   }
00187   this->origin = base->origin;
00188 }
00189 
00198 uint Path::AddFlow(uint new_flow, LinkGraphJob &job, uint max_saturation)
00199 {
00200   if (this->parent != NULL) {
00201     LinkGraphJob::Edge edge = job[this->parent->node][this->node];
00202     if (max_saturation != UINT_MAX) {
00203       uint usable_cap = edge.Capacity() * max_saturation / 100;
00204       if (usable_cap > edge.Flow()) {
00205         new_flow = min(new_flow, usable_cap - edge.Flow());
00206       } else {
00207         return 0;
00208       }
00209     }
00210     new_flow = this->parent->AddFlow(new_flow, job, max_saturation);
00211     if (this->flow == 0 && new_flow > 0) {
00212       job[this->parent->node].Paths().push_front(this);
00213     }
00214     edge.AddFlow(new_flow);
00215   }
00216   this->flow += new_flow;
00217   return new_flow;
00218 }
00219 
00225 Path::Path(NodeID n, bool source) :
00226   distance(source ? 0 : UINT_MAX),
00227   capacity(source ? UINT_MAX : 0),
00228   free_capacity(source ? INT_MAX : INT_MIN),
00229   flow(0), node(n), origin(source ? n : INVALID_NODE),
00230   num_children(0), parent(NULL)
00231 {}
00232