def test_variables(self): N = 10 ocp = Ocp(t0=2 * pi, T=10) p = ocp.parameter(grid='control') v = ocp.variable(grid='control') x = ocp.state() ocp.set_der(x, 0) ocp.subject_to(ocp.at_t0(x) == 0) ts = linspace(0, 10, N) ocp.add_objective(ocp.integral(sin(v - p)**2, grid='control')) ocp.method(MultipleShooting(N=N)) ocp.solver('ipopt') ocp.set_value(p, ts) ocp.set_initial(v, ts) sol = ocp.solve() _, xs = sol.sample(v, grid='control') assert_array_almost_equal(xs[:-1], ts) ocp.set_initial(v, 0.1 + 2 * pi + ts) sol = ocp.solve() _, xs = sol.sample(v, grid='control') assert_array_almost_equal(xs[:-1], 2 * pi + ts) ocp.set_initial(v, 0.1 + ocp.t) sol = ocp.solve() _, xs = sol.sample(v, grid='control') assert_array_almost_equal(xs[:-1], 2 * pi + ts) ocp.set_initial(v, 0.1 + 2 * pi) sol = ocp.solve() _, xs = sol.sample(v, grid='control') with self.assertRaises(AssertionError): assert_array_almost_equal(xs[:-1], 2 * pi + ts)
def test_param(self): ocp = Ocp(T=1) x = ocp.state() u = ocp.control() p = ocp.parameter() ocp.set_der(x, u) ocp.subject_to(u <= 1) ocp.subject_to(-1 <= u) ocp.add_objective(ocp.at_tf(x)) ocp.subject_to(ocp.at_t0(x) == p) ocp.solver('ipopt') ocp.method(MultipleShooting()) ocp.set_value(p, 0) sol = ocp.solve() ts, xs = sol.sample(x, grid='control') self.assertAlmostEqual(xs[0], 0) ocp.set_value(p, 1) sol = ocp.solve() ts, xs = sol.sample(x, grid='control') self.assertAlmostEqual(xs[0], 1)
# Define states x = ocp.state(2) # Define controls u = ocp.control() # Specify ODE model_rhs = pendulum_ode(x, u, model_param) ocp.set_der(x, model_rhs) # Set initial conditions ocp.subject_to(ocp.at_t0(x) == x0) # Define reference y_ref = ocp.parameter(grid='control') ocp.set_value(y_ref, y_ref_val) # Define output correction beta = ocp.parameter(grid='control') # Define previous control u_prev = ocp.parameter(grid='control') # Set ILC objective ocp.add_objective( ocp.integral((y_ref - beta - x[0])**2, grid='control') + 1e-3 * ocp.integral((u - u_prev)**2, grid='control')) # Pick a solution method ocp.solver('ipopt')
sdot_obs = ocp.control() #-----------------------------ODEs ocp.set_der(x, v * cos(theta)) ocp.set_der(y, v * sin(theta)) ocp.set_der(theta, w) ocp.set_der(s_obs, sdot_obs) #-------------------------------------------------------------------------------# # Solve the first iteration # #-------------------------------------------------------------------------------# #------------------------- Constraints on initial point X_0 = ocp.parameter(4) X = vertcat(x, y, theta, s_obs) ocp.subject_to(ocp.at_t0(X) == X_0) current_X = vertcat(initial_pos_x, initial_pos_y, 0.0, 0.0) ocp.set_value(X_0, current_X) #------------------------- Constraints on Final point end_goal_x = ocp.parameter(1) end_goal_y = ocp.parameter(1) ocp.set_value(end_goal_x, global_end_goal_x) ocp.set_value(end_goal_y, global_end_goal_y)
v = ocp.control() w = ocp.control() #-----------------------------ODEs ocp.set_der(x, v * cos(theta)) ocp.set_der(y, v * sin(theta)) ocp.set_der(theta, w) #-------------------------------------------------------------------------------# # Solve the first iteration # #-------------------------------------------------------------------------------# #------------------------- Constraints on initial point X_0 = ocp.parameter(3) X = vertcat(x, y, theta) ocp.subject_to(ocp.at_t0(X) == X_0) current_X = vertcat(initial_pos_x, initial_pos_y, 0.0) ocp.set_value(X_0, current_X) #------------------------- Constraints on Final point global_goal = vertcat(global_end_goal_x, global_end_goal_y) end_goal_x = ocp.parameter(1) end_goal_y = ocp.parameter(1) ocp.set_value(end_goal_x, global_end_goal_x)
ocp.set_der(x , v*cos(theta)) ocp.set_der(y , v*sin(theta)) ocp.set_der(theta , w) ocp.set_der(s_obs , sdot_obs) #-------------------------------------------------------------------------------# # Solve the first iteration # #-------------------------------------------------------------------------------# #------------------------- Constraints on initial point X_0 = ocp.parameter(4) X = vertcat(x, y, theta, s_obs) ocp.subject_to(ocp.at_t0(X) == X_0) current_X = vertcat(initial_pos_x,initial_pos_y,0.0,0.0) ocp.set_value(X_0, current_X) #------------------------- Constraints on Final point global_goal = vertcat(global_end_goal_x,global_end_goal_y)