:py:mod:`multirotor.optimize` ============================= .. py:module:: multirotor.optimize Module Contents --------------- Functions ~~~~~~~~~ .. autoapisummary:: multirotor.optimize.get_study multirotor.optimize.run_sim multirotor.optimize.get_controller multirotor.optimize.make_controller_from_trial multirotor.optimize.make_env multirotor.optimize.make_objective multirotor.optimize.optimize multirotor.optimize.apply_params Attributes ~~~~~~~~~~ .. autoapisummary:: multirotor.optimize.DEFAULTS multirotor.optimize.parser .. py:data:: DEFAULTS .. py:function:: get_study(study_name: str = None, seed: int = 0) -> optuna.Study Create an `optuna.Study` set to maximize reward. Parameters ---------- study_name : str, optional If name is given, a study is created in `./studies/study_name.db`, by default None seed : int, optional The random seed to use for parameter sampling, by default 0 Returns ------- optuna.Study The study object. Best params can be accessed via `study.best_params`. .. py:function:: run_sim(env, traj: multirotor.trajectories.Trajectory, ctrl: multirotor.controller.Controller) -> multirotor.helpers.DataLog Run a single episode, where the environment is controlled by the `Controller` object. The `env` object must be `reset()` before calling. Parameters ---------- env : DynamicsMultirotorEnv The environment to run traj : Trajectory The waypoints to follow. ctrl : Controller The controller which outputs dynamics to be fed to the environment. Returns ------- DataLog A log containing state measurements for each step. .. py:function:: get_controller(m: multirotor.simulation.Multirotor, scurve=False, args: argparse.Namespace = DEFAULTS) -> multirotor.controller.Controller Create a `multirotor.controller.Controller` object with default values. Note: `max_acceleration`, `max_velocity`, `max_error_i` etc are important parameters that are set as defaults. Parameters ---------- m : Multirotor The vehicle to control. scurve : bool, optional Whether to use an `SCurve` trajectory planning approach, by default False args : Namespace, optional The default settings, by default DEFAULTS Returns ------- Controller .. py:function:: make_controller_from_trial(trial: optuna.Trial, args: argparse.Namespace = DEFAULTS, prefix='') -> dict Makes a dictionary of parameters which can be used with the `Controller.set_params()` Parameters ---------- trial : optuna.Trial The trail with the suggest parameters. args : Namespace, optional Namespace of optimization arguments, by default DEFAULTS prefix : str, optional _description_, by default '' Returns ------- Dict A dictionary of parameters to be used by `Controller.set_params(**dict)` .. py:function:: make_env(vp: multirotor.vehicle.VehicleParams, sp: multirotor.vehicle.SimulationParams, args: argparse.Namespace = DEFAULTS) -> multirotor.env.DynamicsMultirotorEnv Make the environment instance to be used by `objective()`, and set default env parameters. Parameters ---------- vp : VehicleParams sp : SimulationParams args : Namespace, optional Optimization params, by default DEFAULTS Returns ------- DynamicsMultirotorEnv .. py:function:: make_objective(vp: multirotor.vehicle.VehicleParams, sp: multirotor.vehicle.SimulationParams, ctrl: multirotor.controller.Controller = None, args: argparse.Namespace = DEFAULTS) -> Callable Make the function that `optuna` will use to optimize. Parameters ---------- vp : VehicleParams sp : SimulationParams ctrl: Controller, optional The controller class to use. If not provided, `get_controller` is used with `DEFAULTS` to make a controller instance. args : Namespace, optional Optimization params, by default DEFAULTS .. py:function:: optimize(vp: multirotor.vehicle.VehicleParams, sp: multirotor.vehicle.SimulationParams, ctrl: multirotor.controller.Controller = None, ntrials: int = 1000, args: argparse.Namespace = DEFAULTS, seed: int = 0, study_name: str = None, n_jobs: int = 1, timeout: float = None, verbosity=optuna.logging.WARNING) -> optuna.Study Search over parameter space to optimize controller parameters in `make_controller_from_trial()`. Parameters ---------- vp : VehicleParams sp : SimulationParams ctrl: Controller, optional The controller class to use. If not provided, `get_controller` is used with `DEFAULTS` to make a controller instance. ntrials : int, optional Number of trials, by default 1000 args : Namespace, optional Optimization params, by default DEFAULTS seed : int, optional Optimization seed, by default 0 study_name : str, optional Name of study to be saved in `studies/study_name.db`, by default None n_jobs: int, optional Number of parallel optimization jobs to run. If -1, sets to number of processors. By default 1 timeout: float, optional Number of seconds to run the study for. By default, run until n_trials reached verbosity : _type_, optional Logging level of `optuna.optimize()`, by default optuna.logging.WARNING Returns ------- optuna.Study The completed `Study` object with `study.best_params` which can be used by `apply_params(Controller, **best_params)` to get the optimized controller. .. py:function:: apply_params(ctrl: multirotor.controller.Controller, params: dict) -> Dict[str, Dict[str, numpy.ndarray]] Apply parameters from an optuna.Study to a `Controller`. Converts the dictionary of `best_params` in the format accepted by `controller.set_params()` Parameters ---------- ctrl : Controller The controller to which to apply the parameters. If None, just return the dictionary of parameters which `Controller.set_params()` can use. params : dict[str, np.ndarray] The dictionary of parameters returned by `optuna.Study` Returns ------- Dict[str, Dict[str, np.ndarray]] A nested dictionary for `Controller.set_params()` .. py:data:: parser