Skip to content

Make global constants available to interaction-based model expressions - #1095

Merged
jpn-- merged 5 commits into
mainfrom
copilot/fix-global-constants-availability
Aug 13, 2026
Merged

Make global constants available to interaction-based model expressions#1095
jpn-- merged 5 commits into
mainfrom
copilot/fix-global-constants-availability

Conversation

Copilot AI commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Constants defined in constants.yaml were unavailable in location choice, destination choice, and tour scheduling specs. Those models evaluate utilities via interaction_simulate.eval_interaction_utilities(), which used only the caller-supplied locals_d, unlike simulate.eval_utilities() / assign.local_utilities() which already merge in the global constants.

Changes

  • activitysim/core/interaction_simulate.py: seed the evaluation locals with state.get_global_constants() before applying the caller's locals_d, so model-supplied locals (model CONSTANTS, skims, df) still win on name collisions. Because this is the single entry point used by interaction_sample, interaction_simulate, and interaction_sample_simulate, it covers all the affected models, in both the sharrow and non-sharrow paths.
# avoid altering caller's passed-in locals_d parameter (they may be looping)
# global constants are always available, but can be overridden by locals_d
locals_d = {**state.get_global_constants(), **(locals_d or {})}
  • activitysim/core/test/test_interaction_simulate.py: tests that a constant from constants.yaml resolves in an interaction spec expression, and that locals_d takes precedence over a same-named global constant.

With this, a spec expression such as distance_km * KM_TO_MILE resolves in destination/location choice and tour scheduling specs using a single system-wide constant.

Note

Constants now shadow chooser columns of the same name in interaction specs (resolvers precede DataFrame columns in fast_eval). This matches existing behavior in simulate.eval_utilities, but is worth a look during review.

Copilot AI linked an issue Jul 30, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix global constants not available in all models Make global constants available to interaction-based model expressions Jul 30, 2026
Copilot finished work on behalf of jpn-- July 30, 2026 21:14
Copilot AI requested a review from jpn-- July 30, 2026 21:14
@jpn--

jpn-- commented Jul 30, 2026

Copy link
Copy Markdown
Member

@copilot your PR is failing the checks. Run the pre-commit checks on your code, do not attempt to fix black formatting errors without actually calling black to fix them.

Copilot finished work on behalf of jpn-- July 30, 2026 21:44
… global constants

Refactor the test file to include a new test case that verifies global
constants (`SAMPLE_SCALE` and `SIMULATE_SCALE`) are available in both
destination-choice substeps. This ensures consistency across sampling and
simulation operations, enhancing reliability and maintainability of the
codebase.
@jpn--
jpn-- marked this pull request as ready for review August 3, 2026 21:06
@jpn--
jpn-- requested a review from dhensle August 3, 2026 21:06
@jpn-- jpn-- added this to Phase 11 Aug 7, 2026
@jpn-- jpn-- moved this to Under Review in Phase 11 Aug 7, 2026
@dhensle

dhensle commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Made the following additions to the work copilot did:

  • Removed redundant call of get_global_constants in trip destination and simulate.py
  • Added line in preprocessor test and simple simulate test to use a global constant
  • Added explicit global constants in sharrow flow and added test to ensure they get added

@jpn-- this is ready for your final review.

@jpn--
jpn-- merged commit e23bb12 into main Aug 13, 2026
18 checks passed
@github-project-automation github-project-automation Bot moved this from Under Review to Done in Phase 11 Aug 13, 2026
@jpn--
jpn-- deleted the copilot/fix-global-constants-availability branch August 13, 2026 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Global constants not available in all models

3 participants