Building OCPs
Info: The functionality presented here was introduced in
opengen version 0.10.0a1. The API is still young and is likely to change in version 0.11.
Once an OCP has been defined, it can be turned into a generated optimizer
using og.ocp.OCPBuilder. This builder lowers the high-level OCP to the
standard OpEn problem format and then invokes the usual code-generation
pipeline.
At a minimum, you need:
- the OCP definition,
- optimizer metadata,
- a build configuration, and
- a solver configuration.
For example:
ocp_optimizer = og.ocp.OCPBuilder(
ocp,
metadata=og.config.OptimizerMeta().with_optimizer_name("my_ocp"),
build_configuration=og.config.BuildConfiguration()
.with_build_python_bindings(),
solver_configuration=og.config.SolverConfiguration()
.with_tolerance(1e-5)
.with_delta_tolerance(1e-5),
).build()
The call to .build() generates the solver and returns a
GeneratedOptimizer, which can then be used directly:
result = ocp_optimizer.solve(x0=[0.4, 0.2], xref=[0.0, 0.0])
If you prefer to access the generated solver over TCP instead of direct Python
bindings, use with_tcp_interface_config(...) in the build configuration:
ocp_optimizer = og.ocp.OCPBuilder(
ocp,
metadata=og.config.OptimizerMeta().with_optimizer_name("my_ocp_tcp"),
build_configuration=og.config.BuildConfiguration()
.with_tcp_interface_config(),
solver_configuration=og.config.SolverConfiguration(),
).build()
In both cases, the high-level interface remains the same:
result = ocp_optimizer.solve(x0=[0.4, 0.2], xref=[0.0, 0.0])
For more details on build configurations, TCP servers, Python bindings, and the low-level code-generation pipeline, see the main Python interface documentation.
