primeiro commit

This commit is contained in:
2024-08-10 10:51:42 -03:00
commit 13a9ba0750
1569 changed files with 721067 additions and 0 deletions

284
src/linkgraph/demands.cpp Normal file
View File

@@ -0,0 +1,284 @@
/** @file demands.cpp Definition of demand calculating link graph handler. */
#include "../stdafx.h"
#include "demands.h"
#include <list>
#include "../safeguards.h"
typedef std::list<NodeID> NodeList;
/**
* Scale various things according to symmetric/asymmetric distribution.
*/
class Scaler {
public:
void SetDemands(LinkGraphJob &job, NodeID from, NodeID to, uint demand_forw);
};
/**
* Scaler for symmetric distribution.
*/
class SymmetricScaler : public Scaler {
public:
/**
* Constructor.
* @param mod_size Size modifier to be used. Determines how much demands
* increase with the supply of the remote station.
*/
inline SymmetricScaler(uint mod_size) : mod_size(mod_size), supply_sum(0),
demand_per_node(0)
{}
/**
* Count a node's supply into the sum of supplies.
* @param node Node.
*/
inline void AddNode(const Node &node)
{
this->supply_sum += node.Supply();
}
/**
* Calculate the mean demand per node using the sum of supplies.
* @param num_demands Number of accepting nodes.
*/
inline void SetDemandPerNode(uint num_demands)
{
this->demand_per_node = max(this->supply_sum / num_demands, 1U);
}
/**
* Get the effective supply of one node towards another one. In symmetric
* distribution the supply of the other node is weighed in.
* @param from The supplying node.
* @param to The receiving node.
* @return Effective supply.
*/
inline uint EffectiveSupply(const Node &from, const Node &to)
{
return max(from.Supply() * max(1U, to.Supply()) * this->mod_size / 100 / this->demand_per_node, 1U);
}
/**
* Check if there is any acceptance left for this node. In symmetric distribution
* nodes only accept anything if they also supply something. So if
* undelivered_supply == 0 at the node there isn't any demand left either.
* @param to Node to be checked.
* @return If demand is left.
*/
inline bool HasDemandLeft(const Node &to)
{
return (to.Supply() == 0 || to.UndeliveredSupply() > 0) && to.Demand() > 0;
}
void SetDemands(LinkGraphJob &job, NodeID from, NodeID to, uint demand_forw);
private:
uint mod_size; ///< Size modifier. Determines how much demands increase with the supply of the remote station.
uint supply_sum; ///< Sum of all supplies in the component.
uint demand_per_node; ///< Mean demand associated with each node.
};
/**
* A scaler for asymmetric distribution.
*/
class AsymmetricScaler : public Scaler {
public:
/**
* Nothing to do here.
* @param unused.
*/
inline void AddNode(const Node &)
{
}
/**
* Nothing to do here.
* @param unused.
*/
inline void SetDemandPerNode(uint)
{
}
/**
* Get the effective supply of one node towards another one.
* @param from The supplying node.
* @param unused.
*/
inline uint EffectiveSupply(const Node &from, const Node &)
{
return from.Supply();
}
/**
* Check if there is any acceptance left for this node. In asymmetric distribution
* nodes always accept as long as their demand > 0.
* @param to The node to be checked.
* @param to_anno Unused.
*/
inline bool HasDemandLeft(const Node &to) { return to.Demand() > 0; }
};
/**
* Set the demands between two nodes using the given base demand. In symmetric mode
* this sets demands in both directions.
* @param job The link graph job.
* @param from_id The supplying node.
* @param to_id The receiving node.
* @param demand_forw Demand calculated for the "forward" direction.
*/
void SymmetricScaler::SetDemands(LinkGraphJob &job, NodeID from_id, NodeID to_id, uint demand_forw)
{
if (job[from_id].Demand() > 0) {
uint demand_back = demand_forw * this->mod_size / 100;
uint undelivered = job[to_id].UndeliveredSupply();
if (demand_back > undelivered) {
demand_back = undelivered;
demand_forw = max(1U, demand_back * 100 / this->mod_size);
}
this->Scaler::SetDemands(job, to_id, from_id, demand_back);
}
this->Scaler::SetDemands(job, from_id, to_id, demand_forw);
}
/**
* Set the demands between two nodes using the given base demand. In asymmetric mode
* this only sets demand in the "forward" direction.
* @param job The link graph job.
* @param from_id The supplying node.
* @param to_id The receiving node.
* @param demand_forw Demand calculated for the "forward" direction.
*/
inline void Scaler::SetDemands(LinkGraphJob &job, NodeID from_id, NodeID to_id, uint demand_forw)
{
job[from_id].DeliverSupply(to_id, demand_forw);
}
/**
* Do the actual demand calculation, called from constructor.
* @param job Job to calculate the demands for.
* @tparam Tscaler Scaler to be used for scaling demands.
*/
template<class Tscaler>
void DemandCalculator::CalcDemand(LinkGraphJob &job, Tscaler scaler)
{
NodeList supplies;
NodeList demands;
uint num_supplies = 0;
uint num_demands = 0;
for (NodeID node = 0; node < job.Size(); node++) {
scaler.AddNode(job[node]);
if (job[node].Supply() > 0) {
supplies.push_back(node);
num_supplies++;
}
if (job[node].Demand() > 0) {
demands.push_back(node);
num_demands++;
}
}
if (num_supplies == 0 || num_demands == 0) return;
/* Mean acceptance attributed to each node. If the distribution is
* symmetric this is relative to remote supply, otherwise it is
* relative to remote demand. */
scaler.SetDemandPerNode(num_demands);
uint chance = 0;
while (!supplies.empty() && !demands.empty()) {
NodeID from_id = supplies.front();
supplies.pop_front();
for (uint i = 0; i < num_demands; ++i) {
assert(!demands.empty());
NodeID to_id = demands.front();
demands.pop_front();
if (from_id == to_id) {
/* Only one node with supply and demand left */
if (demands.empty() && supplies.empty()) return;
demands.push_back(to_id);
continue;
}
int32 supply = scaler.EffectiveSupply(job[from_id], job[to_id]);
assert(supply > 0);
/* Scale the distance by mod_dist around max_distance */
int32 distance = this->max_distance - (this->max_distance -
(int32)DistanceMaxPlusManhattan(job[from_id].XY(), job[to_id].XY())) *
this->mod_dist / 100;
/* Scale the accuracy by distance around accuracy / 2 */
int32 divisor = this->accuracy * (this->mod_dist - 50) / 100 +
this->accuracy * distance / this->max_distance + 1;
assert(divisor > 0);
uint demand_forw = 0;
if (divisor <= supply) {
/* At first only distribute demand if
* effective supply / accuracy divisor >= 1
* Others are too small or too far away to be considered. */
demand_forw = supply / divisor;
} else if (++chance > this->accuracy * num_demands * num_supplies) {
/* After some trying, if there is still supply left, distribute
* demand also to other nodes. */
demand_forw = 1;
}
demand_forw = min(demand_forw, job[from_id].UndeliveredSupply());
scaler.SetDemands(job, from_id, to_id, demand_forw);
if (scaler.HasDemandLeft(job[to_id])) {
demands.push_back(to_id);
} else {
num_demands--;
}
if (job[from_id].UndeliveredSupply() == 0) break;
}
if (job[from_id].UndeliveredSupply() != 0) {
supplies.push_back(from_id);
} else {
num_supplies--;
}
}
}
/**
* Create the DemandCalculator and immediately do the calculation.
* @param job Job to calculate the demands for.
*/
DemandCalculator::DemandCalculator(LinkGraphJob &job) :
max_distance(DistanceMaxPlusManhattan(TileXY(0,0), TileXY(MapMaxX(), MapMaxY())))
{
const LinkGraphSettings &settings = job.Settings();
CargoID cargo = job.Cargo();
this->accuracy = settings.accuracy;
this->mod_dist = settings.demand_distance;
if (this->mod_dist > 100) {
/* Increase effect of mod_dist > 100 */
int over100 = this->mod_dist - 100;
this->mod_dist = 100 + over100 * over100;
}
switch (settings.GetDistributionType(cargo)) {
case DT_SYMMETRIC:
this->CalcDemand<SymmetricScaler>(job, SymmetricScaler(settings.demand_size));
break;
case DT_ASYMMETRIC:
this->CalcDemand<AsymmetricScaler>(job, AsymmetricScaler());
break;
default:
/* Nothing to do. */
break;
}
}

43
src/linkgraph/demands.h Normal file
View File

@@ -0,0 +1,43 @@
/** @file demands.h Declaration of demand calculating link graph handler. */
#ifndef DEMANDS_H
#define DEMANDS_H
#include "linkgraphjob_base.h"
/**
* Calculate the demands. This class has a state, but is recreated for each
* call to of DemandHandler::Run.
*/
class DemandCalculator {
public:
DemandCalculator(LinkGraphJob &job);
private:
int32 max_distance; ///< Maximum distance possible on the map.
int32 mod_dist; ///< Distance modifier, determines how much demands decrease with distance.
int32 accuracy; ///< Accuracy of the calculation.
template<class Tscaler>
void CalcDemand(LinkGraphJob &job, Tscaler scaler);
};
/**
* Stateless, thread safe demand hander. Doesn't do anything but call DemandCalculator.
*/
class DemandHandler : public ComponentHandler {
public:
/**
* Call the demand calculator on the given component.
* @param graph Component to calculate the demands for.
*/
virtual void Run(LinkGraphJob &job) const { DemandCalculator c(job); }
/**
* Virtual destructor has to be defined because of virtual Run().
*/
virtual ~DemandHandler() {}
};
#endif /* DEMANDS_H */

View File

@@ -0,0 +1,69 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file flowmapper.cpp Definition of flowmapper. */
#include "../stdafx.h"
#include "flowmapper.h"
#include "../safeguards.h"
/**
* Map the paths generated by the MCF solver into flows associated with nodes.
* @param component the link graph component to be used.
*/
void FlowMapper::Run(LinkGraphJob &job) const
{
for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
Node prev_node = job[node_id];
StationID prev = prev_node.Station();
PathList &paths = prev_node.Paths();
for (PathList::iterator i = paths.begin(); i != paths.end(); ++i) {
Path *path = *i;
uint flow = path->GetFlow();
if (flow == 0) break;
Node node = job[path->GetNode()];
StationID via = node.Station();
StationID origin = job[path->GetOrigin()].Station();
assert(prev != via && via != origin);
/* Mark all of the flow for local consumption at "first". */
node.Flows().AddFlow(origin, via, flow);
if (prev != origin) {
/* Pass some of the flow marked for local consumption at "prev" on
* to this node. */
prev_node.Flows().PassOnFlow(origin, via, flow);
} else {
/* Prev node is origin. Simply add flow. */
prev_node.Flows().AddFlow(origin, via, flow);
}
}
}
for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
/* Remove local consumption shares marked as invalid. */
Node node = job[node_id];
FlowStatMap &flows = node.Flows();
flows.FinalizeLocalConsumption(node.Station());
if (this->scale) {
/* Scale by time the graph has been running without being compressed. Add 1 to avoid
* division by 0 if spawn date == last compression date. This matches
* LinkGraph::Monthly(). */
uint runtime = job.JoinDate() - job.Settings().recalc_time - job.LastCompression() + 1;
for (FlowStatMap::iterator i = flows.begin(); i != flows.end(); ++i) {
i->second.ScaleToMonthly(runtime);
}
}
/* Clear paths. */
PathList &paths = node.Paths();
for (PathList::iterator i = paths.begin(); i != paths.end(); ++i) {
delete *i;
}
paths.clear();
}
}

View File

@@ -0,0 +1,47 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file flowmapper.h Declaration of flow mapper; maps paths into flows at nodes. */
#ifndef FLOWMAPPER_H_
#define FLOWMAPPER_H_
#include "linkgraphjob_base.h"
/**
* Map the path trees generated by the MCF solver into flows. The path tree is
* useful to cache capacities and distances and allow quick disconnecting and
* reconnecting to other paths. The flows show how much cargo from which nodes
* is to be routed in which direction at a given node. This is what we need in
* the end.
*/
class FlowMapper : public ComponentHandler {
public:
/**
* Create a flow mapper.
* @param scale Whether the flow mapper should scale all flows to monthly
* values. Only do that on the very last flow mapping.
*/
FlowMapper(bool scale) : scale(scale) {}
virtual void Run(LinkGraphJob &job) const;
/**
* Virtual destructor has to be defined because of virtual Run().
*/
virtual ~FlowMapper() {}
private:
/**
* Whether the flow mapper should scale all flows to monthly values.
*/
const bool scale;
};
#endif /* FLOWMAPPER_H_ */

27
src/linkgraph/init.h Normal file
View File

@@ -0,0 +1,27 @@
/** @file init.h Declaration of initializing link graph handler. */
#ifndef INIT_H
#define INIT_H
#include "linkgraphjob_base.h"
/**
* Stateless, thread safe initialization hander. Initializes node and edge
* annotations.
*/
class InitHandler : public ComponentHandler {
public:
/**
* Initialize the link graph job.
* @param job Job to be initialized.
*/
virtual void Run(LinkGraphJob &job) const { job.Init(); }
/**
* Virtual destructor has to be defined because of virtual Run().
*/
virtual ~InitHandler() {}
};
#endif /* INIT_H */

293
src/linkgraph/linkgraph.cpp Normal file
View File

