MCPcopy Index your code
hub / github.com/alphaville/optimization-engine

github.com/alphaville/optimization-engine @v0.12.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.12.0 ↗ · + Follow
1,407 symbols 4,070 edges 140 files 591 documented · 42%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

OpEn logo

CodeFactor CI on master build status

MIT license Apache v2 license Chat on Discord

Optimization Engine (OpEn) is a solver for Fast & Accurate Embedded Optimization for next-generation Robotics and Autonomous Systems. Read on to see how you can use OpEn on your robot today.

Documentation available at alphaville.github.io/optimization-engine

Features

OpEn is the counterpart of CVXGen for nonconvex problems.

  • Fast nonconvex parametric optimization
  • Numerical algorithm written in Rust
  • Provably safe memory management
  • Auto-generation of ROS packages

OpEn is ideal for:

  • Embedded nonlinear nonlinear model predictive control and moving horizon estimation
  • Robotics and advanced manufacturing
  • Autonomous (aerial/ground) vehicles

Demos

Code generation

Code generation? Piece of cake!

OpEn generates parametric optimizer modules in Rust - it's blazingly fast - it's safe - it can run on embedded devices.

You can use the Python interface of OpEn, opengen, to generate Rust code for your parametric optimizer. The optimizer can then be called from Python, used as a solver in Rust, consumed as a service over TCP (even remotely), or used in ROS on your robot.

Easy Code Generation

You can generate a parametric optimizer in just very few lines of code and in no time.

OpEn allows application developers and researchers to focus on the challenges of the application, rather than the tedious task of solving the associated parametric optimization problems (as in nonlinear model predictive control).

Embedded applications

OpEn can run on embedded devices; here we see it running on an intel Atom for the autonomous navigation of a lab-scale micro aerial vehicle - the controller runs at 20Hz using only 15% CPU!

Fast NMPC of MAV

Optimal Control

Optimal control problems can now be set up directly from their natural ingredients: dynamics, prediction horizon, stage cost, terminal cost, and constraints. You can easily choose between a single or multiple shooting formulation. The solver is parametrizable, so you don't have to recompile whenever a parameter changes.

OpEn allows to solve optimal control problems of the form

image

Here is a minimal Python example (try it in Google Colab):

Try it In Colab

import opengen as og
import casadi.casadi as cs

ocp = og.ocp.OptimalControlProblem(nx=2, nu=1, horizon=20)
ocp.add_parameter("x0", 2)
ocp.add_parameter("xref", 2, default=[0.0, 0.0])

ocp.with_dynamics(
  lambda x, u, p: 
    cs.vertcat(x[1], -x[0] + u[0]))

ocp.with_stage_cost(
  lambda x, u, p, t: 
    cs.dot(x - p["xref"], x - p["xref"]) 
    + 0.1 * cs.dot(u, u))

ocp.with_terminal_cost(
  lambda x, p: 
    10.0 * cs.dot(x - p["xref"], x - p["xref"]))

ocp.with_input_constraints(og.constraints.BallInf(radius=1.))

optimizer = og.ocp.OCPBuilder(
    ocp, 
    meta=..., 
    build_config=..., 
    solver_config=...).build()

result = optimizer.solve(x0=[1.0, 0.0], xref=[0.0, 0.0])

See the dedicated OCP documentation for more details and examples.

General Parametric Optimization

OpEn can solve nonconvex parametric optimization problems of the general form

standard parametric optimziation problem

where f is a smooth cost, U is a simple - possibly nonconvex - set, F1 and F2 are nonlinear smooth mappings and C is a convex set (read more).

Code generation in Python in just a few lines of code (read the docs for details)

Open In Colab

import opengen as og
import casadi.casadi as cs

# Define variables
# ------------------------------------
u = cs.SX.sym("u", 5)
p = cs.SX.sym("p", 2)

# Define cost function and constraints
# ------------------------------------
phi = og.functions.rosenbrock(u, p)
f2 = cs.vertcat(1.5 * u[0] - u[1],
                cs.fmax(0.0, u[2] - u[3] + 0.1))
bounds = og.constraints.Ball2(None, 1.5)
problem = og.builder.Problem(u, p, phi) \
    .with_penalty_constraints(f2)       \
    .with_constraints(bounds)

# Configuration and code generation
# ------------------------------------
build_config = og.config.BuildConfiguration()  \
    .with_build_directory("python_test_build") \
    .with_tcp_interface_config()
