Skip to content

Commit

Permalink
fix: Fixing the memory leak issues from #43 for real this time. Also …
Browse files Browse the repository at this point in the history
…fixing the segfault errors from #43
  • Loading branch information
FinbarArgus committed Sep 24, 2024
1 parent 3ad4771 commit 0f2b7da
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
11 changes: 9 additions & 2 deletions src/param_id/paramID.py
Original file line number Diff line number Diff line change
Expand Up @@ -2131,6 +2131,13 @@ def run(self):
# rand_survivor_idx = np.random.choice(np.arange(num_elite, num_pop), p=survive_prob)
# param_vals_norm[:, param_idx] = param_vals_norm[:, rand_survivor_idx]
#

# set the cases with nan cost to have a very large but not nan cost
for idx in range(num_pop):
if np.isnan(cost[idx]):
cost[idx] = 1e25
if cost[idx] > 1e25:
cost[idx] = 1e25
survive_prob = cost[num_elite:num_pop]**-1/sum(cost[num_elite:num_pop]**-1)
rand_survivor_idxs = np.random.choice(np.arange(num_elite, num_pop),
size=num_survivors-num_elite, p=survive_prob)
Expand Down Expand Up @@ -2468,7 +2475,7 @@ def get_cost_obs_and_pred_from_params(self, param_vals, reset=True,
pred_outputs_list.append(pred_outputs)
# reset params
if reset:
self.sim_helper.reset_and_clear(only_one_exp=only_one_exp)
self.sim_helper.reset_and_clear()

else:
# simulation set cost to large,
Expand Down Expand Up @@ -2557,7 +2564,7 @@ def get_cost_obs_and_pred_from_params(self, param_vals, reset=True,

return cost, operands_outputs_list, pred_outputs_list

def get_cost_and_obs_from_params(self, param_vals, reset=True, only_one_exp=1):
def get_cost_and_obs_from_params(self, param_vals, reset=True, only_one_exp=-1):
cost, obs, _ = self.get_cost_obs_and_pred_from_params(param_vals, reset=reset, only_one_exp=only_one_exp)
return cost, obs

Expand Down
9 changes: 3 additions & 6 deletions src/utilities/opencor_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ def reset_and_clear(self, only_one_exp=-1):
# mem = self.process_memory()
# print(f'memory_pre_clear={mem}')
self.simulation.reset(True)
# if only_one_exp != -1:
# # for some reason if i only do one simulation and then release values there is a seg fault.
# # so here i don't release all values.
self.simulation.release_all_values()
self.simulation.clear_results()
# mem = self.process_memory()
Expand Down Expand Up @@ -104,11 +101,11 @@ def get_results(self, variables_list_of_lists, flatten=False):
if variable_name == 'time':
results[JJ].append(self.tSim)
elif variable_name in self.simulation.results().states():
results[JJ].append(self.simulation.results().states()[variable_name].values()[-self.n_steps - 1:])
results[JJ].append(self.simulation.results().states()[variable_name].values()[-self.n_steps - 1:].copy())
elif variable_name in self.simulation.results().algebraic():
results[JJ].append(self.simulation.results().algebraic()[variable_name].values()[-self.n_steps-1:])
results[JJ].append(self.simulation.results().algebraic()[variable_name].values()[-self.n_steps-1:].copy())
elif variable_name in self.data.constants():
results[JJ].append(self.data.constants()[variable_name])
results[JJ].append(self.data.constants()[variable_name].copy())
else:
print(f'variable {variable_name} is not a model variable. model variables are')
print([name for name in self.simulation.results().states()])
Expand Down

0 comments on commit 0f2b7da

Please sign in to comment.