@@ -0,0 +1,293 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file linkgraph.cpp Definition of link graph classes used for cargo distribution. */
#include "../stdafx.h"
#include "../core/pool_func.hpp"
#include "linkgraph.h"
#include "../safeguards.h"
/* Initialize the link-graph-pool */
LinkGraphPool _link_graph_pool("LinkGraph");
INSTANTIATE_POOL_METHODS(LinkGraph)
/**
* Create a node or clear it.
* @param xy Location of the associated station.
* @param st ID of the associated station.
* @param demand Demand for cargo at the station.
*/
inline void LinkGraph::BaseNode::Init(TileIndex xy, StationID st, uint demand)
{
this->xy = xy;
this->supply = 0;
this->demand = demand;
this->station = st;
this->last_update = INVALID_DATE;
}
/**
* Create an edge.
*/
inline void LinkGraph::BaseEdge::Init()
{
this->capacity = 0;
this->usage = 0;
this->last_unrestricted_update = INVALID_DATE;
this->last_restricted_update = INVALID_DATE;
this->next_edge = INVALID_NODE;
}
/**
* Shift all dates by given interval.
* This is useful if the date has been modified with the cheat menu.
* @param interval Number of days to be added or subtracted.
*/
void LinkGraph::ShiftDates(int interval)
{
this->last_compression += interval;
for (NodeID node1 = 0; node1 < this->Size(); ++node1) {
BaseNode &source = this->nodes[node1];
if (source.last_update != INVALID_DATE) source.last_update += interval;
for (NodeID node2 = 0; node2 < this->Size(); ++node2) {
BaseEdge &edge = this->edges[node1][node2];
if (edge.last_unrestricted_update != INVALID_DATE) edge.last_unrestricted_update += interval;
if (edge.last_restricted_update != INVALID_DATE) edge.last_restricted_update += interval;
}
}
}
void LinkGraph::Compress()
{
this->last_compression = (_date + this->last_compression) / 2;
for (NodeID node1 = 0; node1 < this->Size(); ++node1) {
this->nodes[node1].supply /= 2;
for (NodeID node2 = 0; node2 < this->Size(); ++node2) {
BaseEdge &edge = this->edges[node1][node2];
if (edge.capacity > 0) {
edge.capacity = max(1U, edge.capacity / 2);
edge.usage /= 2;
}
}
}
}
/**
* Merge a link graph with another one.
* @param other LinkGraph to be merged into this one.
*/
void LinkGraph::Merge(LinkGraph *other)
{
Date age = _date - this->last_compression + 1;
Date other_age = _date - other->last_compression + 1;
NodeID first = this->Size();
for (NodeID node1 = 0; node1 < other->Size(); ++node1) {
Station *st = Station::Get(other->nodes[node1].station);
NodeID new_node = this->AddNode(st);
this->nodes[new_node].supply = LinkGraph::Scale(other->nodes[node1].supply, age, other_age);
st->goods[this->cargo].link_graph = this->index;
st->goods[this->cargo].node = new_node;
for (NodeID node2 = 0; node2 < node1; ++node2) {
BaseEdge &forward = this->edges[new_node][first + node2];
BaseEdge &backward = this->edges[first + node2][new_node];
forward = other->edges[node1][node2];
backward = other->edges[node2][node1];
forward.capacity = LinkGraph::Scale(forward.capacity, age, other_age);
forward.usage = LinkGraph::Scale(forward.usage, age, other_age);
if (forward.next_edge != INVALID_NODE) forward.next_edge += first;
backward.capacity = LinkGraph::Scale(backward.capacity, age, other_age);
backward.usage = LinkGraph::Scale(backward.usage, age, other_age);
if (backward.next_edge != INVALID_NODE) backward.next_edge += first;
}
BaseEdge &new_start = this->edges[new_node][new_node];
new_start = other->edges[node1][node1];
if (new_start.next_edge != INVALID_NODE) new_start.next_edge += first;
}
delete other;
}
/**
* Remove a node from the link graph by overwriting it with the last node.
* @param id ID of the node to be removed.
*/
void LinkGraph::RemoveNode(NodeID id)
{
assert(id < this->Size());
NodeID last_node = this->Size() - 1;
for (NodeID i = 0; i <= last_node; ++i) {
(*this)[i].RemoveEdge(id);
BaseEdge *node_edges = this->edges[i];
NodeID prev = i;
NodeID next = node_edges[i].next_edge;
while (next != INVALID_NODE) {
if (next == last_node) {
node_edges[prev].next_edge = id;
break;
}
prev = next;
next = node_edges[prev].next_edge;
}
node_edges[id] = node_edges[last_node];
}
Station::Get(this->nodes[last_node].station)->goods[this->cargo].node = id;
this->nodes.Erase(this->nodes.Get(id));
this->edges.EraseColumn(id);
/* Not doing EraseRow here, as having the extra invalid row doesn't hurt
* and removing it would trigger a lot of memmove. The data has already
* been copied around in the loop above. */
}
/**
* Add a node to the component and create empty edges associated with it. Set
* the station's last_component to this component. Calculate the distances to all
* other nodes. The distances to _all_ nodes are important as the demand
* calculator relies on their availability.
* @param st New node's station.
* @return New node's ID.
*/
NodeID LinkGraph::AddNode(const Station *st)
{
const GoodsEntry &good = st->goods[this->cargo];
NodeID new_node = this->Size();
this->nodes.Append();
/* Avoid reducing the height of the matrix as that is expensive and we
* most likely will increase it again later which is again expensive. */
this->edges.Resize(new_node + 1U,
max(new_node + 1U, this->edges.Height()));
this->nodes[new_node].Init(st->xy, st->index,
HasBit(good.status, GoodsEntry::GES_ACCEPTANCE));
BaseEdge *new_edges = this->edges[new_node];
/* Reset the first edge starting at the new node */
new_edges[new_node].next_edge = INVALID_NODE;
for (NodeID i = 0; i <= new_node; ++i) {
new_edges[i].Init();
this->edges[i][new_node].Init();
}
return new_node;
}
/**
* Fill an edge with values from a link. Set the restricted or unrestricted
* update timestamp according to the given update mode.
* @param to Destination node of the link.
* @param capacity Capacity of the link.
* @param usage Usage to be added.
* @param mode Update mode to be used.
*/
void LinkGraph::Node::AddEdge(NodeID to, uint capacity, uint usage, EdgeUpdateMode mode)
{
assert(this->index != to);
BaseEdge &edge = this->edges[to];
BaseEdge &first = this->edges[this->index];
edge.capacity = capacity;
edge.usage = usage;
edge.next_edge = first.next_edge;
first.next_edge = to;
if (mode & EUM_UNRESTRICTED) edge.last_unrestricted_update = _date;
if (mode & EUM_RESTRICTED) edge.last_restricted_update = _date;
}
/**
* Creates an edge if none exists yet or updates an existing edge.
* @param to Target node.
* @param capacity Capacity of the link.
* @param usage Usage to be added.
* @param mode Update mode to be used.
*/
void LinkGraph::Node::UpdateEdge(NodeID to, uint capacity, uint usage, EdgeUpdateMode mode)
{
assert(capacity > 0);
assert(usage <= capacity);
if (this->edges[to].capacity == 0) {
this->AddEdge(to, capacity, usage, mode);
} else {
(*this)[to].Update(capacity, usage, mode);
}
}
/**
* Remove an outgoing edge from this node.
* @param to ID of destination node.
*/
void LinkGraph::Node::RemoveEdge(NodeID to)
{
if (this->index == to) return;
BaseEdge &edge = this->edges[to];
edge.capacity = 0;
edge.last_unrestricted_update = INVALID_DATE;
edge.last_restricted_update = INVALID_DATE;
edge.usage = 0;
NodeID prev = this->index;
NodeID next = this->edges[this->index].next_edge;
while (next != INVALID_NODE) {
if (next == to) {
/* Will be removed, skip it. */
this->edges[prev].next_edge = edge.next_edge;
edge.next_edge = INVALID_NODE;
break;
} else {
prev = next;
next = this->edges[next].next_edge;
}
}
}
/**
* Update an edge. If mode contains UM_REFRESH refresh the edge to have at
* least the given capacity and usage, otherwise add the capacity and usage.
* In any case set the respective update timestamp(s), according to the given
* mode.
* @param from Start node of the edge.
* @param to End node of the edge.
* @param capacity Capacity to be added/updated.
* @param usage Usage to be added.
* @param mode Update mode to be applied.
*/
void LinkGraph::Edge::Update(uint capacity, uint usage, EdgeUpdateMode mode)
{
assert(this->edge.capacity > 0);
assert(capacity >= usage);
if (mode & EUM_INCREASE) {
this->edge.capacity += capacity;
this->edge.usage += usage;
} else if (mode & EUM_REFRESH) {
this->edge.capacity = max(this->edge.capacity, capacity);
this->edge.usage = max(this->edge.usage, usage);
}
if (mode & EUM_UNRESTRICTED) this->edge.last_unrestricted_update = _date;
if (mode & EUM_RESTRICTED) this->edge.last_restricted_update = _date;
}
/**
* Resize the component and fill it with empty nodes and edges. Used when
* loading from save games. The component is expected to be empty before.
* @param size New size of the component.
*/
void LinkGraph::Init(uint size)
{
assert(this->Size() == 0);
this->edges.Resize(size, size);
this->nodes.Resize(size);
for (uint i = 0; i < size; ++i) {
this->nodes[i].Init();
BaseEdge *column = this->edges[i];
for (uint j = 0; j < size; ++j) column[j].Init();
}
}

541
src/linkgraph/linkgraph.h Normal file
View File

@@ -0,0 +1,541 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file linkgraph.h Declaration of link graph classes used for cargo distribution. */
#ifndef LINKGRAPH_H
#define LINKGRAPH_H
#include "../core/pool_type.hpp"
#include "../core/smallmap_type.hpp"
#include "../core/smallmatrix_type.hpp"
#include "../station_base.h"
#include "../cargotype.h"
#include "../date_func.h"
#include "linkgraph_type.h"
struct SaveLoad;
class LinkGraph;
/**
* Type of the pool for link graph components. Each station can be in at up to
* 32 link graphs. So we allow for plenty of them to be created.
*/
typedef Pool<LinkGraph, LinkGraphID, 32, 0xFFFF> LinkGraphPool;
/** The actual pool with link graphs. */
extern LinkGraphPool _link_graph_pool;
/**
* A connected component of a link graph. Contains a complete set of stations
* connected by links as nodes and edges. Each component also holds a copy of
* the link graph settings at the time of its creation. The global settings
* might change between the creation and join time so we can't rely on them.
*/
class LinkGraph : public LinkGraphPool::PoolItem<&_link_graph_pool> {
public:
/**
* Node of the link graph. contains all relevant information from the associated
* station. It's copied so that the link graph job can work on its own data set
* in a separate thread.
*/
struct BaseNode {
uint supply; ///< Supply at the station.
uint demand; ///< Acceptance at the station.
StationID station; ///< Station ID.
TileIndex xy; ///< Location of the station referred to by the node.
Date last_update; ///< When the supply was last updated.
void Init(TileIndex xy = INVALID_TILE, StationID st = INVALID_STATION, uint demand = 0);
};
/**
* An edge in the link graph. Corresponds to a link between two stations or at
* least the distance between them. Edges from one node to itself contain the
* ID of the opposite Node of the first active edge (i.e. not just distance) in
* the column as next_edge.
*/
struct BaseEdge {
uint capacity; ///< Capacity of the link.
uint usage; ///< Usage of the link.
Date last_unrestricted_update; ///< When the unrestricted part of the link was last updated.
Date last_restricted_update; ///< When the restricted part of the link was last updated.
NodeID next_edge; ///< Destination of next valid edge starting at the same source node.
void Init();
};
/**
* Wrapper for an edge (const or not) allowing retrieval, but no modification.
* @tparam Tedge Actual edge class, may be "const BaseEdge" or just "BaseEdge".
*/
template<typename Tedge>
class EdgeWrapper {
protected:
Tedge &edge; ///< Actual edge to be used.
public:
/**
* Wrap a an edge.
* @param edge Edge to be wrapped.
*/
EdgeWrapper (Tedge &edge) : edge(edge) {}
/**
* Get edge's capacity.
* @return Capacity.
*/
uint Capacity() const { return this->edge.capacity; }
/**
* Get edge's usage.
* @return Usage.
*/
uint Usage() const { return this->edge.usage; }
/**
* Get the date of the last update to the edge's unrestricted capacity.
* @return Last update.
*/
Date LastUnrestrictedUpdate() const { return this->edge.last_unrestricted_update; }
/**
* Get the date of the last update to the edge's restricted capacity.
* @return Last update.
*/
Date LastRestrictedUpdate() const { return this->edge.last_restricted_update; }
/**
* Get the date of the last update to any part of the edge's capacity.
* @return Last update.
*/
Date LastUpdate() const { return max(this->edge.last_unrestricted_update, this->edge.last_restricted_update); }
};
/**
* Wrapper for a node (const or not) allowing retrieval, but no modification.
* @tparam Tedge Actual node class, may be "const BaseNode" or just "BaseNode".
* @tparam Tedge Actual edge class, may be "const BaseEdge" or just "BaseEdge".
*/
template<typename Tnode, typename Tedge>
class NodeWrapper {
protected:
Tnode &node; ///< Node being wrapped.
Tedge *edges; ///< Outgoing edges for wrapped node.
NodeID index; ///< ID of wrapped node.
public:
/**
* Wrap a node.
* @param node Node to be wrapped.
* @param edges Outgoing edges for node to be wrapped.
* @param index ID of node to be wrapped.
*/
NodeWrapper(Tnode &node, Tedge *edges, NodeID index) : node(node),
edges(edges), index(index) {}
/**
* Get supply of wrapped node.
* @return Supply.
*/
uint Supply() const { return this->node.supply; }
/**
* Get demand of wrapped node.
* @return Demand.
*/
uint Demand() const { return this->node.demand; }
/**
* Get ID of station belonging to wrapped node.
* @return ID of node's station.
*/
StationID Station() const { return this->node.station; }
/**
* Get node's last update.
* @return Last update.
*/
Date LastUpdate() const { return this->node.last_update; }
/**
* Get the location of the station associated with the node.
* @return Location of the station.
*/
TileIndex XY() const { return this->node.xy; }
};
/**
* Base class for iterating across outgoing edges of a node. Only the real
* edges (those with capacity) are iterated. The ones with only distance
* information are skipped.
* @tparam Tedge Actual edge class. May be "BaseEdge" or "const BaseEdge".
* @tparam Titer Actual iterator class.
*/
template <class Tedge, class Tedge_wrapper, class Titer>
class BaseEdgeIterator {
protected:
Tedge *base; ///< Array of edges being iterated.
NodeID current; ///< Current offset in edges array.
/**
* A "fake" pointer to enable operator-> on temporaries. As the objects
* returned from operator* aren't references but real objects, we have
* to return something that implements operator->, but isn't a pointer
* from operator->. A fake pointer.
*/
class FakePointer : public SmallPair<NodeID, Tedge_wrapper> {
public:
/**
* Construct a fake pointer from a pair of NodeID and edge.
* @param pair Pair to be "pointed" to (in fact shallow-copied).
*/
FakePointer(const SmallPair<NodeID, Tedge_wrapper> &pair) : SmallPair<NodeID, Tedge_wrapper>(pair) {}
/**
* Retrieve the pair by operator->.
* @return Pair being "pointed" to.
*/
SmallPair<NodeID, Tedge_wrapper> *operator->() { return this; }
};
public:
/**
* Constructor.
* @param base Array of edges to be iterated.
* @param current ID of current node (to locate the first edge).
*/
BaseEdgeIterator (Tedge *base, NodeID current) :
base(base),
current(current == INVALID_NODE ? current : base[current].next_edge)
{}
/**
* Prefix-increment.
* @return This.
*/
Titer &operator++()
{
this->current = this->base[this->current].next_edge;
return static_cast<Titer &>(*this);
}
/**
* Postfix-increment.
* @return Version of this before increment.
*/
Titer operator++(int)
{
Titer ret(static_cast<Titer &>(*this));
this->current = this->base[this->current].next_edge;
return ret;
}
/**
* Compare with some other edge iterator. The other one may be of a
* child class.
* @tparam Tother Class of other iterator.
* @param other Instance of other iterator.
* @return If the iterators have the same edge array and current node.
*/
template<class Tother>
bool operator==(const Tother &other)
{
return this->base == other.base && this->current == other.current;
}
/**
* Compare for inequality with some other edge iterator. The other one
* may be of a child class.
* @tparam Tother Class of other iterator.
* @param other Instance of other iterator.
* @return If either the edge arrays or the current nodes differ.
*/
template<class Tother>
bool operator!=(const Tother &other)
{
return this->base != other.base || this->current != other.current;
}
/**
* Dereference with operator*.
* @return Pair of current target NodeID and edge object.
*/
SmallPair<NodeID, Tedge_wrapper> operator*() const
{
return SmallPair<NodeID, Tedge_wrapper>(this->current, Tedge_wrapper(this->base[this->current]));
}
/**
* Dereference with operator->.
* @return Fake pointer to Pair of current target NodeID and edge object.
*/
FakePointer operator->() const {
return FakePointer(this->operator*());
}
};
/**
* A constant edge class.
*/
typedef EdgeWrapper<const BaseEdge> ConstEdge;
/**
* An updatable edge class.
*/
class Edge : public EdgeWrapper<BaseEdge> {
public:
/**
* Constructor
* @param edge Edge to be wrapped.
*/
Edge(BaseEdge &edge) : EdgeWrapper<BaseEdge>(edge) {}
void Update(uint capacity, uint usage, EdgeUpdateMode mode);
void Restrict() { this->edge.last_unrestricted_update = INVALID_DATE; }
void Release() { this->edge.last_restricted_update = INVALID_DATE; }
};
/**
* An iterator for const edges. Cannot be typedef'ed because of
* template-reference to ConstEdgeIterator itself.
*/
class ConstEdgeIterator : public BaseEdgeIterator<const BaseEdge, ConstEdge, ConstEdgeIterator> {
public:
/**
* Constructor.
* @param edges Array of edges to be iterated over.
* @param current ID of current edge's end node.
*/
ConstEdgeIterator(const BaseEdge *edges, NodeID current) :
BaseEdgeIterator<const BaseEdge, ConstEdge, ConstEdgeIterator>(edges, current) {}
};
/**
* An iterator for non-const edges. Cannot be typedef'ed because of
* template-reference to EdgeIterator itself.
*/
class EdgeIterator : public BaseEdgeIterator<BaseEdge, Edge, EdgeIterator> {
public:
/**
* Constructor.
* @param edges Array of edges to be iterated over.
* @param current ID of current edge's end node.
*/
EdgeIterator(BaseEdge *edges, NodeID current) :
BaseEdgeIterator<BaseEdge, Edge, EdgeIterator>(edges, current) {}
};
/**
* Constant node class. Only retrieval operations are allowed on both the
* node itself and its edges.
*/
class ConstNode : public NodeWrapper<const BaseNode, const BaseEdge> {
public:
/**
* Constructor.
* @param lg LinkGraph to get the node from.
* @param node ID of the node.
*/
ConstNode(const LinkGraph *lg, NodeID node) :
NodeWrapper<const BaseNode, const BaseEdge>(lg->nodes[node], lg->edges[node], node)
{}
/**
* Get a ConstEdge. This is not a reference as the wrapper objects are
* not actually persistent.
* @param to ID of end node of edge.
* @return Constant edge wrapper.
*/
ConstEdge operator[](NodeID to) const { return ConstEdge(this->edges[to]); }
/**
* Get an iterator pointing to the start of the edges array.
* @return Constant edge iterator.
*/
ConstEdgeIterator Begin() const { return ConstEdgeIterator(this->edges, this->index); }
/**
* Get an iterator pointing beyond the end of the edges array.
* @return Constant edge iterator.
*/
ConstEdgeIterator End() const { return ConstEdgeIterator(this->edges, INVALID_NODE); }
};
/**
* Updatable node class. The node itself as well as its edges can be modified.
*/
class Node : public NodeWrapper<BaseNode, BaseEdge> {
public:
/**
* Constructor.
* @param lg LinkGraph to get the node from.
* @param node ID of the node.
*/
Node(LinkGraph *lg, NodeID node) :
NodeWrapper<BaseNode, BaseEdge>(lg->nodes[node], lg->edges[node], node)
{}
/**
* Get an Edge. This is not a reference as the wrapper objects are not
* actually persistent.
* @param to ID of end node of edge.
* @return Edge wrapper.
*/
Edge operator[](NodeID to) { return Edge(this->edges[to]); }
/**
* Get an iterator pointing to the start of the edges array.
* @return Edge iterator.
*/
EdgeIterator Begin() { return EdgeIterator(this->edges, this->index); }
/**
* Get an iterator pointing beyond the end of the edges array.
* @return Constant edge iterator.
*/
EdgeIterator End() { return EdgeIterator(this->edges, INVALID_NODE); }
/**
* Update the node's supply and set last_update to the current date.
* @param supply Supply to be added.
*/
void UpdateSupply(uint supply)
{
this->node.supply += supply;
this->node.last_update = _date;
}
/**
* Update the node's location on the map.
* @param xy New location.
*/
void UpdateLocation(TileIndex xy)
{
this->node.xy = xy;
}
/**
* Set the node's demand.
* @param demand New demand for the node.
*/
void SetDemand(uint demand)
{
this->node.demand = demand;
}
void AddEdge(NodeID to, uint capacity, uint usage, EdgeUpdateMode mode);
void UpdateEdge(NodeID to, uint capacity, uint usage, EdgeUpdateMode mode);
void RemoveEdge(NodeID to);
};
typedef SmallVector<BaseNode, 16> NodeVector;
typedef SmallMatrix<BaseEdge> EdgeMatrix;
/** Minimum effective distance for timeout calculation. */
static const uint MIN_TIMEOUT_DISTANCE = 32;
/** Minimum number of days between subsequent compressions of a LG. */
static const uint COMPRESSION_INTERVAL = 256;
/**
* Scale a value from a link graph of age orig_age for usage in one of age
* target_age. Make sure that the value stays > 0 if it was > 0 before.
* @param val Value to be scaled.
* @param target_age Age of the target link graph.
* @param orig_age Age of the original link graph.
* @return scaled value.
*/
inline static uint Scale(uint val, uint target_age, uint orig_age)
{
return val > 0 ? max(1U, val * target_age / orig_age) : 0;
}
/** Bare constructor, only for save/load. */
LinkGraph() : cargo(INVALID_CARGO), last_compression(0) {}
/**
* Real constructor.
* @param cargo Cargo the link graph is about.
*/
LinkGraph(CargoID cargo) : cargo(cargo), last_compression(_date) {}
void Init(uint size);
void ShiftDates(int interval);
void Compress();
void Merge(LinkGraph *other);
/* Splitting link graphs is intentionally not implemented.
* The overhead in determining connectedness would probably outweigh the
* benefit of having to deal with smaller graphs. In real world examples
* networks generally grow. Only rarely a network is permanently split.
* Reacting to temporary splits here would obviously create performance
* problems and detecting the temporary or permanent nature of splits isn't
* trivial. */
/**
* Get a node with the specified id.
* @param num ID of the node.
* @return the Requested node.
*/
inline Node operator[](NodeID num) { return Node(this, num); }
/**
* Get a const reference to a node with the specified id.
* @param num ID of the node.
* @return the Requested node.
*/
inline ConstNode operator[](NodeID num) const { return ConstNode(this, num); }
/**
* Get the current size of the component.
* @return Size.
*/
inline uint Size() const { return this->nodes.Length(); }
/**
* Get date of last compression.
* @return Date of last compression.
*/
inline Date LastCompression() const { return this->last_compression; }
/**
* Get the cargo ID this component's link graph refers to.
* @return Cargo ID.
*/
inline CargoID Cargo() const { return this->cargo; }
/**
* Scale a value to its monthly equivalent, based on last compression.
* @param base Value to be scaled.
* @return Scaled value.
*/
inline uint Monthly(uint base) const
{
return base * 30 / (_date - this->last_compression + 1);
}
NodeID AddNode(const Station *st);
void RemoveNode(NodeID id);
protected:
friend class LinkGraph::ConstNode;
friend class LinkGraph::Node;
friend const SaveLoad *GetLinkGraphDesc();
friend const SaveLoad *GetLinkGraphJobDesc();
friend void SaveLoad_LinkGraph(LinkGraph &lg);
CargoID cargo; ///< Cargo of this component's link graph.
Date last_compression; ///< Last time the capacities and supplies were compressed.
NodeVector nodes; ///< Nodes in the component.
EdgeMatrix edges; ///< Edges in the component.
};
#define FOR_ALL_LINK_GRAPHS(var) FOR_ALL_ITEMS_FROM(LinkGraph, link_graph_index, var, 0)
#endif /* LINKGRAPH_H */

