symforce.opt.optimization_problem module

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

Bases: object

An optimization problem.

Defined by a collection of sub_problem.SubProblem, 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 sub_problem.SubProblem.build_residuals() on each subproblem with the appropriate arguments, and then pass the subproblems and residual_blocks to the OptimizationProblem constructor.

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 self in C++. This currently assumes there is only one factor generated for the optimization problem.

Parameters:
  • output_dir (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 (CppConfig | None) – 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 symforce.opt.optimizer.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 (CodegenConfig | None) – Language the factors will be generated in when generate() is called. If not provided, uses the same default as the factor.Factor constructor.

Return type:

List[Factor]

make_numeric_factors(name, optimized_keys=None)[source]

Returns a list of NumericFactor 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 (Sequence[str] | None) – 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 (Any | None) – Optional additional shared inputs

Returns:

inputs – the combined Values

Return type:

Values