Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
CCHMetricFamily.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
18// A family of per-vehicle-type CCH metrics over one shared CCHGraph -- the
19// MetricProvider machinery behind CCHRouter, shared between the simulation
20// and duarouter the same way CCHGraph shares the topology.
21//
22// Keying by the vehicle TYPE (rather than the class) makes everything the
23// effort function reads from the vehicle exact per metric: the type's
24// maximum speed, per-class edge speed restrictions, routing preferences,
25// the bicycle speed table and one frozen weights.random-factor realization;
26// only individual speed-factor draws within one type share a metric (the
27// same approximation the CH family makes with its per-class prototype
28// vehicles, but at finer granularity).
29//
30// Two modes:
31//
32// STATIC -- weights never change while a (type, weight period) pair is in
33// use. Metrics are customized lazily on the first query of a pair under a
34// mutex and cached (duarouter, marouter and the simulation's free-flow
35// routers). When the efforts do change between queries -- a runtime
36// permission flip, marouter's travel times after an assignment iteration
37// -- the host flags it (flagStale) and every cached metric is refilled
38// and re-customized on the next query.
39//
40// LIVE -- weights track an adaptive speed table (the rerouting device).
41// Metrics are double-buffered: worker threads acquire-load the published
42// front while the owner refills and customizes the back at its barrier
43// and flips, so queries never observe a metric mid-customization. The
44// customize is sparse (RoutingKit partial customization over the edges
45// whose effort actually moved, behind a configurable deadband) with
46// wholesale refills after permission flips. Staleness is bounded to one
47// barrier -- CH's weightPeriod semantics: once any metric exists,
48// customize(now) must run at every barrier where an effort moved (see
49// atBarrier), and an epoch pair guards the bound as an invariant.
50//
51// The family is intentionally ignorant of WHEN to customize -- only the
52// owner knows when no query is in flight (the simulation's adaptation
53// barrier, duarouter's mutex) -- and of HOW reference vehicles are built
54// (injected factory; V construction is host-specific).
55/****************************************************************************/
56#pragma once
57#include <config.h>
58
59#include <algorithm>
60#include <atomic>
61#include <cmath>
62#include <limits>
63#include <map>
64#include <memory>
65#include <mutex>
66#include <utility>
67#include <vector>
71
72
73// ===========================================================================
74// class definitions
75// ===========================================================================
85template<class E, class V, class K>
87public:
99 typedef V* (*RefVehicleFactory)(const K*, int);
102 typedef void (*WeightPatch)(const Graph*, const V*, std::vector<unsigned>&);
103
114 SUMOTime begin, SUMOTime weightPeriod,
115 RefVehicleFactory factory, WeightPatch patch) :
116 myGraph(graph), myFillEffort(fillEffort), myGateEffort(nullptr),
117 myFactory(factory), myPatch(patch), myLive(false),
118 myBegin(begin), myWeightPeriod(weightPeriod),
120 }
121
154 EffortOperation gateEffort, double updateFactor,
155 double updateConstant, RefVehicleFactory factory,
156 int ensembleK = 1) :
157 myGraph(graph), myFillEffort(fillEffort), myGateEffort(gateEffort),
158 myFactory(factory), myPatch(nullptr), myLive(true),
160 myUpdateFactor(updateFactor), myUpdateConstant(updateConstant),
161 myEnsembleK(ensembleK > 1 ? ensembleK : 1) {
162 const unsigned space = myGraph->edgeIdSpace();
163 // NaN = "never applied": the first pending occurrence always
164 // passes the deadband and primes the entry
165 myAppliedEffort.assign(space, std::numeric_limits<double>::quiet_NaN());
166 myPendingFlag.assign(space, 0);
167 }
168
170 for (LiveMetric* c : myLiveMetrics) {
171 delete c->refVehicle;
172 delete c;
173 }
174 for (auto& item : myStaticMetrics) {
175 delete item.second.refVehicle;
176 }
177 }
178
181
191 MetricPtr get(const K* key, SUMOVehicleClass vClass, SUMOTime time, const V* veh) {
192 std::lock_guard<std::mutex> lock(myStaticLock);
193 if (myStale) {
194 for (auto& item : myStaticMetrics) {
195 StaticMetric& sm = item.second;
196 fillStatic(sm, item.first.second, time, sm.refVehicle != nullptr ? sm.refVehicle : veh, veh);
197 sm.metric->customize();
198 }
199 myStale = false;
200 }
201 const int period = periodOf(time);
202 const std::pair<const K*, int> mapKey(key, period);
203 auto it = myStaticMetrics.find(mapKey);
204 if (it == myStaticMetrics.end()) {
205 StaticMetric& sm = myStaticMetrics[mapKey];
206 sm.vClass = vClass;
207 const V* ref = veh;
208 if (myFactory != nullptr) {
209 sm.refVehicle = myFactory(key, 0);
210 ref = sm.refVehicle;
211 }
212 fillStatic(sm, period, time, ref, veh);
214 myGraph->cch(), sm.weights));
215 sm.metric->customize();
216 return sm.metric.get();
217 }
218 return it->second.metric.get();
219 }
220
225 return SUMOTime_MAX;
226 }
227 return myBegin + (periodOf(time) + 1) * myWeightPeriod;
228 }
229
235 void flagStale() {
236 std::lock_guard<std::mutex> lock(myStaticLock);
237 myStale = true;
238 }
239
242 flagStale();
243 }
245
248
252 void seedKey(const K* key) {
253 if (myByKey.count(key) == 0) {
254 std::vector<LiveMetric*>& slots = myByKey[key];
255 for (int k = 0; k < myEnsembleK; k++) {
256 slots.push_back(buildLiveMetric(key, k));
257 }
258 }
259 }
260
263 void markDirty(const E* e) {
264 const int id = e->getNumericalID();
265 if (id < 0 || id >= (int)myPendingFlag.size()) {
266 return;
267 }
268 if (!myPendingFlag[id]) {
269 myPendingFlag[id] = 1;
270 myPendingList.push_back(e);
271 }
272 }
273
279 void invalidateEdge(const E* e) {
280 const int id = e->getNumericalID();
281 if (id < 0 || id >= (int)myPendingFlag.size()) {
282 return;
283 }
284 markDirty(e);
285 // NaN sentinel: the deadband always passes, so the flip reaches the
286 // metric at the next barrier even though the speed table did not move
287 myAppliedEffort[id] = std::numeric_limits<double>::quiet_NaN();
289 myMetricStale.store(true, std::memory_order_release);
290 }
291
301 void atBarrier(double now, bool effortsMoved) {
302 if (effortsMoved) {
303 mySpeedEpoch.fetch_add(1, std::memory_order_release);
304 }
305 if (myQueried.exchange(false, std::memory_order_acq_rel)
306 || myMetricStale.load(std::memory_order_acquire)
307 || (effortsMoved && !myLiveMetrics.empty())) {
308 customize(now);
309 }
310 }
311
316 void customize(double now) {
317 // types that queried since the last barrier without a metric
318 // (streamed route files, TraCI-added types): create their state now
319 {
320 std::lock_guard<std::mutex> lock(myWantedLock);
321 for (const K* key : myWantedKeys) {
322 seedKey(key);
323 }
324 myWantedKeys.clear();
325 }
326 if (myLiveMetrics.empty()) {
327 return;
328 }
329 // Permission flip pending: run a FULL refill on this pass's back
330 // buffer, the next pass copies it to the other buffer
331 // (myFullFillsPending counts both down). The fills below re-prime the
332 // graph's connection masks, which the flip's caller invalidated.
333 const int fullFill = myFullFillsPending;
334 if (myFullFillsPending > 0) {
336 }
337 // Deadband pass, once, on the type-shared gate effort: accept edges
338 // whose effort moved by more than BOTH bounds since they were last
339 // applied; rejected edges stay pending (see the constructor doc)
340 std::vector<const E*> accepted;
341 if (fullFill < 2) {
342 std::vector<const E*> stillPending;
343 for (const E* e : myPendingList) {
344 const int id = e->getNumericalID();
345 const double effNow = myGateEffort(e, nullptr, now);
346 const double effApplied = myAppliedEffort[id];
347 bool pass = true;
348 // without a threshold every marked edge passes: the gate effort may be clamped
349 // at the minimum travel time while a type with speedFactor > 1 still sees changes
350 if ((myUpdateFactor > 1. || myUpdateConstant > 0.) && !std::isnan(effApplied) && effApplied > 0. && effNow > 0.) {
351 const double hi = MAX2(effNow, effApplied);
352 const double lo = MIN2(effNow, effApplied);
353 pass = (hi / lo > myUpdateFactor) && (hi - lo > myUpdateConstant);
354 }
355 if (pass) {
356 accepted.push_back(e);
357 myAppliedEffort[id] = effNow;
358 myPendingFlag[id] = 0;
359 } else {
360 stillPending.push_back(e);
361 }
362 }
363 myPendingList.swap(stillPending);
364 }
365 for (LiveMetric* c : myLiveMetrics) {
366 const int back = 1 - c->frontIndex;
367 const std::vector<unsigned>& front = c->weight[c->frontIndex];
368 // Fill from the live efforts, masking arcs the class is not
369 // permitted on -- this is where per-class permissions AND active
370 // closures become inf_weight. The fill reference is the type's
371 // OWNED refVehicle, so the effort captures the type's extras
372 // exactly (including each folded via edge inside viaChainEffort)
373 // and freezes ONE random realization per metric -- the same
374 // approximation CHRouterWrapper makes per hierarchy; exact
375 // per-vehicle randomization remains the domain of the exact
376 // routers.
377 if (c->metric[back] == nullptr || fullFill > 0) {
378 if (fullFill == 2 || c->metric[c->frontIndex] == nullptr) {
379 // first use of THIS type (streamed/late vType) or a
380 // permission flip: wholesale refill from the re-primed
381 // masks and live permissions. Only the flip invalidates
382 // the type-shared deadband state below.
383 myGraph->fillInputWeights(myFillEffort, c->vClass, c->refVehicle, now, c->weight[back]);
384 } else {
385 // second buffer of this type, or the second pass after a
386 // flip: copy the front, then apply the accepted edges
387 std::copy(front.begin(), front.end(), c->weight[back].begin());
388 for (const E* e : accepted) {
389 for (const unsigned a : myGraph->arcsOfEdge(e)) {
390 c->weight[back][a] = myGraph->computeArcWeight(a, myFillEffort, c->vClass, c->refVehicle, now);
391 }
392 }
393 }
394 if (c->metric[back] == nullptr) {
395 c->metric[back] = std::make_shared<RoutingKit::CustomizableContractionHierarchyMetric>(
396 myGraph->cch(), c->weight[back]);
397 }
398 c->metric[back]->customize(); // serial; avoids OpenMP oversubscription under FOX
399 } else {
400 // SPARSE PATH: metric[back] is the customization of the
401 // current contents of weight[back]. Bring the back up to the
402 // front (the arcs the last pass changed there), recompute only
403 // the arcs of accepted edges (edge->arc reverse image), write
404 // the ones that moved, and propagate through affected
405 // triangles only. Cost scales with traffic transitions, not
406 // with the network.
407 std::vector<unsigned>& applied = c->weight[back];
408 c->partial->reset(myGraph->cch());
409 unsigned changed = 0;
410 for (const E* e : myLastAccepted) {
411 for (const unsigned a : myGraph->arcsOfEdge(e)) {
412 if (applied[a] != front[a]) {
413 applied[a] = front[a];
414 c->partial->update_arc(a);
415 ++changed;
416 }
417 }
418 }
419 for (const E* e : accepted) {
420 for (const unsigned a : myGraph->arcsOfEdge(e)) {
421 const unsigned newW = myGraph->computeArcWeight(a, myFillEffort, c->vClass, c->refVehicle, now);
422 if (newW != applied[a]) {
423 applied[a] = newW;
424 c->partial->update_arc(a); // takes INPUT arc ids
425 ++changed;
426 }
427 }
428 }
429 if (changed > 0) {
430 if (changed * 64 > applied.size()) {
431 // the partial customization costs much more per changed arc than the full one per arc,
432 // so above about 1/64 of the arcs the full one is faster (and gives the same metric)
433 c->metric[back]->customize();
434 } else {
435 c->partial->customize(*c->metric[back]);
436 }
437 }
438 }
439 // publish lock-free: release store at the barrier, worker
440 // acquire-loads on the hot path
441 c->frontIndex = back;
442 c->front.store(c->metric[back].get(), std::memory_order_release);
443 }
444 if (fullFill == 2) {
445 // a permission flip's full refill matched every arc to the live
446 // efforts/masks: type-shared deadband entries are stale; NaN
447 // re-arms the first-change auto-pass. (A plain per-type first
448 // fill above does NOT reach here -- it must not wipe deadband
449 // state that other, already-running types still depend on.)
450 for (const E* e : myPendingList) {
451 myPendingFlag[e->getNumericalID()] = 0;
452 }
453 myPendingList.clear();
454 myAppliedEffort.assign(myAppliedEffort.size(), std::numeric_limits<double>::quiet_NaN());
455 }
456 myLastAccepted.swap(accepted);
457 // every published front now reflects the live permissions (the other
458 // buffer, if still pending a full refill, is repaired before its
459 // next publish) and the live efforts as of this barrier (both epoch
460 // counters only move on the owner's thread at the barrier, so the
461 // pair cannot tear)
462 myMetricStale.store(false, std::memory_order_release);
463 myPublishedEpoch.store(mySpeedEpoch.load(std::memory_order_relaxed),
464 std::memory_order_release);
465 }
466
471 MetricPtr published(const K* key, const std::string& vehID) {
472 const auto it = myByKey.find(key);
473 if (it == myByKey.end()) {
474 // streamed-in type without a metric yet: register it for
475 // creation at the next barrier and fall back
476 {
477 std::lock_guard<std::mutex> lock(myWantedLock);
478 myWantedKeys.push_back(key);
479 }
480 myQueried.store(true, std::memory_order_relaxed);
481 return nullptr;
482 }
483 // mark the metric as consumed. Test before set: an unconditional
484 // store from every query would ping-pong the cache line between
485 // cores; the read is shared and the store fires once per barrier
486 // window (relaxed: only gates bootstrap/re-arm, never visibility)
487 if (!myQueried.load(std::memory_order_relaxed)) {
488 myQueried.store(true, std::memory_order_relaxed);
489 }
490 // A permission flip that is not yet customized in makes every
491 // published metric unusable: it may still route THROUGH a
492 // just-closed edge, and CCHRouter's coverage test consults the LIVE
493 // permissions, which would wrongly bless the query. Route exactly
494 // until the barrier repairs the metrics.
495 if (myMetricStale.load(std::memory_order_acquire)) {
496 return nullptr;
497 }
498 // Safety invariant: never serve a metric the efforts have moved away
499 // from -- the exact routers see live weights and CH is bounded to
500 // one weightPeriod. atBarrier re-customizes at every moved barrier
501 // once metrics exist, so this cannot fire in steady state; if a gap
502 // ever opens, this burst routes on the exact fallback (live weights)
503 // and the queried flag set above arms the customize that closes it.
504 if (myPublishedEpoch.load(std::memory_order_acquire)
505 != mySpeedEpoch.load(std::memory_order_acquire)) {
506 return nullptr;
507 }
508 // stable slot assignment: FNV-1a over the id (std::hash is
509 // implementation-defined and would break cross-platform test
510 // reproducibility)
511 const LiveMetric* c = myEnsembleK == 1 ? it->second.front()
512 : it->second[fnv1a(vehID) % myEnsembleK];
513 return c->front.load(std::memory_order_acquire);
514 }
515
517 bool empty() const {
518 return myLiveMetrics.empty();
519 }
520
523 bool hasDeadband() const {
524 return myUpdateFactor > 1. || myUpdateConstant > 0.;
525 }
526
528 double getAppliedEffort(const E* e) const {
529 return myAppliedEffort[e->getNumericalID()];
530 }
531
533 bool isPending(const E* e) const {
534 return myPendingFlag[e->getNumericalID()] != 0;
535 }
536
538 void loadEdge(const E* e, double appliedEffort, bool pending) {
539 myAppliedEffort[e->getNumericalID()] = appliedEffort;
540 if (pending) {
541 markDirty(e);
542 }
543 }
544
546 std::vector<const K*> getKeys() const {
547 std::vector<const K*> keys;
548 for (const auto& item : myByKey) {
549 keys.push_back(item.first);
550 }
551 return keys;
552 }
553
555 int getEnsembleSize() const {
556 return myEnsembleK;
557 }
558
561 void getLaggingArcs(const K* key, int slot, double now, std::vector<int>& arcs, std::vector<int>& weights) const {
562 const LiveMetric* c = myByKey.find(key)->second[slot];
563 if (c->metric[c->frontIndex] == nullptr) {
564 return;
565 }
566 std::vector<unsigned> fresh;
568 const std::vector<unsigned>& front = c->weight[c->frontIndex];
569 for (unsigned a = 0; a < (unsigned)fresh.size(); a++) {
570 if (front[a] != fresh[a]) {
571 arcs.push_back((int)a);
572 weights.push_back((int)front[a]);
573 }
574 }
575 }
576
580 void loadMetric(const K* key, int slot, double now, const std::vector<int>& arcs, const std::vector<int>& weights) {
581 seedKey(key);
582 if (slot < 0 || slot >= myEnsembleK) {
583 return;
584 }
585 LiveMetric* c = myByKey[key][slot];
587 for (int i = 0; i < (int)arcs.size() && i < (int)weights.size(); i++) {
588 if (arcs[i] >= 0 && arcs[i] < (int)c->weight[0].size()) {
589 c->weight[0][arcs[i]] = (unsigned)weights[i];
590 }
591 }
592 c->metric[0] = std::make_shared<RoutingKit::CustomizableContractionHierarchyMetric>(myGraph->cch(), c->weight[0]);
593 c->metric[0]->customize();
594 c->metric[1].reset();
595 c->frontIndex = 0;
596 c->front.store(c->metric[0].get(), std::memory_order_release);
597 }
599
600private:
603 struct LiveMetric {
604 std::vector<unsigned> weight[2]; // ping-pong input-weight buffers, live whole run
605 std::shared_ptr<RoutingKit::CustomizableContractionHierarchyMetric> metric[2];
606 std::atomic<const RoutingKit::CustomizableContractionHierarchyMetric*> front{nullptr};
607 int frontIndex = 1; // first customize uses back = 0
610 V* refVehicle = nullptr;
613 std::shared_ptr<RoutingKit::CustomizableContractionHierarchyPartialCustomization> partial;
614 };
615
620 std::vector<unsigned> weights;
621 std::unique_ptr<RoutingKit::CustomizableContractionHierarchyMetric> metric;
625 V* refVehicle = nullptr;
627 bool filled = false;
630 std::vector<std::pair<unsigned, unsigned> > patched;
631 };
632
635 int periodOf(SUMOTime time) const {
636 if (myWeightPeriod > 0 && myWeightPeriod != SUMOTime_MAX && time > myBegin) {
637 return (int)((time - myBegin) / myWeightPeriod);
638 }
639 return 0;
640 }
641
651 void fillStatic(StaticMetric& sm, int period, SUMOTime time, const V* ref, const V* veh) {
652 const double fillTime = myWeightPeriod == SUMOTime_MAX
653 ? STEPS2TIME(time) : STEPS2TIME(myBegin + period * myWeightPeriod);
654 myGraph->fillInputWeights(myFillEffort, sm.vClass, ref, fillTime, sm.weights);
655 if (!sm.filled) {
656 sm.filled = true;
657 if (myPatch != nullptr) {
658 const std::vector<unsigned> unpatched(sm.weights);
659 myPatch(myGraph, veh, sm.weights);
660 for (unsigned a = 0; a < (unsigned)sm.weights.size(); a++) {
661 if (sm.weights[a] != unpatched[a]) {
662 sm.patched.emplace_back(a, sm.weights[a]);
663 }
664 }
665 }
666 } else {
667 for (const auto& p : sm.patched) {
668 sm.weights[p.first] = p.second;
669 }
670 }
671 }
672
674 static uint64_t fnv1a(const std::string& s) {
675 uint64_t h = 1469598103934665603ull;
676 for (const char ch : s) {
677 h = (h ^ (unsigned char)ch) * 1099511628211ull;
678 }
679 return h;
680 }
681
684 LiveMetric* buildLiveMetric(const K* key, int slot) {
685 LiveMetric* c = new LiveMetric();
686 c->refVehicle = myFactory(key, slot);
687 c->vClass = c->refVehicle->getVClass();
688 c->weight[0].resize(myGraph->arcCount());
689 c->weight[1].resize(myGraph->arcCount());
690 c->partial = std::make_shared<RoutingKit::CustomizableContractionHierarchyPartialCustomization>(myGraph->cch());
691 myLiveMetrics.push_back(c);
692 return c;
693 }
694
706 const bool myLive;
707
712 std::map<std::pair<const K*, int>, StaticMetric> myStaticMetrics;
713 std::mutex myStaticLock;
716 bool myStale = false;
718
725 int myEnsembleK = 1;
728 std::vector<LiveMetric*> myLiveMetrics;
729 std::map<const K*, std::vector<LiveMetric*> > myByKey;
733 std::vector<const K*> myWantedKeys;
734 std::mutex myWantedLock;
737 std::atomic<bool> myQueried{false};
740 std::atomic<bool> myMetricStale{false};
746 std::atomic<uint64_t> mySpeedEpoch{0};
747 std::atomic<uint64_t> myPublishedEpoch{0};
757 std::vector<double> myAppliedEffort;
758 std::vector<char> myPendingFlag;
759 std::vector<const E*> myPendingList;
760 std::vector<const E*> myLastAccepted;
762
763private:
766};
long long int SUMOTime
Definition GUI.h:36
#define STEPS2TIME(x)
Definition SUMOTime.h:58
#define SUMOTime_MAX
Definition SUMOTime.h:34
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
@ SVC_PASSENGER
vehicle is a passenger car (a "normal" car)
T MIN2(T a, T b)
Definition StdDefs.h:80
T MAX2(T a, T b)
Definition StdDefs.h:86
Metric-independent RoutingKit CCH topology over the PURE road graph.
Definition CCHGraph.h:97
unsigned edgeIdSpace() const
size of the edge numerical-id space the graph was built over (for callers keeping per-edge-id side ar...
Definition CCHGraph.h:245
unsigned arcCount() const
number of input arcs (== number of mapped connections)
Definition CCHGraph.h:239
double(* EffortOperation)(const E *const, const V *const, double)
effort callback signature, matching SUMOAbstractRouter::Operation
Definition CCHGraph.h:100
void fillInputWeights(EffortOperation effort, SUMOVehicleClass maskClass, const V *veh, double time, std::vector< unsigned > &weight) const
Fill a centisecond input-weight buffer for one vehicle class.
Definition CCHGraph.h:347
const RoutingKit::CustomizableContractionHierarchy & cch() const
the immutable CCH (share const& across clones)
Definition CCHGraph.h:234
const std::vector< unsigned > & arcsOfEdge(const E *e) const
the arcs whose weight depends on the given edge (arcs it heads plus arcs whose folded via chain conta...
Definition CCHGraph.h:281
unsigned computeArcWeight(unsigned a, EffortOperation effort, SUMOVehicleClass maskClass, const V *veh, double time) const
Recompute the input weight of one arc – the exact per-arc body of fillInputWeights (same masking,...
Definition CCHGraph.h:326
per-vehicle-type CCH metric store over a shared CCHGraph
std::map< const K *, std::vector< LiveMetric * > > myByKey
bool myStale
the efforts changed behind the cached metrics: refill and re-customize them all on the next get() (se...
MetricPtr get(const K *key, SUMOVehicleClass vClass, SUMOTime time, const V *veh)
the metric for (type key, period of time), built on the first query of the pair; nullptr before init....
void seedKey(const K *key)
allocate the metric state (all ensemble slots) for one type; call only while no query is in flight (s...
const Graph * myGraph
the immutable shared topology
EffortOperation myFillEffort
the effort the metrics are filled from
std::map< std::pair< const K *, int >, StaticMetric > myStaticMetrics
int periodOf(SUMOTime time) const
the weight period containing time: 0 is everything before begin and the whole run when the weights ar...
bool hasDeadband() const
whether the deadband can keep the metrics behind the efforts (then its state and the lagging arcs bel...
WeightPatch myPatch
post-fill weight hook (STATIC)
std::mutex myWantedLock
void loadMetric(const K *key, int slot, double now, const std::vector< int > &arcs, const std::vector< int > &weights)
publish the metric of a type and ensemble slot as a fresh fill at now with the given arcs overwritten...
const bool myLive
LIVE or STATIC (fixed at construction)
std::vector< LiveMetric * > myLiveMetrics
every type's state, in creation order (barrier iteration) and by key (query lookup; only mutated whil...
std::vector< const E * > myPendingList
void atBarrier(double now, bool effortsMoved)
the owner's customization barrier: bump the speed epoch when an effort moved and re-customize when it...
int myFullFillsPending
how many customize passes must replace a whole buffer after a permission flip: 2 = refill one,...
std::vector< double > myAppliedEffort
edge state for the sparse path, shared by both buffers: the gate effort each edge's arcs were last fi...
std::atomic< bool > myQueried
whether a query arrived since the last barrier (bootstraps type creation and re-arms after resets – s...
RefVehicleFactory myFactory
reference-vehicle factory (required LIVE, optional STATIC)
std::atomic< uint64_t > myPublishedEpoch
void customize(double now)
refill + customize + publish every type's metric from the live efforts and permissions....
void getLaggingArcs(const K *key, int slot, double now, std::vector< int > &arcs, std::vector< int > &weights) const
the arcs of the published metric of a type and ensemble slot whose weight differs from a fresh fill a...
std::vector< const K * > myWantedKeys
types that queried but have no metric yet (demand streams, so types can appear after seeding); querie...
V *(* RefVehicleFactory)(const K *, int)
builds an OWNED effort-reference vehicle for a type: never registered, counted or inserted – it exist...
CCHMetricFamily(const Graph *graph, EffortOperation fillEffort, SUMOTime begin, SUMOTime weightPeriod, RefVehicleFactory factory, WeightPatch patch)
construct a STATIC family (lazy build per (type, period))
std::vector< const E * > myLastAccepted
void fillStatic(StaticMetric &sm, int period, SUMOTime time, const V *ref, const V *veh)
(re)fill a STATIC metric's input weights from the live efforts. A weight-period grid evaluates at the...
CCHMetricFamily & operator=(const CCHMetricFamily &)=delete
bool empty() const
whether any type has live metric state yet
int getEnsembleSize() const
the number of metrics per type
void flagStale()
the efforts behind the cached metrics changed – a runtime permission change in the simulation (the ca...
const RoutingKit::CustomizableContractionHierarchyMetric * MetricPtr
std::vector< char > myPendingFlag
void invalidateEdge(const E *e)
a runtime permission change (closure / re-opening) hit this edge: queue it bypassing the deadband and...
std::atomic< uint64_t > mySpeedEpoch
barrier counter of the tracked efforts and the counter value the published metrics were customized fr...
CCHMetricFamily(const Graph *graph, EffortOperation fillEffort, EffortOperation gateEffort, double updateFactor, double updateConstant, RefVehicleFactory factory, int ensembleK=1)
construct a LIVE family (double-buffered, barrier-customized)
double getAppliedEffort(const E *e) const
the gate effort the edge was last applied with (NaN = never)
std::vector< const K * > getKeys() const
the type keys with metric state
std::atomic< bool > myMetricStale
a permission flip has not yet been customized into the published metrics (queries divert to the exact...
bool isPending(const E *e) const
whether the edge waits for the deadband
CCHGraph< E, V > Graph
SUMOTime periodEnd(SUMOTime time) const
the end of the weight period containing the given time (SUMOTime_MAX when the weights are static); CC...
int myEnsembleK
frozen random-factor realizations per type (see the LIVE ctor)
EffortOperation myGateEffort
the type-independent effort of the LIVE deadband
Graph::EffortOperation EffortOperation
void loadEdge(const E *e, double appliedEffort, bool pending)
restore the deadband state of an edge (state loading)
MetricPtr published(const K *key, const std::string &vehID)
the published metric for a type key and querying vehicle id (the id picks the ensemble slot – see the...
void flagPermissionsStale()
flagStale() under the simulation's name for the permission case
static uint64_t fnv1a(const std::string &s)
stable 64-bit FNV-1a for the ensemble slot assignment
void markDirty(const E *e)
queue an edge whose gate effort changed (the deadband applies later, at the customize barrier)
void(* WeightPatch)(const Graph *, const V *, std::vector< unsigned > &)
post-fill hook on the input weights (STATIC mode), e.g. duarouter masking the edges that restrict a t...
LiveMetric * buildLiveMetric(const K *key, int slot)
allocate LIVE metric state for one (type, ensemble slot) (owner's thread only)
std::mutex myStaticLock
CCHMetricFamily(const CCHMetricFamily &)=delete
const Graph * graph
per-type LIVE metric state. Heap-owned: the atomic makes it non-movable, so it cannot live in a map b...
V * refVehicle
OWNED effort-reference vehicle (see RefVehicleFactory)
std::vector< unsigned > weight[2]
std::atomic< const RoutingKit::CustomizableContractionHierarchyMetric * > front
std::shared_ptr< RoutingKit::CustomizableContractionHierarchyPartialCustomization > partial
partial-customization worker (queue over the shared CCH); one per metric – its queue is drained by ev...
std::shared_ptr< RoutingKit::CustomizableContractionHierarchyMetric > metric[2]
per-(type, period) STATIC metric state. RoutingKit metrics BORROW their input-weight buffer,...
std::vector< unsigned > weights
V * refVehicle
OWNED reference vehicle (nullptr when filling with the querying vehicle)
std::vector< std::pair< unsigned, unsigned > > patched
the (arc, weight) pairs the patch wrote on the first fill, re-applied on every refill
bool filled
whether the first fill (which runs the patch) is done
std::unique_ptr< RoutingKit::CustomizableContractionHierarchyMetric > metric