Skip to content

Commit 981104b

Browse files
committed
use obc as standard prefix for control.optimal
1 parent 0422c82 commit 981104b

3 files changed

Lines changed: 21 additions & 21 deletions

File tree

examples/kincar-flatsys.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import matplotlib.pyplot as plt
1111
import control as ct
1212
import control.flatsys as fs
13-
import control.optimal as opt
13+
import control.optimal as obc
1414

1515
#
1616
# System model and utility functions
@@ -147,7 +147,7 @@ def plot_results(t, x, ud, rescale=True):
147147
basis = fs.PolyFamily(8)
148148

149149
# Define the cost function (penalize lateral error and steering)
150-
traj_cost = opt.quadratic_cost(
150+
traj_cost = obc.quadratic_cost(
151151
vehicle_flat, np.diag([0, 0.1, 0]), np.diag([0.1, 1]), x0=xf, u0=uf)
152152

153153
# Solve for an optimal solution
@@ -168,7 +168,7 @@ def plot_results(t, x, ud, rescale=True):
168168

169169
# Constraint the input values
170170
constraints = [
171-
opt.input_range_constraint(vehicle_flat, [8, -0.1], [12, 0.1]) ]
171+
obc.input_range_constraint(vehicle_flat, [8, -0.1], [12, 0.1]) ]
172172

173173
# TEST: Change the basis to use B-splines
174174
basis = fs.BSplineFamily([0, Tf/2, Tf], 6)
@@ -198,11 +198,11 @@ def plot_results(t, x, ud, rescale=True):
198198
#
199199