View File

@@ -0,0 +1,27 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file linkgraph_base.h Some typedefs for the main game. */
#ifndef LINKGRAPH_BASE_H
#define LINKGRAPH_BASE_H
#include "linkgraph.h"
#include "linkgraphschedule.h"
typedef LinkGraph::Node Node;
typedef LinkGraph::Edge Edge;
typedef LinkGraph::EdgeIterator EdgeIterator;
typedef LinkGraph::ConstNode ConstNode;
typedef LinkGraph::ConstEdge ConstEdge;
typedef LinkGraph::ConstEdgeIterator ConstEdgeIterator;
#endif /* LINKGRAPH_BASE_H */

View File

@@ -0,0 +1,575 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file linkgraph_gui.cpp Implementation of linkgraph overlay GUI. */
#include "../stdafx.h"
#include "../window_gui.h"
#include "../window_func.h"
#include "../company_base.h"
#include "../company_gui.h"
#include "../date_func.h"
#include "../viewport_func.h"
#include "../smallmap_gui.h"
#include "../core/geometry_func.hpp"
#include "../widgets/link_graph_legend_widget.h"
#include "table/strings.h"
#include "../safeguards.h"
/**
* Colours for the various "load" states of links. Ordered from "unused" to
* "overloaded".
*/
const uint8 LinkGraphOverlay::LINK_COLOURS[] = {
0x0f, 0xd1, 0xd0, 0x57,
0x55, 0x53, 0xbf, 0xbd,
0xba, 0xb9, 0xb7, 0xb5
};
/**
* Get a DPI for the widget we will be drawing to.
* @param dpi DrawPixelInfo to fill with the desired dimensions.
*/
void LinkGraphOverlay::GetWidgetDpi(DrawPixelInfo *dpi) const
{
const NWidgetBase *wi = this->window->GetWidget<NWidgetBase>(this->widget_id);
dpi->left = dpi->top = 0;
dpi->width = wi->current_x;
dpi->height = wi->current_y;
}
/**
* Rebuild the cache and recalculate which links and stations to be shown.
*/
void LinkGraphOverlay::RebuildCache()
{
this->cached_links.clear();
this->cached_stations.clear();
if (this->company_mask == 0) return;
DrawPixelInfo dpi;
this->GetWidgetDpi(&dpi);
const Station *sta;
FOR_ALL_STATIONS(sta) {
if (sta->rect.IsEmpty()) continue;
Point pta = this->GetStationMiddle(sta);
StationID from = sta->index;
StationLinkMap &seen_links = this->cached_links[from];
uint supply = 0;
CargoID c;
FOR_EACH_SET_CARGO_ID(c, this->cargo_mask) {
if (!CargoSpec::Get(c)->IsValid()) continue;
if (!LinkGraph::IsValidID(sta->goods[c].link_graph)) continue;
const LinkGraph &lg = *LinkGraph::Get(sta->goods[c].link_graph);
ConstNode from_node = lg[sta->goods[c].node];
supply += lg.Monthly(from_node.Supply());
for (ConstEdgeIterator i = from_node.Begin(); i != from_node.End(); ++i) {
StationID to = lg[i->first].Station();
assert(from != to);
if (!Station::IsValidID(to) || seen_links.find(to) != seen_links.end()) {
continue;
}
const Station *stb = Station::Get(to);
assert(sta != stb);
/* Show links between stations of selected companies or "neutral" ones like oilrigs. */
if (stb->owner != OWNER_NONE && sta->owner != OWNER_NONE && !HasBit(this->company_mask, stb->owner)) continue;
if (stb->rect.IsEmpty()) continue;
if (!this->IsLinkVisible(pta, this->GetStationMiddle(stb), &dpi)) continue;
this->AddLinks(sta, stb);
seen_links[to]; // make sure it is created and marked as seen
}
}
if (this->IsPointVisible(pta, &dpi)) {
this->cached_stations.push_back(std::make_pair(from, supply));
}
}
}
/**
* Determine if a certain point is inside the given DPI, with some lee way.
* @param pt Point we are looking for.
* @param dpi Visible area.
* @param padding Extent of the point.
* @return If the point or any of its 'extent' is inside the dpi.
*/
inline bool LinkGraphOverlay::IsPointVisible(Point pt, const DrawPixelInfo *dpi, int padding) const
{
return pt.x > dpi->left - padding && pt.y > dpi->top - padding &&
pt.x < dpi->left + dpi->width + padding &&
pt.y < dpi->top + dpi->height + padding;
}
/**
* Determine if a certain link crosses through the area given by the dpi with some lee way.
* @param pta First end of the link.
* @param ptb Second end of the link.
* @param dpi Visible area.
* @param padding Width or thickness of the link.
* @return If the link or any of its "thickness" is visible. This may return false positives.
*/
inline bool LinkGraphOverlay::IsLinkVisible(Point pta, Point ptb, const DrawPixelInfo *dpi, int padding) const
{
return !((pta.x < dpi->left - padding && ptb.x < dpi->left - padding) ||
(pta.y < dpi->top - padding && ptb.y < dpi->top - padding) ||
(pta.x > dpi->left + dpi->width + padding &&
ptb.x > dpi->left + dpi->width + padding) ||
(pta.y > dpi->top + dpi->height + padding &&
ptb.y > dpi->top + dpi->height + padding));
}
/**
* Add all "interesting" links between the given stations to the cache.
* @param from The source station.
* @param to The destination station.
*/
void LinkGraphOverlay::AddLinks(const Station *from, const Station *to)
{
CargoID c;
FOR_EACH_SET_CARGO_ID(c, this->cargo_mask) {
if (!CargoSpec::Get(c)->IsValid()) continue;
const GoodsEntry &ge = from->goods[c];
if (!LinkGraph::IsValidID(ge.link_graph) ||
ge.link_graph != to->goods[c].link_graph) {
continue;
}
const LinkGraph &lg = *LinkGraph::Get(ge.link_graph);
ConstEdge edge = lg[ge.node][to->goods[c].node];
if (edge.Capacity() > 0) {
this->AddStats(lg.Monthly(edge.Capacity()), lg.Monthly(edge.Usage()),
ge.flows.GetFlowVia(to->index), from->owner == OWNER_NONE || to->owner == OWNER_NONE,
this->cached_links[from->index][to->index]);
}
}
}
/**
* Add information from a given pair of link stat and flow stat to the given
* link properties. The shown usage or plan is always the maximum of all link
* stats involved.
* @param new_cap Capacity of the new link.
* @param new_usg Usage of the new link.
* @param new_plan Planned flow for the new link.
* @param new_shared If the new link is shared.
* @param cargo LinkProperties to write the information to.
*/
/* static */ void LinkGraphOverlay::AddStats(uint new_cap, uint new_usg, uint new_plan, bool new_shared, LinkProperties &cargo)
{
/* multiply the numbers by 32 in order to avoid comparing to 0 too often. */
if (cargo.capacity == 0 ||
max(cargo.usage, cargo.planned) * 32 / (cargo.capacity + 1) < max(new_usg, new_plan) * 32 / (new_cap + 1)) {
cargo.capacity = new_cap;
cargo.usage = new_usg;
cargo.planned = new_plan;
}
if (new_shared) cargo.shared = true;
}
/**
* Draw the linkgraph overlay or some part of it, in the area given.
* @param dpi Area to be drawn to.
*/
void LinkGraphOverlay::Draw(const DrawPixelInfo *dpi) const
{
this->DrawLinks(dpi);
this->DrawStationDots(dpi);
}
/**
* Draw the cached links or part of them into the given area.
* @param dpi Area to be drawn to.
*/
void LinkGraphOverlay::DrawLinks(const DrawPixelInfo *dpi) const
{
for (LinkMap::const_iterator i(this->cached_links.begin()); i != this->cached_links.end(); ++i) {
if (!Station::IsValidID(i->first)) continue;
Point pta = this->GetStationMiddle(Station::Get(i->first));
for (StationLinkMap::const_iterator j(i->second.begin()); j != i->second.end(); ++j) {
if (!Station::IsValidID(j->first)) continue;
Point ptb = this->GetStationMiddle(Station::Get(j->first));
if (!this->IsLinkVisible(pta, ptb, dpi, this->scale + 2)) continue;
this->DrawContent(pta, ptb, j->second);
}
}
}
/**
* Draw one specific link.
* @param pta Source of the link.
* @param ptb Destination of the link.
* @param cargo Properties of the link.
*/
void LinkGraphOverlay::DrawContent(Point pta, Point ptb, const LinkProperties &cargo) const
{
uint usage_or_plan = min(cargo.capacity * 2 + 1, max(cargo.usage, cargo.planned));
int colour = LinkGraphOverlay::LINK_COLOURS[usage_or_plan * lengthof(LinkGraphOverlay::LINK_COLOURS) / (cargo.capacity * 2 + 2)];
int dash = cargo.shared ? this->scale * 4 : 0;
/* Move line a bit 90° against its dominant direction to prevent it from
* being hidden below the grey line. */
int side = _settings_game.vehicle.road_side ? 1 : -1;
if (abs(pta.x - ptb.x) < abs(pta.y - ptb.y)) {
int offset_x = (pta.y > ptb.y ? 1 : -1) * side * this->scale;
GfxDrawLine(pta.x + offset_x, pta.y, ptb.x + offset_x, ptb.y, colour, this->scale, dash);
} else {
int offset_y = (pta.x < ptb.x ? 1 : -1) * side * this->scale;
GfxDrawLine(pta.x, pta.y + offset_y, ptb.x, ptb.y + offset_y, colour, this->scale, dash);
}
GfxDrawLine(pta.x, pta.y, ptb.x, ptb.y, _colour_gradient[COLOUR_GREY][1], this->scale);
}
/**
* Draw dots for stations into the smallmap. The dots' sizes are determined by the amount of
* cargo produced there, their colours by the type of cargo produced.
*/
void LinkGraphOverlay::DrawStationDots(const DrawPixelInfo *dpi) const
{
for (StationSupplyList::const_iterator i(this->cached_stations.begin()); i != this->cached_stations.end(); ++i) {
const Station *st = Station::GetIfValid(i->first);
if (st == NULL) continue;
Point pt = this->GetStationMiddle(st);
if (!this->IsPointVisible(pt, dpi, 3 * this->scale)) continue;
uint r = this->scale * 2 + this->scale * 2 * min(200, i->second) / 200;
LinkGraphOverlay::DrawVertex(pt.x, pt.y, r,
_colour_gradient[st->owner != OWNER_NONE ?
(Colours)Company::Get(st->owner)->colour : COLOUR_GREY][5],
_colour_gradient[COLOUR_GREY][1]);
}
}
/**
* Draw a square symbolizing a producer of cargo.
* @param x X coordinate of the middle of the vertex.
* @param y Y coordinate of the middle of the vertex.
* @param size Y and y extend of the vertex.
* @param colour Colour with which the vertex will be filled.
* @param border_colour Colour for the border of the vertex.
*/
/* static */ void LinkGraphOverlay::DrawVertex(int x, int y, int size, int colour, int border_colour)
{
size--;
int w1 = size / 2;
int w2 = size / 2 + size % 2;
GfxFillRect(x - w1, y - w1, x + w2, y + w2, colour);
w1++;
w2++;
GfxDrawLine(x - w1, y - w1, x + w2, y - w1, border_colour);
GfxDrawLine(x - w1, y + w2, x + w2, y + w2, border_colour);
GfxDrawLine(x - w1, y - w1, x - w1, y + w2, border_colour);
GfxDrawLine(x + w2, y - w1, x + w2, y + w2, border_colour);
}
/**
* Determine the middle of a station in the current window.
* @param st The station we're looking for.
* @return Middle point of the station in the current window.
*/
Point LinkGraphOverlay::GetStationMiddle(const Station *st) const
{
if (this->window->viewport != NULL) {
return GetViewportStationMiddle(this->window->viewport, st);
} else {
/* assume this is a smallmap */
return static_cast<const SmallMapWindow *>(this->window)->GetStationMiddle(st);
}
}
/**
* Set a new cargo mask and rebuild the cache.
* @param cargo_mask New cargo mask.
*/
void LinkGraphOverlay::SetCargoMask(uint32 cargo_mask)
{
this->cargo_mask = cargo_mask;
this->RebuildCache();
this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
}
/**
* Set a new company mask and rebuild the cache.
* @param company_mask New company mask.
*/
void LinkGraphOverlay::SetCompanyMask(uint32 company_mask)
{
this->company_mask = company_mask;
this->RebuildCache();
this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
}
/** Make a number of rows with buttons for each company for the linkgraph legend window. */
NWidgetBase *MakeCompanyButtonRowsLinkGraphGUI(int *biggest_index)
{
return MakeCompanyButtonRows(biggest_index, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST, 3, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES);
}
NWidgetBase *MakeSaturationLegendLinkGraphGUI(int *biggest_index)
{
NWidgetVertical *panel = new NWidgetVertical(NC_EQUALSIZE);
for (uint i = 0; i < lengthof(LinkGraphOverlay::LINK_COLOURS); ++i) {
NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_DARK_GREEN, i + WID_LGL_SATURATION_FIRST);
wid->SetMinimalSize(50, FONT_HEIGHT_SMALL);
wid->SetFill(1, 1);
wid->SetResize(0, 0);
panel->Add(wid);
}
*biggest_index = WID_LGL_SATURATION_LAST;
return panel;
}
NWidgetBase *MakeCargoesLegendLinkGraphGUI(int *biggest_index)
{
static const uint ENTRIES_PER_ROW = CeilDiv(NUM_CARGO, 5);
NWidgetVertical *panel = new NWidgetVertical(NC_EQUALSIZE);
NWidgetHorizontal *row = NULL;
for (uint i = 0; i < NUM_CARGO; ++i) {
if (i % ENTRIES_PER_ROW == 0) {
if (row) panel->Add(row);
row = new NWidgetHorizontal(NC_EQUALSIZE);
}
NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, i + WID_LGL_CARGO_FIRST);
wid->SetMinimalSize(25, FONT_HEIGHT_SMALL);
wid->SetFill(1, 1);
wid->SetResize(0, 0);
row->Add(wid);
}
/* Fill up last row */
for (uint i = 0; i < 4 - (NUM_CARGO - 1) % 5; ++i) {
NWidgetSpacer *spc = new NWidgetSpacer(25, FONT_HEIGHT_SMALL);
spc->SetFill(1, 1);
spc->SetResize(0, 0);
row->Add(spc);
}
panel->Add(row);
*biggest_index = WID_LGL_CARGO_LAST;
return panel;
}
static const NWidgetPart _nested_linkgraph_legend_widgets[] = {
NWidget(NWID_HORIZONTAL),
NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_LGL_CAPTION), SetDataTip(STR_LINKGRAPH_LEGEND_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
EndContainer(),
NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
NWidget(NWID_HORIZONTAL),
NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_SATURATION),
SetPadding(WD_FRAMERECT_TOP, 0, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
NWidgetFunction(MakeSaturationLegendLinkGraphGUI),
EndContainer(),
NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_COMPANIES),
SetPadding(WD_FRAMERECT_TOP, 0, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
NWidget(NWID_VERTICAL, NC_EQUALSIZE),
NWidgetFunction(MakeCompanyButtonRowsLinkGraphGUI),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_COMPANIES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_COMPANIES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
EndContainer(),
EndContainer(),
NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_CARGOES),
SetPadding(WD_FRAMERECT_TOP, WD_FRAMERECT_RIGHT, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
NWidget(NWID_VERTICAL, NC_EQUALSIZE),
NWidgetFunction(MakeCargoesLegendLinkGraphGUI),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_CARGOES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_CARGOES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
EndContainer(),
EndContainer(),
EndContainer(),
EndContainer()
};
assert_compile(WID_LGL_SATURATION_LAST - WID_LGL_SATURATION_FIRST ==
lengthof(LinkGraphOverlay::LINK_COLOURS) - 1);
static WindowDesc _linkgraph_legend_desc(
WDP_AUTO, "toolbar_linkgraph", 0, 0,
WC_LINKGRAPH_LEGEND, WC_NONE,
0,
_nested_linkgraph_legend_widgets, lengthof(_nested_linkgraph_legend_widgets)
);
/**
* Open a link graph legend window.
*/
void ShowLinkGraphLegend()
{
AllocateWindowDescFront<LinkGraphLegendWindow>(&_linkgraph_legend_desc, 0);
}
LinkGraphLegendWindow::LinkGraphLegendWindow(WindowDesc *desc, int window_number) : Window(desc)
{
this->InitNested(window_number);
this->InvalidateData(0);
this->SetOverlay(FindWindowById(WC_MAIN_WINDOW, 0)->viewport->overlay);
}
/**
* Set the overlay belonging to this menu and import its company/cargo settings.
* @params overlay New overlay for this menu.
*/
void LinkGraphLegendWindow::SetOverlay(LinkGraphOverlay *overlay) {
this->overlay = overlay;
uint32 companies = this->overlay->GetCompanyMask();
for (uint c = 0; c < MAX_COMPANIES; c++) {
if (!this->IsWidgetDisabled(WID_LGL_COMPANY_FIRST + c)) {
this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, HasBit(companies, c));
}
}
uint32 cargoes = this->overlay->GetCargoMask();
for (uint c = 0; c < NUM_CARGO; c++) {
if (!this->IsWidgetDisabled(WID_LGL_CARGO_FIRST + c)) {
this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, HasBit(cargoes, c));
}
}
}
void LinkGraphLegendWindow::UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
{
if (IsInsideMM(widget, WID_LGL_SATURATION_FIRST, WID_LGL_SATURATION_LAST + 1)) {
StringID str = STR_NULL;
if (widget == WID_LGL_SATURATION_FIRST) {
str = STR_LINKGRAPH_LEGEND_UNUSED;
} else if (widget == WID_LGL_SATURATION_LAST) {
str = STR_LINKGRAPH_LEGEND_OVERLOADED;
} else if (widget == (WID_LGL_SATURATION_LAST + WID_LGL_SATURATION_FIRST) / 2) {
str = STR_LINKGRAPH_LEGEND_SATURATED;
}
if (str != STR_NULL) {
Dimension dim = GetStringBoundingBox(str);
dim.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
dim.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
*size = maxdim(*size, dim);
}
}
if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
if (cargo->IsValid()) {
Dimension dim = GetStringBoundingBox(cargo->abbrev);
dim.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
dim.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
*size = maxdim(*size, dim);
}
}
}
void LinkGraphLegendWindow::DrawWidget(const Rect &r, int widget) const
{
if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
if (this->IsWidgetDisabled(widget)) return;
CompanyID cid = (CompanyID)(widget - WID_LGL_COMPANY_FIRST);
Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
DrawCompanyIcon(cid, (r.left + r.right + 1 - sprite_size.width) / 2, (r.top + r.bottom + 1 - sprite_size.height) / 2);
}
if (IsInsideMM(widget, WID_LGL_SATURATION_FIRST, WID_LGL_SATURATION_LAST + 1)) {
GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, LinkGraphOverlay::LINK_COLOURS[widget - WID_LGL_SATURATION_FIRST]);
StringID str = STR_NULL;
if (widget == WID_LGL_SATURATION_FIRST) {
str = STR_LINKGRAPH_LEGEND_UNUSED;
} else if (widget == WID_LGL_SATURATION_LAST) {
str = STR_LINKGRAPH_LEGEND_OVERLOADED;
} else if (widget == (WID_LGL_SATURATION_LAST + WID_LGL_SATURATION_FIRST) / 2) {
str = STR_LINKGRAPH_LEGEND_SATURATED;
}
if (str != STR_NULL) DrawString(r.left, r.right, (r.top + r.bottom + 1 - FONT_HEIGHT_SMALL) / 2, str, TC_FROMSTRING, SA_HOR_CENTER);
}
if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
if (this->IsWidgetDisabled(widget)) return;
CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, cargo->legend_colour);
DrawString(r.left, r.right, (r.top + r.bottom + 1 - FONT_HEIGHT_SMALL) / 2, cargo->abbrev, TC_BLACK, SA_HOR_CENTER);
}
}
/**
* Update the overlay with the new company selection.
*/
void LinkGraphLegendWindow::UpdateOverlayCompanies()
{
uint32 mask = 0;
for (uint c = 0; c < MAX_COMPANIES; c++) {
if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
if (!this->IsWidgetLowered(c + WID_LGL_COMPANY_FIRST)) continue;
SetBit(mask, c);
}
this->overlay->SetCompanyMask(mask);
}
/**
* Update the overlay with the new cargo selection.
*/
void LinkGraphLegendWindow::UpdateOverlayCargoes()
{
uint32 mask = 0;
for (uint c = 0; c < NUM_CARGO; c++) {
if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
if (!this->IsWidgetLowered(c + WID_LGL_CARGO_FIRST)) continue;
SetBit(mask, c);
}
this->overlay->SetCargoMask(mask);
}
void LinkGraphLegendWindow::OnClick(Point pt, int widget, int click_count)
{
/* Check which button is clicked */
if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
if (!this->IsWidgetDisabled(widget)) {
this->ToggleWidgetLoweredState(widget);
this->UpdateOverlayCompanies();
}
} else if (widget == WID_LGL_COMPANIES_ALL || widget == WID_LGL_COMPANIES_NONE) {
for (uint c = 0; c < MAX_COMPANIES; c++) {
if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, widget == WID_LGL_COMPANIES_ALL);
}
this->UpdateOverlayCompanies();
this->SetDirty();
} else if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
if (!this->IsWidgetDisabled(widget)) {
this->ToggleWidgetLoweredState(widget);
this->UpdateOverlayCargoes();
}
} else if (widget == WID_LGL_CARGOES_ALL || widget == WID_LGL_CARGOES_NONE) {
for (uint c = 0; c < NUM_CARGO; c++) {
if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, widget == WID_LGL_CARGOES_ALL);
}
this->UpdateOverlayCargoes();
}
this->SetDirty();
}
/**
* Invalidate the data of this window if the cargoes or companies have changed.
* @param data ignored
* @param gui_scope ignored
*/
void LinkGraphLegendWindow::OnInvalidateData(int data, bool gui_scope)
{
/* Disable the companies who are not active */
for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
this->SetWidgetDisabledState(i + WID_LGL_COMPANY_FIRST, !Company::IsValidID(i));
}
for (CargoID i = 0; i < NUM_CARGO; i++) {
this->SetWidgetDisabledState(i + WID_LGL_CARGO_FIRST, !CargoSpec::Get(i)->IsValid());
}
}

