in theseus/core/objective.py [0:0]
def copy(self) -> "Objective":
new_objective = Objective()
# First copy all individual cost weights
old_to_new_cost_weight_map: Dict[CostWeight, CostWeight] = {}
for cost_weight in self.cost_functions_for_weights:
new_cost_weight = cost_weight.copy(
new_name=cost_weight.name, keep_variable_names=True
)
old_to_new_cost_weight_map[cost_weight] = new_cost_weight
# Now copy the cost functions and assign the corresponding cost weight copy
new_cost_functions: List[CostFunction] = []
for cost_function in self.cost_functions.values():
new_cost_function = cost_function.copy(
new_name=cost_function.name, keep_variable_names=True
)
# we assign the allocated weight copies to avoid saving duplicates
new_cost_function.weight = old_to_new_cost_weight_map[cost_function.weight]
new_cost_functions.append(new_cost_function)
# Handle case where a variable is copied in 2+ cost functions or cost weights,
# since only a single copy should be maintained by objective
for cost_function in new_cost_functions:
# CostFunction
for i, var in enumerate(cost_function.optim_vars):
if new_objective.has_optim_var(var.name):
cost_function.set_optim_var_at(
i, new_objective.optim_vars[var.name]
)
for i, aux_var in enumerate(cost_function.aux_vars):
if new_objective.has_aux_var(aux_var.name):
cost_function.set_aux_var_at(
i, new_objective.aux_vars[aux_var.name]
)
# CostWeight
for i, var in enumerate(cost_function.weight.optim_vars):
if var.name in new_objective.cost_weight_optim_vars:
cost_function.weight.set_optim_var_at(
i, new_objective.cost_weight_optim_vars[var.name]
)
for i, aux_var in enumerate(cost_function.weight.aux_vars):
if new_objective.has_aux_var(aux_var.name):
cost_function.weight.set_aux_var_at(
i, new_objective.aux_vars[aux_var.name]
)
new_objective.add(cost_function)
return new_objective