problem
Core optimization problem interface for trajectory optimization.
This module provides the Problem class, the main entry point for defining and solving trajectory optimization problems using Sequential Convex Programming (SCP).
Example
The prototypical flow is to define a problem, then initialize, solve, and post-process the results::
problem = Problem(dynamics, constraints, states, controls, N, time)
problem.initialize()
result = problem.solve()
result = problem.post_process()
For batched solves over many initial conditions::
results = problem.solve_batched(parameters={"initial_position": ic_batch})
results = problem.post_process_batched(results)
Problem
¶
Source code in openscvx/problem.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 | |
algorithm: Algorithm
property
¶
Access the SCP algorithm instance.
Returns:
| Type | Description |
|---|---|
Algorithm
|
The algorithm instance (e.g., PenalizedTrustRegion). |
discretizer: Discretizer
property
¶
Access the discretizer instance.
Attributes such as dis_type, ode_solver, and diffrax_kwargs
can be modified freely before initialize is called:
problem.discretizer.dis_type = "ZOH"
problem.discretizer.ode_solver = "Dopri8"
problem.discretizer.diffrax_kwargs = {"num_substeps": 100}
problem.initialize()
Warning
Discretizer settings are compiled into the JIT-cached solver
during initialize. Changes made after
initialize() will have no effect on subsequent solves.
Returns:
| Type | Description |
|---|---|
Discretizer
|
The discretizer instance (e.g., |
dynamics: dict
property
¶
The user-authored dynamics dict, {state_name: expr}.
This is the problem as written, before the solver augments it with a
time-dilation control and per-CTCS penalty states. Handy for rendering
or inspecting a single equation, e.g. ox.to_latex(problem.dynamics["v"]).
Returns:
| Type | Description |
|---|---|
dict
|
A dict mapping each user state name to its symbolic time derivative. |
history: Optional[AlgorithmHistory]
property
¶
Access the CPU-side iteration history (trajectories, weights, costs).
Companion to attr:
state — :class:AlgorithmState carries the
current-iterate pytree, while :class:AlgorithmHistory carries the
append-only per-iteration log.
lowered: LoweredProblem
property
¶
Access the lowered problem containing JAX/CVXPy objects.
Returns:
| Type | Description |
|---|---|
LoweredProblem
|
LoweredProblem with dynamics, constraints, unified interfaces, and CVXPy vars |
parameters: ParameterDict
property
writable
¶
Get the parameters dictionary.
The returned dictionary automatically syncs to CVXPy when modified
problem.parameters["obs_radius"] = 2.0 # Auto-syncs to CVXPy problem.parameters.update({"gate_0_center": center}) # Also syncs
Returns:
| Name | Type | Description |
|---|---|---|
ParameterDict |
ParameterDict
|
Special dict that syncs to CVXPy on assignment. |
slices: dict[str, slice]
property
¶
Get mapping of state and control names to their slices in unified vectors.
This property returns a dictionary mapping each state and control variable name to its slice in the respective unified vector. This is particularly useful for expert users working with byof (bring-your-own functions) who need to manually index into the unified x and u vectors.
Returns:
| Type | Description |
|---|---|
dict[str, slice]
|
Dictionary mapping variable names to slice objects. State variables map to slices in the x vector. Control variables map to slices in the u vector. |
Example
Usage with byof::
problem = ox.Problem(dynamics, states, controls, ...)
print(problem.slices)
# {'position': slice(0, 3), 'velocity': slice(3, 6), 'theta': slice(0, 1)}
# Use in byof functions
byof = {
"nodal_constraints": [
lambda x, u, node, params: x[problem.slices["velocity"][0]] - 10.0,
lambda x, u, node, params: u[problem.slices["theta"][0]] - 1.57,
]
}
solver: ConvexSolver
property
¶
Access the convex subproblem solver instance.
Backend-specific attributes (e.g. cvx_solver, solver_args,
cvxpygen, cvxpygen_override on :class:CVXPyPTRSolver) can
be modified freely before initialize is called::
problem.solver.solver_args = {"abstol": 1e-6, "reltol": 1e-9}
problem.solver.cvxpygen = True
problem.initialize()
Warning
Solver settings are compiled into the solve function during
initialize(). Changes made after initialize()
will have no effect on subsequent solves.
Returns:
| Type | Description |
|---|---|
ConvexSolver
|
The solver instance — a concrete :class: |
state: Optional[AlgorithmState]
property
¶
Access the current solver state.
The solver state contains all mutable state from the SCP iterations, including current guesses, costs, weights, and history.
Returns:
| Type | Description |
|---|---|
Optional[AlgorithmState]
|
AlgorithmState if initialized, None otherwise |
Example
When using Problem.step() can use the state to check convergence etc.
problem.initialize()
problem.step()
print(f"Iteration {problem.state.k}, J_tr={problem.state.J_tr}")
u_unified
property
¶
Unified control interface (delegates to lowered.u_unified).
x_unified
property
¶
Unified state interface (delegates to lowered.x_unified).
__init__(dynamics: Union[dict, DynamicsAdapter], constraints: List[Union[Constraint, CTCS]], states: List[State], controls: List[Control], N: int, time: Time, *, dynamics_discrete: Optional[dict] = None, dynamics_prop: Optional[dict] = None, states_prop: Optional[List[State]] = None, algebraic_prop: Optional[dict] = None, licq_max: float = 0.0001, algorithm: Optional[Union[Algorithm, dict]] = None, discretizer: Optional[Union[Discretizer, dict]] = None, solver: Optional[Union[ConvexSolver, dict]] = None, byof: Optional[Union[ByofSpec, dict]] = None, float_dtype: str = 'float32')
¶
The primary class in charge of compiling and exporting the solvers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynamics
|
Union[dict, DynamicsAdapter]
|
Dictionary mapping state names to their dynamics
expressions. Each key should be a state name, and each value
should be an |
required |
constraints
|
List[Union[CTCSConstraint, NodalConstraint]]
|
List of constraints decorated with @ctcs or @nodal |
required |
states
|
List[State]
|
List of State objects representing the state variables. May optionally include a State named "time" (see time parameter below). |
required |
controls
|
List[Control]
|
List of Control objects representing the control variables |
required |
N
|
int
|
Number of segments in the trajectory |
required |
time
|
Time
|
Time configuration object with initial, final, min, max. Required. If including a "time" state in states, the Time object will be ignored and time properties should be set on the time State object instead. |
required |
dynamics_prop
|
dict
|
Dictionary mapping EXTRA state names to their dynamics expressions for propagation. Only specify additional states beyond optimization states (e.g., {"distance": speed}). Do NOT duplicate optimization state dynamics here. |
None
|
states_prop
|
List[State]
|
List of EXTRA State objects for propagation only. Only specify additional states beyond optimization states. Used with dynamics_prop. |
None
|
algebraic_prop
|
dict
|
Dictionary mapping names to symbolic expressions for outputs evaluated (not integrated) during propagation. |
None
|
licq_max
|
float
|
Problem-wide upper bound for CTCS augmented states
(default: 1e-4). Serves as the default for all constraint
groups; a |
0.0001
|
algorithm
|
Optional[Union[Algorithm, dict]]
|
SCP algorithm configuration. Accepts:
The When a dict is provided, every state that has a
minimize/maximize objective must have an entry. Dict
values may be scalars (broadcast to all components) or
arrays matching the state's shape. States without
objectives are automatically assigned weight 0. The
dict is expanded to an array of shape Examples:: |
None
|
discretizer
|
Optional[Union[Discretizer, dict]]
|
Discretization method configuration. Accepts:
Examples:: |
None
|
solver
|
Optional[Union[ConvexSolver, dict]]
|
Convex subproblem solver configuration. Accepts:
Examples:: |
None
|
byof
|
ByofSpec
|
Expert mode only. Raw JAX functions to
bypass symbolic layer. See :class: |
None
|
float_dtype
|
str
|
Default floating-point dtype for JAX lowering.
Must be |
'float32'
|
Note
There are two approaches for handling time: 1. Auto-create (simple): Don't include "time" in states, provide Time object 2. User-provided (for time-dependent constraints): Include "time" State in states and in dynamics dict, don't provide Time object
Source code in openscvx/problem.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 | |
citation() -> str
¶
Return BibTeX citations for all components used in this problem.
Aggregates citations from the algorithm, its autotuner, the convex solver, the discretization, and every symbolic node in the problem's expression graphs (e.g. GMSR STL operators, jaxlie-backed Lie-group maps). Each section is prefixed with a comment indicating which component the citation is for.
Returns:
| Type | Description |
|---|---|
str
|
Formatted string containing all BibTeX citations with comments. |
Example
Print all citations for a problem::
problem = Problem(dynamics, constraints, states, controls, N, time)
print(problem.citation())
Source code in openscvx/problem.py
2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 | |
initialize()
¶
Compile dynamics, constraints, and solvers; prepare for optimization.
This method vmaps dynamics, JIT-compiles constraints, builds the convex
subproblem, and initializes the solver state. Must be called before solve().
The solver state is seeded from the current .guess and .initial/.final
on the State/Control objects; to re-seed after initialize(), use :meth:reset.
Example
Prior to calling the .solve() method it is necessary to initialize the problem
problem = Problem(dynamics, constraints, states, controls, N, time)
problem.initialize() # Compile and prepare
problem.solve() # Run optimization
Source code in openscvx/problem.py
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 | |
post_process() -> OptimizationResults
¶
Propagate solution through full nonlinear dynamics for high-fidelity trajectory.
Integrates the converged SCP solution through the nonlinear dynamics to produce x_full, u_full, and t_full. Call after solve() for final results.
Propagation uses the parameter values recorded at solve time
(results.parameters), so mutating self.parameters between
:meth:solve and here only seeds the next solve.
Returns:
| Type | Description |
|---|---|
OptimizationResults
|
OptimizationResults with propagated trajectory fields |
Raises:
| Type | Description |
|---|---|
ValueError
|
If solve() has not been called yet. |
Source code in openscvx/problem.py
post_process_batched(results: OptimizationResults, *, n_times: Optional[int] = None) -> OptimizationResults
¶
Propagate all batch elements through the nonlinear dynamics.
Applies the same high-fidelity propagation as :meth:post_process to
every element of a batched :class:~openscvx.algorithms.OptimizationResults
returned by :meth:solve_batched. Results are stacked so that the
post-process fields carry a leading batch axis B:
results.t_full—(B, n_times)results.x_full—(B, n_times, n_prop_states)results.u_full—(B, n_times, n_controls)results.trajectory—{name: (B, n_times, ...)}results.cost—(B,)results.ctcs_violation—(B, ...)orNone
Propagation runs sequentially over the batch in a Python loop; each
element propagates with its own parameter values from the snapshot
:meth:solve_batched recorded on results.parameters. Results
without a snapshot (e.g. loaded from an older save) fall back to
self.parameters with a warning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
OptimizationResults
|
Batched :class: |
required |
n_times
|
Optional[int]
|
Number of interpolation points for the dense time grid.
When |
None
|
Returns:
| Type | Description |
|---|---|
OptimizationResults
|
The same |
OptimizationResults
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the problem has not been initialized yet. |
Source code in openscvx/problem.py
2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 | |
reset()
¶
Reset solver state to re-run optimization from initial conditions.
Creates fresh AlgorithmState while preserving compiled dynamics and solvers. Use this to run multiple optimizations without re-initializing.
This method automatically syncs
- Trajectory guesses from State/Control
.guessattributes - Boundary conditions from State
.initialand.finalattributes
Raises:
| Type | Description |
|---|---|
ValueError
|
If initialize() has not been called yet. |
Example
After calling .step() it may be necessary to reset the problem back to the initial
conditions
problem.initialize()
result1 = problem.step()
problem.reset()
result2 = problem.solve() # Fresh run with same setup
MPC with warm-starting from previous solution::
for measured_state in measurements:
# Update initial condition
pos.initial = measured_state[:3]
# Warm-start: shift previous solution as new guess
pos.guess = np.roll(prev_result.nodes["pos"], -1, axis=0)
problem.reset() # Syncs guesses and boundary conditions
result = problem.solve()
Source code in openscvx/problem.py
solve(max_iters: Optional[int] = None, time_limit: Optional[float] = None, continuous: bool = False) -> OptimizationResults
¶
Run the SCP algorithm until convergence or iteration limit.
Drives the fused iteration_fn body from a Python while
loop — real-time prints, wall-clock time_limit, continuous
mode, and the populated per-iteration history live here. For
batched solves, jax.jit compilation across calls, or
jax.grad through the solver, use :meth:solve_jax.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_iters
|
Optional[int]
|
Maximum iterations (default: algorithm.k_max) |
None
|
time_limit
|
Optional[float]
|
Wall-clock time limit in seconds. Overrides
|
None
|
continuous
|
bool
|
If True, run all iterations regardless of convergence |
False
|
Returns:
| Type | Description |
|---|---|
OptimizationResults
|
OptimizationResults with trajectory and convergence info
(call post_process() for full propagation). The parameter
values used for the solve are recorded on
|
Source code in openscvx/problem.py
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 | |
solve_batched(x_initial: Optional[jnp.ndarray] = None, x_final: Optional[jnp.ndarray] = None, parameters: Optional[dict] = None, *, x_guess: Optional[jnp.ndarray] = None, u_guess: Optional[jnp.ndarray] = None, max_iters: Optional[jnp.ndarray] = None, algorithm: Optional[dict] = None, in_axes: Optional[dict] = None) -> OptimizationResults
¶
Run B SCP solves in parallel as one batched XLA program.
Takes the same arguments as :meth:solve_jax, with one rule on top:
add a leading batch axis to whatever varies across solves. An
argument whose shape matches its unbatched shape is shared by every
batch element; an argument with exactly one extra leading axis is
batched along it. The batch size B is the common leading axis of
the batched entries, and every array leaf of the returned
:class:OptimizationResults carries it (e.g. result.x.shape ==
(B, N, n_x))::
results = problem.solve_batched(
x_initial=x0_batch, # (B, n_x): batched pins
parameters={
"gate_center": centers, # (B, 3) vs declared (3,) -> batched
"gate_radius": 0.5, # declared shape -> shared
},
algorithm={
"ep_tr": jnp.logspace(-5, -3, B), # (B,) vs field () -> tolerance sweep
"lam_prox": jnp.linspace(1.0, 4.0, B), # (B,) fill: one scalar per element
},
)
Rank against the declared shape decides — never the leading-axis
value — so a shared parameter whose leading axis happens to equal
B stays shared. For the rare entry the rank rule cannot express
(e.g. batching a declared rank-1 parameter element-wise), pass an
explicit in_axes.
Why this exists alongside jax.vmap(solve_jax): because the batch
axis is owned here the whole vmapped loop is a single function that
jax.export can serialize. Under save_compiled=True the artifact
is written to the solver cache on the first call and deserialized on
later processes — skipping the XLA compile that jax.vmap(solve_jax)
pays on every fresh launch. jax.vmap(solve_jax) is the right tool
for in-program batching that composes with grad/scan;
solve_batched is for when cross-process cold-start dominates.
Export requires a pure-JAX backend: under save_compiled=True the
CVXPy backend raises. The cache key invalidates on any change that
alters the exported loop — backend, algorithm, discretizer, scaling,
B, the shared/batched parameter split, and the JAX version — so a
stale artifact is never silently reused. Tolerances and max_iters
are runtime inputs on the state pytree, so one artifact serves every
setting of them.
What cannot be batched: N, the solver backend, the
discretizer/integrator, the autotuner type, and float_dtype
change array shapes or program structure, so B such solves can
never share one XLA program. There is deliberately no slot for them
here — sweep them with a Python loop over separately built
Problem instances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_initial
|
Optional[ndarray]
|
Initial-state boundary pin — shape |
None
|
x_final
|
Optional[ndarray]
|
Terminal-state boundary pin — |
None
|
parameters
|
Optional[dict]
|
Problem parameters for this solve, merged over
|
None
|
x_guess
|
Optional[ndarray]
|
State trajectory guess — |
None
|
u_guess
|
Optional[ndarray]
|
Control trajectory guess — |
None
|
max_iters
|
Optional[ndarray]
|
SCP iteration cap — |
None
|
algorithm
|
Optional[dict]
|
Per-solve algorithm knobs, by the same names as the
|
None
|
in_axes
|
Optional[dict]
|
Explicit batching override in |
None
|
Returns:
| Type | Description |
|---|---|
OptimizationResults
|
class: |
OptimizationResults
|
every leaf and empty per-iteration histories. The merged |
OptimizationResults
|
parameter values used for the solve are recorded on |
OptimizationResults
|
|
OptimizationResults
|
leading |
OptimizationResults
|
meth: |
OptimizationResults
|
own values. |
Source code in openscvx/problem.py
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 | |
solve_jax(x_initial: Optional[jnp.ndarray] = None, x_final: Optional[jnp.ndarray] = None, parameters: Optional[dict] = None, *, x_guess: Optional[jnp.ndarray] = None, u_guess: Optional[jnp.ndarray] = None, max_iters: Optional[int] = None, algorithm: Optional[dict] = None) -> OptimizationResults
¶
Run the SCP algorithm — JAX-pure.
Composes with jax.vmap and jax.jit. Drives the fused
iteration_fn inside a lax.while_loop rather than the Python
while loop that :meth:solve uses. Returns an
:class:OptimizationResults pytree built by
:meth:OptimizationResults.from_final_state — per-iteration history
(X/U lists past the final iterate, *_history fields,
discretization_history) is empty under this path, because list
growth doesn't fit inside lax.while_loop. For prints, wall-clock
time_limit, continuous mode, or populated history, use
:meth:solve.
With the CVXPy backend, jax.vmap(problem.solve_jax) runs B
sequential CVXPy solves (host CVXPy is not thread-safe); the QPAX
and Moreau backends run in parallel under vmap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_initial
|
Optional[ndarray]
|
Initial-state boundary pin, shape |
None
|
x_final
|
Optional[ndarray]
|
Terminal-state boundary pin, shape |
None
|
parameters
|
Optional[dict]
|
Problem parameters dict for the current solve. Use
this rather than mutating |
None
|
x_guess
|
Optional[ndarray]
|
Initial state trajectory guess, shape |
None
|
u_guess
|
Optional[ndarray]
|
Initial control trajectory guess, shape |
None
|
max_iters
|
Optional[int]
|
SCP iteration cap (scalar). Sugar for
|
None
|
algorithm
|
Optional[dict]
|
Per-solve algorithm knobs, by the same names as the
|
None
|
Returns:
| Type | Description |
|---|---|
OptimizationResults
|
class: |
OptimizationResults
|
empty histories. |
Source code in openscvx/problem.py
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 | |
step() -> dict
¶
Perform a single SCP iteration.
Designed for real-time plotting and interactive optimization. Performs one iteration including subproblem solve, state update, and progress emission.
Note
This method is NOT idempotent - it mutates internal state and advances the iteration counter. Use reset() to return to initial conditions.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Contains "converged" (bool) and current iteration state |
Example
Call .step() manually in a loop to control the algorithm directly
problem.initialize()
while not problem.step()["converged"]:
plot_trajectory(problem.state.trajs[-1])
Source code in openscvx/problem.py
sync()
¶
Sync parameters and boundary conditions to the solver.
Call this after modifying State.initial/final or parameters when using step() without reset(). This allows warm-starting from the previous solution while updating problem data.
Note
This is automatically called by solve() and reset(). Only needed when using step() directly with modified parameters or boundary conditions between iterations.
Example
MPC with warm-starting::
problem.initialize()
while running:
# Update initial condition from measurement
pos.initial = measured_state
problem.sync() # Sync without resetting algorithm state
# Continue from previous solution (warm-start)
for _ in range(max_iters):
if problem.step()["converged"]:
break
Source code in openscvx/problem.py
to_latex(*, dynamics: Literal['inline', 'separate'] = 'separate', constraints: Literal['inline', 'symbolic', 'separate'] = 'inline', weights: Literal['symbolic', 'numeric'] = 'symbolic') -> str
¶
Render the problem as a Mayer-form LaTeX formulation.
The rendered problem is the one you wrote — pre-augmentation, so no
time-dilation control or CTCS penalty states appear; CTCS constraints
show up as continuous-time path constraints. The result is a complete
display-math fragment — a subequations + align block (with
"separate" definitions as bare align blocks) that pastes into a
paper as-is, no $$ wrapping.
Dynamics render at one of two detail levels:
"separate"(default): the formulation carries\dot{x} = f(x, u)and the per-state\dot{x}_{i} = \ldotsdefinitions follow as their ownalignblock."inline": one\dot{x}_{i} = \ldotsrow per equation inside the formulation.
Constraints add a middle level:
"inline"(default): full constraint bodies with annotations."symbolic": numberedg_i(x, u) \le 0/h_j(x, u) = 0references only (with their\forall t/ node annotations)."separate": symbolic references plus an appended definition block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynamics
|
Literal['inline', 'separate']
|
Detail level for the dynamics section. |
'separate'
|
constraints
|
Literal['inline', 'symbolic', 'separate']
|
Detail level for the constraint section. |
'inline'
|
weights
|
Literal['symbolic', 'numeric']
|
How objective coefficients render. |
'symbolic'
|
Returns:
| Type | Description |
|---|---|
str
|
The formulation as a single LaTeX string; |
str
|
the definition blocks after it. |
Example
Default (separate dynamics, inline constraints, symbolic weights)::
print(problem.to_latex())
Everything expanded in a single block::
print(problem.to_latex(dynamics="inline"))
Substitute the tuned objective weights::
print(problem.to_latex(weights="numeric"))