Skip to content

Commit a390454

Browse files
simplified API for development
1 parent 7824e88 commit a390454

5 files changed

Lines changed: 28 additions & 18 deletions

examples/cartpole_example_code_generation.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
N = 20
1616

1717
prob = tinympc.TinyMPC()
18-
prob.setup(A, B, Q, R, N, rho=1, max_iter=10)
1918

20-
# Optional bounds via new API
2119
u_min = np.array([-0.5])
2220
u_max = np.array([0.5])
23-
prob.set_bound_constraints([], [], u_min, u_max)
21+
prob.setup(A, B, Q, R, N, rho=1, max_iter=10, u_min=u_min, u_max=u_max)
2422

2523
prob.codegen("out", verbose=1)

examples/cartpole_example_mpc_reference_constrained.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@
1818
N = 20
1919

2020
prob = tinympc.TinyMPC()
21-
prob.setup(A, B, Q, R, N, rho=1.0)
2221

2322
# Define input constraints
2423
u_min = np.array([-0.45])
2524
u_max = np.array([0.45])
26-
prob.set_bound_constraints([], [], u_min, u_max)
25+
prob.setup(A, B, Q, R, N, rho=1.0, u_min=u_min, u_max=u_max)
2726

2827
# Goal must be another equilibrium position
2928
prob.set_x_ref(np.array([1.0, 0, 0, 0]))

examples/quadrotor_hover_code_generation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import autograd.numpy as anp
55

66
# Toggle switch for adaptive rho
7-
ENABLE_ADAPTIVE_RHO = False # Set to True to enable adaptive rho
7+
ENABLE_ADAPTIVE_RHO = False # Set to True to enable adaptive rho
88

99
# Quadrotor system matrices (12 states, 4 inputs)
1010
rho_value = 5.0

examples/rocket_landing_constraints.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,16 @@
5151
qcu = np.array([3]) # dimensions for input cones
5252

5353
# Setup solver
54+
cone_constraints = {
55+
'Acu': Acu, 'qcu': qcu, 'cu': cu,
56+
'Acx': Acx, 'qcx': qcx, 'cx': cx
57+
}
58+
5459
solver = tinympc.TinyMPC()
5560
solver.setup(A, B, Q, R, NHORIZON, rho=1.0, fdyn=fdyn,
56-
max_iter=100, abs_pri_tol=2e-3, verbose=True)
57-
58-
# Set bounds explicitly
59-
solver.set_bound_constraints(x_min, x_max, u_min, u_max)
60-
61-
# Set cone constraints (inputs first)
62-
solver.set_cone_constraints(Acu, qcu, cu, Acx, qcx, cx)
61+
max_iter=100, abs_pri_tol=2e-3, verbose=True,
62+
x_min=x_min, x_max=x_max, u_min=u_min, u_max=u_max,
63+
cone_constraints=cone_constraints)
6364

6465
# Initial and goal states
6566
xinit = np.array([4.0, 2.0, 20.0, -3.0, 2.0, -4.5])

src/tinympc/interface.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,23 @@ def expand_ndarray(self, array_, expected_rows, expected_cols, fallback):
8181
return array_
8282

8383
# Setup the problem data and solver options
84-
def setup(self, A, B, Q, R, N, rho=1.0, fdyn=None, verbose=False, **settings):
84+
def setup(self, A, B, Q, R, N, rho=1.0, fdyn=None,
85+
x_min=None, x_max=None, u_min=None, u_max=None,
86+
cone_constraints=None, verbose=False, **settings):
8587
"""Instantiate necessary algorithm variables and parameters
8688
8789
:param A (np.ndarray): State transition matrix of the linear system, size nx x nx
8890
:param B (np.ndarray): Input matrix of the linear system, size nx x nu
8991
:param Q (np.ndarray): Stage cost for state, must be diagonal and positive semi-definite, size nx x nx
9092
:param R (np.ndarray): Stage cost for input, must be diagonal and positive definite, size nu x nu
93+
:param N (int): Prediction horizon length
9194
:param rho (float, optional): Penalty term used in ADMM, default 1.0
9295
:param fdyn (np.ndarray or None, optional): Affine offset vector for dynamics, size nx x 1. If None, defaults to zeros (linear system), default None
93-
:param x_min (list[float] or None, optional): Lower bound state constraints of the same length as nx, default None
94-
:param x_max (list[float] or None, optional): Upper bound state constraints of the same length as nx, default None
95-
:param u_min (list[float] or None, optional): Lower bound input constraints of the same length as nu, default None
96-
:param u_max (list[float] or None, optional): Upper bound input constraints of the same length as nu, default None
96+
:param x_min (np.ndarray or None, optional): Lower bound state constraints, size nx or nx x N, default None
97+
:param x_max (np.ndarray or None, optional): Upper bound state constraints, size nx or nx x N, default None
98+
:param u_min (np.ndarray or None, optional): Lower bound input constraints, size nu or nu x (N-1), default None
99+
:param u_max (np.ndarray or None, optional): Upper bound input constraints, size nu or nu x (N-1), default None
100+
:param cone_constraints (dict or None, optional): Cone constraints dict with keys {Acu, qcu, cu, Acx, qcx, cx}, default None
97101
:param verbose (bool): Whether or not to print data to console during setup, default False
98102
:params settings: Dictionary of optional settings
99103
:param abs_pri_tol (float): Solution tolerance for primal variables
@@ -155,6 +159,14 @@ def setup(self, A, B, Q, R, N, rho=1.0, fdyn=None, verbose=False, **settings):
155159
self.nx, self.nu, self.N,
156160
self.settings, self.verbose)
157161

162+
# Handle constraints if provided
163+
if any(x is not None for x in [x_min, x_max, u_min, u_max]):
164+
self.set_bound_constraints(x_min, x_max, u_min, u_max)
165+
166+
if cone_constraints is not None:
167+
# cone_constraints should be a dict with keys: Acu, qcu, cu, Acx, qcx, cx
168+
self.set_cone_constraints(**cone_constraints)
169+
158170
def set_x0(self, x0):
159171
assert len(x0.shape) == 1
160172
assert len(x0) == self.nx

0 commit comments

Comments
 (0)