Skip to content

Adding a New Oracle

An oracle implements the ground-truth evaluation function \(f(x, m)\) for one or more supported fidelity levels \(m \subseteq \mathcal{M}\). Implement a new oracle subclass to:

  • Wrap a new simulator, benchmark function, or lab instrument.
  • Define a different fidelity structure (different levels, costs, or confidences).
  • Implement a custom routing rule for multi-fidelity queries.

A single oracle does not need to cover every fidelity level. The experiment's top-level oracle configuration, however, must collectively support all fidelity levels used during the run. You can achieve this with a single oracle that covers all levels, or by using a CompositeOracle to combine several oracles that each cover a subset. If you only need to combine existing oracles across fidelity levels, see Using CompositeOracle before writing a new class.

What to implement

Choosing a base class

Approach When to use
Subclass Oracle directly You need full control over cost logic (e.g., candidate-dependent costs) or scoring behaviour that MultiFidelityOracle does not support.
Subclass MultiFidelityOracle Each fidelity level has a constant cost per sample and can be described by a simple score_fn callable. This avoids re-implementing get_costs, get_min_query_cost, and query.
Use CompositeOracle (no new class) You already have oracle classes that each cover a subset of fidelity levels and want to combine them into a single oracle that covers all levels. CompositeOracle merges their supported fidelities and routes candidates to the cheapest sub-oracle per level.

MultiFidelityOracle is a convenience subclass of Oracle. Because it assumes a fixed cost_per_sample per fidelity level and a stateless score_fn, it is not suitable when costs depend on the candidate itself or when querying requires shared state across fidelity levels. In those cases, subclass Oracle directly.

Required methods (when subclassing Oracle)

Subclass activelearning.oracle.oracle.Oracle and implement three methods:

Method Required? Notes
get_fidelity_confidences() Yes Returns dict[int, float] — maps each fidelity id to a confidence in [0, 1]
get_costs(candidates) Yes Returns list[float] — one cost per input candidate, in the same order
query(candidates) Yes Returns list[Observation] — one Observation per input candidate, in the same order

Reference implementations

Review the built-in oracles as concrete examples before writing your own:

  • BraninOracle — 2D benchmark with configurable single and multi-fidelity costs.
  • Hartmann6DOracle — 6-dimensional benchmark with the same structure.
  • CompositeOracle — routes queries across sub-oracles by fidelity level.

Source: src/activelearning/oracle/augmented_function_oracle.py and src/activelearning/oracle/composite_oracle.py.

Config model and registration

Add a Pydantic config model in src/activelearning/oracle/config.py and extend the OracleConfig union. See the existing models in that file as reference.

class MyOracleConfig(BaseModel):
    type: Literal["MyOracle"] = "MyOracle"
    # your parameters here

    def build(self) -> Oracle:
        return MyOracle(...)

OracleConfig = Annotated[
    Union[..., MyOracleConfig],
    Field(discriminator="type"),
]

Then specify it in your YAML:

oracle:
  type: MyOracle

Fidelity id alignment with the sampler

The fidelity ids used in get_fidelity_confidences() must match the ids your sampler stamps onto Candidate.fidelity. If the sampler emits fidelity 0 or 1 but your oracle only declares fidelity 2, every query will raise a ValueError (e.g., the HypercubeSampler fidelities parameter controls which ids it assigns — keep them consistent).

Common pitfalls

Order alignment — get_costs() and query() must return one item per input candidate, in the exact same order. Never sort, group, or filter the input list before building your return list.

Runtime tensors — build tensors inside query(), not in __init__(). Use self.dtype and self.device so the runtime binding takes effect.

Using CompositeOracle instead of building from scratch

When different oracle implementations each handle a subset of fidelity levels, use CompositeOracle. It merges get_fidelity_confidences() from all sub-oracles, routes candidates to the cheapest sub-oracle that handles each fidelity, and assembles results in the original candidate order.

oracle:
  type: CompositeOracle
  sub_oracles:
    - type: BraninOracle
      fidelity_costs: {0: 1.0}
    - type: BraninOracle
      fidelity_costs: {1: 5.0}

Use CompositeOracle when fidelity levels are cleanly separated across implementations. Write a new oracle class when the routing logic cannot be expressed as independent sub-oracles.