View File

@@ -0,0 +1,115 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file linkgraph_gui.h Declaration of linkgraph overlay GUI. */
#ifndef LINKGRAPH_GUI_H
#define LINKGRAPH_GUI_H
#include "../company_func.h"
#include "../station_base.h"
#include "../widget_type.h"
#include "linkgraph_base.h"
#include <map>
#include <list>
/**
* Properties of a link between two stations.
*/
struct LinkProperties {
LinkProperties() : capacity(0), usage(0), planned(0), shared(false) {}
uint capacity; ///< Capacity of the link.
uint usage; ///< Actual usage of the link.
uint planned; ///< Planned usage of the link.
bool shared; ///< If this is a shared link to be drawn dashed.
};
/**
* Handles drawing of links into some window.
* The window must either be a smallmap or have a valid viewport.
*/
class LinkGraphOverlay {
public:
typedef std::map<StationID, LinkProperties> StationLinkMap;
typedef std::map<StationID, StationLinkMap> LinkMap;
typedef std::list<std::pair<StationID, uint> > StationSupplyList;
static const uint8 LINK_COLOURS[];
/**
* Create a link graph overlay for the specified window.
* @param w Window to be drawn into.
* @param wid ID of the widget to draw into.
* @param cargo_mask Bitmask of cargoes to be shown.
* @param company_mask Bitmask of companies to be shown.
* @param scale Desired thickness of lines and size of station dots.
*/
LinkGraphOverlay(const Window *w, uint wid, uint32 cargo_mask, uint32 company_mask, uint scale) :
window(w), widget_id(wid), cargo_mask(cargo_mask), company_mask(company_mask), scale(scale)
{}
void RebuildCache();
void Draw(const DrawPixelInfo *dpi) const;
void SetCargoMask(uint32 cargo_mask);
void SetCompanyMask(uint32 company_mask);
/** Get a bitmask of the currently shown cargoes. */
uint32 GetCargoMask() { return this->cargo_mask; }
/** Get a bitmask of the currently shown companies. */
uint32 GetCompanyMask() { return this->company_mask; }
protected:
const Window *window; ///< Window to be drawn into.
const uint widget_id; ///< ID of Widget in Window to be drawn to.
uint32 cargo_mask; ///< Bitmask of cargos to be displayed.
uint32 company_mask; ///< Bitmask of companies to be displayed.
LinkMap cached_links; ///< Cache for links to reduce recalculation.
StationSupplyList cached_stations; ///< Cache for stations to be drawn.
uint scale; ///< Width of link lines.
Point GetStationMiddle(const Station *st) const;
void DrawForwBackLinks(Point pta, StationID sta, Point ptb, StationID stb) const;
void AddLinks(const Station *sta, const Station *stb);
void DrawLinks(const DrawPixelInfo *dpi) const;
void DrawStationDots(const DrawPixelInfo *dpi) const;
void DrawContent(Point pta, Point ptb, const LinkProperties &cargo) const;
bool IsLinkVisible(Point pta, Point ptb, const DrawPixelInfo *dpi, int padding = 0) const;
bool IsPointVisible(Point pt, const DrawPixelInfo *dpi, int padding = 0) const;
void GetWidgetDpi(DrawPixelInfo *dpi) const;
static void AddStats(uint new_cap, uint new_usg, uint new_flow, bool new_shared, LinkProperties &cargo);
static void DrawVertex(int x, int y, int size, int colour, int border_colour);
};
void ShowLinkGraphLegend();
/**
* Menu window to select cargoes and companies to show in a link graph overlay.
*/
struct LinkGraphLegendWindow : Window {
public:
LinkGraphLegendWindow(WindowDesc *desc, int window_number);
void SetOverlay(LinkGraphOverlay *overlay);
virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize);
virtual void DrawWidget(const Rect &r, int widget) const;
virtual void OnClick(Point pt, int widget, int click_count);
virtual void OnInvalidateData(int data = 0, bool gui_scope = true);
private:
LinkGraphOverlay *overlay;
void UpdateOverlayCompanies();
void UpdateOverlayCargoes();
};
#endif /* LINKGRAPH_GUI_H */

View File

@@ -0,0 +1,64 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file linkgraph_type.h Declaration of link graph types used for cargo distribution. */
#ifndef LINKGRAPH_TYPE_H
#define LINKGRAPH_TYPE_H
typedef uint16 LinkGraphID;
static const LinkGraphID INVALID_LINK_GRAPH = UINT16_MAX;
typedef uint16 LinkGraphJobID;
static const LinkGraphID INVALID_LINK_GRAPH_JOB = UINT16_MAX;
typedef uint16 NodeID;
static const NodeID INVALID_NODE = UINT16_MAX;
enum DistributionType {
DT_BEGIN = 0,
DT_MIN = 0,
DT_MANUAL = 0, ///< Manual distribution. No link graph calculations are run.
DT_ASYMMETRIC = 1, ///< Asymmetric distribution. Usually cargo will only travel in one direction.
DT_MAX_NONSYMMETRIC = 1, ///< Maximum non-symmetric distribution.
DT_SYMMETRIC = 2, ///< Symmetric distribution. The same amount of cargo travels in each direction between each pair of nodes.
DT_MAX = 2,
DT_NUM = 3,
DT_END = 3
};
/* It needs to be 8bits, because we save and load it as such
* Define basic enum properties
*/
template <> struct EnumPropsT<DistributionType> : MakeEnumPropsT<DistributionType, byte, DT_BEGIN, DT_END, DT_NUM> {};
typedef TinyEnumT<DistributionType> DistributionTypeByte; // typedefing-enumification of DistributionType
/**
* Special modes for updating links. 'Restricted' means that vehicles with
* 'no loading' orders are serving the link. If a link is only served by
* such vehicles it's 'fully restricted'. This means the link can be used
* by cargo arriving in such vehicles, but not by cargo generated or
* transferring at the source station of the link. In order to find out
* about this condition we keep two update timestamps in each link, one for
* the restricted and one for the unrestricted part of it. If either one
* times out while the other is still valid the link becomes fully
* restricted or fully unrestricted, respectively.
* Refreshing a link makes just sure a minimum capacity is kept. Increasing
* actually adds the given capacity.
*/
enum EdgeUpdateMode {
EUM_INCREASE = 1, ///< Increase capacity.
EUM_REFRESH = 1 << 1, ///< Refresh capacity.
EUM_RESTRICTED = 1 << 2, ///< Use restricted link.
EUM_UNRESTRICTED = 1 << 3, ///< Use unrestricted link.
};
DECLARE_ENUM_AS_BIT_SET(EdgeUpdateMode)
#endif /* LINKGRAPH_TYPE_H */

