From 6587ec9da86ee1d46d09064e0cbf770d0f06fc29 Mon Sep 17 00:00:00 2001 From: "felix.stegemerten" Date: Wed, 25 Feb 2026 14:44:27 +0100 Subject: [PATCH 1/2] Fix bug in simulator result file --- agentlib/modules/simulation/simulator.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/agentlib/modules/simulation/simulator.py b/agentlib/modules/simulation/simulator.py index 164657e9..027d3b8e 100644 --- a/agentlib/modules/simulation/simulator.py +++ b/agentlib/modules/simulation/simulator.py @@ -440,7 +440,7 @@ def __init__(self, *, config: dict, agent: Agent): self._model = None self.model = self.config.model - self._inputs_changed_since_last_results_saving = False + self._pending_input_change_time = None # Caching variables for performance (avoid list comprehensions in loop) self._input_vars = self._get_result_input_variables() @@ -545,19 +545,19 @@ def _register_input_callbacks(self): "Updating model %s %s=%s", _type, var.name, var.value ) self.model.set(name=var.name, value=var.value) - self._inputs_changed_since_last_results_saving = True + self._pending_input_change_time = self.env.time def _callback_update_model_input(self, inp: AgentVariable, name: str): """Set given model input value to the model""" self.logger.debug("Updating model input %s=%s", name, inp.value) self.model.set_input_value(name=name, value=inp.value) - self._inputs_changed_since_last_results_saving = True + self._pending_input_change_time = self.env.time def _callback_update_model_parameter(self, par: AgentVariable, name: str): """Set given model parameter value to the model""" self.logger.debug("Updating model parameter %s=%s", name, par.value) self.model.set_parameter_value(name=name, value=par.value) - self._inputs_changed_since_last_results_saving = True + self._pending_input_change_time = self.env.time def process(self): """ @@ -571,7 +571,7 @@ def process(self): self._result.initialize_inputs(in_values) self._result.initialize_outputs(self.env.time) # Prevent false positive "input change" log at t=0 due to initialization callbacks - self._inputs_changed_since_last_results_saving = False + self._pending_input_change_time = None while True: # Determine the time points for the next communication step t_samples = create_time_samples( @@ -588,12 +588,12 @@ def process(self): # This ensures the new inputs are recorded at the current timestamp, # separate from the outputs of the *previous* step (which were logged at # the end of the last loop). - if self._inputs_changed_since_last_results_saving: + if self._pending_input_change_time: if self.config.save_results: # Create row: [t=Current, Out=NaN, In=New] - self._log_inputs(self.env.time, + self._log_inputs(self._pending_input_change_time, capture_all_inputs=self.config.capture_all_inputs) - self._inputs_changed_since_last_results_saving = False + self._pending_input_change_time = None # 3. Perform Simulation Step self.model.do_step( From 60c16a77971f8083c6accf2400cc0930dbf3be72 Mon Sep 17 00:00:00 2001 From: "felix.stegemerten" Date: Wed, 25 Feb 2026 17:50:21 +0100 Subject: [PATCH 2/2] Fix bug in simulator result file --- agentlib/modules/simulation/simulator.py | 25 ++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/agentlib/modules/simulation/simulator.py b/agentlib/modules/simulation/simulator.py index 027d3b8e..10d1d2c8 100644 --- a/agentlib/modules/simulation/simulator.py +++ b/agentlib/modules/simulation/simulator.py @@ -440,7 +440,7 @@ def __init__(self, *, config: dict, agent: Agent): self._model = None self.model = self.config.model - self._pending_input_change_time = None + self._inputs_changed_since_last_results_saving = False # Caching variables for performance (avoid list comprehensions in loop) self._input_vars = self._get_result_input_variables() @@ -545,19 +545,19 @@ def _register_input_callbacks(self): "Updating model %s %s=%s", _type, var.name, var.value ) self.model.set(name=var.name, value=var.value) - self._pending_input_change_time = self.env.time + self._inputs_changed_since_last_results_saving = True def _callback_update_model_input(self, inp: AgentVariable, name: str): """Set given model input value to the model""" self.logger.debug("Updating model input %s=%s", name, inp.value) self.model.set_input_value(name=name, value=inp.value) - self._pending_input_change_time = self.env.time + self._inputs_changed_since_last_results_saving = True def _callback_update_model_parameter(self, par: AgentVariable, name: str): """Set given model parameter value to the model""" self.logger.debug("Updating model parameter %s=%s", name, par.value) self.model.set_parameter_value(name=name, value=par.value) - self._pending_input_change_time = self.env.time + self._inputs_changed_since_last_results_saving = True def process(self): """ @@ -571,7 +571,7 @@ def process(self): self._result.initialize_inputs(in_values) self._result.initialize_outputs(self.env.time) # Prevent false positive "input change" log at t=0 due to initialization callbacks - self._pending_input_change_time = None + self._inputs_changed_since_last_results_saving = False while True: # Determine the time points for the next communication step t_samples = create_time_samples( @@ -584,16 +584,21 @@ def process(self): dt_sim = float(t_samples[i + 1] - t_samples[i]) # 2. Check for Input Changes (Pre-Step) - # If inputs changed since the last step (or during the yield), we log them now. + # If inputs changed since the last step (or during the yield), + # we log them now. # This ensures the new inputs are recorded at the current timestamp, # separate from the outputs of the *previous* step (which were logged at # the end of the last loop). - if self._pending_input_change_time: + current_time = self.env.time + # Track if a communication step is reached. + # At communication times, the inputs are always saved + full_comm_step = ((current_time - self._last_communication_time) == 0) + if self._inputs_changed_since_last_results_saving or full_comm_step: if self.config.save_results: # Create row: [t=Current, Out=NaN, In=New] - self._log_inputs(self._pending_input_change_time, + self._log_inputs(self.env.time, capture_all_inputs=self.config.capture_all_inputs) - self._pending_input_change_time = None + self._inputs_changed_since_last_results_saving = False # 3. Perform Simulation Step self.model.do_step( @@ -744,4 +749,4 @@ def convert_agent_vars_to_list_of_dicts(var: AgentVariables) -> List[Dict]: agent_var.dict(exclude={"source", "alias", "shared", "rdf_class"}) for agent_var in var ] - return var_dict_list \ No newline at end of file + return var_dict_list