Skip to main content
Suppose a study compares five products. Each respondent sees two and chooses one. A shared Machine saves the assigned pair, records the preference, updates item scores, and uses those scores to choose pairs for later respondents. This example keeps ordinary Survey navigation: an internal computed gate, one multiple-choice question with dynamic options, and a stop rule. The Machine definition uses existing general primitives. The survey adapter and simulation need no navigation redesign or registered algorithm.

Run the example

The simulation creates 60 scripted respondents. All prefer Atlas over Beacon, Beacon over Cedar, Cedar over Dune, and Dune over Ember. This ground truth belongs to the agents; the Machine updates scores from each submitted choice. It starts with equal scores and stops after 40 valid, distinct-respondent comparisons. The demo makes no model API calls. The actual run produces: The resulting scores are approximately 1.873, 1.019, -0.014, -0.949, and -1.929. The closest neighbors in this ordering receive more comparisons: This verifies the mechanism against a deliberately simple population. It does not establish ranking accuracy for noisy, heterogeneous, or cyclic preferences. If the agent pool runs out first, the report says population_exhausted.

Pair selection

The authoring helper enumerates unordered pairs once and includes them in the serialized definition. Runtime selection uses map expressions and a generic record sorter, not a Python callback. assign(respondent_id=...) follows this policy:
  1. While any pair has no completed comparison, prioritize the least-compared pairs, breaking ties by smallest absolute score gap and then pair ID.
  2. After this warm-up, prioritize the smallest score gap, breaking ties by completed pair count and then pair ID.
  3. After four consecutive score-driven responses, make the next selection an exploration comparison using the first ordering. Repeat this cycle.
explore_every=1 makes every selection an exploration selection. The pair ID comes from the input item order, so equal-priority ties are deterministic. The selection mode is bound with let before ranking candidates, avoiding repeated coverage scans for every candidate. The Machine then uses seeded_order to choose the pair’s display order, keyed by the configured seed, trusted study ID, and respondent ID. It stores that order with the pair and its selection mode. A retry retains the entire assignment, even after other respondents change the scores. Randomized positions do not guarantee an exactly balanced number of left/right appearances in a finite run. Assignments consume neither comparison budget nor pair coverage. An abandoned pair can therefore be offered to a later respondent. A resumed respondent keeps their saved pair; previously recorded respondents are not asked again. A budget smaller than the number of pairs can stop the study before warm-up finishes.

Score updates and atomic accounting

All item scores start at zero. compare(respondent_id=..., winner=...) accepts one of the two assigned labels. Let w and l denote the winner’s and loser’s scores immediately before the accepted answer. The update is:
For an equal-score comparison with the default rate, the winner gains 0.25 and the loser loses 0.25. An unexpected win produces a larger adjustment than an expected win. The implementation computes the probability through logsumexp and exp to avoid overflow at extreme score differences. Floating-point scores conserve their total within numerical precision. The update, pair count, total comparison count, exploration counter, and response ledger commit atomically. Scores are updated against the current state at acceptance, while pair selection and display order remain fixed from assignment. The response ledger allows one answer per stable respondent ID. A same-ID, same-choice retry is a no-op, including after completion. Changing that choice is rejected with answer_changed. An unassigned respondent gets pair_not_assigned; choosing another known item gets winner_not_in_pair. Unknown labels fail the declared input schema. None of these invalid attempts increments the budget or changes scores. The budget counts accepted answers. New comparisons after exhaustion or an explicit close are rejected with comparisons_closed. An explicit early close does not claim that the budget was reached. Item labels must be distinct and nonempty, budget and exploration interval positive integers, the seed nonempty text, and the learning rate finite and in (0, 1].

Survey integration and resumption

Each agent supplies a trusted study_id, stable respondent_id, and numeric turn. The returned schedule runs whole interviews sequentially within each study and checks the Machine’s completion condition before further questions. Before the gate, the survey requests an assignment and reads its view. The gate ends the interview when no new response is accepted. Otherwise the preference question receives exactly:
The view contains the respondent’s ordered pair, acceptance status, comparison count, and completion status. It excludes scores, other respondents’ assignments, and the response ledger. The preference answer triggers the atomic compare command. No additional respondent question is needed for routing. Survey and schedule JSON transport preserves state identity, question bindings, and ordering. Keep that identity to resume the same SQLite state. Tests cover resuming an unanswered assignment with identical options and resuming an already answered assignment without presenting the preference question again. This is a sequential allocation policy. Concurrent answer commits cannot exceed the budget, but overlapping interviews can receive stale selections or have an answer rejected when another respondent uses the last place. Concurrent adaptive selection needs a separate reservation and scheduling policy.

Tests and design findings

The focused tests compare mixed-winner streams against an independent selection/scoring reference, including abandoned assignments. They check exploration boundaries, conserved score totals, extreme score gaps, exact stopping, retries, concurrent budget bounds, independent scopes, SQLite restart, and fresh-process replay without the authoring module. End-to-end tests reverse the supplied agent list to check schedule ordering and inspect the actual choices received by scripted agents. Those choices must also match row.get_question_options("preference") and the saved assignment. The Runner preserves their order and the observed read/version in per-question presentation metadata, including after Results serialization. This closes the reporting gap first exposed by this example without changing Survey navigation. This application exposes two further boundaries:
  • Decision policy versus statistical uncertainty. Similar scores are a selection heuristic. They are not confidence intervals or evidence that a pair is poorly measured. The fixed budget does not claim convergence. Ties, respondent-specific tastes, and preference cycles need explicit models.
  • Candidate-set growth. The definition enumerates all unordered pairs, so candidate storage grows quadratically with the number of items. A much larger catalog should probe candidate generation, evaluation budgets, and bounded history rather than assuming this five-item construction scales unchanged.
The current definition is 21,952 JSON bytes, with 294 expression occurrences and 112 distinct trees. Its 12-comparison corpus replay, covering both warm-up and adaptive selection, peaks at 18,709 host budget steps per transition. Reproduce these development measurements with python -m edsl.examples.shared_state.machine_primitives.profile_corpus. They are not portable cost guarantees or a claim of bit-identical floating-point scoring across hosts. The local demonstration establishes neither live-model preference validity nor hosted human-survey deployment support. Remaining design work is tracked in issue #2665.