in theseus/core/objective.py [0:0]
def erase(self, name: str):
self.current_version += 1
if name in self.cost_functions:
cost_function = self.cost_functions[name]
# erase variables associated to this cost function (if needed)
self._erase_function_variables(cost_function, optim_vars=True)
self._erase_function_variables(cost_function, optim_vars=False)
# delete cost function from list of cost functions connected to its weight
cost_weight = cost_function.weight
cost_fn_idx = self.cost_functions_for_weights[cost_weight].index(
cost_function
)
del self.cost_functions_for_weights[cost_weight][cost_fn_idx]
# No more cost functions associated to this weight, so can also delete
if len(self.cost_functions_for_weights[cost_weight]) == 0:
# erase its variables (if needed)
self._erase_function_variables(
cost_weight, optim_vars=True, is_cost_weight=True
)
self._erase_function_variables(
cost_weight, optim_vars=False, is_cost_weight=True
)
del self.cost_functions_for_weights[cost_weight]
# finally, delete the cost function
del self.cost_functions[name]
else:
warnings.warn(
"This cost function is not in the objective, nothing to be done."
)