00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "../stdafx.h"
00013 #include "../map_func.h"
00014 #include "../core/bitmath_func.hpp"
00015 #include "../debug.h"
00016 #include "../window_func.h"
00017 #include "../window_gui.h"
00018 #include "../moving_average.h"
00019 #include "linkgraph.h"
00020 #include "demands.h"
00021 #include "mcf.h"
00022 #include "flowmapper.h"
00023 #include <queue>
00024
00028 LinkGraph _link_graphs[NUM_CARGO];
00029
00033 LinkGraphJob::HandlerList LinkGraphJob::_handlers;
00034
00041 void Node::Init(StationID st, uint sup, uint dem)
00042 {
00043 this->supply = sup;
00044 this->undelivered_supply = sup;
00045 this->demand = dem;
00046 this->station = st;
00047
00048 for (PathSet::iterator i = this->paths.begin(); i != this->paths.end(); ++i) {
00049 delete *i;
00050 }
00051 this->paths.clear();
00052 this->flows.clear();
00053 }
00054
00060 FORCEINLINE void Edge::Init(uint distance, uint capacity)
00061 {
00062 this->distance = distance;
00063 this->capacity = capacity;
00064 this->demand = 0;
00065 this->unsatisfied_demand = 0;
00066 this->flow = 0;
00067 this->next_edge = INVALID_NODE;
00068 }
00069
00070
00077 void LinkGraph::CreateComponent(Station *first)
00078 {
00079 std::map<Station *, NodeID> index;
00080 index[first] = this->AddNode(first);
00081
00082 std::queue<Station *> search_queue;
00083 search_queue.push(first);
00084
00085
00086 while (!search_queue.empty()) {
00087 Station *source = search_queue.front();
00088 search_queue.pop();
00089
00090 const LinkStatMap &links = source->goods[this->cargo].link_stats;
00091 for (LinkStatMap::const_iterator i = links.begin(); i != links.end(); ++i) {
00092 Station *target = Station::GetIfValid(i->first);
00093 if (target == NULL) continue;
00094
00095 std::map<Station *, NodeID>::iterator index_it = index.find(target);
00096 if (index_it == index.end()) {
00097 search_queue.push(target);
00098 NodeID node = this->AddNode(target);
00099 index[target] = node;
00100
00101 this->AddEdge(index[source], node, i->second.Capacity());
00102 } else {
00103 this->AddEdge(index[source], index_it->second, i->second.Capacity());
00104 }
00105 }
00106 }
00107
00108
00109 this->SpawnThread();
00110 }
00111
00125 void LinkGraph::NextComponent()
00126 {
00127
00128
00129
00130 if (Station::GetNumItems() == 0) return;
00131
00132
00133 if (this->GetSize() > 0) return;
00134
00135
00136
00137
00138
00139
00140 StationID last_station_id = min(this->current_station_id, Station::GetPoolSize() - 1);
00141 LinkGraphComponentID current_component_id = this->LinkGraphComponent::index;
00142
00143 do {
00144 if (++this->current_station_id >= Station::GetPoolSize()) {
00145
00146
00147
00148
00149 this->current_station_id = 0;
00150 if (current_component_id % 2 == 0) {
00151 current_component_id = 1;
00152 } else {
00153 current_component_id = 0;
00154 }
00155 }
00156
00157
00158 Station *station = Station::GetIfValid(this->current_station_id);
00159 if (station != NULL) {
00160 GoodsEntry &ge = station->goods[this->cargo];
00161 if (ge.last_component == INVALID_LINKGRAPH_COMPONENT ||
00162 (ge.last_component + current_component_id) % 2 != 0) {
00163
00164
00165
00166
00167 if (!ge.link_stats.empty()) {
00168 this->LinkGraphComponent::Init(current_component_id + 2);
00169 CreateComponent(station);
00170 return;
00171 }
00172 }
00173 }
00174
00175 } while (this->current_station_id != last_station_id);
00176 }
00177
00183 void OnTick_LinkGraph()
00184 {
00185 if (_date_fract == LinkGraph::COMPONENTS_SPAWN_TICK ||
00186 _date_fract == LinkGraph::COMPONENTS_JOIN_TICK) {
00187
00188
00189
00190
00191 for (uint cargo = _date % _settings_game.linkgraph.recalc_interval; cargo < NUM_CARGO;
00192 cargo += _settings_game.linkgraph.recalc_interval) {
00193
00194
00195 if (_settings_game.linkgraph.GetDistributionType(cargo) == DT_MANUAL) continue;
00196
00197 if (_date_fract == LinkGraph::COMPONENTS_SPAWN_TICK) {
00198 _link_graphs[cargo].NextComponent();
00199 } else {
00200 _link_graphs[cargo].Join();
00201 }
00202 }
00203 }
00204 }
00205
00214 NodeID LinkGraphComponent::AddNode(Station *st)
00215 {
00216 GoodsEntry &good = st->goods[this->cargo];
00217 good.last_component = this->index;
00218
00219 bool do_resize = (this->nodes.size() == this->num_nodes);
00220
00221 if (do_resize) {
00222 this->nodes.push_back(Node());
00223 this->edges.push_back(std::vector<Edge>(this->num_nodes + 1));
00224 }
00225
00226 this->nodes[this->num_nodes].Init(st->index, good.supply,
00227 HasBit(good.acceptance_pickup, GoodsEntry::GES_ACCEPTANCE));
00228
00229 std::vector<Edge> &new_edges = this->edges[this->num_nodes];
00230
00231
00232 new_edges[this->num_nodes].next_edge = INVALID_NODE;
00233
00234 for (NodeID i = 0; i < this->num_nodes; ++i) {
00235 uint distance = DistanceManhattan(st->xy, Station::Get(this->nodes[i].station)->xy);
00236 if (do_resize) this->edges[i].push_back(Edge());
00237 new_edges[i].Init(distance);
00238 this->edges[i][this->num_nodes].Init(distance);
00239 }
00240 return this->num_nodes++;
00241 }
00242
00249 FORCEINLINE void LinkGraphComponent::AddEdge(NodeID from, NodeID to, uint capacity)
00250 {
00251 assert(from != to);
00252 Edge &edge = this->edges[from][to];
00253 Edge &first = this->edges[from][from];
00254 edge.capacity = capacity;
00255 edge.next_edge = first.next_edge;
00256 first.next_edge = to;
00257 }
00258
00268 void LinkGraphComponent::SetSize()
00269 {
00270 if (this->nodes.size() < this->num_nodes) {
00271 for (EdgeMatrix::iterator i = this->edges.begin(); i != this->edges.end(); ++i) {
00272 i->resize(this->num_nodes);
00273 }
00274 this->nodes.resize(this->num_nodes);
00275 this->edges.resize(this->num_nodes, std::vector<Edge>(this->num_nodes));
00276 }
00277
00278 for (uint i = 0; i < this->num_nodes; ++i) {
00279 this->nodes[i].Init();
00280 for (uint j = 0; j < this->num_nodes; ++j) {
00281 this->edges[i][j].Init();
00282 }
00283 }
00284 }
00285
00289 LinkGraphComponent::LinkGraphComponent() :
00290 settings(_settings_game.linkgraph),
00291 cargo(INVALID_CARGO),
00292 num_nodes(0),
00293 index(INVALID_LINKGRAPH_COMPONENT)
00294 {}
00295
00299 void LinkGraphComponent::Init(LinkGraphComponentID id)
00300 {
00301 assert(this->num_nodes == 0);
00302 this->index = id;
00303 this->settings = _settings_game.linkgraph;
00304 }
00305
00306
00315 void Node::ExportNewFlows(FlowMap::iterator &it, FlowStatSet &dest, CargoID cargo)
00316 {
00317 StationID source = it->first;
00318 FlowViaMap &source_flows = it->second;
00319 if (!Station::IsValidID(source)) {
00320 source_flows.clear();
00321 } else {
00322 Station *curr_station = Station::Get(this->station);
00323 for (FlowViaMap::iterator update = source_flows.begin(); update != source_flows.end();) {
00324 StationID next = update->first;
00325 int planned = update->second;
00326 assert(planned >= 0);
00327
00328 Station *via = Station::GetIfValid(next);
00329 if (planned > 0 && via != NULL) {
00330 uint distance = GetMovingAverageLength(curr_station, via);
00331 if (next != this->station) {
00332 const LinkStatMap &ls = curr_station->goods[cargo].link_stats;
00333 if (ls.find(next) != ls.end()) {
00334 dest.insert(FlowStat(distance, next, planned, 0));
00335 }
00336 } else {
00337 dest.insert(FlowStat(distance, next, planned, 0));
00338 }
00339 }
00340 source_flows.erase(update++);
00341 }
00342 }
00343 assert(source_flows.empty());
00344
00345 this->flows.erase(it++);
00346 }
00347
00352 void Node::ExportFlows(CargoID cargo)
00353 {
00354 FlowStatMap &station_flows = Station::Get(this->station)->goods[cargo].flows;
00355 FlowStatSet new_flows;
00356
00357 for (FlowStatMap::iterator station_outer_it(station_flows.begin()); station_outer_it != station_flows.end();) {
00358 FlowMap::iterator node_outer_it(this->flows.find(station_outer_it->first));
00359 if (node_outer_it == this->flows.end()) {
00360
00361 station_flows.erase(station_outer_it++);
00362 } else {
00363 FlowViaMap &source = node_outer_it->second;
00364 FlowStatSet &dest = station_outer_it->second;
00365
00366 for (FlowStatSet::iterator station_inner_it(dest.begin()); station_inner_it != dest.end();) {
00367 FlowViaMap::iterator node_inner_it(source.find(station_inner_it->Via()));
00368 if (node_inner_it != source.end()) {
00369 assert(node_inner_it->second >= 0);
00370 if (node_inner_it->second > 0) {
00371 new_flows.insert(FlowStat(*station_inner_it, node_inner_it->second));
00372 }
00373 source.erase(node_inner_it);
00374 }
00375 dest.erase(station_inner_it++);
00376 }
00377
00378 dest.swap(new_flows);
00379 assert(new_flows.empty());
00380
00381 ExportNewFlows(node_outer_it, dest, cargo);
00382
00383 ++station_outer_it;
00384 }
00385 }
00386
00387 for (FlowMap::iterator it(this->flows.begin()); it != this->flows.end();) {
00388 ExportNewFlows(it, station_flows[it->first], cargo);
00389 }
00390 assert(this->flows.empty());
00391 }
00392
00396 void LinkGraph::Join()
00397 {
00398 this->LinkGraphJob::Join();
00399
00400 for (NodeID node_id = 0; node_id < this->GetSize(); ++node_id) {
00401 Node &node = this->GetNode(node_id);
00402 if (Station::IsValidID(node.station)) {
00403 node.ExportFlows(this->cargo);
00404 InvalidateWindowData(WC_STATION_VIEW, node.station, this->GetCargo());
00405 }
00406 }
00407
00408 this->LinkGraphComponent::Clear();
00409 }
00410
00415 void LinkGraphJob::RunLinkGraphJob(void *j)
00416 {
00417 LinkGraphJob *job = (LinkGraphJob *)j;
00418 for (HandlerList::iterator i = _handlers.begin(); i != _handlers.end(); ++i) {
00419 (*i)->Run(job);
00420 }
00421 }
00422
00426 void LinkGraphJob::ClearHandlers()
00427 {
00428 for (HandlerList::iterator i = _handlers.begin(); i != _handlers.end(); ++i) {
00429 delete *i;
00430 }
00431 _handlers.clear();
00432 }
00433
00441 void Path::Fork(Path *base, uint cap, int free_cap, uint dist)
00442 {
00443 this->capacity = min(base->capacity, cap);
00444 this->free_capacity = min(base->free_capacity, free_cap);
00445 this->distance = base->distance + dist;
00446 assert(this->distance > 0);
00447 if (this->parent != base) {
00448 this->Detach();
00449 this->parent = base;
00450 this->parent->num_children++;
00451 }
00452 this->origin = base->origin;
00453 }
00454
00463 uint Path::AddFlow(uint new_flow, LinkGraphComponent *graph, bool only_positive)
00464 {
00465 if (this->parent != NULL) {
00466 Edge &edge = graph->GetEdge(this->parent->node, this->node);
00467 if (only_positive) {
00468 uint usable_cap = edge.capacity * graph->GetSettings().short_path_saturation / 100;
00469 if (usable_cap > edge.flow) {
00470 new_flow = min(new_flow, usable_cap - edge.flow);
00471 } else {
00472 return 0;
00473 }
00474 }
00475 new_flow = this->parent->AddFlow(new_flow, graph, only_positive);
00476 if (new_flow > 0) {
00477 graph->GetNode(this->parent->node).paths.insert(this);
00478 }
00479 edge.flow += new_flow;
00480 }
00481 this->flow += new_flow;
00482 return new_flow;
00483 }
00484
00490 Path::Path(NodeID n, bool source) :
00491 distance(source ? 0 : UINT_MAX),
00492 capacity(0),
00493 free_capacity(source ? INT_MAX : INT_MIN),
00494 flow(0), node(n), origin(source ? n : INVALID_NODE),
00495 num_children(0), parent(NULL)
00496 {}
00497
00501 FORCEINLINE void LinkGraphJob::Join()
00502 {
00503 if (this->thread == NULL) return;
00504 this->thread->Join();
00505 delete this->thread;
00506 this->thread = NULL;
00507 }
00508
00513 void LinkGraphJob::SpawnThread()
00514 {
00515 assert(this->thread == NULL);
00516 if (!ThreadObject::New(&(LinkGraphJob::RunLinkGraphJob), this, &(this->thread))) {
00517 this->thread = NULL;
00518
00519
00520
00521
00522
00523
00524
00525
00526 LinkGraphJob::RunLinkGraphJob(this);
00527 }
00528 }
00529
00535 void LinkGraph::Init(CargoID cargo)
00536 {
00537 this->LinkGraphJob::Join();
00538 this->LinkGraphComponent::Clear();
00539
00540 this->current_station_id = 0;
00541 this->LinkGraphComponent::cargo = cargo;
00542 }
00543
00547 void InitializeLinkGraphs()
00548 {
00549 for (CargoID c = 0; c < NUM_CARGO; ++c) _link_graphs[c].Init(c);
00550
00551 LinkGraphJob::ClearHandlers();
00552 LinkGraphJob::AddHandler(new DemandHandler);
00553 LinkGraphJob::AddHandler(new MCFHandler<MCF1stPass>);
00554 LinkGraphJob::AddHandler(new FlowMapper);
00555 LinkGraphJob::AddHandler(new MCFHandler<MCF2ndPass>);
00556 LinkGraphJob::AddHandler(new FlowMapper);
00557 }