in theseus/core/objective.py [0:0]
def add(self, cost_function: CostFunction):
# adds the cost function if not already present
if cost_function.name in self.cost_functions:
if cost_function is not self.cost_functions[cost_function.name]:
raise ValueError(
f"Two different cost function objects with the "
f"same name ({cost_function.name}) are not allowed "
"in the same objective."
)
else:
warnings.warn(
"This cost function has already been added to the objective, "
"nothing to be done."
)
else:
self.cost_functions[cost_function.name] = cost_function
self.current_version += 1
# ----- Book-keeping for the cost function ------- #
# adds information about the optimization variables in this cost function
self._add_function_variables(cost_function, optim_vars=True)
# adds information about the auxiliary variables in this cost function
self._add_function_variables(cost_function, optim_vars=False)
if cost_function.weight not in self.cost_functions_for_weights:
# ----- Book-keeping for the cost weight ------- #
# adds information about the variables in this cost function's weight
self._add_function_variables(
cost_function.weight, optim_vars=True, is_cost_weight=True
)
# adds information about the auxiliary variables in this cost function's weight
self._add_function_variables(
cost_function.weight, optim_vars=False, is_cost_weight=True
)
self.cost_functions_for_weights[cost_function.weight] = []
if cost_function.weight.num_optim_vars() > 0:
warnings.warn(
f"The cost weight associated to {cost_function.name} receives one "
"or more optimization variables. Differentiating cost "
"weights with respect to optimization variables is not currently "
"supported, thus jacobians computed by our optimizers will be "
"incorrect. You may want to consider moving the weight computation "
"inside the cost function, so that the cost weight only receives "
"auxiliary variables.",
RuntimeWarning,
)
self.cost_functions_for_weights[cost_function.weight].append(cost_function)
if self.optim_vars.keys() & self.aux_vars.keys():
raise ValueError(
"Objective does not support a variable being both "
"an optimization variable and an auxiliary variable."
)