@@ -172,3 +172,75 @@ def test_interaction_simulate_eet_large_utilities(state):
172172 assert not choices_eet .isna ().any ()
173173 # With such a large difference, Alt 1 should be the dominant choice
174174 assert (choices_eet == 1 ).all ()
175+
176+
177+ def test_eval_interaction_utilities_global_constants (tmp_path ):
178+ # global constants (from constants.yaml) should be available to expressions
179+ # evaluated for interaction models (e.g. location choice, destination choice,
180+ # tour scheduling), see issue #1015
181+
182+ configs_dir = tmp_path .joinpath ("configs" )
183+ configs_dir .mkdir ()
184+ configs_dir .joinpath ("constants.yaml" ).write_text ("KM_TO_MILE: 0.621371\n " )
185+ tmp_path .joinpath ("data" ).mkdir ()
186+
187+ state = workflow .State ()
188+ state .initialize_filesystem (
189+ working_dir = tmp_path , configs_dir = ("configs" ,)
190+ ).default_settings ()
191+ state .settings .check_for_variability = False
192+
193+ df = pd .DataFrame ({"distance_km" : [1.0 , 10.0 ]}, index = [0 , 1 ])
194+
195+ spec = pd .DataFrame (
196+ {"coefficient" : [1.0 ]},
197+ index = pd .Index (["distance_km * KM_TO_MILE" ], name = "Expression" ),
198+ )
199+
200+ utilities , _ = interaction_simulate .eval_interaction_utilities (
201+ state ,
202+ spec ,
203+ df ,
204+ locals_d = None ,
205+ trace_label = "test_global_constants" ,
206+ trace_rows = None ,
207+ )
208+
209+ np .testing .assert_allclose (
210+ utilities .utility .to_numpy (), df .distance_km .to_numpy () * 0.621371
211+ )
212+
213+
214+ def test_eval_interaction_utilities_locals_override_global_constants (tmp_path ):
215+ # values passed in locals_d take precedence over global constants
216+
217+ configs_dir = tmp_path .joinpath ("configs" )
218+ configs_dir .mkdir ()
219+ configs_dir .joinpath ("constants.yaml" ).write_text ("KM_TO_MILE: 0.621371\n " )
220+ tmp_path .joinpath ("data" ).mkdir ()
221+
222+ state = workflow .State ()
223+ state .initialize_filesystem (
224+ working_dir = tmp_path , configs_dir = ("configs" ,)
225+ ).default_settings ()
226+ state .settings .check_for_variability = False
227+
228+ df = pd .DataFrame ({"distance_km" : [1.0 , 10.0 ]}, index = [0 , 1 ])
229+
230+ spec = pd .DataFrame (
231+ {"coefficient" : [1.0 ]},
232+ index = pd .Index (["distance_km * KM_TO_MILE" ], name = "Expression" ),
233+ )
234+
235+ utilities , _ = interaction_simulate .eval_interaction_utilities (
236+ state ,
237+ spec ,
238+ df ,
239+ locals_d = {"KM_TO_MILE" : 1.0 },
240+ trace_label = "test_global_constants_override" ,
241+ trace_rows = None ,
242+ )
243+
244+ np .testing .assert_allclose (
245+ utilities .utility .to_numpy (), df .distance_km .to_numpy ()
246+ )
0 commit comments