View File

@@ -0,0 +1,279 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file linkgraphjob.cpp Definition of link graph job classes used for cargo distribution. */
#include "../stdafx.h"
#include "../core/pool_func.hpp"
#include "../window_func.h"
#include "linkgraphjob.h"
#include "linkgraphschedule.h"
#include "../safeguards.h"
/* Initialize the link-graph-job-pool */
LinkGraphJobPool _link_graph_job_pool("LinkGraphJob");
INSTANTIATE_POOL_METHODS(LinkGraphJob)
/**
* Static instance of an invalid path.
* Note: This instance is created on task start.
* Lazy creation on first usage results in a data race between the CDist threads.
*/
/* static */ Path *Path::invalid_path = new Path(INVALID_NODE, true);
/**
* Create a link graph job from a link graph. The link graph will be copied so
* that the calculations don't interfer with the normal operations on the
* original. The job is immediately started.
* @param orig Original LinkGraph to be copied.
*/
LinkGraphJob::LinkGraphJob(const LinkGraph &orig) :
/* Copying the link graph here also copies its index member.
* This is on purpose. */
link_graph(orig),
settings(_settings_game.linkgraph),
thread(NULL),
join_date(_date + _settings_game.linkgraph.recalc_time)
{
}
/**
* Erase all flows originating at a specific node.
* @param from Node to erase flows for.
*/
void LinkGraphJob::EraseFlows(NodeID from)
{
for (NodeID node_id = 0; node_id < this->Size(); ++node_id) {
(*this)[node_id].Flows().erase(from);
}
}
/**
* Spawn a thread if possible and run the link graph job in the thread. If
* that's not possible run the job right now in the current thread.
*/
void LinkGraphJob::SpawnThread()
{
if (!ThreadObject::New(&(LinkGraphSchedule::Run), this, &this->thread)) {
this->thread = NULL;
/* Of course this will hang a bit.
* On the other hand, if you want to play games which make this hang noticably
* on a platform without threads then you'll probably get other problems first.
* OK:
* If someone comes and tells me that this hangs for him/her, I'll implement a
* smaller grained "Step" method for all handlers and add some more ticks where
* "Step" is called. No problem in principle. */
LinkGraphSchedule::Run(this);
}
}
/**
* Join the calling thread with this job's thread if threading is enabled.
*/
void LinkGraphJob::JoinThread()
{
if (this->thread != NULL) {
this->thread->Join();
delete this->thread;
this->thread = NULL;
}
}
/**
* Join the link graph job and destroy it.
*/
LinkGraphJob::~LinkGraphJob()
{
this->JoinThread();
/* Don't update stuff from other pools, when everything is being removed.
* Accessing other pools may be invalid. */
if (CleaningPool()) return;
/* Link graph has been merged into another one. */
if (!LinkGraph::IsValidID(this->link_graph.index)) return;
uint size = this->Size();
for (NodeID node_id = 0; node_id < size; ++node_id) {
Node from = (*this)[node_id];
/* The station can have been deleted. Remove all flows originating from it then. */
Station *st = Station::GetIfValid(from.Station());
if (st == NULL) {
this->EraseFlows(node_id);
continue;
}
/* Link graph merging and station deletion may change around IDs. Make
* sure that everything is still consistent or ignore it otherwise. */
GoodsEntry &ge = st->goods[this->Cargo()];
if (ge.link_graph != this->link_graph.index || ge.node != node_id) {
this->EraseFlows(node_id);
continue;
}
LinkGraph *lg = LinkGraph::Get(ge.link_graph);
FlowStatMap &flows = from.Flows();
for (EdgeIterator it(from.Begin()); it != from.End(); ++it) {
if (from[it->first].Flow() == 0) continue;
StationID to = (*this)[it->first].Station();
Station *st2 = Station::GetIfValid(to);
if (st2 == NULL || st2->goods[this->Cargo()].link_graph != this->link_graph.index ||
st2->goods[this->Cargo()].node != it->first ||
(*lg)[node_id][it->first].LastUpdate() == INVALID_DATE) {
/* Edge has been removed. Delete flows. */
StationIDStack erased = flows.DeleteFlows(to);
/* Delete old flows for source stations which have been deleted
* from the new flows. This avoids flow cycles between old and
* new flows. */
while (!erased.IsEmpty()) ge.flows.erase(erased.Pop());
} else if ((*lg)[node_id][it->first].LastUnrestrictedUpdate() == INVALID_DATE) {
/* Edge is fully restricted. */
flows.RestrictFlows(to);
}
}
/* Swap shares and invalidate ones that are completely deleted. Don't
* really delete them as we could then end up with unroutable cargo
* somewhere. Do delete them and also reroute relevant cargo if
* automatic distribution has been turned off for that cargo. */
for (FlowStatMap::iterator it(ge.flows.begin()); it != ge.flows.end();) {
FlowStatMap::iterator new_it = flows.find(it->first);
if (new_it == flows.end()) {
if (_settings_game.linkgraph.GetDistributionType(this->Cargo()) != DT_MANUAL) {
it->second.Invalidate();
++it;
} else {
FlowStat shares(INVALID_STATION, 1);
it->second.SwapShares(shares);
ge.flows.erase(it++);
for (FlowStat::SharesMap::const_iterator shares_it(shares.GetShares()->begin());
shares_it != shares.GetShares()->end(); ++shares_it) {
RerouteCargo(st, this->Cargo(), shares_it->second, st->index);
}
}
} else {
it->second.SwapShares(new_it->second);
flows.erase(new_it);
++it;
}
}
ge.flows.insert(flows.begin(), flows.end());
InvalidateWindowData(WC_STATION_VIEW, st->index, this->Cargo());
}
}
/**
* Initialize the link graph job: Resize nodes and edges and populate them.
* This is done after the constructor so that we can do it in the calculation
* thread without delaying the main game.
*/
void LinkGraphJob::Init()
{
uint size = this->Size();
this->nodes.Resize(size);
this->edges.Resize(size, size);
for (uint i = 0; i < size; ++i) {
this->nodes[i].Init(this->link_graph[i].Supply());
EdgeAnnotation *node_edges = this->edges[i];
for (uint j = 0; j < size; ++j) {
node_edges[j].Init();
}
}
}
/**
* Initialize a linkgraph job edge.
*/
void LinkGraphJob::EdgeAnnotation::Init()
{
this->demand = 0;
this->flow = 0;
this->unsatisfied_demand = 0;
}
/**
* Initialize a Linkgraph job node. The underlying memory is expected to be
* freshly allocated, without any constructors having been called.
* @param supply Initial undelivered supply.
*/
void LinkGraphJob::NodeAnnotation::Init(uint supply)
{
this->undelivered_supply = supply;
new (&this->flows) FlowStatMap;
new (&this->paths) PathList;
}
/**
* Add this path as a new child to the given base path, thus making this path
* a "fork" of the base path.
* @param base Path to fork from.
* @param cap Maximum capacity of the new leg.
* @param free_cap Remaining free capacity of the new leg.
* @param dist Distance of the new leg.
*/
void Path::Fork(Path *base, uint cap, int free_cap, uint dist)
{
this->capacity = min(base->capacity, cap);
this->free_capacity = min(base->free_capacity, free_cap);
this->distance = base->distance + dist;
assert(this->distance > 0);
if (this->parent != base) {
this->Detach();
this->parent = base;
this->parent->num_children++;
}
this->origin = base->origin;
}
/**
* Push some flow along a path and register the path in the nodes it passes if
* successful.
* @param new_flow Amount of flow to push.
* @param job Link graph job this node belongs to.
* @param max_saturation Maximum saturation of edges.
* @return Amount of flow actually pushed.
*/
uint Path::AddFlow(uint new_flow, LinkGraphJob &job, uint max_saturation)
{
if (this->parent != NULL) {
LinkGraphJob::Edge edge = job[this->parent->node][this->node];
if (max_saturation != UINT_MAX) {
uint usable_cap = edge.Capacity() * max_saturation / 100;
if (usable_cap > edge.Flow()) {
new_flow = min(new_flow, usable_cap - edge.Flow());
} else {
return 0;
}
}
new_flow = this->parent->AddFlow(new_flow, job, max_saturation);
if (this->flow == 0 && new_flow > 0) {
job[this->parent->node].Paths().push_front(this);
}
edge.AddFlow(new_flow);
}
this->flow += new_flow;
return new_flow;
}
/**
* Create a leg of a path in the link graph.
* @param n Id of the link graph node this path passes.
* @param source If true, this is the first leg of the path.
*/
Path::Path(NodeID n, bool source) :
distance(source ? 0 : UINT_MAX),
capacity(source ? UINT_MAX : 0),
free_capacity(source ? INT_MAX : INT_MIN),
flow(0), node(n), origin(source ? n : INVALID_NODE),
num_children(0), parent(NULL)
{}

View File

@@ -0,0 +1,436 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file linkgraphjob.h Declaration of link graph job classes used for cargo distribution. */
#ifndef LINKGRAPHJOB_H
#define LINKGRAPHJOB_H
#include "../thread/thread.h"
#include "linkgraph.h"
#include <list>
class LinkGraphJob;
class Path;
typedef std::list<Path *> PathList;
/** Type of the pool for link graph jobs. */
typedef Pool<LinkGraphJob, LinkGraphJobID, 32, 0xFFFF> LinkGraphJobPool;
/** The actual pool with link graph jobs. */
extern LinkGraphJobPool _link_graph_job_pool;
/**
* Class for calculation jobs to be run on link graphs.
*/
class LinkGraphJob : public LinkGraphJobPool::PoolItem<&_link_graph_job_pool>{
private:
/**
* Annotation for a link graph edge.
*/
struct EdgeAnnotation {
uint demand; ///< Transport demand between the nodes.
uint unsatisfied_demand; ///< Demand over this edge that hasn't been satisfied yet.
uint flow; ///< Planned flow over this edge.
void Init();
};
/**
* Annotation for a link graph node.
*/
struct NodeAnnotation {
uint undelivered_supply; ///< Amount of supply that hasn't been distributed yet.
PathList paths; ///< Paths through this node, sorted so that those with flow == 0 are in the back.
FlowStatMap flows; ///< Planned flows to other nodes.
void Init(uint supply);
};
typedef SmallVector<NodeAnnotation, 16> NodeAnnotationVector;
typedef SmallMatrix<EdgeAnnotation> EdgeAnnotationMatrix;
friend const SaveLoad *GetLinkGraphJobDesc();
friend class LinkGraphSchedule;
protected:
const LinkGraph link_graph; ///< Link graph to by analyzed. Is copied when job is started and mustn't be modified later.
const LinkGraphSettings settings; ///< Copy of _settings_game.linkgraph at spawn time.
ThreadObject *thread; ///< Thread the job is running in or NULL if it's running in the main thread.
Date join_date; ///< Date when the job is to be joined.
NodeAnnotationVector nodes; ///< Extra node data necessary for link graph calculation.
EdgeAnnotationMatrix edges; ///< Extra edge data necessary for link graph calculation.
void EraseFlows(NodeID from);
void JoinThread();
void SpawnThread();
public:
/**
* A job edge. Wraps a link graph edge and an edge annotation. The
* annotation can be modified, the edge is constant.
*/
class Edge : public LinkGraph::ConstEdge {
private:
EdgeAnnotation &anno; ///< Annotation being wrapped.
public:
/**
* Constructor.
* @param edge Link graph edge to be wrapped.
* @param anno Annotation to be wrapped.
*/
Edge(const LinkGraph::BaseEdge &edge, EdgeAnnotation &anno) :
LinkGraph::ConstEdge(edge), anno(anno) {}
/**
* Get the transport demand between end the points of the edge.
* @return Demand.
*/
uint Demand() const { return this->anno.demand; }
/**
* Get the transport demand that hasn't been satisfied by flows, yet.
* @return Unsatisfied demand.
*/
uint UnsatisfiedDemand() const { return this->anno.unsatisfied_demand; }
/**
* Get the total flow on the edge.
* @return Flow.
*/
uint Flow() const { return this->anno.flow; }
/**
* Add some flow.
* @param flow Flow to be added.
*/
void AddFlow(uint flow) { this->anno.flow += flow; }
/**
* Remove some flow.
* @param flow Flow to be removed.
*/
void RemoveFlow(uint flow)
{
assert(flow <= this->anno.flow);
this->anno.flow -= flow;
}
/**
* Add some (not yet satisfied) demand.
* @param demand Demand to be added.
*/
void AddDemand(uint demand)
{
this->anno.demand += demand;
this->anno.unsatisfied_demand += demand;
}
/**
* Satisfy some demand.
* @param demand Demand to be satisfied.
*/
void SatisfyDemand(uint demand)
{
assert(demand <= this->anno.unsatisfied_demand);
this->anno.unsatisfied_demand -= demand;
}
};
/**
* Iterator for job edges.
*/
class EdgeIterator : public LinkGraph::BaseEdgeIterator<const LinkGraph::BaseEdge, Edge, EdgeIterator> {
EdgeAnnotation *base_anno; ///< Array of annotations to be (indirectly) iterated.
public:
/**
* Constructor.
* @param base Array of edges to be iterated.
* @param base_anno Array of annotations to be iterated.
* @param current Start offset of iteration.
*/
EdgeIterator(const LinkGraph::BaseEdge *base, EdgeAnnotation *base_anno, NodeID current) :
LinkGraph::BaseEdgeIterator<const LinkGraph::BaseEdge, Edge, EdgeIterator>(base, current),
base_anno(base_anno) {}
/**
* Dereference.
* @return Pair of the edge currently pointed to and the ID of its
* other end.
*/
SmallPair<NodeID, Edge> operator*() const
{
return SmallPair<NodeID, Edge>(this->current, Edge(this->base[this->current], this->base_anno[this->current]));
}
/**
* Dereference. Has to be repeated here as operator* is different than
* in LinkGraph::EdgeWrapper.
* @return Fake pointer to pair of NodeID/Edge.
*/
FakePointer operator->() const {
return FakePointer(this->operator*());
}
};
/**
* Link graph job node. Wraps a constant link graph node and a modifiable
* node annotation.
*/
class Node : public LinkGraph::ConstNode {
private:
NodeAnnotation &node_anno; ///< Annotation being wrapped.
EdgeAnnotation *edge_annos; ///< Edge annotations belonging to this node.
public:
/**
* Constructor.
* @param lgj Job to take the node from.
* @param node ID of the node.
*/
Node (LinkGraphJob *lgj, NodeID node) :
LinkGraph::ConstNode(&lgj->link_graph, node),
node_anno(lgj->nodes[node]), edge_annos(lgj->edges[node])
{}
/**
* Retrieve an edge starting at this node. Mind that this returns an
* object, not a reference.
* @param to Remote end of the edge.
* @return Edge between this node and "to".
*/
Edge operator[](NodeID to) const { return Edge(this->edges[to], this->edge_annos[to]); }
/**
* Iterator for the "begin" of the edge array. Only edges with capacity
* are iterated. The others are skipped.
* @return Iterator pointing to the first edge.
*/
EdgeIterator Begin() const { return EdgeIterator(this->edges, this->edge_annos, index); }
/**
* Iterator for the "end" of the edge array. Only edges with capacity
* are iterated. The others are skipped.
* @return Iterator pointing beyond the last edge.
*/
EdgeIterator End() const { return EdgeIterator(this->edges, this->edge_annos, INVALID_NODE); }
/**
* Get amount of supply that hasn't been delivered, yet.
* @return Undelivered supply.
*/
uint UndeliveredSupply() const { return this->node_anno.undelivered_supply; }
/**
* Get the flows running through this node.
* @return Flows.
*/
FlowStatMap &Flows() { return this->node_anno.flows; }
/**
* Get a constant version of the flows running through this node.
* @return Flows.
*/
const FlowStatMap &Flows() const { return this->node_anno.flows; }
/**
* Get the paths this node is part of. Paths are always expected to be
* sorted so that those with flow == 0 are in the back of the list.
* @return Paths.
*/
PathList &Paths() { return this->node_anno.paths; }
/**
* Get a constant version of the paths this node is part of.
* @return Paths.
*/
const PathList &Paths() const { return this->node_anno.paths; }
/**
* Deliver some supply, adding demand to the respective edge.
* @param to Destination for supply.
* @param amount Amount of supply to be delivered.
*/
void DeliverSupply(NodeID to, uint amount)
{
this->node_anno.undelivered_supply -= amount;
(*this)[to].AddDemand(amount);
}
};
/**
* Bare constructor, only for save/load. link_graph, join_date and actually
* settings have to be brutally const-casted in order to populate them.
*/
LinkGraphJob() : settings(_settings_game.linkgraph), thread(NULL),
join_date(INVALID_DATE) {}
LinkGraphJob(const LinkGraph &orig);
~LinkGraphJob();
void Init();
/**
* Check if job is supposed to be finished.
* @return True if job should be finished by now, false if not.
*/
inline bool IsFinished() const { return this->join_date <= _date; }
/**
* Get the date when the job should be finished.
* @return Join date.
*/
inline Date JoinDate() const { return join_date; }
/**
* Change the join date on date cheating.
* @param interval Number of days to add.
*/
inline void ShiftJoinDate(int interval) { this->join_date += interval; }
/**
* Get the link graph settings for this component.
* @return Settings.
*/
inline const LinkGraphSettings &Settings() const { return this->settings; }
/**
* Get a node abstraction with the specified id.
* @param num ID of the node.
* @return the Requested node.
*/
inline Node operator[](NodeID num) { return Node(this, num); }
/**
* Get the size of the underlying link graph.
* @return Size.
*/
inline uint Size() const { return this->link_graph.Size(); }
/**
* Get the cargo of the underlying link graph.
* @return Cargo.
*/
inline CargoID Cargo() const { return this->link_graph.Cargo(); }
/**
* Get the date when the underlying link graph was last compressed.
* @return Compression date.
*/
inline Date LastCompression() const { return this->link_graph.LastCompression(); }
/**
* Get the ID of the underlying link graph.
* @return Link graph ID.
*/
inline LinkGraphID LinkGraphIndex() const { return this->link_graph.index; }
/**
* Get a reference to the underlying link graph. Only use this for save/load.
* @return Link graph.
*/
inline const LinkGraph &Graph() const { return this->link_graph; }
};
#define FOR_ALL_LINK_GRAPH_JOBS(var) FOR_ALL_ITEMS_FROM(LinkGraphJob, link_graph_job_index, var, 0)
/**
* A leg of a path in the link graph. Paths can form trees by being "forked".
*/
class Path {
public:
static Path *invalid_path;
Path(NodeID n, bool source = false);
/** Get the node this leg passes. */
inline NodeID GetNode() const { return this->node; }
/** Get the overall origin of the path. */
inline NodeID GetOrigin() const { return this->origin; }
/** Get the parent leg of this one. */
inline Path *GetParent() { return this->parent; }
/** Get the overall capacity of the path. */
inline uint GetCapacity() const { return this->capacity; }
/** Get the free capacity of the path. */
inline int GetFreeCapacity() const { return this->free_capacity; }
/**
* Get ratio of free * 16 (so that we get fewer 0) /
* max(total capacity, 1) (so that we don't divide by 0).
* @param free Free capacity.
* @param total Total capacity.
* @return free * 16 / max(total, 1).
*/
inline static int GetCapacityRatio(int free, uint total)
{
return Clamp(free, PATH_CAP_MIN_FREE, PATH_CAP_MAX_FREE) * PATH_CAP_MULTIPLIER / max(total, 1U);
}
/**
* Get capacity ratio of this path.
* @return free capacity * 16 / (total capacity + 1).
*/
inline int GetCapacityRatio() const
{
return Path::GetCapacityRatio(this->free_capacity, this->capacity);
}
/** Get the overall distance of the path. */
inline uint GetDistance() const { return this->distance; }
/** Reduce the flow on this leg only by the specified amount. */
inline void ReduceFlow(uint f) { this->flow -= f; }
/** Increase the flow on this leg only by the specified amount. */
inline void AddFlow(uint f) { this->flow += f; }
/** Get the flow on this leg. */
inline uint GetFlow() const { return this->flow; }
/** Get the number of "forked off" child legs of this one. */
inline uint GetNumChildren() const { return this->num_children; }
/**
* Detach this path from its parent.
*/
inline void Detach()
{
if (this->parent != NULL) {
this->parent->num_children--;
this->parent = NULL;
}
}
uint AddFlow(uint f, LinkGraphJob &job, uint max_saturation);
void Fork(Path *base, uint cap, int free_cap, uint dist);
protected:
/**
* Some boundaries to clamp agains in order to avoid integer overflows.
*/
enum PathCapacityBoundaries {
PATH_CAP_MULTIPLIER = 16,
PATH_CAP_MIN_FREE = (INT_MIN + 1) / PATH_CAP_MULTIPLIER,
PATH_CAP_MAX_FREE = (INT_MAX - 1) / PATH_CAP_MULTIPLIER
};
uint distance; ///< Sum(distance of all legs up to this one).
uint capacity; ///< This capacity is min(capacity) fom all edges.
int free_capacity; ///< This capacity is min(edge.capacity - edge.flow) for the current run of Dijkstra.
uint flow; ///< Flow the current run of the mcf solver assigns.
NodeID node; ///< Link graph node this leg passes.
NodeID origin; ///< Link graph node this path originates from.
uint num_children; ///< Number of child legs that have been forked from this path.
Path *parent; ///< Parent leg of this one.
};
#endif /* LINKGRAPHJOB_H */

