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 theValues
) 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 theresidual_blocks
argument. Typical workflow is to construct a set of SubProblems (which should also construct each SubProblem Inputs), build theresidual_blocks
Values by callingsub_problem.SubProblem.build_residuals()
on each subproblem with the appropriate arguments, and then pass the subproblems andresidual_blocks
to theOptimizationProblem
constructor.- Parameters:
subproblems (Mapping[str, SubProblem]) – Mapping from subproblem names to subproblems
residual_blocks (Values) – Values where each leaf is a
residual_block.ResidualBlock
, containing all the residuals for the problem. Typically created by callingsub_problem.SubProblem.build_residuals()
on each subproblem.shared_inputs (Dataclass | None) – If provided, an additional
shared_inputs
block to be added to the Values
- subproblems: Mapping[str, SubProblem]¶
- static split_residual_blocks(residual_blocks)[source]¶
Split
residual_blocks
intoresiduals
andextra_values
- 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
, usemake_numeric_factors()
instead.- Parameters:
name (str) – Name of factors. Note that the generated linearization functions will have
"_factor"
appended to the function name (seeCodegen._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 thefactor.Factor
constructor.
- Return type:
- 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 (seeCodegen._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:
- 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: