OrderScorer#

class causalbase.order.OrderScorer(score, max_steps=1000, incremental=True, memo_slots=8, exact_cap=1024, n_restarts=0, cache_mb=None)[source]#

Hill climbing restricted to an order, with exact incremental replay.

Parent sets are bitmasks internally, and local scores are cached here per node and parent mask, so each family is scored once: by the compiled engine of a BDeuScore (which does not cache them), by the score itself otherwise.

Parameters:
scoreFamilyScore

The score, kept as family_score; a BDeuScore scores the families with its compiled engine. Any other object with n_vars and local_score(node, parents) (and, optionally, the batch local_scores_add(node, parents, candidates)) is used as it is.

max_stepsint, default=1000

Maximum number of moves per node (1000 in the Java implementation).

incrementalbool, default=True

Replay the stored greedy path when a node’s predecessors change, instead of climbing again from the empty parent set.

memo_slotsint, default=8

Greedy paths kept per node. A new predecessor set is replayed from the stored one closest to it (fewest predecessors added or removed), which matters when rollouts alternate between several base orders.

exact_capint, default=1024

Answers kept per node for predecessor sets met before, oldest dropped first; 0 keeps none. A node often meets a set again after it has left the slots (same base order, prefix variables already in front).

n_restartsint, default=0

Extra greedy climbs per node. Climb i + 1 runs on the predecessors of climb i minus the first parent climb i added (its best single parent), and the node keeps the best of all climbs (the earliest on ties). This gets past a parent that wins alone but blocks better sets, such as a variable with many states whose CPT leaves no room for more parents. Each climb is an ordinary greedy climb on a smaller predecessor set, so it reuses the memos and replays, and breaks() and path_moves() stay exact.

cache_mbfloat or None, default=None

Memory budget of the caches, in MiB; None leaves them unbounded. When a query leaves them above it, every cache drops its oldest quarter (least recently used, for the neighbour vectors) until they fit. Answers do not change, since dropped entries are recomputed when needed; a budget far below what a search visits only costs time.

Attributes:
cache_nbytesint

Estimated bytes held by the caches the budget covers: family scores, neighbour vectors, final-state verdicts and stored answers. The greedy paths in the slots (memo_slots per node) are not counted.

n_evictedint

Cache entries dropped to keep within cache_mb.

local(node, preds)[source]#

Best local score of node and its parents, given a predecessor bitmask.

path_moves(node, preds)[source]#

Return the bitmask of the candidates toggled along the greedy path of node.

The path is the one given preds. Removing a predecessor outside this mask leaves the result of local() unchanged. With restarts it is the union over the climbs of the chain: a predecessor off every path changes no climb, hence no first parent either.

breaks(node, preds)[source]#

For each variable c outside preds, where adding it would alter the greedy path.

Entry c is the first step of the path of node that c would beat (the path length if it only improves on the final state), or -1 if adding c to preds leaves the result of local() unchanged. Entries for node and for preds are -1. The test is the one _replay() applies, done for all candidates at once, so their neighbour vectors are filled in large batches. With restarts an entry is non-negative when c would alter any climb of the chain (the value is then only a flag): if it alters none, every climb and every first parent stay as they were.

score(order)[source]#

Score a complete order: the sum of each node’s best local score.