View File

@@ -0,0 +1,23 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file linkgraphjob_base.h Some typedefs for component handlers. */
#ifndef LINKGRAPHJOB_BASE_H
#define LINKGRAPHJOB_BASE_H
#include "linkgraph.h"
#include "linkgraphjob.h"
#include "linkgraphschedule.h"
typedef LinkGraphJob::Node Node;
typedef LinkGraphJob::Edge Edge;
typedef LinkGraphJob::EdgeIterator EdgeIterator;
#endif /* LINKGRAPHJOB_BASE_H */

View File

@@ -0,0 +1,158 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file linkgraphschedule.cpp Definition of link graph schedule used for cargo distribution. */
#include "../stdafx.h"
#include "linkgraphschedule.h"
#include "init.h"
#include "demands.h"
#include "mcf.h"
#include "flowmapper.h"
#include "../safeguards.h"
/**
* Static instance of LinkGraphSchedule.
* Note: This instance is created on task start.
* Lazy creation on first usage results in a data race between the CDist threads.
*/
/* static */ LinkGraphSchedule LinkGraphSchedule::instance;
/**
* Start the next job in the schedule.
*/
void LinkGraphSchedule::SpawnNext()
{
if (this->schedule.empty()) return;
LinkGraph *next = this->schedule.front();
LinkGraph *first = next;
while (next->Size() < 2) {
this->schedule.splice(this->schedule.end(), this->schedule, this->schedule.begin());
next = this->schedule.front();
if (next == first) return;
}
assert(next == LinkGraph::Get(next->index));
this->schedule.pop_front();
if (LinkGraphJob::CanAllocateItem()) {
LinkGraphJob *job = new LinkGraphJob(*next);
job->SpawnThread();
this->running.push_back(job);
} else {
NOT_REACHED();
}
}
/**
* Join the next finished job, if available.
*/
void LinkGraphSchedule::JoinNext()
{
if (this->running.empty()) return;
LinkGraphJob *next = this->running.front();
if (!next->IsFinished()) return;
this->running.pop_front();
LinkGraphID id = next->LinkGraphIndex();
delete next; // implicitly joins the thread
if (LinkGraph::IsValidID(id)) {
LinkGraph *lg = LinkGraph::Get(id);
this->Unqueue(lg); // Unqueue to avoid double-queueing recycled IDs.
this->Queue(lg);
}
}
/**
* Run all handlers for the given Job. This method is tailored to
* ThreadObject::New.
* @param j Pointer to a link graph job.
*/
/* static */ void LinkGraphSchedule::Run(void *j)
{
LinkGraphJob *job = (LinkGraphJob *)j;
for (uint i = 0; i < lengthof(instance.handlers); ++i) {
instance.handlers[i]->Run(*job);
}
}
/**
* Start all threads in the running list. This is only useful for save/load.
* Usually threads are started when the job is created.
*/
void LinkGraphSchedule::SpawnAll()
{
for (JobList::iterator i = this->running.begin(); i != this->running.end(); ++i) {
(*i)->SpawnThread();
}
}
/**
* Clear all link graphs and jobs from the schedule.
*/
/* static */ void LinkGraphSchedule::Clear()
{
for (JobList::iterator i(instance.running.begin()); i != instance.running.end(); ++i) {
(*i)->JoinThread();
}
instance.running.clear();
instance.schedule.clear();
}
/**
* Shift all dates (join dates and edge annotations) of link graphs and link
* graph jobs by the number of days given.
* @param interval Number of days to be added or subtracted.
*/
void LinkGraphSchedule::ShiftDates(int interval)
{
LinkGraph *lg;
FOR_ALL_LINK_GRAPHS(lg) lg->ShiftDates(interval);
LinkGraphJob *lgj;
FOR_ALL_LINK_GRAPH_JOBS(lgj) lgj->ShiftJoinDate(interval);
}
/**
* Create a link graph schedule and initialize its handlers.
*/
LinkGraphSchedule::LinkGraphSchedule()
{
this->handlers[0] = new InitHandler;
this->handlers[1] = new DemandHandler;
this->handlers[2] = new MCFHandler<MCF1stPass>;
this->handlers[3] = new FlowMapper(false);
this->handlers[4] = new MCFHandler<MCF2ndPass>;
this->handlers[5] = new FlowMapper(true);
}
/**
* Delete a link graph schedule and its handlers.
*/
LinkGraphSchedule::~LinkGraphSchedule()
{
this->Clear();
for (uint i = 0; i < lengthof(this->handlers); ++i) {
delete this->handlers[i];
}
}
/**
* Spawn or join a link graph job or compress a link graph if any link graph is
* due to do so.
*/
void OnTick_LinkGraph()
{
if (_date_fract != LinkGraphSchedule::SPAWN_JOIN_TICK) return;
Date offset = _date % _settings_game.linkgraph.recalc_interval;
if (offset == 0) {
LinkGraphSchedule::instance.SpawnNext();
} else if (offset == _settings_game.linkgraph.recalc_interval / 2) {
LinkGraphSchedule::instance.JoinNext();
}
}

View File

@@ -0,0 +1,81 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file linkgraphschedule.h Declaration of link graph schedule used for cargo distribution. */
#ifndef LINKGRAPHSCHEDULE_H
#define LINKGRAPHSCHEDULE_H
#include "linkgraph.h"
class LinkGraphJob;
/**
* A handler doing "something" on a link graph component. It must not keep any
* state as it is called concurrently from different threads.
*/
class ComponentHandler {
public:
/**
* Destroy the handler. Must be given due to virtual Run.
*/
virtual ~ComponentHandler() {}
/**
* Run the handler. A link graph handler must not read or write any data
* outside the given component as that would create a potential desync.
* @param job Link graph component to run the handler on.
*/
virtual void Run(LinkGraphJob &job) const = 0;
};
class LinkGraphSchedule {
private:
LinkGraphSchedule();
~LinkGraphSchedule();
typedef std::list<LinkGraph *> GraphList;
typedef std::list<LinkGraphJob *> JobList;
friend const SaveLoad *GetLinkGraphScheduleDesc();
protected:
ComponentHandler *handlers[6]; ///< Handlers to be run for each job.
GraphList schedule; ///< Queue for new jobs.
JobList running; ///< Currently running jobs.
public:
/* This is a tick where not much else is happening, so a small lag might go unnoticed. */
static const uint SPAWN_JOIN_TICK = 21; ///< Tick when jobs are spawned or joined every day.
static LinkGraphSchedule instance;
static void Run(void *j);
static void Clear();
void SpawnNext();
void JoinNext();
void SpawnAll();
void ShiftDates(int interval);
/**
* Queue a link graph for execution.
* @param lg Link graph to be queued.
*/
void Queue(LinkGraph *lg)
{
assert(LinkGraph::Get(lg->index) == lg);
this->schedule.push_back(lg);
}
/**
* Remove a link graph from the execution queue.
* @param lg Link graph to be removed.
*/
void Unqueue(LinkGraph *lg) { this->schedule.remove(lg); }
};
#endif /* LINKGRAPHSCHEDULE_H */

579
src/linkgraph/mcf.cpp Normal file
View File

