# Define constraints ocp.constraints() \ .initial('x-x_0','m') \ .initial('y-y_0','m') \ .initial('theta-theta_0','rad') \ .terminal('x-x_f','m') \ .terminal('y-y_f','m') \ .terminal('theta-theta_f','rad') \ .path('steering','delta - steeringLim','<',0.0,'rad^2') # .path('gLoading','(D^2+L^2)/(mass*g0)','<',0.0,'m^2/s^2') ocp.scale(m='x', s='x/V', rad=1, nd=1) guess_maker = beluga.guess_generator('auto', start=[0, 0, pi / 4], direction='forward', time_integrate=1.0, costate_guess=0.0) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(2) \ .terminal('y', 1.0) \ .terminal('x', 1.0) \ .terminal('theta', pi/2) continuation_steps.add_step('bisection') \ .num_cases(21) \ .terminal('y', 5.0) \ .terminal('x', 5.0) \
ocp.initial_constraint('h-h_0', 'm') ocp.initial_constraint('theta', 'rad') ocp.initial_constraint('v-v_0', 'm/s') ocp.initial_constraint('gam-gam_0', 'rad') ocp.initial_constraint('t', 's') ocp.terminal_constraint('h-h_f', 'm') ocp.terminal_constraint('theta-theta_f', 'rad') ocp.scale(m='h', s='h/v', kg='mass', rad=1) bvp_solver = beluga.bvp_algorithm('spbvp') guess_maker = beluga.guess_generator( 'auto', start=[40000, 0, 2000, (-90)*pi/180], direction='forward', costate_guess=-0.1, control_guess=[0], use_control_guess=True) continuation_steps = beluga.init_continuation() # Start by flying straight towards the ground continuation_steps.add_step('bisection') \ .num_cases(5) \ .const('h_f', 0) # Slowly turn up the density continuation_steps.add_step('bisection') \ .num_cases(3) \ .const('rho0', 1.2)
derivative_method='fd', max_error=20, ) bvp_solver = beluga.bvp_algorithm('qcpi', tolerance=1e-3, max_iterations=500, verbose=True, max_error=20, N=81) guess_maker = beluga.guess_generator( 'auto', start=[-.8, 0., -.1, -pi / 12.] + [-.8, .1, -.1, 0., 1.], direction='forward', costate_guess=[0., 0., 0., -0.] + [0., 0., 0., 0., 0.] + [0.] * 2, control_guess=[0.00, .01, -0.00, .01] + [0.0, 0.0] * 2, time_integrate=.1, use_control_guess=True, ) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(21) \ .terminal('xbar', -0.6)\ .terminal('ybar', -.25) \ .terminal('zbar', 0.) \ .terminal('psi', +15*pi/180) \ .terminal('xbar2', -0.6)\ .terminal('ybar2', .25) \
ocp.initial_constraint('x - x_0', 'ft') ocp.initial_constraint('y - y_0', 'ft') ocp.initial_constraint('s', 'ft') ocp.terminal_constraint('x - x_f', 'ft') ocp.terminal_constraint('y - y_f', 'ft') ocp.terminal_constraint('s - s_f', 'ft') ocp.scale(ft='s', rad=1) bvp_solver = beluga.bvp_algorithm('spbvp') guess_maker = beluga.guess_generator('auto', start=[0, 0], costate_guess=-0.1, time_integrate=1.1, control_guess=[0], use_control_guess=True) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(6) \ .const('x_f', 1) \ .const('y_f', 0) \ .const('s_f', 1.1) continuation_steps.add_step('bisection') \ .num_cases(6) \ .const('x_f', 1) \ .const('y_f', 0) \
ocp.initial_constraint('theta - theta_0', 'rad') ocp.initial_constraint('v_r - v_r_0', 'L/s') ocp.initial_constraint('v_theta - v_theta_0', 'L/s') ocp.initial_constraint('m - m_0', 'M') ocp.initial_constraint('t', 's') ocp.terminal_constraint('v_r - v_r_f', 'L/s') ocp.terminal_constraint('v_theta - sqrt(mu / r)', 'L/s') ocp.terminal_constraint('t - t_f', 's') ocp.scale(L='r', s='r/v_theta', M='m', rad=1) bvp_solver_shooting = beluga.bvp_algorithm('Shooting', algorithm='Armijo') bvp_solver_collocation = beluga.bvp_algorithm('spbvp') guess_maker = beluga.guess_generator('auto', start=[1, 0, 0, 1, 1], direction='forward', costate_guess=-0.1) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(3) \ .const('v_r_f', 0) continuation_steps.add_step('bisection') \ .num_cases(40) \ .const('t_f', 80) beluga.add_logger(file_level=logging.DEBUG, display_level=logging.INFO) sol_set_collocation = beluga.solve(
ocp.initial_constraint('y - y_0', 'm') ocp.initial_constraint('v_x - v_x_0', 'm/s') ocp.initial_constraint('v_y - v_y_0', 'm/s') ocp.initial_constraint('mass - mass_0', 'kg') ocp.initial_constraint('t', 's') ocp.terminal_constraint('y - y_f', 'm') ocp.terminal_constraint('v_x - sqrt(mu/(y_f+Re))', 'm/s') ocp.terminal_constraint('v_y - v_y_f', 'm/s') ocp.scale(m='y', s='y/v_x', kg='mass', newton='mass*v_x^2/y', rad=1) bvp_solver = beluga.bvp_algorithm('spbvp') guess_maker = beluga.guess_generator( 'auto', start=[0, 0, 0, 0.01, 60880], # Starting values for states in order costate_guess=-0.1, control_guess=[0], use_control_guess=False) beluga.add_logger(file_level=logging.DEBUG, display_level=logging.INFO) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(10) \ .const('eps', 1) \ .const('mass_0', 1.1702e5) continuation_steps.add_step('bisection') \ .num_cases(10) \ .const('rho_ref', 1.225)
# Define constraints ocp.constraints() \ .initial('x', 'm') \ .initial('y', 'm') \ .initial('v', 'm/s') \ .initial('t', 's') \ .terminal('x-x_f', 'm') \ .terminal('y-y_f', 'm') ocp.scale(m='y', s='y/v', kg=1, rad=1, nd=1) bvp_solver = beluga.bvp_algorithm('Shooting') guess_maker = beluga.guess_generator( 'auto', start=[0, 0, 0], # Starting values for states in order costate_guess=-0.1, control_guess=[-pi / 2], use_control_guess=True) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(21) \ .const('x_f', 10) \ .const('y_f', -10) beluga.add_logger(logging_level=logging.DEBUG, display_level=logging.INFO) sol_set = beluga.solve(ocp=ocp, method='diffyg', optim_options={'reduction': True},
ocp.path_constraint('bank', 'rad', lower='-bmax', upper='bmax', activator='eps', method='utm') ocp.scale(ft='theta*re', s='theta*re/v', slug='mass', rad=1) bvp_solver = beluga.bvp_algorithm('spbvp') guess_maker = beluga.guess_generator('auto', start=[(h_0 - h_f) * 0.5 + h_f, 1, 1, v_0 * 0.25, gam_0, psi_0], direction='forward', costate_guess=[ -1.30487794e-07, -1.00000000e+00, 0, -5.71719036e-06, -7.16743700e-03, 0. ], control_guess=[18. / 180 * np.pi, 0], time_integrate=25) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(20) \ .const('theta_0', 0) \ .const('phi_0', 0) \ continuation_steps.add_step('bisection') \ .num_cases(201) \ .const('h_f', h_f) \
.terminal('x-x_f', 'm') \ .terminal('y-y_f', 'm') ocp.scale(m='x', s='x/V', rad=1) bvp_solver = beluga.bvp_algorithm( 'Shooting', derivative_method='fd', tolerance=1e-4 ) guess_maker = beluga.guess_generator( 'auto', start=[0, 0], quad_guess=[-0.01], costate_guess=[-0.01, -0.01], param_guess=[0], control_guess=[0], use_control_guess=True, direction='forward' ) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(5) \ .const('x_f', 10) continuation_steps.add_step('bisection') \ .num_cases(5) \ .const('y_f', 10)
.initial('v - v_0', '1') \ .initial('m - m_0', '1') \ .path('thrust', '1', lower=0, upper=1, activator='eps', method='epstrig') \ .path('1/2 * d * v**2 * rho0 * exp(-h/H) * pi * (A/2)**2', '1', lower=-1, upper='drag_max', activator='eps2', method='utm') \ .terminal('v - v_f', '1') \ .terminal('m - m_f', '1') ocp.scale(m=1, s=1, kg=1, rad=1, nd=1) bvp_solver = beluga.bvp_algorithm('spbvp') guess_maker = beluga.guess_generator( 'auto', start=[h_0 / meter, v_0 / meter * second, m_0 / kilogram], # Starting values for states in order costate_guess=-0.1, control_guess=[pi / 3], time_integrate=0.1, ) continuation_steps = beluga.init_continuation() continuation_steps.add_step() \ .num_cases(5) \ .const('v_f', 0) continuation_steps.add_step() \ .num_cases(5) \ .const('m_f', 3.6e3/kilogram) continuation_steps.add_step() \
ocp.initial_constraint('theta', 'rad') ocp.initial_constraint('v-v_0', 'm/s') ocp.initial_constraint('gam-gam_0', 'rad') ocp.initial_constraint('t', 's') ocp.terminal_constraint('h-h_f', 'm') ocp.terminal_constraint('theta-theta_f', 'rad') ocp.scale(m='h', s='h/v', kg='mass', rad=1) bvp_solver = beluga.bvp_algorithm('spbvp') guess_maker = beluga.guess_generator( 'auto', start=[h_0, 0., v_0, -45/180*np.pi], direction='forward', costate_guess=[-1e5, -0.01, -0.01, -0.01], control_guess=[5/180*np.pi], time_integrate=1 ) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(21) \ .const('h_f', 0) \ .const('theta_f', 50e3/re) continuation_steps.add_step('bisection') \ .num_cases(81) \ .const('theta_f', 300e3/re)
.terminal('theta-theta_f','rad') \ .path('heatRate','(qdot/qdotMax)','<',1.0,'nd',start_eps=1e-2) \ bvp_solver = beluga.bvp_algorithm('MultipleShooting', derivative_method='fd', tolerance=1e-4, max_iterations=25, verbose = True, max_error=50 ) ocp.scale(m='h', s='h/v', kg='mass', rad=1, nd=1, W=1e7) guess_maker = beluga.guess_generator('auto', start=[80e3,0.0,4e3,-pi/2], # Starting values for states in order direction='forward', costate_guess = 0.1 ) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection').num_cases(41) \ .terminal('h', 15000.0) \ continuation_steps.add_step('bisection').num_cases(31) \ .initial('gam', -60*pi/180) \ .terminal('theta', 0.5*pi/180) continuation_steps.add_step('bisection').num_cases(11) \ .initial('v',5e3)\ .terminal('theta', 1*pi/180)
def test_planarhypersonic(): from math import pi import beluga ocp = beluga.OCP('planarHypersonic') # Define independent variables ocp.independent('t', 's') # Define equations of motion ocp.state('h', 'v*sin(gam)', 'm') \ .state('theta', 'v*cos(gam)/r', 'rad') \ .state('v', '-D/mass - mu*sin(gam)/r**2', 'm/s') \ .state('gam', 'L/(mass*v) + (v/r - mu/(v*r^2))*cos(gam)', 'rad') # Define quantities used in the problem ocp.quantity('rho', 'rho0*exp(-h/H)') ocp.quantity('Cl', '(1.5658*alfa + -0.0000)') ocp.quantity('Cd', '(1.6537*alfa^2 + 0.0612)') ocp.quantity('D', '0.5*rho*v^2*Cd*Aref') ocp.quantity('L', '0.5*rho*v^2*Cl*Aref') ocp.quantity('r', 're+h') # Define controls ocp.control('alfa', 'rad') # Define constants ocp.constant('mu', 3.986e5 * 1e9, 'm^3/s^2') # Gravitational parameter, m^3/s^2 ocp.constant('rho0', 0.0001 * 1.2, 'kg/m^3') # Sea-level atmospheric density, kg/m^3 ocp.constant('H', 7500, 'm') # Scale height for atmosphere of Earth, m ocp.constant('mass', 750 / 2.2046226, 'kg') # Mass of vehicle, kg ocp.constant('re', 6378000, 'm') # Radius of planet, m ocp.constant('Aref', pi * (24 * .0254 / 2)**2, 'm^2') # Reference area of vehicle, m^2 ocp.constant('h_0', 80000, 'm') ocp.constant('v_0', 4000, 'm/s') ocp.constant('h_f', 80000, 'm') ocp.constant('theta_f', 0, 'rad') # Define costs ocp.terminal_cost('-v^2', 'm^2/s^2') # Define constraints ocp.constraints() \ .initial('h-h_0', 'm') \ .initial('theta', 'rad') \ .initial('v-v_0', 'm/s') \ .terminal('h-h_f', 'm') \ .terminal('theta-theta_f', 'rad') ocp.scale(m='h', s='h/v', kg='mass', rad=1) bvp_solver = beluga.bvp_algorithm('Shooting', algorithm='SLSQP', tolerance=1e-6) guess_maker = beluga.guess_generator( 'auto', start=[80000, 0, 4000, -90 * pi / 180], direction='forward', costate_guess=-0.1) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(11) \ .const('h_f', 0) \ .const('theta_f', 0.01 * pi / 180) continuation_steps.add_step('bisection') \ .num_cases(11) \ .const('theta_f', 5.0 * pi / 180) continuation_steps.add_step('bisection') \ .num_cases(11) \ .const('rho0', 1.2) sol = beluga.solve(ocp, method='traditional', bvp_algorithm=bvp_solver, steps=continuation_steps, guess_generator=guess_maker) y0 = sol.y[0] yf = sol.y[-1] y0e = [ 8.00000000e+04, 0.00000000e+00, 4.00000000e+03, 1.95069984e-02, -1.68249327e+01, 1.21634197e+06, -2.83598229e+03, -6.15819100e-17 ] yfe = [ 5.23214346e-04, 8.72664626e-02, 2.69147623e+03, -9.38246813e-01, 5.46455659e+02, 1.21634197e+06, -5.38295257e+03, 1.67911185e-01 ] tfe = 144.5678 assert sol.t.shape[0] == sol.y.shape[0] assert sol.t.shape[0] == sol.u.shape[0] assert sol.y.shape[1] == 8 assert sol.u.shape[1] == 1 assert abs((y0[0] - y0e[0]) / y0e[0]) < tol assert abs((y0[1] - y0e[1])) < tol assert abs((y0[2] - y0e[2]) / y0e[2]) < tol assert abs((y0[3] - y0e[3]) / y0e[3]) < tol assert abs((y0[4] - y0e[4]) / y0e[4]) < tol assert abs((y0[5] - y0e[5]) / y0e[5]) < tol assert abs((y0[6] - y0e[6]) / y0e[6]) < tol assert abs((y0[7] - y0e[7])) < tol assert abs((sol.t[-1] - tfe) / tfe) < tol assert abs((yf[0] - yfe[0])) < tol assert abs((yf[1] - yfe[1]) / yfe[1]) < tol assert abs((yf[2] - yfe[2]) / yfe[2]) < tol assert abs((yf[3] - yfe[3]) / yfe[3]) < tol assert abs((yf[4] - yfe[4]) / yfe[4]) < tol assert abs((yf[5] - yfe[5]) / yfe[5]) < tol assert abs((yf[6] - yfe[6]) / yfe[6]) < tol assert abs((yf[7] - yfe[7]) / yfe[7]) < tol
def test_zermelo_custom_functions(): import beluga ocp = beluga.OCP('zermelos_problem') def drift_x(x, y): return 0 def drift_y(x, y): return ((x - 5)**4 - 625) / 625 ocp.custom_function('drift_x', drift_x) ocp.custom_function('drift_y', drift_y) # Define independent variables ocp.independent('t', 's') # Define equations of motion ocp.state('x', 'V*cos(theta) + epsilon*drift_x(x,y)', 'm') \ .state('y', 'V*sin(theta) + epsilon*drift_y(x,y)', 'm') # Define controls ocp.control('theta', 'rad') # Define constants ocp.constant('V', 10, 'm/s') ocp.constant('epsilon', 0.001, '1') ocp.constant('x_f', 0, 'm') ocp.constant('y_f', 0, 'm') # Define costs ocp.path_cost('1', '1') # Define constraints ocp.constraints() \ .initial('x', 'm') \ .initial('y', 'm') \ .terminal('x-x_f', 'm') \ .terminal('y-y_f', 'm') ocp.scale(m='x', s='x/V', rad=1) bvp_solver = beluga.bvp_algorithm('Shooting', derivative_method='fd', tolerance=1e-4, max_error=100, max_iterations=100) guess_maker = beluga.guess_generator('auto', start=[0, 0], control_guess=[0], use_control_guess=True, direction='forward') continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(10) \ .const('x_f', 10) continuation_steps.add_step('bisection') \ .num_cases(10) \ .const('y_f', 10) continuation_steps.add_step('bisection') \ .num_cases(10) \ .const('epsilon', 1) sol = beluga.solve(ocp, method='icrm', bvp_algorithm=bvp_solver, steps=continuation_steps, guess_generator=guess_maker) from beluga.ivpsol import Trajectory assert isinstance(sol, Trajectory)
.initial('x-x_0','m') \ .initial('y-y_0','m') \ .initial('theta-theta_0','rad') \ .terminal('x-x_f','m') \ .terminal('y-y_f','m') \ .terminal('theta-theta_f','rad') \ .path('steering','delta','<>','steeringLim','rad', start_eps=1) \ .path('accLimit','a','<>',1.0,'m/s^2',start_eps=1) # ocp.scale(m='x', s='x/V', rad=1, nd=1) ocp.scale(m=1, s=1, rad=1, nd=1) guess_maker = beluga.guess_generator( 'auto', start=[0, 0, 0.1, pi / 4], direction='forward', time_integrate=1.0, costate_guess=-0.01, control_guess=[0.1, 0.0, 0.1, 0.1, 0.1, 0.1], use_control_guess=False) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(51) \ .terminal('y', 5.0) \ .terminal('x', 5.0) \ .terminal('theta', pi/4) continuation_steps.add_step('bisection') \ .num_cases(51) \ .terminal('y1', 0.0) \
method='utm') ocp.path_constraint('bank', 'rad', lower='-bmax', upper='bmax', activator='eps', method='utm') ocp.scale(m='h', s='h/v', kg='mass', rad=1) bvp_solver = beluga.bvp_algorithm('spbvp') guess_maker = beluga.guess_generator( 'auto', start=[0, 0, 100, 200, -80 * pi / 180, 0 * pi / 180], direction='reverse', costate_guess=-0.00001, time_integrate=0.1, control_guess=[0, 0]) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(10) \ .const('v_0', 200) \ .const('h_0', 1000) \ .const('gam_f', gam_f) continuation_steps.add_step('bisection') \ .num_cases(10) \ .const('phi_0', 0) \
ocp.initial_constraint('t', 's') ocp.initial_constraint('x', 'ft') ocp.initial_constraint('y', 'ft') ocp.initial_constraint('v', 'ft/s') ocp.terminal_constraint('x - x_f', 'ft') ocp.terminal_constraint('y-y_f', 'ft') ocp.scale(ft='y', s='y/v', rad=1) bvp_solver = beluga.bvp_algorithm('Shooting') guess_maker = beluga.guess_generator('auto', start=[0, 0, 0], costate_guess=-0.1, control_guess=[-3.14159 / 2], use_control_guess=True, time_integrate=0.1) beluga.add_logger(display_level=logging.INFO) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(21) \ .const('x_f', 10) \ .const('y_f', -10) sol_set = beluga.solve(ocp=ocp, method='indirect', bvp_algorithm=bvp_solver,
def test_planarhypersonic(): from math import pi import beluga ocp = beluga.OCP('planarHypersonic') # Define independent variables ocp.independent('t', 's') # Define equations of motion ocp.state('h', 'v*sin(gam)', 'm') \ .state('theta', 'v*cos(gam)/r', 'rad') \ .state('v', '-D/mass - mu*sin(gam)/r**2', 'm/s') \ .state('gam', 'L/(mass*v) + (v/r - mu/(v*r^2))*cos(gam)', 'rad') # Define quantities used in the problem ocp.quantity('rho', 'rho0*exp(-h/H)') ocp.quantity('Cl', '(1.5658*alfa + -0.0000)') ocp.quantity('Cd', '(1.6537*alfa^2 + 0.0612)') ocp.quantity('D', '0.5*rho*v^2*Cd*Aref') ocp.quantity('L', '0.5*rho*v^2*Cl*Aref') ocp.quantity('r', 're+h') # Define controls ocp.control('alfa', 'rad') # Define constants ocp.constant('mu', 3.986e5 * 1e9, 'm^3/s^2') # Gravitational parameter, m^3/s^2 ocp.constant('rho0', 0.0001 * 1.2, 'kg/m^3') # Sea-level atmospheric density, kg/m^3 ocp.constant('H', 7500, 'm') # Scale height for atmosphere of Earth, m ocp.constant('mass', 750 / 2.2046226, 'kg') # Mass of vehicle, kg ocp.constant('re', 6378000, 'm') # Radius of planet, m ocp.constant('Aref', pi * (24 * .0254 / 2) ** 2, 'm^2') # Reference area of vehicle, m^2 # Define costs ocp.terminal_cost('-v^2', 'm^2/s^2') # Define constraints ocp.constraints() \ .initial('h-h_0', 'm') \ .initial('theta-theta_0', 'rad') \ .initial('v-v_0', 'm/s') \ .terminal('h-h_f', 'm') \ .terminal('theta-theta_f', 'rad') ocp.scale(m='h', s='h/v', kg='mass', rad=1) bvp_solver = beluga.bvp_algorithm('Shooting') guess_maker = beluga.guess_generator('auto', start=[80000, 0, 4000, -90 * pi / 180], direction='forward', costate_guess=-0.1) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(11) \ .terminal('h', 0) \ .terminal('theta', 0.01 * pi / 180) continuation_steps.add_step('bisection') \ .num_cases(11) \ .terminal('theta', 5.0 * pi / 180) continuation_steps.add_step('bisection') \ .num_cases(11) \ .const('rho0', 1.2) sol = beluga.solve(ocp, method='traditional', bvp_algorithm=bvp_solver, steps=continuation_steps, guess_generator=guess_maker) y0 = sol.y[0] yf = sol.y[-1] y0e = [80000, 0, 4000, 0.0195, -16.8243, 1212433.8085, -2836.0620, 0] yfe = [0, 0.0873, 2691.4733, -0.9383, 546.4540, 1212433.8085, -5382.9467, 0.1840] tfe = 144.5677 assert sol.t.shape[0] == sol.y.shape[0] assert sol.t.shape[0] == sol.u.shape[0] assert sol.y.shape[1] == 8 assert sol.u.shape[1] == 1 assert abs((y0[0] - y0e[0]) / y0e[0]) < tol assert abs((y0[1] - y0e[1])) < tol assert abs((y0[2] - y0e[2]) / y0e[2]) < tol assert abs((y0[3] - y0e[3]) / y0e[3]) < tol assert abs((y0[4] - y0e[4]) / y0e[4]) < tol assert abs((y0[5] - y0e[5]) / y0e[5]) < tol assert abs((y0[6] - y0e[6]) / y0e[6]) < tol assert abs((y0[7] - y0e[7])) < tol assert abs((sol.t[-1] - tfe) / tfe) < tol assert abs((yf[0] - yfe[0])) < tol assert abs((yf[1] - yfe[1]) / yfe[1]) < tol assert abs((yf[2] - yfe[2]) / yfe[2]) < tol assert abs((yf[3] - yfe[3]) / yfe[3]) < tol assert abs((yf[4] - yfe[4]) / yfe[4]) < tol assert abs((yf[5] - yfe[5]) / yfe[5]) < tol assert abs((yf[6] - yfe[6]) / yfe[6]) < tol assert abs((yf[7] - yfe[7]) / yfe[7]) < tol
'MultipleShooting', tolerance=1e-4, max_iterations=50, verbose=True, ) # bvp_solver = beluga.bvp_algorithm('SingleShooting', # derivative_method='fd', # tolerance=1e-4, # max_iterations=50, # verbose = True, # ) guess_maker = beluga.guess_generator( 'auto', start=[0, 0, 1], # Starting values for states in order direction='forward', costate_guess=-0.1) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(11) \ .terminal('x', 5) \ .terminal('y',-5) beluga.solve(ocp, method='icrm', bvp_algorithm=bvp_solver, steps=continuation_steps, guess_generator=guess_maker)
def test_brachistochrone_shooting(): from math import pi import beluga from beluga.ivpsol import Trajectory from beluga.bvpsol import Solution ocp = beluga.OCP('brachisto') # Define independent variables ocp.independent('t', 's') # Define equations of motion ocp.state('x', 'v*cos(theta)', 'm') \ .state('y', 'v*sin(theta)', 'm') \ .state('v', 'g*sin(theta)', 'm/s') # Define controls ocp.control('theta', 'rad') # Define constants ocp.constant('g', -9.81, 'm/s^2') # Define costs ocp.path_cost('1', '1') # Define constraints ocp.constraints() \ .initial('x-x_0', 'm') \ .initial('y-y_0', 'm') \ .initial('v-v_0', 'm/s') \ .terminal('x-x_f', 'm') \ .terminal('y-y_f', 'm') ocp.scale(m='y', s='y/v', kg=1, rad=1) shooting_solver = beluga.bvp_algorithm('Shooting') guess_maker = beluga.guess_generator('auto', start=[0, 0, 0], direction='forward', costate_guess=-0.1, control_guess = [-pi/2], use_control_guess=True) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(21) \ .terminal('x', 10) \ .terminal('y', -10) sol = beluga.solve(ocp, method='icrm', bvp_algorithm=shooting_solver, steps=continuation_steps, guess_generator=guess_maker) assert isinstance(sol, Trajectory) assert isinstance(sol, Solution) assert sol.t.shape[0] == sol.y.shape[0] assert sol.t.shape[0] == sol.u.shape[0] assert sol.y.shape[1] == 7 assert sol.u.shape[1] == 1 y0 = sol.y[0] yf = sol.y[-1] assert abs(y0[0] - 0) < tol assert abs(y0[1] - 0) < tol assert abs(y0[2] - 0) < tol assert abs(y0[3] + 0.0667) < tol assert abs(y0[4] - 0.0255) < tol assert abs(y0[5] + 0.1019) < tol assert abs(sol.t[-1] - 1.8433) < tol assert abs(yf[0] - 10) < tol assert abs(yf[1] + 10) < tol assert abs(yf[2] - 14.0071) < tol assert abs(yf[3] + 0.0667) < tol assert abs(yf[4] - 0.0255) < tol assert abs(yf[5] - 0) < tol assert abs(y0[3] - yf[3]) < tol assert abs(y0[4] - yf[4]) < tol sol = beluga.solve(ocp, method='traditional', bvp_algorithm=shooting_solver, steps=continuation_steps, guess_generator=guess_maker) y0 = sol.y[0] yf = sol.y[-1] assert sol.t.shape[0] == sol.y.shape[0] assert sol.t.shape[0] == sol.u.shape[0] assert sol.y.shape[1] == 6 assert sol.u.shape[1] == 1 assert abs(y0[0] - 0) < tol assert abs(y0[1] - 0) < tol assert abs(y0[2] - 0) < tol assert abs(y0[3] + 0.0667) < tol assert abs(y0[4] - 0.0255) < tol assert abs(y0[5] + 0.1019) < tol assert abs(sol.t[-1] - 1.8433) < tol assert abs(yf[0] - 10) < tol assert abs(yf[1] + 10) < tol assert abs(yf[2] - 14.0071) < tol assert abs(yf[3] + 0.0667) < tol assert abs(yf[4] - 0.0255) < tol assert abs(yf[5] - 0) < tol assert abs(y0[3] - yf[3]) < tol assert abs(y0[4] - yf[4]) < tol
.initial('gam-gam_0', 'rad') \ .initial('t', 's') \ .terminal('h-h_f', 'm') \ .terminal('theta-theta_f', 'rad') ocp.scale(m='h', s='h/v', kg='mass', rad=1) bvp_solver = beluga.bvp_algorithm('Shooting', algorithm='Armijo', derivative_method='fd', tolerance=1e-6, max_iterations=100, max_error=100) guess_maker = beluga.guess_generator('auto', start=[40000, 0, 4000, (-90) * pi / 180], direction='forward', costate_guess=-0.1) continuation_steps = beluga.init_continuation() # Start by flying straight towards the ground continuation_steps.add_step('bisection') \ .num_cases(11) \ .const('h_f', 0) # Slowly turn up the density continuation_steps.add_step('bisection') \ .num_cases(11) \ .const('rho0', 1.2) # Move downrange out a tad
ocp.path_constraint('u', 'rad', lower='CLmin', upper='CLmax', activator='eps', method='utm') ocp.scale(m='h', s='h/v', kg='mass', rad=1) bvp_solver = beluga.bvp_algorithm('spbvp') guess_maker = beluga.guess_generator( 'auto', start=[0, 905, 13.2275675, -1.28750052], direction='forward', control_guess=[0.9], use_control_guess=True, costate_guess=[-.1, -1, -1, 0], time_integrate=3, ) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(10) \ .const('y_f', 900) \ .const('vx_f', 13.2275675) \ .const('vy_f', -1.28750052) continuation_steps.add_step('bisection') \ .num_cases(20) \
ocp.terminal_constraint('w1 - w1_f', 'rad/s') ocp.terminal_constraint('w2 - w2_f', 'rad/s') ocp.terminal_constraint('w3 - w3_f', 'rad/s') ocp.path_constraint('u1', 'rad/s**2', lower='u_min', upper='u_max', activator='epsilon1', method='utm') ocp.path_constraint('u2', 'rad/s**2', lower='u_min', upper='u_max', activator='epsilon1', method='utm') ocp.path_constraint('u3', 'rad/s**2', lower='u_min', upper='u_max', activator='epsilon1', method='utm') ocp.scale(rad=1, kg=1, s=1, m=1) bvp_solver = beluga.bvp_algorithm('spbvp') guess_maker = beluga.guess_generator( 'auto', start=[w0[0], w0[1], w0[2]], direction='forward', costate_guess=0.1, control_guess=[-0.1, -0.1, -0.1] ) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(21) \ .const('w1_f', 0) \ .const('w2_f', 0) \ .const('w3_f', 0) continuation_steps.add_step('bisection') \ .num_cases(160, 'log') \ .const('epsilon1', 1e-6)
ocp.constant('phi_f', 0, 'rad') ocp.scale(m='h', s='h/v', kg='mass', rad=1) bvp_solver = beluga.bvp_algorithm('Shooting', derivative_method='fd', tolerance=1e-4, max_iterations=100, max_error=400, algorithm='SLSQP') guess_maker = beluga.guess_generator( 'auto', start=[40000, 0, 0, 2000, -(90 - 10) * pi / 180, 0], direction='forward', costate_guess=-0.1, control_guess=[0.0, 0.0], use_control_guess=True, time_integrate=0.5, ) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection').num_cases(21) \ .const('h_f', 0) \ .const('theta_f', 6945.9 / 6378000) # Isn't h_f 0 a no-op? continuation_steps.add_step('bisection').num_cases(3) \ .const('theta_f', 2 ** -14 * pi / 180)
.initial('t', 's') \ .terminal('x-x_f', 'm') \ .terminal('y-y_f', 'm') ocp.scale(m='x', s='x/V', rad=1) bvp_solver = beluga.bvp_algorithm( 'Shooting', derivative_method='fd', tolerance=1e-4 ) guess_maker = beluga.guess_generator( 'auto', start=[0, 0], control_guess=[0], use_control_guess=True, direction='forward' ) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(10) \ .const('x_f', 10) continuation_steps.add_step('bisection') \ .num_cases(10) \ .const('y_f', 10) continuation_steps.add_step('bisection') \
ocp_indirect.path_constraint('u', 'rad', lower='-2', upper='2', activator='epsilon1', method='epstrig') ocp_indirect.terminal_constraint('t - 1', 'nd') ocp_indirect.scale(rad=1, nd=1) bvp_solver_indirect = beluga.bvp_algorithm('spbvp') guess_maker_indirect = beluga.guess_generator('auto', start=[3, 5], direction='forward', costate_guess=-0.1, control_guess=[0], use_control_guess=True, time_integrate=0.1) beluga.add_logger(file_level=logging.DEBUG, display_level=logging.INFO) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(40, 'log') \ .const('epsilon1', 2e-4) sol_set_indirect = beluga.solve(ocp=ocp_indirect, method='indirect', optim_options={
ocp.scale(m=1, s=1, kg=1, rad=1, nd=1) # ocp.bvp_solver = algorithms.SingleShooting(derivative_method='fd',tolerance=1e-4, max_iterations=30, verbose = True, cached=False) bvp_solver = beluga.bvp_algorithm('MultipleShooting', derivative_method='fd', tolerance=1e-3, max_iterations=1000, verbose = True, max_error=200 ) guess_maker = beluga.guess_generator('auto', start=[0,.01], # Starting values for states in order direction='forward', costate_guess = -0.1, time_integrate = 1 ## REQUIRED BECAUSE OF FIXED FINAL TIME ) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection', max_divisions=30).num_cases(5) \ .terminal('x',0) \ .initial('v',1) \ .terminal('v', -1) continuation_steps.add_step('activate_constraint', name='xlim') continuation_steps.add_step('bisection') \ .num_cases(11) \ .constraint('xlim', 0.0, index=1)
# Define constraints ocp.initial_constraint('h-h_0', 'm') ocp.initial_constraint('theta', 'rad') ocp.initial_constraint('v-v_0', 'm/s') ocp.initial_constraint('t', 's') ocp.terminal_constraint('h-h_f', 'm') ocp.terminal_constraint('theta-theta_f', 'rad') ocp.scale(m='h', s='h/v', kg='mass', rad=1) bvp_solver = beluga.bvp_algorithm('spbvp') guess_maker = beluga.guess_generator('auto', start=[50000, 0, 4000, -89 * pi / 180], control_guess=[0], direction='forward', costate_guess=-0.1) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(101) \ .const('h_f', 0) \ .const('theta_f', 0.01*pi/180) continuation_steps.add_step('bisection') \ .num_cases(11) \ .const('theta_f', 5.0*pi/180) \ beluga.add_logger(file_level=logging.DEBUG, display_level=logging.INFO)
ocp.initial_constraint('l - 0', 'm') ocp.terminal_constraint('r', 'm') ocp.terminal_constraint('l - 2', 'm') ocp.path_constraint('u', 'm/s', lower='-5', upper='5', method='utm', activator='eps1') ocp.scale(m='x', rad=1) guess_maker = beluga.guess_generator( 'ones', start=[1.0], # Starting values for states in order costate_guess=0.1, control_guess=[0.35], use_control_guess=True) continuation_steps = beluga.init_continuation() continuation_steps.add_step('bisection') \ .num_cases(10, 'log') \ .const('eps1', 2e-1) beluga.add_logger(file_level=logging.DEBUG, display_level=logging.INFO) bvp_solver = beluga.bvp_algorithm('spbvp') beluga.solve(ocp=ocp, method='indirect',
derivative_method='fd', max_error=20, ) bvp_solver = beluga.bvp_algorithm('qcpi', tolerance=1e-3, max_iterations=300, verbose=True, max_error=20, N=141) guess_maker = beluga.guess_generator( 'auto', start=[-.8, 0., -.1, -pi / 12.] + [-.8, .1, -.1, 0., 1.], direction='forward', costate_guess=[0., 0., 0., -0.] + [0., 0., 0., 0., 0.] + [0.] * 2, control_guess=[0.00, .01, -0.00, .01] + [0.0, 0.0] * 2, time_integrate=.1, use_control_guess=True, ) continuation_steps = beluga.init_continuation() # # continuation_steps.add_step('bisection') \ # .num_cases(101) \ # .terminal('xbar', -0.)\ # .terminal('ybar', .0) \ # .terminal('zbar', 0.) \ # .terminal('psi', +0*pi/180) \ # .terminal('xbar2', -0.)\ # .terminal('ybar2', .0) \