200200
# Define the cost function (mainly penalize steering angle)
201-
traj_cost = opt.quadratic_cost(
201+
traj_cost = obc.quadratic_cost(
202202
vehicle_flat, None, np.diag([0.1, 10]), x0=xf, u0=uf)
203203

204204
# Set terminal cost to bring us close to xf
205-
terminal_cost = opt.quadratic_cost(vehicle_flat, 1e3 * np.eye(3), None, x0=xf)
205+
terminal_cost = obc.quadratic_cost(vehicle_flat, 1e3 * np.eye(3), None, x0=xf)
206206

207207
# Change the basis to use B-splines
208208
basis = fs.BSplineFamily([0, Tf/2, Tf], [4, 6], vars=2)

examples/mpc_aircraft.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"source": [
2020
"import control as ct\n",
2121
"import numpy as np\n",
22-
"import control.optimal as opt\n",
22+
"import control.optimal as obc\n",
2323
"import matplotlib.pyplot as plt"
2424
]
2525
},
@@ -70,15 +70,15 @@
7070
"# model.y.reference = ys;\n",
7171
"\n",
7272
"# provide constraints on the system signals\n",
73-
"constraints = [opt.input_range_constraint(sys, [-5, -6], [5, 6])]\n",
73+
"constraints = [obc.input_range_constraint(sys, [-5, -6], [5, 6])]\n",
7474
"\n",
7575
"# provide penalties on the system signals\n",
7676
"Q = model.C.transpose() @ np.diag([10, 10, 10, 10]) @ model.C\n",
7777
"R = np.diag([3, 2])\n",
78-
"cost = opt.quadratic_cost(model, Q, R, x0=xd, u0=ud)\n",
78+
"cost = obc.quadratic_cost(model, Q, R, x0=xd, u0=ud)\n",
7979
"\n",
8080
"# online MPC controller object is constructed with a horizon 6\n",
81-
"ctrl = opt.create_mpc_iosystem(model, np.arange(0, 6) * 0.2, cost, constraints)"
81+
"ctrl = obc.create_mpc_iosystem(model, np.arange(0, 6) * 0.2, cost, constraints)"
8282
]
8383
},
8484
{

examples/steering-optimal.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import numpy as np
99
import math
1010
import control as ct
11-
import control.optimal as opt
11+
import control.optimal as obc
1212
import matplotlib.pyplot as plt
1313
import logging
1414
import time
@@ -106,7 +106,7 @@ def plot_lanechange(t, y, u, yf=None, figure=None):
106106
# Set up the cost functions
107107
Q = np.diag([.1, 10, .1]) # keep lateral error low
108108
R = np.diag([.1, 1]) # minimize applied inputs
109-
quad_cost = opt.quadratic_cost(vehicle, Q, R, x0=xf, u0=uf)
109+
quad_cost = obc.quadratic_cost(vehicle, Q, R, x0=xf, u0=uf)
110110

111111
# Define the time horizon (and spacing) for the optimization
112112
timepts = np.linspace(0, Tf, 20, endpoint=True)
@@ -124,7 +124,7 @@ def plot_lanechange(t, y, u, yf=None, figure=None):
124124

125125
# Compute the optimal control, setting step size for gradient calculation (eps)
126126
start_time = time.process_time()
127-
result1 = opt.solve_ocp(
127+
result1 = obc.solve_ocp(
128128
vehicle, timepts, x0, quad_cost, initial_guess=straight_line, log=True,
129129
# minimize_method='trust-constr',
130130
# minimize_options={'finite_diff_rel_step': 0.01},
@@ -158,9 +158,9 @@ def plot_lanechange(t, y, u, yf=None, figure=None):
158158
print("\nApproach 2: input cost and constraints plus terminal cost")
159159

160160
# Add input constraint, input cost, terminal cost
161-
constraints = [ opt.input_range_constraint(vehicle, [8, -0.1], [12, 0.1]) ]
162-
traj_cost = opt.quadratic_cost(vehicle, None, np.diag([0.1, 1]), u0=uf)
163-
term_cost = opt.quadratic_cost(vehicle, np.diag([1, 10, 10]), None, x0=xf)
161+
constraints = [ obc.input_range_constraint(vehicle, [8, -0.1], [12, 0.1]) ]
162+
traj_cost = obc.quadratic_cost(vehicle, None, np.diag([0.1, 1]), u0=uf)
163+
term_cost = obc.quadratic_cost(vehicle, np.diag([1, 10, 10]), None, x0=xf)
164164

165165
# Change logging to keep less information
166166
logging.basicConfig(
@@ -175,7 +175,7 @@ def plot_lanechange(t, y, u, yf=None, figure=None):
175175

176176
# Compute the optimal control
177177
start_time = time.process_time()
178-
result2 = opt.solve_ocp(
178+
result2 = obc.solve_ocp(
179179
vehicle, timepts, x0, traj_cost, constraints, terminal_cost=term_cost,
180180
initial_guess=straight_line, log=True,
181181
# minimize_method='SLSQP', minimize_options={'eps': 0.01}
@@ -207,10 +207,10 @@ def plot_lanechange(t, y, u, yf=None, figure=None):
207207

208208
# Input cost and terminal constraints
209209
R = np.diag([1, 1]) # minimize applied inputs
210-
cost3 = opt.quadratic_cost(vehicle, np.zeros((3,3)), R, u0=uf)
210+
cost3 = obc.quadratic_cost(vehicle, np.zeros((3,3)), R, u0=uf)
211211
constraints = [
212-
opt.input_range_constraint(vehicle, [8, -0.1], [12, 0.1]) ]
213-
terminal = [ opt.state_range_constraint(vehicle, xf, xf) ]
212+
obc.input_range_constraint(vehicle, [8, -0.1], [12, 0.1]) ]
213+
terminal = [ obc.state_range_constraint(vehicle, xf, xf) ]
214214

215215
# Reset logging to its default values
216216
logging.basicConfig(
@@ -219,7 +219,7 @@ def plot_lanechange(t, y, u, yf=None, figure=None):
219219

220220
# Compute the optimal control
221221
start_time = time.process_time()
222-
result3 = opt.solve_ocp(
222+
result3 = obc.solve_ocp(
223223
vehicle, timepts, x0, cost3, constraints,
224224
terminal_constraints=terminal, initial_guess=straight_line, log=False,
225225
# solve_ivp_kwargs={'atol': 1e-3, 'rtol': 1e-2},
@@ -254,7 +254,7 @@ def plot_lanechange(t, y, u, yf=None, figure=None):
254254

255255
# Compute the optimal control
256256
start_time = time.process_time()
257-
result4 = opt.solve_ocp(
257+
result4 = obc.solve_ocp(
258258
vehicle, timepts, x0, quad_cost,
259259
constraints,
260260
terminal_constraints=terminal,

0 commit comments

Comments
 (0)