@@ -0,0 +1,579 @@
/** @file mcf.cpp Definition of Multi-Commodity-Flow solver. */
#include "../stdafx.h"
#include "../core/math_func.hpp"
#include "mcf.h"
#include <set>
#include "../safeguards.h"
typedef std::map<NodeID, Path *> PathViaMap;
/**
* Distance-based annotation for use in the Dijkstra algorithm. This is close
* to the original meaning of "annotation" in this context. Paths are rated
* according to the sum of distances of their edges.
*/
class DistanceAnnotation : public Path {
public:
/**
* Constructor.
* @param n ID of node to be annotated.
* @param source If the node is the source of its path.
*/
DistanceAnnotation(NodeID n, bool source = false) : Path(n, source) {}
bool IsBetter(const DistanceAnnotation *base, uint cap, int free_cap, uint dist) const;
/**
* Return the actual value of the annotation, in this case the distance.
* @return Distance.
*/
inline uint GetAnnotation() const { return this->distance; }
/**
* Comparator for std containers.
*/
struct Comparator {
bool operator()(const DistanceAnnotation *x, const DistanceAnnotation *y) const;
};
};
/**
* Capacity-based annotation for use in the Dijkstra algorithm. This annotation
* rates paths according to the maximum capacity of their edges. The Dijkstra
* algorithm still gives meaningful results like this as the capacity of a path
* can only decrease or stay the same if you add more edges.
*/
class CapacityAnnotation : public Path {
public:
/**
* Constructor.
* @param n ID of node to be annotated.
* @param source If the node is the source of its path.
*/
CapacityAnnotation(NodeID n, bool source = false) : Path(n, source) {}
bool IsBetter(const CapacityAnnotation *base, uint cap, int free_cap, uint dist) const;
/**
* Return the actual value of the annotation, in this case the capacity.
* @return Capacity.
*/
inline int GetAnnotation() const { return this->GetCapacityRatio(); }
/**
* Comparator for std containers.
*/
struct Comparator {
bool operator()(const CapacityAnnotation *x, const CapacityAnnotation *y) const;
};
};
/**
* Iterator class for getting the edges in the order of their next_edge
* members.
*/
class GraphEdgeIterator {
private:
LinkGraphJob &job; ///< Job being executed
EdgeIterator i; ///< Iterator pointing to current edge.
EdgeIterator end; ///< Iterator pointing beyond last edge.
public:
/**
* Construct a GraphEdgeIterator.
* @param job Job to iterate on.
*/
GraphEdgeIterator(LinkGraphJob &job) : job(job),
i(NULL, NULL, INVALID_NODE), end(NULL, NULL, INVALID_NODE)
{}
/**
* Setup the node to start iterating at.
* @param source Unused.
* @param node Node to start iterating at.
*/
void SetNode(NodeID source, NodeID node)
{
this->i = this->job[node].Begin();
this->end = this->job[node].End();
}
/**
* Retrieve the ID of the node the next edge points to.
* @return Next edge's target node ID or INVALID_NODE.
*/
NodeID Next()
{
return this->i != this->end ? (this->i++)->first : INVALID_NODE;
}
};
/**
* Iterator class for getting edges from a FlowStatMap.
*/
class FlowEdgeIterator {
private:
LinkGraphJob &job; ///< Link graph job we're working with.
/** Lookup table for getting NodeIDs from StationIDs. */
std::map<StationID, NodeID> station_to_node;
/** Current iterator in the shares map. */
FlowStat::SharesMap::const_iterator it;
/** End of the shares map. */
FlowStat::SharesMap::const_iterator end;
public:
/**
* Constructor.
* @param job Link graph job to work with.
*/
FlowEdgeIterator(LinkGraphJob &job) : job(job)
{
for (NodeID i = 0; i < job.Size(); ++i) {
this->station_to_node[job[i].Station()] = i;
}
}
/**
* Setup the node to retrieve edges from.
* @param source Root of the current path tree.
* @param node Current node to be checked for outgoing flows.
*/
void SetNode(NodeID source, NodeID node)
{
const FlowStatMap &flows = this->job[node].Flows();
FlowStatMap::const_iterator it = flows.find(this->job[source].Station());
if (it != flows.end()) {
this->it = it->second.GetShares()->begin();
this->end = it->second.GetShares()->end();
} else {
this->it = FlowStat::empty_sharesmap.begin();
this->end = FlowStat::empty_sharesmap.end();
}
}
/**
* Get the next node for which a flow exists.
* @return ID of next node with flow.
*/
NodeID Next()
{
if (this->it == this->end) return INVALID_NODE;
return this->station_to_node[(this->it++)->second];
}
};
/**
* Determines if an extension to the given Path with the given parameters is
* better than this path.
* @param base Other path.
* @param cap Capacity of the new edge to be added to base.
* @param dist Distance of the new edge.
* @return True if base + the new edge would be better than the path associated
* with this annotation.
*/
bool DistanceAnnotation::IsBetter(const DistanceAnnotation *base, uint cap,
int free_cap, uint dist) const
{
/* If any of the paths is disconnected, the other one is better. If both
* are disconnected, this path is better.*/
if (base->distance == UINT_MAX) {
return false;
} else if (this->distance == UINT_MAX) {
return true;
}
if (free_cap > 0 && base->free_capacity > 0) {
/* If both paths have capacity left, compare their distances.
* If the other path has capacity left and this one hasn't, the
* other one's better (thus, return true). */
return this->free_capacity > 0 ? (base->distance + dist < this->distance) : true;
} else {
/* If the other path doesn't have capacity left, but this one has,
* the other one is worse (thus, return false).
* If both paths are out of capacity, do the regular distance
* comparison. */
return this->free_capacity > 0 ? false : (base->distance + dist < this->distance);
}
}
/**
* Determines if an extension to the given Path with the given parameters is
* better than this path.
* @param base Other path.
* @param cap Capacity of the new edge to be added to base.
* @param dist Distance of the new edge.
* @return True if base + the new edge would be better than the path associated
* with this annotation.
*/
bool CapacityAnnotation::IsBetter(const CapacityAnnotation *base, uint cap,
int free_cap, uint dist) const
{
int min_cap = Path::GetCapacityRatio(min(base->free_capacity, free_cap), min(base->capacity, cap));
int this_cap = this->GetCapacityRatio();
if (min_cap == this_cap) {
/* If the capacities are the same and the other path isn't disconnected
* choose the shorter path. */
return base->distance == UINT_MAX ? false : (base->distance + dist < this->distance);
} else {
return min_cap > this_cap;
}
}
/**
* A slightly modified Dijkstra algorithm. Grades the paths not necessarily by
* distance, but by the value Tannotation computes. It uses the max_saturation
* setting to artificially decrease capacities.
* @tparam Tannotation Annotation to be used.
* @tparam Tedge_iterator Iterator to be used for getting outgoing edges.
* @param source_node Node where the algorithm starts.
* @param paths Container for the paths to be calculated.
*/
template<class Tannotation, class Tedge_iterator>
void MultiCommodityFlow::Dijkstra(NodeID source_node, PathVector &paths)
{
typedef std::set<Tannotation *, typename Tannotation::Comparator> AnnoSet;
Tedge_iterator iter(this->job);
uint size = this->job.Size();
AnnoSet annos;
paths.resize(size, NULL);
for (NodeID node = 0; node < size; ++node) {
Tannotation *anno = new Tannotation(node, node == source_node);
annos.insert(anno);
paths[node] = anno;
}
while (!annos.empty()) {
typename AnnoSet::iterator i = annos.begin();
Tannotation *source = *i;
annos.erase(i);
NodeID from = source->GetNode();
iter.SetNode(source_node, from);
for (NodeID to = iter.Next(); to != INVALID_NODE; to = iter.Next()) {
if (to == from) continue; // Not a real edge but a consumption sign.
Edge edge = this->job[from][to];
uint capacity = edge.Capacity();
if (this->max_saturation != UINT_MAX) {
capacity *= this->max_saturation;
capacity /= 100;
if (capacity == 0) capacity = 1;
}
/* punish in-between stops a little */
uint distance = DistanceMaxPlusManhattan(this->job[from].XY(), this->job[to].XY()) + 1;
Tannotation *dest = static_cast<Tannotation *>(paths[to]);
if (dest->IsBetter(source, capacity, capacity - edge.Flow(), distance)) {
annos.erase(dest);
dest->Fork(source, capacity, capacity - edge.Flow(), distance);
annos.insert(dest);
}
}
}
}
/**
* Clean up paths that lead nowhere and the root path.
* @param source_id ID of the root node.
* @param paths Paths to be cleaned up.
*/
void MultiCommodityFlow::CleanupPaths(NodeID source_id, PathVector &paths)
{
Path *source = paths[source_id];
paths[source_id] = NULL;
for (PathVector::iterator i = paths.begin(); i != paths.end(); ++i) {
Path *path = *i;
if (path == NULL) continue;
if (path->GetParent() == source) path->Detach();
while (path != source && path != NULL && path->GetFlow() == 0) {
Path *parent = path->GetParent();
path->Detach();
if (path->GetNumChildren() == 0) {
paths[path->GetNode()] = NULL;
delete path;
}
path = parent;
}
}
delete source;
paths.clear();
}
/**
* Push flow along a path and update the unsatisfied_demand of the associated
* edge.
* @param edge Edge whose ends the path connects.
* @param path End of the path the flow should be pushed on.
* @param accuracy Accuracy of the calculation.
* @param max_saturation If < UINT_MAX only push flow up to the given
* saturation, otherwise the path can be "overloaded".
*/
uint MultiCommodityFlow::PushFlow(Edge &edge, Path *path, uint accuracy,
uint max_saturation)
{
assert(edge.UnsatisfiedDemand() > 0);
uint flow = Clamp(edge.Demand() / accuracy, 1, edge.UnsatisfiedDemand());
flow = path->AddFlow(flow, this->job, max_saturation);
edge.SatisfyDemand(flow);
return flow;
}
/**
* Find the flow along a cycle including cycle_begin in path.
* @param path Set of paths that form the cycle.
* @param cycle_begin Path to start at.
* @return Flow along the cycle.
*/
uint MCF1stPass::FindCycleFlow(const PathVector &path, const Path *cycle_begin)
{
uint flow = UINT_MAX;
const Path *cycle_end = cycle_begin;
do {
flow = min(flow, cycle_begin->GetFlow());
cycle_begin = path[cycle_begin->GetNode()];
} while (cycle_begin != cycle_end);
return flow;
}
/**
* Eliminate a cycle of the given flow in the given set of paths.
* @param path Set of paths containing the cycle.
* @param cycle_begin Part of the cycle to start at.
* @param flow Flow along the cycle.
*/
void MCF1stPass::EliminateCycle(PathVector &path, Path *cycle_begin, uint flow)
{
Path *cycle_end = cycle_begin;
do {
NodeID prev = cycle_begin->GetNode();
cycle_begin->ReduceFlow(flow);
if (cycle_begin->GetFlow() == 0) {
PathList &node_paths = this->job[cycle_begin->GetParent()->GetNode()].Paths();
for (PathList::iterator i = node_paths.begin(); i != node_paths.end(); ++i) {
if (*i == cycle_begin) {
node_paths.erase(i);
node_paths.push_back(cycle_begin);
break;
}
}
}
cycle_begin = path[prev];
Edge edge = this->job[prev][cycle_begin->GetNode()];
edge.RemoveFlow(flow);
} while (cycle_begin != cycle_end);
}
/**
* Eliminate cycles for origin_id in the graph. Start searching at next_id and
* work recursively. Also "summarize" paths: Add up the flows along parallel
* paths in one.
* @param path Paths checked in parent calls to this method.
* @param origin_id Origin of the paths to be checked.
* @param next_id Next node to be checked.
* @return If any cycles have been found and eliminated.
*/
bool MCF1stPass::EliminateCycles(PathVector &path, NodeID origin_id, NodeID next_id)
{
Path *at_next_pos = path[next_id];
/* this node has already been searched */
if (at_next_pos == Path::invalid_path) return false;
if (at_next_pos == NULL) {
/* Summarize paths; add up the paths with the same source and next hop
* in one path each. */
PathList &paths = this->job[next_id].Paths();
PathViaMap next_hops;
for (PathList::iterator i = paths.begin(); i != paths.end();) {
Path *new_child = *i;
uint new_flow = new_child->GetFlow();
if (new_flow == 0) break;
if (new_child->GetOrigin() == origin_id) {
PathViaMap::iterator via_it = next_hops.find(new_child->GetNode());
if (via_it == next_hops.end()) {
next_hops[new_child->GetNode()] = new_child;
++i;
} else {
Path *child = via_it->second;
child->AddFlow(new_flow);
new_child->ReduceFlow(new_flow);
/* We might hit end() with with the ++ here and skip the
* newly push_back'ed path. That's good as the flow of that
* path is 0 anyway. */
paths.erase(i++);
paths.push_back(new_child);
}
} else {
++i;
}
}
bool found = false;
/* Search the next hops for nodes we have already visited */
for (PathViaMap::iterator via_it = next_hops.begin();
via_it != next_hops.end(); ++via_it) {
Path *child = via_it->second;
if (child->GetFlow() > 0) {
/* Push one child into the path vector and search this child's
* children. */
path[next_id] = child;
found = this->EliminateCycles(path, origin_id, child->GetNode()) || found;
}
}
/* All paths departing from this node have been searched. Mark as
* resolved if no cycles found. If cycles were found further cycles
* could be found in this branch, thus it has to be searched again next
* time we spot it.
*/
path[next_id] = found ? NULL : Path::invalid_path;
return found;
}
/* This node has already been visited => we have a cycle.
* Backtrack to find the exact flow. */
uint flow = this->FindCycleFlow(path, at_next_pos);
if (flow > 0) {
this->EliminateCycle(path, at_next_pos, flow);
return true;
}
return false;
}
/**
* Eliminate all cycles in the graph. Check paths starting at each node for
* potential cycles.
* @return If any cycles have been found and eliminated.
*/
bool MCF1stPass::EliminateCycles()
{
bool cycles_found = false;
uint size = this->job.Size();
PathVector path(size, NULL);
for (NodeID node = 0; node < size; ++node) {
/* Starting at each node in the graph find all cycles involving this
* node. */
std::fill(path.begin(), path.end(), (Path *)NULL);
cycles_found |= this->EliminateCycles(path, node, node);
}
return cycles_found;
}
/**
* Run the first pass of the MCF calculation.
* @param job Link graph job to calculate.
*/
MCF1stPass::MCF1stPass(LinkGraphJob &job) : MultiCommodityFlow(job)
{
PathVector paths;
uint size = job.Size();
uint accuracy = job.Settings().accuracy;
bool more_loops;
do {
more_loops = false;
for (NodeID source = 0; source < size; ++source) {
/* First saturate the shortest paths. */
this->Dijkstra<DistanceAnnotation, GraphEdgeIterator>(source, paths);
for (NodeID dest = 0; dest < size; ++dest) {
Edge edge = job[source][dest];
if (edge.UnsatisfiedDemand() > 0) {
Path *path = paths[dest];
assert(path != NULL);
/* Generally only allow paths that don't exceed the
* available capacity. But if no demand has been assigned
* yet, make an exception and allow any valid path *once*. */
if (path->GetFreeCapacity() > 0 && this->PushFlow(edge, path,
accuracy, this->max_saturation) > 0) {
/* If a path has been found there is a chance we can
* find more. */
more_loops = more_loops || (edge.UnsatisfiedDemand() > 0);
} else if (edge.UnsatisfiedDemand() == edge.Demand() &&
path->GetFreeCapacity() > INT_MIN) {
this->PushFlow(edge, path, accuracy, UINT_MAX);
}
}
}
this->CleanupPaths(source, paths);
}
} while (more_loops || this->EliminateCycles());
}
/**
* Run the second pass of the MCF calculation which assigns all remaining
* demands to existing paths.
* @param job Link graph job to calculate.
*/
MCF2ndPass::MCF2ndPass(LinkGraphJob &job) : MultiCommodityFlow(job)
{
this->max_saturation = UINT_MAX; // disable artificial cap on saturation
PathVector paths;
uint size = job.Size();
uint accuracy = job.Settings().accuracy;
bool demand_left = true;
while (demand_left) {
demand_left = false;
for (NodeID source = 0; source < size; ++source) {
this->Dijkstra<CapacityAnnotation, FlowEdgeIterator>(source, paths);
for (NodeID dest = 0; dest < size; ++dest) {
Edge edge = this->job[source][dest];
Path *path = paths[dest];
if (edge.UnsatisfiedDemand() > 0 && path->GetFreeCapacity() > INT_MIN) {
this->PushFlow(edge, path, accuracy, UINT_MAX);
if (edge.UnsatisfiedDemand() > 0) demand_left = true;
}
}
this->CleanupPaths(source, paths);
}
}
}
/**
* Relation that creates a weak order without duplicates.
* Avoid accidentally deleting different paths of the same capacity/distance in
* a set. When the annotation is the same node IDs are compared, so there are
* no equal ranges.
* @tparam T Type to be compared on.
* @param x_anno First value.
* @param y_anno Second value.
* @param x Node id associated with the first value.
* @param y Node id associated with the second value.
*/
template <typename T>
bool Greater(T x_anno, T y_anno, NodeID x, NodeID y)
{
if (x_anno > y_anno) return true;
if (x_anno < y_anno) return false;
return x > y;
}
/**
* Compare two capacity annotations.
* @param x First capacity annotation.
* @param y Second capacity annotation.
* @return If x is better than y.
*/
bool CapacityAnnotation::Comparator::operator()(const CapacityAnnotation *x,
const CapacityAnnotation *y) const
{
return x != y && Greater<int>(x->GetAnnotation(), y->GetAnnotation(),
x->GetNode(), y->GetNode());
}
/**
* Compare two distance annotations.
* @param x First distance annotation.
* @param y Second distance annotation.
* @return If x is better than y.
*/
bool DistanceAnnotation::Comparator::operator()(const DistanceAnnotation *x,
const DistanceAnnotation *y) const
{
return x != y && !Greater<uint>(x->GetAnnotation(), y->GetAnnotation(),
x->GetNode(), y->GetNode());
}

92
src/linkgraph/mcf.h Normal file
View File

@@ -0,0 +1,92 @@
/** @file mcf.h Declaration of Multi-Commodity-Flow solver */
#ifndef MCF_H
#define MCF_H
#include "linkgraphjob_base.h"
#include <vector>
typedef std::vector<Path *> PathVector;
/**
* Multi-commodity flow calculating base class.
*/
class MultiCommodityFlow {
protected:
/**
* Constructor.
* @param job Link graph job being executed.
*/
MultiCommodityFlow(LinkGraphJob &job) : job(job),
max_saturation(job.Settings().short_path_saturation)
{}
template<class Tannotation, class Tedge_iterator>
void Dijkstra(NodeID from, PathVector &paths);
uint PushFlow(Edge &edge, Path *path, uint accuracy, uint max_saturation);
void CleanupPaths(NodeID source, PathVector &paths);
LinkGraphJob &job; ///< Job we're working with.
uint max_saturation; ///< Maximum saturation for edges.
};
/**
* First pass of the MCF calculation. Saturates shortest paths first, creates
* new paths if needed, eliminates cycles. This calculation is of exponential
* complexity in the number of nodes but the constant factors are sufficiently
* small to make it usable for most real-life link graph components. You can
* deal with performance problems that might occur here in multiple ways:
* - The overall accuracy is used here to determine how much flow is assigned
* in each loop. The lower the accuracy, the more flow is assigned, the less
* loops it takes to assign all flow.
* - The short_path_saturation setting determines when this pass stops. The
* lower you set it, the less flow will be assigned in this pass, the less
* time it will take.
* - You can increase the recalculation interval to allow for longer running
* times without creating lags.
*/
class MCF1stPass : public MultiCommodityFlow {
private:
bool EliminateCycles();
bool EliminateCycles(PathVector &path, NodeID origin_id, NodeID next_id);
void EliminateCycle(PathVector &path, Path *cycle_begin, uint flow);
uint FindCycleFlow(const PathVector &path, const Path *cycle_begin);
public:
MCF1stPass(LinkGraphJob &job);
};
/**
* Second pass of the MCF calculation. Saturates paths with most capacity left
* first and doesn't create any paths along edges that haven't been visited in
* the first pass. This is why it doesn't have to do any cycle detection and
* elimination. As cycle detection is the most intense problem in the first
* pass this pass is cheaper. The accuracy is used here, too.
*/
class MCF2ndPass : public MultiCommodityFlow {
public:
MCF2ndPass(LinkGraphJob &job);
};
/**
* Link graph handler for MCF. Creates MultiCommodityFlow instance according to
* the template parameter.
*/
template<class Tpass>
class MCFHandler : public ComponentHandler {
public:
/**
* Run the calculation.
* @param graph Component to be calculated.
*/
virtual void Run(LinkGraphJob &job) const { Tpass pass(job); }
/**
* Destructor. Has to be given because of virtual Run().
*/
virtual ~MCFHandler() {}
};
#endif /* MCF_H */