meta = og.config.OptimizerMeta()
solver_config = og.config.SolverConfiguration()    \
    .with_tolerance(1e-5)                          \
    .with_constraints_tolerance(1e-4)
builder = og.builder.OpEnOptimizerBuilder(problem, meta,
                                          build_config, solver_config)
builder.build()

Use the Solver in Rust

You can also use OpEn directly in Rust by defining a cost function, its gradient, and a set of constraints, then solving the problem.

Here is a minimal Rust example:

use optimization_engine::{constraints, panoc::*, Problem, SolverError};

let f = |u: &[f64], c: &mut f64| -> Result<(), SolverError> {
    *c = (1.0 - u[0]).powi(2) + 200.0 * (u[1] - u[0].powi(2)).powi(2);
    Ok(())
};

let df = |u: &[f64], grad: &mut [f64]| -> Result<(), SolverError> {
    grad[0] = 2.0 * (u[0] - 1.0) - 800.0 * u[0] * (u[1] - u[0].powi(2));
    grad[1] = 400.0 * (u[1] - u[0].powi(2));
    Ok(())
};

let bounds = constraints::Ball2::new(None, 1.0);
let problem = Problem::new(&bounds, df, f);
let mut cache = PANOCCache::new(2, 1e-8, 10);
let mut optimizer = PANOCOptimizer::new(problem, &mut cache).with_max_iter(100);
let mut u = [-1.5, 0.9];
let status = optimizer.solve(&mut u)?;

See the dedicated Rust documentation for a full introduction and more complete examples. See more Rust examples here.

Check out next...

Contact us

Do you like OpEn?

Show us with a star on github...

Star

License

OpEn is a free open source project. You can use it under the terms of either Apache license v2.0 or MIT license.

Core Team

Pantelis Sopasakis Emil Fresk

Contributions

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Before you contribute to Optimization Engine, please read our contributing guidelines.

A list of contributors is automatically generated by github here.

Citing OpEn

Please, cite OpEn as follows (arXiv version):

@inproceedings{open2020,
  author="P. Sopasakis and E. Fresk and P. Patrinos",
  title="{OpEn}: Code Generation for Embedded Nonconvex Optimization",
  booktitle="IFAC World Congress",
  year="2020",
  address="Berlin"
}

Extension points exported contracts — how you extend this code

Constraint (Interface)
A set which can be used as a constraint This trait defines an abstract function that allows to compute projections on s [16 …
rust/src/constraints/mod.rs
Optimizer (Interface)
A general optimizer [2 implementers]
rust/src/core/mod.rs
AlgorithmEngine (Interface)
Engine supporting an algorithm An engine is responsible for the allocation of memory for an algorithm, especially memor [2 …
rust/src/core/mod.rs
CostFunction (Interface)
Trait alias (type aliases are not stable yet, so the alternative is to use the following trait definition, i.e., to "ext [1 …
rust/src/alm/tests.rs

Core symbols most depended-on inside this repo

project
called by 110
rust/src/constraints/soc.rs
with_optimizer_name
called by 26
python/opengen/config/meta.py
add_constraint
called by 26
rust/src/constraints/cartesian_product.rs
size
called by 24
python/opengen/ocp/parameter.py
norm2_squared
called by 24
rust/src/matrix_operations.rs
with_open_version
called by 22
python/opengen/config/build_config.py
add_parameter
called by 20
python/opengen/ocp/problem.py
norm2
called by 19
rust/src/matrix_operations.rs

Shape

Method 834
Function 463
Class 99
Enum 7
Interface 4

Languages

Python53%
Rust41%
C3%
C++1%
TypeScript1%

Modules by API surface

rust/src/constraints/tests.rs146 symbols
python/test/test.py88 symbols
python/test/test_constraints.py69 symbols
python/test/test_ocp.py57 symbols
rust/src/alm/alm_optimizer.rs41 symbols
python/opengen/builder/optimizer_builder.py41 symbols
python/test/test_ros2.py37 symbols
rust/src/core/panoc/panoc_engine.rs35 symbols
python/opengen/ocp/problem.py32 symbols
python/opengen/config/solver_config.py31 symbols
python/opengen/ocp/builder.py30 symbols
rust/src/mocks.rs27 symbols

For agents

$ claude mcp add optimization-engine \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact