/** * 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; } }
static bool ShipTrackFollower(TileIndex tile, TrackPathFinder *pfs, uint length) { /* Found dest? */ if (tile == pfs->dest_coords) { pfs->best_bird_dist = 0; pfs->best_length = minu(pfs->best_length, length); return true; } /* Skip this tile in the calculation */ if (tile != pfs->skiptile) { pfs->best_bird_dist = minu(pfs->best_bird_dist, DistanceMaxPlusManhattan(pfs->dest_coords, tile)); } return false; }
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, nullptr); for (NodeID node = 0; node < size; ++node) { Tannotation *anno = new Tannotation(node, node == source_node); anno->UpdateAnnotation(); 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); dest->UpdateAnnotation(); annos.insert(dest); } } } }