319
src/linkgraph/refresh.cpp Normal file
View File

@@ -0,0 +1,319 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file refresh.h Definition of link refreshing utility. */
#include "../stdafx.h"
#include "../core/bitmath_func.hpp"
#include "../station_func.h"
#include "../engine_base.h"
#include "../vehicle_func.h"
#include "refresh.h"
#include "linkgraph.h"
#include "../safeguards.h"
/**
* Refresh all links the given vehicle will visit.
* @param v Vehicle to refresh links for.
* @param allow_merge If the refresher is allowed to merge or extend link graphs.
* @param is_full_loading If the vehicle is full loading.
*/
/* static */ void LinkRefresher::Run(Vehicle *v, bool allow_merge, bool is_full_loading)
{
/* If there are no orders we can't predict anything.*/
if (v->orders.list == NULL) return;
/* Make sure the first order is a useful order. */
const Order *first = v->orders.list->GetNextDecisionNode(v->GetOrder(v->cur_implicit_order_index), 0);
if (first == NULL) return;
HopSet seen_hops;
LinkRefresher refresher(v, &seen_hops, allow_merge, is_full_loading);
refresher.RefreshLinks(first, first, v->last_loading_station != INVALID_STATION ? 1 << HAS_CARGO : 0);
}
/**
* Comparison operator to allow hops to be used in a std::set.
* @param other Other hop to be compared with.
* @return If this hop is "smaller" than the other (defined by from, to and cargo in this order).
*/
bool LinkRefresher::Hop::operator<(const Hop &other) const
{
if (this->from < other.from) {
return true;
} else if (this->from > other.from) {
return false;
}
if (this->to < other.to) {
return true;
} else if (this->to > other.to) {
return false;
}
return this->cargo < other.cargo;
}
/**
* Constructor for link refreshing algorithm.
* @param vehicle Vehicle to refresh links for.
* @param seen_hops Set of hops already seen. This is shared between this
* refresher and all its children.
* @param allow_merge If the refresher is allowed to merge or extend link graphs.
* @param is_full_loading If the vehicle is full loading.
*/
LinkRefresher::LinkRefresher(Vehicle *vehicle, HopSet *seen_hops, bool allow_merge, bool is_full_loading) :
vehicle(vehicle), seen_hops(seen_hops), cargo(CT_INVALID), allow_merge(allow_merge),
is_full_loading(is_full_loading)
{
/* Assemble list of capacities and set last loading stations to 0. */
for (Vehicle *v = this->vehicle; v != NULL; v = v->Next()) {
this->refit_capacities.push_back(RefitDesc(v->cargo_type, v->cargo_cap, v->refit_cap));
if (v->refit_cap > 0) this->capacities[v->cargo_type] += v->refit_cap;
}
}
/**
* Handle refit orders by updating capacities and refit_capacities.
* @param refit_cargo Cargo to refit to.
* @return True if any vehicle was refit; false if none was.
*/
bool LinkRefresher::HandleRefit(CargoID refit_cargo)
{
this->cargo = refit_cargo;
RefitList::iterator refit_it = this->refit_capacities.begin();
bool any_refit = false;
for (Vehicle *v = this->vehicle; v != NULL; v = v->Next()) {
const Engine *e = Engine::Get(v->engine_type);
if (!HasBit(e->info.refit_mask, this->cargo)) {
++refit_it;
continue;
}
any_refit = true;
/* Back up the vehicle's cargo type */
CargoID temp_cid = v->cargo_type;
byte temp_subtype = v->cargo_subtype;
v->cargo_type = this->cargo;
v->cargo_subtype = GetBestFittingSubType(v, v, this->cargo);
uint16 mail_capacity = 0;
uint amount = e->DetermineCapacity(v, &mail_capacity);
/* Restore the original cargo type */
v->cargo_type = temp_cid;
v->cargo_subtype = temp_subtype;
/* Skip on next refit. */
if (this->cargo != refit_it->cargo && refit_it->remaining > 0) {
this->capacities[refit_it->cargo] -= refit_it->remaining;
refit_it->remaining = 0;
} else if (amount < refit_it->remaining) {
this->capacities[refit_it->cargo] -= refit_it->remaining - amount;
refit_it->remaining = amount;
}
refit_it->capacity = amount;
refit_it->cargo = this->cargo;
++refit_it;
/* Special case for aircraft with mail. */
if (v->type == VEH_AIRCRAFT) {
if (mail_capacity < refit_it->remaining) {
this->capacities[refit_it->cargo] -= refit_it->remaining - mail_capacity;
refit_it->remaining = mail_capacity;
}
refit_it->capacity = mail_capacity;
break; // aircraft have only one vehicle
}
}
return any_refit;
}
/**
* Restore capacities and refit_capacities as vehicle might have been able to load now.
*/
void LinkRefresher::ResetRefit()
{
for (RefitList::iterator it(this->refit_capacities.begin()); it != this->refit_capacities.end(); ++it) {
if (it->remaining == it->capacity) continue;
this->capacities[it->cargo] += it->capacity - it->remaining;
it->remaining = it->capacity;
}
}
/**
* Predict the next order the vehicle will execute and resolve conditionals by
* recursion and return next non-conditional order in list.
* @param cur Current order being evaluated.
* @param next Next order to be evaluated.
* @param flags RefreshFlags to give hints about the previous link and state carried over from that.
* @param num_hops Number of hops already taken by recursive calls to this method.
* @return new next Order.
*/
const Order *LinkRefresher::PredictNextOrder(const Order *cur, const Order *next, uint8 flags, uint num_hops)
{
/* next is good if it's either NULL (then the caller will stop the
* evaluation) or if it's not conditional and the caller allows it to be
* chosen (by setting USE_NEXT). */
while (next != NULL && (!HasBit(flags, USE_NEXT) || next->IsType(OT_CONDITIONAL))) {
/* After the first step any further non-conditional order is good,
* regardless of previous USE_NEXT settings. The case of cur and next or
* their respective stations being equal is handled elsewhere. */
SetBit(flags, USE_NEXT);
if (next->IsType(OT_CONDITIONAL)) {
const Order *skip_to = this->vehicle->orders.list->GetNextDecisionNode(
this->vehicle->orders.list->GetOrderAt(next->GetConditionSkipToOrder()), num_hops);
if (skip_to != NULL && num_hops < this->vehicle->orders.list->GetNumOrders()) {
/* Make copies of capacity tracking lists. There is potential
* for optimization here: If the vehicle never refits we don't
* need to copy anything. Also, if we've seen the branched link
* before we don't need to branch at all. */
LinkRefresher branch(*this);
branch.RefreshLinks(cur, skip_to, flags, num_hops + 1);
}
}
/* Reassign next with the following stop. This can be a station or a
* depot.*/
next = this->vehicle->orders.list->GetNextDecisionNode(
this->vehicle->orders.list->GetNext(next), num_hops++);
}
return next;
}
/**
* Refresh link stats for the given pair of orders.
* @param cur Last stop where the consist could interact with cargo.
* @param next Next order to be processed.
*/
void LinkRefresher::RefreshStats(const Order *cur, const Order *next)
{
StationID next_station = next->GetDestination();
Station *st = Station::GetIfValid(cur->GetDestination());
if (st != NULL && next_station != INVALID_STATION && next_station != st->index) {
for (CapacitiesMap::const_iterator i = this->capacities.begin(); i != this->capacities.end(); ++i) {
/* Refresh the link and give it a minimum capacity. */
if (i->second == 0) continue;
CargoID c = i->first;
/* If not allowed to merge link graphs, make sure the stations are
* already in the same link graph. */
if (!this->allow_merge && st->goods[c].link_graph != Station::Get(next_station)->goods[c].link_graph) {
continue;
}
/* A link is at least partly restricted if a vehicle can't load at its source. */
EdgeUpdateMode restricted_mode = (cur->GetLoadType() & OLFB_NO_LOAD) == 0 ?
EUM_UNRESTRICTED : EUM_RESTRICTED;
/* If the vehicle is currently full loading, increase the capacities at the station
* where it is loading by an estimate of what it would have transported if it wasn't
* loading. Don't do that if the vehicle has been waiting for longer than the entire
* order list is supposed to take, though. If that is the case the total duration is
* probably far off and we'd greatly overestimate the capacity by increasing.*/
if (this->is_full_loading && this->vehicle->orders.list != NULL &&
st->index == vehicle->last_station_visited &&
this->vehicle->orders.list->GetTotalDuration() >
(Ticks)this->vehicle->current_order_time) {
uint effective_capacity = i->second * this->vehicle->load_unload_ticks;
if (effective_capacity > (uint)this->vehicle->orders.list->GetTotalDuration()) {
IncreaseStats(st, c, next_station, effective_capacity /
this->vehicle->orders.list->GetTotalDuration(), 0,
EUM_INCREASE | restricted_mode);
} else if (RandomRange(this->vehicle->orders.list->GetTotalDuration()) < effective_capacity) {
IncreaseStats(st, c, next_station, 1, 0, EUM_INCREASE | restricted_mode);
} else {
IncreaseStats(st, c, next_station, i->second, 0, EUM_REFRESH | restricted_mode);
}
} else {
IncreaseStats(st, c, next_station, i->second, 0, EUM_REFRESH | restricted_mode);
}
}
}
}
/**
* Iterate over orders starting at \a cur and \a next and refresh links
* associated with them. \a cur and \a next can be equal. If they're not they
* must be "neigbours" in their order list, which means \a next must be directly
* reachable from \a cur without passing any further OT_GOTO_STATION or
* OT_IMPLICIT orders in between.
* @param cur Current order being evaluated.
* @param next Next order to be checked.
* @param flags RefreshFlags to give hints about the previous link and state carried over from that.
* @param num_hops Number of hops already taken by recursive calls to this method.
*/
void LinkRefresher::RefreshLinks(const Order *cur, const Order *next, uint8 flags, uint num_hops)
{
while (next != NULL) {
if ((next->IsType(OT_GOTO_DEPOT) || next->IsType(OT_GOTO_STATION)) && next->IsRefit()) {
SetBit(flags, WAS_REFIT);
if (!next->IsAutoRefit()) {
this->HandleRefit(next->GetRefitCargo());
} else if (!HasBit(flags, IN_AUTOREFIT)) {
SetBit(flags, IN_AUTOREFIT);
LinkRefresher backup(*this);
for (CargoID c = 0; c != NUM_CARGO; ++c) {
if (CargoSpec::Get(c)->IsValid() && this->HandleRefit(c)) {
this->RefreshLinks(cur, next, flags, num_hops);
*this = backup;
}
}
}
}
/* Only reset the refit capacities if the "previous" next is a station,
* meaning that either the vehicle was refit at the previous station or
* it wasn't at all refit during the current hop. */
if (HasBit(flags, WAS_REFIT) && (next->IsType(OT_GOTO_STATION) || next->IsType(OT_IMPLICIT))) {
SetBit(flags, RESET_REFIT);
} else {
ClrBit(flags, RESET_REFIT);
}
next = this->PredictNextOrder(cur, next, flags, num_hops);
if (next == NULL) break;
Hop hop(cur->index, next->index, this->cargo);
if (this->seen_hops->find(hop) != this->seen_hops->end()) {
break;
} else {
this->seen_hops->insert(hop);
}
/* Don't use the same order again, but choose a new one in the next round. */
ClrBit(flags, USE_NEXT);
/* Skip resetting and link refreshing if next order won't do anything with cargo. */
if (!next->IsType(OT_GOTO_STATION) && !next->IsType(OT_IMPLICIT)) continue;
if (HasBit(flags, RESET_REFIT)) {
this->ResetRefit();
ClrBit(flags, RESET_REFIT);
ClrBit(flags, WAS_REFIT);
}
if (cur->IsType(OT_GOTO_STATION) || cur->IsType(OT_IMPLICIT)) {
if (cur->CanLeaveWithCargo(HasBit(flags, HAS_CARGO))) {
SetBit(flags, HAS_CARGO);
this->RefreshStats(cur, next);
} else {
ClrBit(flags, HAS_CARGO);
}
}
/* "cur" is only assigned here if the stop is a station so that
* whenever stats are to be increased two stations can be found. */
cur = next;
}
}

104
src/linkgraph/refresh.h Normal file
View File

@@ -0,0 +1,104 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file refresh.h Declaration of link refreshing utility. */
#ifndef REFRESH_H
#define REFRESH_H
#include "../cargo_type.h"
#include "../vehicle_base.h"
#include <list>
#include <map>
#include <set>
/**
* Utility to refresh links a consist will visit.
*/
class LinkRefresher {
public:
static void Run(Vehicle *v, bool allow_merge = true, bool is_full_loading = false);
protected:
/**
* Various flags about properties of the last examined link that might have
* an influence on the next one.
*/
enum RefreshFlags {
USE_NEXT, ///< There was a conditional jump. Try to use the given next order when looking for a new one.
HAS_CARGO, ///< Consist could leave the last stop where it could interact with cargo carrying cargo (i.e. not an "unload all" + "no loading" order).
WAS_REFIT, ///< Consist was refit since the last stop where it could interact with cargo.
RESET_REFIT, ///< Consist had a chance to load since the last refit and the refit capacities can be reset.
IN_AUTOREFIT, ///< Currently doing an autorefit loop. Ignore the first autorefit order.
};
/**
* Simulated cargo type and capacity for prediction of future links.
*/
struct RefitDesc {
CargoID cargo; ///< Cargo type the vehicle will be carrying.
uint16 capacity; ///< Capacity the vehicle will have.
uint16 remaining; ///< Capacity remaining from before the previous refit.
RefitDesc(CargoID cargo, uint16 capacity, uint16 remaining) :
cargo(cargo), capacity(capacity), remaining(remaining) {}
};
/**
* A hop the refresh algorithm might evaluate. If the same hop is seen again
* the evaluation is stopped. This of course is a fairly simple heuristic.
* Sequences of refit orders can produce vehicles with all kinds of
* different cargoes and remembering only one can lead to early termination
* of the algorithm. However, as the order language is Turing complete, we
* are facing the halting problem here. At some point we have to draw the
* line.
*/
struct Hop {
OrderID from; ///< Last order where vehicle could interact with cargo or absolute first order.
OrderID to; ///< Next order to be processed.
CargoID cargo; ///< Cargo the consist is probably carrying or CT_INVALID if unknown.
/**
* Default constructor should not be called but has to be visible for
* usage in std::set.
*/
Hop() {NOT_REACHED();}
/**
* Real constructor, only use this one.
* @param from First order of the hop.
* @param to Second order of the hop.
* @param cargo Cargo the consist is probably carrying when passing the hop.
*/
Hop(OrderID from, OrderID to, CargoID cargo) : from(from), to(to), cargo(cargo) {}
bool operator<(const Hop &other) const;
};
typedef std::list<RefitDesc> RefitList;
typedef std::map<CargoID, uint> CapacitiesMap;
typedef std::set<Hop> HopSet;
Vehicle *vehicle; ///< Vehicle for which the links should be refreshed.
CapacitiesMap capacities; ///< Current added capacities per cargo ID in the consist.
RefitList refit_capacities; ///< Current state of capacity remaining from previous refits versus overall capacity per vehicle in the consist.
HopSet *seen_hops; ///< Hops already seen. If the same hop is seen twice we stop the algorithm. This is shared between all Refreshers of the same run.
CargoID cargo; ///< Cargo given in last refit order.
bool allow_merge; ///< If the refresher is allowed to merge or extend link graphs.
bool is_full_loading; ///< If the vehicle is full loading.
LinkRefresher(Vehicle *v, HopSet *seen_hops, bool allow_merge, bool is_full_loading);
bool HandleRefit(CargoID refit_cargo);
void ResetRefit();
void RefreshStats(const Order *cur, const Order *next);
const Order *PredictNextOrder(const Order *cur, const Order *next, uint8 flags, uint num_hops = 0);
void RefreshLinks(const Order *cur, const Order *next, uint8 flags, uint num_hops = 0);
};
#endif /* REFRESH_H */