Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "../stdafx.h"
00013 #include "../core/pool_func.hpp"
00014 #include "linkgraph.h"
00015
00016
00017 LinkGraphPool _link_graph_pool("LinkGraph");
00018 INSTANTIATE_POOL_METHODS(LinkGraph)
00019
00020
00025 inline void LinkGraph::BaseNode::Init(StationID st, uint demand)
00026 {
00027 this->supply = 0;
00028 this->demand = demand;
00029 this->station = st;
00030 this->last_update = INVALID_DATE;
00031 }
00032
00037 inline void LinkGraph::BaseEdge::Init(uint distance)
00038 {
00039 this->distance = distance;
00040 this->capacity = 0;
00041 this->usage = 0;
00042 this->last_unrestricted_update = INVALID_DATE;
00043 this->last_restricted_update = INVALID_DATE;
00044 this->next_edge = INVALID_NODE;
00045 }
00046
00052 void LinkGraph::ShiftDates(int interval)
00053 {
00054 this->last_compression += interval;
00055 for (NodeID node1 = 0; node1 < this->Size(); ++node1) {
00056 BaseNode &source = this->nodes[node1];
00057 if (source.last_update != INVALID_DATE) source.last_update += interval;
00058 for (NodeID node2 = 0; node2 < this->Size(); ++node2) {
00059 BaseEdge &edge = this->edges[node1][node2];
00060 if (edge.last_unrestricted_update != INVALID_DATE) edge.last_unrestricted_update += interval;
00061 if (edge.last_restricted_update != INVALID_DATE) edge.last_restricted_update += interval;
00062 }
00063 }
00064 }
00065
00066 void LinkGraph::Compress()
00067 {
00068 this->last_compression = (_date + this->last_compression) / 2;
00069 for (NodeID node1 = 0; node1 < this->Size(); ++node1) {
00070 this->nodes[node1].supply /= 2;
00071 for (NodeID node2 = 0; node2 < this->Size(); ++node2) {
00072 BaseEdge &edge = this->edges[node1][node2];
00073 if (edge.capacity > 0) {
00074 edge.capacity = max(1U, edge.capacity / 2);
00075 edge.usage /= 2;
00076 }
00077 }
00078 }
00079 }
00080
00085 void LinkGraph::Merge(LinkGraph *other)
00086 {
00087 Date age = _date - this->last_compression + 1;
00088 Date other_age = _date - other->last_compression + 1;
00089 NodeID first = this->Size();
00090 for (NodeID node1 = 0; node1 < other->Size(); ++node1) {
00091 Station *st = Station::Get(other->nodes[node1].station);
00092 NodeID new_node = this->AddNode(st);
00093 this->nodes[new_node].supply = LinkGraph::Scale(other->nodes[node1].supply, age, other_age);
00094 st->goods[this->cargo].link_graph = this->index;
00095 st->goods[this->cargo].node = new_node;
00096 for (NodeID node2 = 0; node2 < node1; ++node2) {
00097 BaseEdge &forward = this->edges[new_node][first + node2];
00098 BaseEdge &backward = this->edges[first + node2][new_node];
00099 forward = other->edges[node1][node2];
00100 backward = other->edges[node2][node1];
00101 forward.capacity = LinkGraph::Scale(forward.capacity, age, other_age);
00102 forward.usage = LinkGraph::Scale(forward.usage, age, other_age);
00103 if (forward.next_edge != INVALID_NODE) forward.next_edge += first;
00104 backward.capacity = LinkGraph::Scale(backward.capacity, age, other_age);
00105 backward.usage = LinkGraph::Scale(backward.usage, age, other_age);
00106 if (backward.next_edge != INVALID_NODE) backward.next_edge += first;
00107 }
00108 BaseEdge &new_start = this->edges[new_node][new_node];
00109 new_start = other->edges[node1][node1];
00110 if (new_start.next_edge != INVALID_NODE) new_start.next_edge += first;
00111 }
00112 delete other;
00113 }
00114
00119 void LinkGraph::RemoveNode(NodeID id)
00120 {
00121 assert(id < this->Size());
00122
00123 NodeID last_node = this->Size() - 1;
00124 for (NodeID i = 0; i <= last_node; ++i) {
00125 (*this)[i].RemoveEdge(id);
00126 BaseEdge *node_edges = this->edges[i];
00127 NodeID prev = i;
00128 NodeID next = node_edges[i].next_edge;
00129 while (next != INVALID_NODE) {
00130 if (next == last_node) {
00131 node_edges[prev].next_edge = id;
00132 break;
00133 }
00134 prev = next;
00135 next = node_edges[prev].next_edge;
00136 }
00137 node_edges[id] = node_edges[last_node];
00138 }
00139 Station::Get(this->nodes[last_node].station)->goods[this->cargo].node = id;
00140 this->nodes.Erase(this->nodes.Get(id));
00141 this->edges.EraseColumn(id);
00142
00143
00144
00145 }
00146
00155 NodeID LinkGraph::AddNode(const Station *st)
00156 {
00157 const GoodsEntry &good = st->goods[this->cargo];
00158
00159 NodeID new_node = this->Size();
00160 this->nodes.Append();
00161
00162
00163 this->edges.Resize(new_node + 1U,
00164 max(new_node + 1U, this->edges.Height()));
00165
00166 this->nodes[new_node].Init(st->index,
00167 HasBit(good.acceptance_pickup, GoodsEntry::GES_ACCEPTANCE));
00168
00169 BaseEdge *new_edges = this->edges[new_node];
00170
00171
00172 new_edges[new_node].next_edge = INVALID_NODE;
00173
00174 for (NodeID i = 0; i <= new_node; ++i) {
00175 uint distance = DistanceManhattan(st->xy, Station::Get(this->nodes[i].station)->xy);
00176 new_edges[i].Init(distance);
00177 this->edges[i][new_node].Init(distance);
00178 }
00179 return new_node;
00180 }
00181
00189 void LinkGraph::Node::AddEdge(NodeID to, uint capacity, uint usage)
00190 {
00191 assert(this->index != to);
00192 BaseEdge &edge = this->edges[to];
00193 BaseEdge &first = this->edges[this->index];
00194 edge.capacity = capacity;
00195 edge.next_edge = first.next_edge;
00196 first.next_edge = to;
00197 switch (usage) {
00198 case REFRESH_UNRESTRICTED:
00199 edge.last_unrestricted_update = _date;
00200 break;
00201 case REFRESH_RESTRICTED:
00202 edge.last_restricted_update = _date;
00203 break;
00204 default:
00205 edge.usage = usage;
00206 break;
00207 }
00208 }
00209
00216 void LinkGraph::Node::UpdateEdge(NodeID to, uint capacity, uint usage)
00217 {
00218 assert(capacity > 0);
00219 assert(usage <= capacity || usage == REFRESH_RESTRICTED || usage == REFRESH_UNRESTRICTED);
00220 if (this->edges[to].capacity == 0) {
00221 this->AddEdge(to, capacity, usage);
00222 } else {
00223 (*this)[to].Update(capacity, usage);
00224 }
00225 }
00226
00231 void LinkGraph::Node::RemoveEdge(NodeID to)
00232 {
00233 if (this->index == to) return;
00234 BaseEdge &edge = this->edges[to];
00235 edge.capacity = 0;
00236 edge.last_unrestricted_update = INVALID_DATE;
00237 edge.last_restricted_update = INVALID_DATE;
00238 edge.usage = 0;
00239
00240 NodeID prev = this->index;
00241 NodeID next = this->edges[this->index].next_edge;
00242 while (next != INVALID_NODE) {
00243 if (next == to) {
00244
00245 this->edges[prev].next_edge = edge.next_edge;
00246 edge.next_edge = INVALID_NODE;
00247 break;
00248 } else {
00249 prev = next;
00250 next = this->edges[next].next_edge;
00251 }
00252 }
00253 }
00254
00264 void LinkGraph::Edge::Update(uint capacity, uint usage)
00265 {
00266 assert(this->edge.capacity > 0);
00267 if (usage > capacity) {
00268 this->edge.capacity = max(this->edge.capacity, capacity);
00269 switch (usage) {
00270 case REFRESH_UNRESTRICTED:
00271 this->edge.last_unrestricted_update = _date;
00272 break;
00273 case REFRESH_RESTRICTED:
00274 this->edge.last_restricted_update = _date;
00275 break;
00276 default:
00277 NOT_REACHED();
00278 break;
00279 }
00280 } else {
00281 this->edge.capacity += capacity;
00282 this->edge.usage += usage;
00283 }
00284 }
00285
00291 void LinkGraph::Init(uint size)
00292 {
00293 assert(this->Size() == 0);
00294 this->edges.Resize(size, size);
00295 this->nodes.Resize(size);
00296
00297 for (uint i = 0; i < size; ++i) {
00298 this->nodes[i].Init();
00299 BaseEdge *column = this->edges[i];
00300 for (uint j = 0; j < size; ++j) column[j].Init();
00301 }
00302 }