rsr.rsr.run_ref_extraction_by_mcs¶
- rsr.rsr.run_ref_extraction_by_mcs(*, sfun, probs, row_names, n_state, sys_upper_st, refs_upper=None, refs_lower=None, refs_mat_upper=None, refs_mat_lower=None, unk_prob_thres=None, unk_prob_opt='rel', max_rounds=10000, prob_update_every=500, save_every=10, n_sample=10000000, sample_batch_size=100000, max_search_loops=0, samples_store=True, samples=None, store_chunk_size=1000000, min_ref_search=True, active_ref_search=True, acq_gamma=1.0, acq_pool_size=1024, ref_update_verbose=True, track_overrides=False, n_workers=1, devices=None, output_dir='rsr_res', upper_json_name=None, lower_json_name=None, upper_pt_name=None, lower_pt_name=None, metrics_path='metrics.json')[source]¶
Extract boundary reference states via Monte Carlo search.
Iterates between exploring the unknown system-event space by Monte Carlo simulation and evaluating the system function on candidate component states. On each round, unknown samples (classified neither upper nor lower by the current reference stores) are found, and one or more of them are selected and resolved by calling
sfun. The resulting upper/lower references are minimised and merged back in. The loop terminates when the probability of the unknown region falls belowunk_prob_thres, when no unknown sample is left, or whenmax_roundsis reached.Where the samples come from is governed by
samples_store. By default (True) the run learns from one sample set,n_samplesamples drawn fromprobsor the ones given insamples, a chunk ofstore_chunk_sizeat a time:The chunk is drawn (or sliced) a batch at a time, and each batch is classified against every reference so far; only its unknown samples are kept, so no more than one chunk’s samples are held at once.
Each round picks from the chunk’s unknown samples and resolves them. A sample, once classified, stays classified (a reference removed as dominated lies inside the one that replaced it), so after an update only the unknown samples are checked, and only against the references just added.
When the chunk’s unknown share is within
unk_prob_thres(judged on the chunk alone), or none is left, the next chunk is taken; the references carry over, so later chunks keep fewer unknown samples.
Every round classifies at least the sample it resolved, so the run is finite: at most one round per sample. The probabilities are shares of the samples taken so far, exact for them; with
unk_prob_thres=0, the default here, every sample ends classified. Withsamples_store=Falsesamples are drawn anew each round, in batches ofsample_batch_sizeuntil an unknown one turns up, and the probabilities are re-estimated everyprob_update_everyrounds onn_samplefresh samples.Which unknown samples get resolved is governed by
active_ref_search. By default the selection is active: each unknown sample is scored by how much a new reference state found there would extend the current frontiers, and the best-scoring one(s) are evaluated. Settingactive_ref_search=Falserestores uniform-random selection.- Parameters:
sfun – System function. Callable
comps_dict -> (fval, sys_state, info).probs (
Tensor) – Categorical component probabilities of shape(n_var, n_state).row_names (
List[str]) – Component names matching the rows ofprobs.n_state (
int) – Number of states per component.sys_upper_st (
int) – System-state threshold that defines the upper reference set (samples withsys_state >= sys_upper_st).refs_upper (
Optional[List[Dict[str,Any]]]) – Optional initial list of upper reference dicts.refs_lower (
Optional[List[Dict[str,Any]]]) – Optional initial list of lower reference dicts.refs_mat_upper (
Optional[Tensor]) – Optional initial upper reference tensor(n_refs, n_var, n_state).refs_mat_lower (
Optional[Tensor]) – Optional initial lower reference tensor.
- Keyword Arguments:
unk_prob_thres – Termination threshold on the unknown-region probability.
None(default) means0withsamples_store, so every sample ends classified, and1e-2without it.unk_prob_opt – Threshold interpretation —
"abs"(absolute) or"rel"(relative to the previous round).max_rounds – Hard cap on the number of rounds.
prob_update_every – Frequency (in rounds) at which the unknown probability is re-estimated.
save_every – Frequency (in rounds) at which references and metrics are written to disk.
n_sample – Total number of samples drawn per probability update.
sample_batch_size – Samples per batch inside one update.
max_search_loops – Max batches per round used to search for new unknown candidates.
0meansn_sample // sample_batch_size. Ignored withsamples_store.samples_store – If True (default), learn from one sample set, chunk by chunk (see above);
n_sampleis its size when it is drawn, andsample_batch_sizebounds how many samples are classified at once. If False, draw samples anew every round.prob_update_everyanddevicesapply only when False.samples – The sample set to learn from, with
samples_store: component states(N, n_var)in the order ofrow_names, or one-hot(N, n_var, n_state). If None,n_samplesamples are drawn fromprobs.store_chunk_size – With
samples_store, how many samples are taken at once. Memory is at most a chunk’s unknown samples, one byte per component each (up to 256 states).0takes the whole set as one chunk.min_ref_search – Whether to minimise newly found references before inserting them.
active_ref_search – If True (default), choose which unknown sample(s) to resolve next by maximising the deficit-based acquisition score
(d+ + d-) - acq_gamma * |d+ - d-|rather than drawing them uniformly at random, whered+/d-are the upper/lower deficits (seeselect_refs_by_acquisition()). Ties are broken by the lexicographically smallest component-state vector. Falls back to random selection while either reference set is still empty, since the deficits are undefined then.acq_gamma – Non-negative weight balancing exploration against uncertainty in the acquisition score.
0is pure exploration (pick the candidate furthest from both frontiers); larger values increasingly favour candidates that sit equidistant between the two frontiers, whose outcome is least predictable. Ignored whenactive_ref_searchis False.acq_pool_size – Cap on how many unknown samples are scored per round. When more are available, a random subset of this size is scored, which bounds the cost of the deficit computation on large reference sets.
0scores every unknown sample. Ignored whenactive_ref_searchis False.ref_update_verbose – Print progress messages during reference updates.
track_overrides – If True, record every override event — i.e. whenever a newly found reference dominates and removes existing reference(s) — and return them under
override_login the result. Each entry is{"round", "kind", "found", "overridden"}wherekindis"upper"/"lower",foundis the newly found reference, andoverriddenis the list of existing references removed because of it. Default off. (In the parallel path,n_workers > 1,foundholds all refs added that round — attribution is round-level.)n_workers – Number of CPU worker processes for parallel
sfunevaluation and state minimisation.devices – GPU devices for multi-GPU sampling, e.g.
["cuda:0", "cuda:1"].output_dir – Directory in which references and metrics are written.
upper_json_name – Filename for upper references (JSON). Defaults to
refs_up_{sys_upper_st}.json.lower_json_name – Filename for lower references (JSON). Defaults to
refs_low_{sys_upper_st-1}.json.upper_pt_name – Filename for the upper reference tensor (PyTorch).
lower_pt_name – Filename for the lower reference tensor (PyTorch).
metrics_path – Filename for the per-round metrics log.
- Return type:
Dict[str,Any]- Returns:
A dictionary with the final
refs_upper,refs_lower,refs_mat_upper,refs_mat_lower, and the metrics log. Whentrack_overridesis True, it also includesoverride_log, a list of override events (seetrack_overrides).- Raises:
ValueError – If
acq_gammais negative whileactive_ref_searchis enabled, or ifacq_pool_sizeis negative.
Notes
Active selection of the next reference state. For an unknown sample
x, the upper deficitd+(x)is the smallest total shortfall ofxbelow any upper reference state, and the lower deficitd-(x)the smallest total excess ofxabove any lower reference state, both counting only the components that fall short (respectively exceed).d+ = 0certifies survival andd- = 0certifies failure, so unknown samples are exactly those with both deficits strictly positive. They are ranked by\[A(x) = \big(d^{+}(x) + d^{-}(x)\big) - \gamma\,\big|d^{+}(x) - d^{-}(x)\big|.\]The sum rewards samples lying far from both frontiers, which are the regions the current reference sets map most poorly. The absolute difference measures how one-sided a sample is: where it is large the outcome of evaluating
sfunis predictable and the resulting reference state would sit adjacent to ones already known, adding little coverage. Subtracting it therefore favours samples that are both far from what is known and near-equidistant between the two frontiers.acq_gammasets the balance.Ties are broken by the lexicographically smallest component-state vector, so a round’s picks do not depend on the order in which samples happen to be drawn.
Two caveats. Selection falls back to uniform-random while either reference set is empty, since the deficits are undefined then — in practice this covers the first few rounds. And when a round yields more unknown samples than
acq_pool_size, a random subset of that size is scored rather than all of them, which bounds the cost of the deficit computation on large reference sets; selection is greedy within that pool, not over every unknown sample.See also
select_refs_by_acquisition: The selection rule itself. compute_deficits: The underlying deficit computation.