|
| 1 | +--- |
| 2 | +id: matlab-api |
| 3 | +title: MATLAB API |
| 4 | +description: TCP-based MATLAB API for parametric and OCP-generated OpEn optimizers |
| 5 | +--- |
| 6 | + |
| 7 | +# MATLAB API |
| 8 | + |
| 9 | +This page documents the current MATLAB API of OpEn. |
| 10 | + |
| 11 | +The current MATLAB interface lives in `matlab/api` and communicates with |
| 12 | +optimizers that were generated in Python and expose a TCP server. |
| 13 | + |
| 14 | +If you are looking for the older MATLAB code-generation workflow, see the |
| 15 | +[legacy MATLAB interface](./matlab-interface) and the |
| 16 | +[MATLAB examples](./matlab-examples). |
| 17 | + |
| 18 | +## Overview |
| 19 | + |
| 20 | +The current MATLAB API supports: |
| 21 | + |
| 22 | +- TCP-based calls to standard parametric optimizers |
| 23 | +- TCP-based calls to OCP-generated optimizers created with `opengen.ocp` |
| 24 | +- Loading `optimizer_manifest.json` for OCP optimizers |
| 25 | +- Automatic endpoint discovery from the sibling `optimizer.yml` |
| 26 | +- Warm-start inputs: |
| 27 | + - `InitialGuess` |
| 28 | + - `InitialLagrangeMultipliers` |
| 29 | + - `InitialPenalty` |
| 30 | +- Health-check and shutdown operations through `ping()` and `kill()` |
| 31 | + |
| 32 | +The main MATLAB entry points are: |
| 33 | + |
| 34 | +- `OpEnTcpOptimizer` |
| 35 | +- `createOpEnTcpOptimizer` |
| 36 | + |
| 37 | +:::important |
| 38 | +The MATLAB API is a TCP client only. It does **not** start the optimizer |
| 39 | +server; the generated optimizer must already be running. |
| 40 | +::: |
| 41 | + |
| 42 | +## Getting Started |
| 43 | + |
| 44 | +Add the MATLAB API directory to your path: |
| 45 | + |
| 46 | +```matlab |
| 47 | +addpath(fullfile(pwd, 'matlab', 'api')); |
| 48 | +``` |
| 49 | + |
| 50 | +Create a client using either: |
| 51 | + |
| 52 | +- `OpEnTcpOptimizer(port)` |
| 53 | +- `OpEnTcpOptimizer(port, ip)` |
| 54 | +- `OpEnTcpOptimizer(ip, port)` |
| 55 | + |
| 56 | +For example: |
| 57 | + |
| 58 | +```matlab |
| 59 | +client = OpEnTcpOptimizer(3301); |
| 60 | +pong = client.ping(); |
| 61 | +disp(pong.Pong); |
| 62 | +``` |
| 63 | + |
| 64 | +## Parametric Optimizers |
| 65 | + |
| 66 | +For a standard parametric optimizer, call `solve` with the flat parameter |
| 67 | +vector: |
| 68 | + |
| 69 | +```matlab |
| 70 | +client = OpEnTcpOptimizer(3301); |
| 71 | +
|
| 72 | +response = client.solve([2.0, 10.0]); |
| 73 | +
|
| 74 | +if response.ok |
| 75 | + disp(response.solution); |
| 76 | + disp(response.cost); |
| 77 | + disp(response.exit_status); |
| 78 | +else |
| 79 | + error('OpEn:SolverError', '%s', response.message); |
| 80 | +end |
| 81 | +``` |
| 82 | + |
| 83 | +You can also provide warm-start data: |
| 84 | + |
| 85 | +```matlab |
| 86 | +response1 = client.solve([2.0, 10.0]); |
| 87 | +
|
| 88 | +response2 = client.solve( ... |
| 89 | + [2.0, 10.0], ... |
| 90 | + 'InitialGuess', response1.solution, ... |
| 91 | + 'InitialLagrangeMultipliers', response1.lagrange_multipliers, ... |
| 92 | + 'InitialPenalty', response1.penalty); |
| 93 | +``` |
| 94 | + |
| 95 | +To stop the server gracefully: |
| 96 | + |
| 97 | +```matlab |
| 98 | +client.kill(); |
| 99 | +``` |
| 100 | + |
| 101 | +## OCP Optimizers |
| 102 | + |
| 103 | +For OCP-generated optimizers, the MATLAB API uses the optimizer manifest to |
| 104 | +pack named parameter blocks into the flat parameter vector expected by the |
| 105 | +underlying TCP solver. |
| 106 | + |
| 107 | +This matches the Python `GeneratedOptimizer.solve(...)` workflow conceptually, |
| 108 | +but in MATLAB you pass the values as **name-value pairs**. |
| 109 | + |
| 110 | +### Loading an OCP Manifest |
| 111 | + |
| 112 | +If `optimizer_manifest.json` and `optimizer.yml` are in the same generated |
| 113 | +optimizer directory, MATLAB can read the TCP endpoint automatically: |
| 114 | + |
| 115 | +```matlab |
| 116 | +manifestPath = fullfile( ... |
| 117 | + pwd, ... |
| 118 | + 'open-codegen', ... |
| 119 | + '.python_test_build_ocp', ... |
| 120 | + 'ocp_single_tcp', ... |
| 121 | + 'optimizer_manifest.json'); |
| 122 | +
|
| 123 | +client = OpEnTcpOptimizer('ManifestPath', manifestPath); |
| 124 | +disp(client.parameterNames()); |
| 125 | +``` |
| 126 | + |
| 127 | +You can also override the TCP endpoint explicitly: |
| 128 | + |
| 129 | +```matlab |
| 130 | +client = OpEnTcpOptimizer(3391, 'ManifestPath', manifestPath); |
| 131 | +``` |
| 132 | + |
| 133 | +### Single-Shooting OCP Example |
| 134 | + |
| 135 | +The following example uses a generated OCP optimizer whose manifest defines |
| 136 | +the parameter blocks `x0` and `xref`: |
| 137 | + |
| 138 | +```matlab |
| 139 | +response = client.solve( ... |
| 140 | + 'x0', [1.0, -1.0], ... |
| 141 | + 'xref', [0.0, 0.0]); |
| 142 | +
|
| 143 | +if response.ok |
| 144 | + disp(response.solution); |
| 145 | + disp(response.inputs); |
| 146 | + disp(response.exit_status); |
| 147 | +else |
| 148 | + error('OpEn:SolverError', '%s', response.message); |
| 149 | +end |
| 150 | +``` |
| 151 | + |
| 152 | +If some manifest parameters have defaults, only the required ones need to be |
| 153 | +provided: |
| 154 | + |
| 155 | +```matlab |
| 156 | +manifestPath = fullfile( ... |
| 157 | + pwd, ... |
| 158 | + 'open-codegen', ... |
| 159 | + '.python_test_build_ocp', ... |
| 160 | + 'ocp_manifest_bindings', ... |
| 161 | + 'optimizer_manifest.json'); |
| 162 | +
|
| 163 | +client = OpEnTcpOptimizer('ManifestPath', manifestPath); |
| 164 | +response = client.solve('x0', [1.0, 0.0]); |
| 165 | +``` |
| 166 | + |
| 167 | +### Multiple-Shooting OCP Example |
| 168 | + |
| 169 | +For multiple-shooting OCPs, the MATLAB response also contains the reconstructed |
| 170 | +state trajectory: |
| 171 | + |
| 172 | +```matlab |
| 173 | +manifestPath = fullfile( ... |
| 174 | + pwd, ... |
| 175 | + 'open-codegen', ... |
| 176 | + '.python_test_build_ocp', ... |
| 177 | + 'ocp_multiple_tcp', ... |
| 178 | + 'optimizer_manifest.json'); |
| 179 | +
|
| 180 | +client = OpEnTcpOptimizer('ManifestPath', manifestPath); |
| 181 | +
|
| 182 | +response = client.solve( ... |
| 183 | + 'x0', [1.0, -1.0], ... |
| 184 | + 'xref', [0.0, 0.0]); |
| 185 | +
|
| 186 | +disp(response.inputs); |
| 187 | +disp(response.states); |
| 188 | +``` |
| 189 | + |
| 190 | +### OCP Warm-Start Example |
| 191 | + |
| 192 | +Warm-start options can be combined with the named OCP parameters: |
| 193 | + |
| 194 | +```matlab |
| 195 | +response1 = client.solve( ... |
| 196 | + 'x0', [1.0, -1.0], ... |
| 197 | + 'xref', [0.0, 0.0]); |
| 198 | +
|
| 199 | +response2 = client.solve( ... |
| 200 | + 'x0', [1.0, -1.0], ... |
| 201 | + 'xref', [0.0, 0.0], ... |
| 202 | + 'InitialGuess', response1.solution, ... |
| 203 | + 'InitialLagrangeMultipliers', response1.lagrange_multipliers, ... |
| 204 | + 'InitialPenalty', response1.penalty); |
| 205 | +``` |
| 206 | + |
| 207 | +## Response Format |
| 208 | + |
| 209 | +All successful solver calls return the low-level solver fields produced by the |
| 210 | +TCP server, together with a MATLAB-friendly `ok` flag. |
| 211 | + |
| 212 | +Typical fields include: |
| 213 | + |
| 214 | +- `ok` |
| 215 | +- `solution` |
| 216 | +- `cost` |
| 217 | +- `exit_status` |
| 218 | +- `solve_time_ms` |
| 219 | +- `penalty` |
| 220 | +- `num_outer_iterations` |
| 221 | +- `num_inner_iterations` |
| 222 | +- `last_problem_norm_fpr` |
| 223 | +- `f1_infeasibility` |
| 224 | +- `f2_norm` |
| 225 | +- `lagrange_multipliers` |
| 226 | + |
| 227 | +For OCP optimizers, the response also includes: |
| 228 | + |
| 229 | +- `packed_parameter` |
| 230 | +- `inputs` |
| 231 | +- `states` for multiple-shooting formulations |
| 232 | + |
| 233 | +## Related Pages |
| 234 | + |
| 235 | +- [Python TCP/IP interface](./python-tcp-ip) |
| 236 | +- [Python OCP guide](./python-ocp-1) |
| 237 | +- [Legacy MATLAB interface](./matlab-interface) |
| 238 | +- [MATLAB examples](./matlab-examples) |
0 commit comments