"""State confidence from amsel for ``[AKMC] confidence_scheme = amsel``.
``[amsel] state_confidence`` selects the score, and ``amsel.state_confidence``
computes it. A process whose product state also lists this state is known from
the far side. The score compares those processes with the ones this state's own
searches found.
What counts as a find of this state is one thing only: a saddle search that
was launched from this state, came back, and ended on the process. The hits,
rates and barriers handed to amsel hold nothing else. When the trajectory
enters a state, eOn writes the reverse of the entry process into its table;
that process is known from the far side and appears in ``implied``, and it is
not a find until a search of this state lands on it. A state none of whose
searches has found a process yet has confidence 0, whatever the score would
make of an empty catalogue, so it is never left on the entry process alone.
``[amsel] state_confidence = coverage`` is not one of these scores and uses no
catalogue. It is the coverage of the state's direction cover by its own
searches, computed by ``amsel.search_confidence`` from the search records.
"""
from __future__ import annotations
from typing import Any
STATE_CONFIDENCE_SCHEMES = ("heuristic", "good-turing", "reverse-capped", "two-sided")
#: The score of a state searched with ``[amsel] displacement = direction_cover``.
COVERAGE = "coverage"
def _catalogue(state: Any, ids: Any = None) -> dict[str, list]:
state.load_process_table()
procs = state.procs
# Not get_proc_random_count: that one also counts the entry process.
counts = state.get_proc_own_search_count()
chosen = list(procs) if ids is None else [i for i in ids if i in procs]
own = [i for i in chosen if counts.get(i, 0) > 0]
implied = []
for i in chosen:
product = procs[i]["product"]
if product < 0 or product == state.number:
continue
other = state.statelist.get_state(product)
other.load_process_table()
if any(p["product"] == state.number for p in other.procs.values()):
implied.append((float(procs[i]["rate"]), float(procs[i]["barrier"])))
return {
"hits": [int(counts[i]) for i in own],
"rates": [float(procs[i]["rate"]) for i in own],
"barriers": [float(procs[i]["barrier"]) for i in own],
"implied": implied,
}
[docs]
def amsel_state_confidence(
state: Any, *, scheme: str, barrier_tolerance: float, ids: Any = None
) -> float | None:
"""Confidence of one state. ``ids`` limits it to these processes.
``coverage`` is scored from the state's search records and takes no
catalogue: amsel rebuilds the direction cover of the state and returns
one minus the posterior probability that a missed exit carries more than
``[amsel] relevant_rate_share`` of the escape rate. None means amsel has
no cover for the state, because no free atom of it is active; the caller
then scores the state as it scores any state searched without amsel.
"""
if scheme == COVERAGE:
from eon.amsel_search import AmselSearch
return AmselSearch(state, state.get_reactant(), state.config).confidence()
try:
from amsel import state_confidence
except ImportError as exc:
raise RuntimeError(
"[AKMC] confidence_scheme = amsel needs the amsel package"
) from exc
own = _catalogue(state, ids)
if not own["hits"]:
return 0.0
network = (0, 0)
if scheme == "two-sided":
recovered = known = 0
for number in range(state.statelist.get_num_states()):
found, total = state_confidence(
scheme,
**_catalogue(state.statelist.get_state(number)),
barrier_tolerance=barrier_tolerance,
)[1]
recovered += found
known += total
network = (recovered, known)
return float(
state_confidence(
scheme,
**own,
network=network,
barrier_tolerance=barrier_tolerance,
)[0]
)