def dyns_1D(a):
     x0 = reshape_pt1(a[:x.shape[1]])
     u0 = reshape_pt1(a[x.shape[1]:])
     version = \
         lambda t, xl, ul, t0, init_control, process_noise_var, **kwargs: \
             duffing_continuous_prior_mean(xl, ul, kwargs)
     xnext = dynamics_traj(x0=x0,
                           u=u0,
                           t0=0,
                           dt=dt,
                           init_control=u0,
                           discrete=False,
                           version=version,
                           meas_noise_var=0,
                           process_noise_var=0,
                           method='RK45',
                           t_span=[0, dt],
                           t_eval=[dt],
                           kwargs=prior_kwargs)
     return reshape_pt1_tonormal(xnext)
     # Return xi_t instead of x_t+1 from x_t,u_t
     true_dynamics = lambda x, control: \
         - dyn_kwargs.get('beta') * x[:, 0] ** 3 - dyn_kwargs.get('alpha') \
         * x[:, 0] - dyn_kwargs.get('delta') * x[:, 1]
 elif ('justvelocity' in system) and ('Duffing' in system):
     if not continuous_model:
         true_dynamics = lambda x, control: dynamics_traj(
             x0=reshape_pt1(x),
             u=lambda t, kwarg, t0, init_control: interpolate(
                 t,
                 np.concatenate(
                     (reshape_dim1(np.arange(len(control))), control),
                     axis=1),
                 t0=t0,
                 init_value=init_control),
             t0=t0,
             dt=dt,
             init_control=init_control,
             version=dynamics,
             meas_noise_var=0,
             process_noise_var=process_noise_var,
             method=optim_method,
             t_span=[0, dt],
             t_eval=[dt],
             kwargs=dyn_kwargs)[:, -1]
     else:
         true_dynamics = lambda x, control: \
             dynamics(t=t0, x=x, u=lambda t, kwarg, t0, init_control:
             interpolate(t, np.concatenate((reshape_dim1(np.arange(
                 len(control))), control), axis=1), t0=t0,
                         init_value=init_control),
Beispiel #3
0
def simulate_dynamics(t_span,
                      t_eval,
                      t0,
                      dt,
                      init_control,
                      init_state,
                      dynamics,
                      controller,
                      process_noise_var,
                      optim_method,
                      dyn_config,
                      discrete=False,
                      verbose=False):
    xtraj = dynamics_traj(x0=reshape_pt1(init_state),
                          u=controller,
                          t0=t0,
                          dt=dt,
                          init_control=init_control,
                          discrete=discrete,
                          version=dynamics,
                          meas_noise_var=0,
                          process_noise_var=process_noise_var,
                          method=optim_method,
                          t_span=t_span,
                          t_eval=t_eval,
                          kwargs=dyn_config)
    utraj = controller(t_eval,
                       kwargs=dyn_config,
                       t0=t0,
                       init_control=init_control)
    t_utraj = np.concatenate((reshape_dim1(t_eval), utraj), axis=1)
    # Trajectory
    plt.plot(t_eval, xtraj[:, 0], label='Position')
    plt.plot(t_eval, xtraj[:, 1], label='Velocity')
    plt.title('States over time')
    plt.xlabel('t')
    plt.ylabel('x')
    plt.legend()
    if verbose:
        plt.show()
    if xtraj.shape[1] > 2:
        for i in range(2, xtraj.shape[1]):
            plt.plot(t_eval, xtraj[:, i], label='True dim ' + str(i))
            plt.title('Dim ' + str(i) + ' over time')
            plt.xlabel('t')
            plt.ylabel('x')
            plt.legend()
            if verbose:
                plt.show()
    # Phase portrait
    plt.plot(xtraj[:, 0], xtraj[:, 1], label='Trajectory')
    plt.title('Phase portrait')
    plt.xlabel('x')
    plt.ylabel(r'$\dot{x}$')
    plt.legend()
    if verbose:
        plt.show()
    plt.close('all')
    plt.clf()

    return xtraj, utraj, t_utraj