symforce.opt.optimization_problem module

class OptimizationProblem(subproblems, residual_blocks, shared_inputs=None)[source]

Bases: object

An optimization problem.

Defined by a collection of SubProblem`s, each of which defines a set of inputs (variables in the Values) and a set of residuals. SubProblems are generally expected to expose inputs that are used by other subproblems; these dependencies should be handled by the user while constructing the `residual_blocks argument. Typical workflow is to construct a set of SubProblems (which should also construct each SubProblem Inputs), build the residual_blocks Values by calling build_residuals on each subproblem with the appropriate arguments, and then pass the subproblems and residual_blocks to the Problem constructor.

Parameters:
  • subproblems (Mapping[str, SubProblem]) – Mapping from subproblem names to subproblems

  • residual_blocks (Values) – Values where each leaf is a ResidualBlock, containing all the residuals for the problem. Typically created by calling build_residuals on each subproblem.

  • shared_inputs (Optional[Dataclass]) – If provided, an additional shared_inputs block to be added to the Values

__init__(subproblems, residual_blocks, shared_inputs=None)[source]
Parameters:
subproblems: Mapping[str, SubProblem]
inputs: Values
residual_blocks: Values
residuals: Values
extra_values: Values
static split_residual_blocks(residual_blocks)[source]

Split residual_blocks into residuals and extra_values

Parameters:

residual_blocks (Values) –

Return type:

Tuple[Values, Values]

keys()[source]

Compute the set of all keys specified by the subproblems

Return type:

List[str]

optimized_keys()[source]

Compute the set of optimized keys, as specified by the subproblems

Return type:

List[str]

generate(output_dir, namespace, name, sparse_linearization=False, config=None)[source]

Generate everything needed to optimize problem in C++. This currently assumes there is only one factor generated for the optimization problem.

Parameters:
  • output_dir (Union[str, PathLike]) – Directory in which to output the generated files.

  • namespace (str) – Namespace used in each generated file.

  • name (str) – Name of the generated factor.

  • sparse_linearization (bool) – Whether the generated factors should use sparse jacobians/hessians

  • config (Optional[CppConfig]) – C++ code configuration used with the linearization functions generated for each factor.

Return type:

None

make_symbolic_factors(name, config=None)[source]

Return a list of symbolic factors for this problem for analysis purposes. If the factors are to be passed to an optimizer, use make_numeric_factors instead.

Parameters:
  • name (str) – Name of factors. Note that the generated linearization functions will have “_factor” appended to the function name (see Codegen._pick_name_for_function_with_derivatives for details).

  • config (Optional[CodegenConfig]) – Language the factors will be generated in when .generate() is called.

Return type:

List[Factor]

make_numeric_factors(name, optimized_keys=None)[source]

Returns a list of NumericFactor`s for this problem, for example to pass to `Optimizer.

Parameters:
  • name (str) – Name of factors. Note that the generated linearization functions will have “_factor” appended to the function name (see Codegen._pick_name_for_function_with_derivatives for details).

  • optimized_keys (Optional[Sequence[str]]) – List of keys to optimize with respect to. Defaults to the optimized keys specified by the subproblems of this optimization problem.

Return type:

List[NumericFactor]

build_inputs(subproblems, shared_inputs=None)[source]

Build the inputs Values for a set of subproblems. The resulting values is structured as:

Values(

subproblem1.name=subproblem1.inputs, …, subproblemN.name=subproblemN.inputs, shared_inputs=shared_inputs,

)

Parameters:
  • subproblems (Iterable[SubProblem]) – Iterable of SubProblems

  • shared_inputs (Optional[Any]) – Optional additional shared inputs

Returns:

the combined Values

Return type:

inputs