pendulum = importlib.util.module_from_spec(spec) spec.loader.exec_module(pendulum) ocp = pendulum.prepare_ocp( biorbd_model_path="pendulum.bioMod", final_time=2, number_shooting_points=10, nb_threads=2, ) X = InitialGuess([0, 0, 0, 0]) U = InitialGuess([-1, 1]) # --- Single shooting --- # sol_simulate_single_shooting = Simulate.from_controls_and_initial_states( ocp, X, U, single_shoot=True) result_single = ShowResult(ocp, sol_simulate_single_shooting) result_single.graphs() # --- Multiple shooting --- # sol_simulate_multiple_shooting = Simulate.from_controls_and_initial_states( ocp, X, U, single_shoot=False) result_multiple = ShowResult(ocp, sol_simulate_multiple_shooting) result_multiple.graphs() # --- Single shooting --- # result_single.animate() # --- Multiple shooting --- # result_multiple.animate()
print("Results using ACADOS") print(f"Final objective: {np.nansum(sol_obj_acados)}") analyse_acados = ObjectivePrinter(ocp_acados, sol_obj_acados) analyse_acados.by_function() print(f"Time to solve: {sol_acados['time_tot']}sec") print(f"") print( f"Results using Ipopt{'' if warm_start_ipopt_from_acados_solution else ' not'} " f"warm started from ACADOS solution" ) print(f"Final objective : {np.nansum(sol_obj_ipopt)}") analyse_ipopt = ObjectivePrinter(ocp_ipopt, sol_obj_ipopt) analyse_ipopt.by_function() print(f"Time to solve: {sol_ipopt['time_tot']}sec") print(f"") result_acados = ShowResult(ocp_acados, sol_acados) result_ipopt = ShowResult(ocp_ipopt, sol_ipopt) visualizer = result_acados.animate(show_now=False) visualizer.extend(result_ipopt.animate(show_now=False)) # Update biorbd-viz by hand so they can be visualized simultaneously should_continue = True while should_continue: for i, b in enumerate(visualizer): if b.vtk_window.is_active: b.update() else: should_continue = False
# ------------- # return OptimalControlProgram( biorbd_model, dynamics, number_shooting_points, final_time, x_init, u_init, x_bounds, u_bounds, objective_functions, constraints, ode_solver=ode_solver, ) if __name__ == "__main__": ocp = prepare_ocp("cube.bioMod", number_shooting_points=30, final_time=2, use_actuators=False) # --- Solve the program --- # sol = ocp.solve(show_online_optim=True) # --- Show results --- # result = ShowResult(ocp, sol) result.animate()
return OptimalControlProgram( biorbd_model, dynamics, number_shooting_points, phase_time, x_init, u_init, x_bounds, u_bounds, objective_functions, tau_mapping=tau_mapping, ) if __name__ == "__main__": model_path = "2segments_4dof_2contacts.bioMod" t = 0.5 ns = 20 ocp = prepare_ocp(model_path=model_path, phase_time=t, number_shooting_points=ns, use_actuators=False) # --- Solve the program --- # sol = ocp.solve(show_online_optim=True) # --- Show results --- # result = ShowResult(ocp, sol) result.animate(nb_frames=40)
u_init = InitialGuessList() u_init.add([tau_init] * biorbd_model.nbGeneralizedTorque() + [muscle_init] * biorbd_model.nbMuscleTotal()) # ------------- # return OptimalControlProgram( biorbd_model, dynamics, number_shooting_points, final_time, x_init, u_init, x_bounds, u_bounds, objective_functions, ) if __name__ == "__main__": ocp = prepare_ocp(biorbd_model_path="arm26_with_contact.bioMod", final_time=2, number_shooting_points=20) # --- Solve the program --- # sol = ocp.solve(show_online_optim=True) # --- Show results --- # result = ShowResult(ocp, sol) result.animate(show_meshes=False) result.graphs()
def main(args=None): init = None if args: init = args[:-1] pwd = args[-1] save_path = "2p_init_"+str(init[0])+"_"+str(init[1])+"_"+str(init[2])+"_"+str(init[3])+"_sol.bo" if os.path.exists(save_path): return model_path = ( "../models/jumper2contacts.bioMod", "../models/jumper1contacts.bioMod", ) time_min = [0.2, 0.05] time_max = [0.5, 1] phase_time = [0.6, 0.2] number_shooting_points = [30, 15] tic = time() ocp = prepare_ocp( model_path=model_path, phase_time=phase_time, ns=number_shooting_points, time_min=time_min, time_max=time_max, init=init, ) sol = ocp.solve( show_online_optim=False, solver_options={"hessian_approximation": "limited-memory", "max_iter": 200} ) utils.warm_start_nmpc(sol, ocp) ocp.solver.set_lagrange_multiplier(sol) sol = ocp.solve( show_online_optim=True, solver_options={"hessian_approximation": "exact", "max_iter": 1000, "warm_start_init_point": "yes", } ) toc = time() - tic print(f"Time to solve : {toc}sec") if init: ocp.save(sol, save_path) ocp.save_get_data(sol, save_path + 'b') ssh = SSHClient() ssh.load_system_host_keys() ssh.connect('pariterre.net', username='******', password=pwd) with SCPClient(ssh.get_transport()) as scp: scp.put(save_path, save_path) scp.get(save_path) scp.put(save_path + 'b', save_path + 'b') scp.get(save_path + 'b') result = ShowResult(ocp, sol) result.animate(nb_frames=241)
ocp = prepare_ocp( model_path=model_path, phase_time=phase_time, ns=number_shooting_points, time_min=time_min, time_max=time_max, ) sol = ocp.solve(show_online_optim=False, solver_options={ "hessian_approximation": "limited-memory", "max_iter": 200 }) utils.warm_start_nmpc(sol, ocp) ocp.solver.set_lagrange_multiplier(sol) sol = ocp.solve(show_online_optim=True, solver_options={ "hessian_approximation": "exact", "max_iter": 1000, "warm_start_init_point": "yes", }) toc = time() - tic print(f"Time to solve : {toc}sec") result = ShowResult(ocp, sol) result.animate(nb_frames=241)
dynamics, number_shooting_points, final_time, x_init, u_init, x_bounds, u_bounds, objective_functions, nb_integration_steps=5, control_type=control_type, ode_solver=ode_solver, ) if __name__ == "__main__": ocp = prepare_ocp( biorbd_model_path="cube_and_line.bioMod", number_shooting_points=30, final_time=2, marker_velocity_or_displacement="disp", # "velo" marker_in_first_coordinates_system=True, control_type=ControlType.LINEAR_CONTINUOUS, ) # --- Solve the program --- # sol = ocp.solve(show_online_optim=True) # --- Show results --- # result = ShowResult(ocp, sol) result.animate(nb_frames=200)