This directory contains the MATLAB interface of Optimization Engine (OpEn).
The current MATLAB API lives in matlab/api. It communicates with
optimizers generated in Python that expose a TCP server interface.
The legacy MATLAB code is preserved in matlab/legacy.
The current MATLAB toolbox supports:
- Connecting to TCP-enabled optimizers generated in Python
- Calling standard parametric optimizers using a flat parameter vector
- Calling OCP-generated optimizers using named parameter blocks from
optimizer_manifest.json - Loading OCP manifests and, when available, automatically reading the TCP
endpoint from the sibling
optimizer.yml - Sending
pingandkillrequests to the optimizer server - Providing optional warm-start data through:
InitialGuessInitialLagrangeMultipliersInitialPenalty
- Returning normalized solver responses with an
okflag and solver diagnostics - Returning stage-wise
inputsfor OCP optimizers andstatesfor multiple-shooting OCP optimizers
The main entry points are:
Add the MATLAB API folder to your path:
addpath(fullfile(pwd, 'matlab', 'api'));Make sure the target optimizer TCP server is already running.
Use a TCP port directly. The IP defaults to 127.0.0.1.
client = OpEnTcpOptimizer(3301);
pong = client.ping();
disp(pong.Pong);You can also specify the endpoint explicitly:
client = OpEnTcpOptimizer('127.0.0.1', 3301);For a standard parametric optimizer, pass the flat parameter vector:
response = client.solve([2.0, 10.0]);
if response.ok
disp(response.solution);
disp(response.cost);
else
error('OpEn:SolverError', '%s', response.message);
endresponse1 = client.solve([2.0, 10.0]);
response2 = client.solve( ...
[2.0, 10.0], ...
'InitialGuess', response1.solution, ...
'InitialLagrangeMultipliers', response1.lagrange_multipliers, ...
'InitialPenalty', response1.penalty);client.kill();For OCP-generated optimizers, MATLAB uses name-value pairs to provide the
parameter blocks listed in optimizer_manifest.json.
If optimizer_manifest.json and optimizer.yml are in the same generated
optimizer directory, the client can infer the TCP endpoint automatically:
manifestPath = fullfile( ...
pwd, ...
'python', ...
'.python_test_build_ocp', ...
'ocp_single_tcp', ...
'optimizer_manifest.json');
client = OpEnTcpOptimizer('ManifestPath', manifestPath);
disp(client.parameterNames());You can also override the endpoint explicitly:
client = OpEnTcpOptimizer(3391, 'ManifestPath', manifestPath);The following example matches the OCP manifest in
python/.python_test_build_ocp/ocp_single_tcp:
response = client.solve( ...
'x0', [1.0, -1.0], ...
'xref', [0.0, 0.0]);
if response.ok
disp(response.solution);
disp(response.inputs);
disp(response.exit_status);
else
error('OpEn:SolverError', '%s', response.message);
endIf the manifest defines default values for some parameters, you only need to provide the required ones:
manifestPath = fullfile( ...
pwd, ...
'python', ...
'.python_test_build_ocp', ...
'ocp_manifest_bindings', ...
'optimizer_manifest.json');
client = OpEnTcpOptimizer('ManifestPath', manifestPath);
response = client.solve('x0', [1.0, 0.0]);For multiple-shooting OCPs, the MATLAB client also returns the state trajectory reconstructed from the manifest slices:
manifestPath = fullfile( ...
pwd, ...
'python', ...
'.python_test_build_ocp', ...
'ocp_multiple_tcp', ...
'optimizer_manifest.json');
client = OpEnTcpOptimizer('ManifestPath', manifestPath);
response = client.solve( ...
'x0', [1.0, -1.0], ...
'xref', [0.0, 0.0]);
disp(response.inputs);
disp(response.states);Warm-start options can be combined with named OCP parameters:
response1 = client.solve( ...
'x0', [1.0, -1.0], ...
'xref', [0.0, 0.0]);
response2 = client.solve( ...
'x0', [1.0, -1.0], ...
'xref', [0.0, 0.0], ...
'InitialGuess', response1.solution, ...
'InitialLagrangeMultipliers', response1.lagrange_multipliers, ...
'InitialPenalty', response1.penalty);- The MATLAB API does not start the optimizer server; it connects to a server that is already running.
- For plain parametric optimizers, use
client.solve(p). - For OCP optimizers, use
client.solve('name1', value1, 'name2', value2, ...). - The helper function
createOpEnTcpOptimizer(...)is a thin wrapper aroundOpEnTcpOptimizer(...).