Beispiel #1
0
    eta = np.linalg.norm(saddle_state - initial_state) / dist
    return (1 - eta) * (1 - np.exp(-level * param))


#%%
#tests
check_ellipsoid_array = 0
potential_well_plot_3D = 0
potential_well_plot = 0

if check_ellipsoid_array:
    import matplotlib.pyplot as plt
    #ell = ellipsoid_fun.get_ellipsoid_array(target_state, quad_form, level, bound)
    plt.scatter(ell.T[0], ell.T[1])
    CS = ellipsoid_fun.draw_ellipsoid_2D(force_matrix,
                                         target_state,
                                         noise=sigma)
    foo = ellipsoid_fun.check_ellipsoid(ell,
                                        score_function_simexp_ell_param,
                                        threshold=threshold_simexp,
                                        tolerance=1e-3)

    score_level = ellipsoid_fun.get_levelset_array(target_state,
                                                   score_function_simexp_ell,
                                                   level=1 - threshold_simexp,
                                                   bound=2 * bound,
                                                   tolerance=1e-3)
    plt.scatter(score_level.T[0], score_level.T[1], alpha=0.5)

    print(foo)
Beispiel #2
0
                color='black',
                s=40,
                zorder=10)
    plt.scatter(tw.target_state[0],
                tw.target_state[1],
                marker='x',
                color='black',
                s=40,
                zorder=10)

    #plt.scatter(pre_branching_points[:,0], pre_branching_points[:,1], label = 'pre-branching points', marker = 'D', c = np.arange(len(pre_branching_points)), cmap = plt.cm.plasma)
    #plt.scatter(branching_points[:,0], branching_points[:,1], label = 'branching points',c = np.linspace(0,1, len(pre_branching_points)), cmap = plt.cm.plasma)

    plt.legend(loc='upper left')
    CS = ellipsoid_fun.draw_ellipsoid_2D(tw.force_matrix,
                                         tw.initial_state,
                                         noise=sigma,
                                         confidence=0.95)
    CS.collections[0].set_label('confidence ellipsoid')
    TAMS_output_file.close()
    plt.ylim(-5, 25)
    plt.xlim(-10, 10)
    plt.xlabel('x')
    plt.ylabel('y')
    plt.text(-0.2,
             1,
             '(a)',
             horizontalalignment='center',
             verticalalignment='center',
             transform=plt.gca().transAxes,
             fontsize=9)
    plt.savefig('../../Report/overleaf/branch_normal', bbox_inches='tight')
Beispiel #3
0
def trajectory_plot(time_array,
                    dt,
                    force,
                    initial_state,
                    target_state,
                    sigma1,
                    save_path,
                    sigma2=None,
                    force_matrix=None,
                    score_threshold=None,
                    score_function=None,
                    xmin=-10,
                    xmax=10,
                    ymin=-10,
                    ymax=15):
    """
    plots the trajectories of two particles starting in the initial states, for two values of noise strength sigma
    """

    fig, ax = plt.subplots(1, 1)
    x, y = np.linspace(xmin, xmax, 100), np.linspace(ymin, ymax, 100)
    xx, yy = np.meshgrid(x, y)

    #solve first trajectory

    if sigma2 is not None:
        vs = schemes.Euler_Maruyama_no_stop(0,
                                            initial_state,
                                            sigma2,
                                            dt=dt,
                                            dims=2,
                                            force=force,
                                            time_array_length=len(time_array))
        ax.plot(vs[:, 0], vs[:, 1], linewidth=0.5, color='C1')

    vs = schemes.Euler_Maruyama_no_stop(0,
                                        initial_state,
                                        sigma1,
                                        dt=dt,
                                        dims=2,
                                        force=force,
                                        time_array_length=len(time_array))

    ax.plot(vs[:, 0], vs[:, 1], linewidth=0.5, color='C0')

    #ax.set_title(r'$\sigma$'+' = {}'.format(sigma1))
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_ylim((ymin, ymax))
    ax.set_xlim((xmin, xmax))

    plt.scatter(initial_state[0],
                initial_state[1],
                marker='o',
                color='black',
                s=40,
                zorder=10)
    plt.scatter(target_state[0],
                target_state[1],
                marker='x',
                color='black',
                s=40,
                zorder=10)

    #threshold and stopping criterion
    if score_function is not None:
        #score function threshold for the TAMS algorithm stopping criterion
        score_levels = np.apply_along_axis(score_function, 0,
                                           np.array([xx, yy]))
        ax.contour(xx,
                   yy,
                   score_levels,
                   levels=[1 - score_threshold],
                   zorder=10)

    if force_matrix is not None:
        ellipsoid_fun.draw_ellipsoid_2D(force_matrix,
                                        target_state,
                                        noise=sigma1,
                                        zorder=20)
        #CS.collections[0].set_label('confidence ellipsoid')

    #plt.legend()
    plt.text(-0.2,
             1,
             '(a)',
             horizontalalignment='center',
             verticalalignment='center',
             transform=plt.gca().transAxes,
             fontsize=9)
    if save_path is not None:
        plt.savefig(save_path, bbox_inches='tight')
    plt.show()
Beispiel #4
0
def trajectory_plot_2D_compare_sigma(time_array,
                                     dt,
                                     force,
                                     initial_state,
                                     target_state,
                                     sigma1,
                                     sigma2,
                                     save_path,
                                     force_matrix=None,
                                     score_threshold=None,
                                     score_function=None,
                                     xmin=-6,
                                     xmax=6,
                                     ymin=-2,
                                     ymax=10):
    """
    plots the trajectories of two particles starting in the initial states, for two values of noise strength sigma
    """

    fig, axes = plt.subplots(2, 2, sharex='col')
    x, y = np.linspace(xmin, xmax, 100), np.linspace(ymin, ymax, 100)
    xx, yy = np.meshgrid(x, y)

    #solve first trajectory
    vs = schemes.Euler_Maruyama_no_stop(0,
                                        initial_state,
                                        sigma1,
                                        dt=dt,
                                        dims=2,
                                        force=force,
                                        time_array_length=len(time_array))

    axes[0, 0].plot(time_array, vs[:, 0], linewidth=0.3, label='x')
    #axes[0,0].plot(time, vs[:,1], linewidth = 0.3, label = 'y')
    axes[0, 0].set_title(r'$\sigma$' + ' = {}'.format(sigma1))
    axes[0, 0].set_ylabel('x')
    axes[0, 0].set_ylim((xmin, xmax))
    #axes[0,0].legend()
    axes[0, 1].plot(vs[:, 0], vs[:, 1], linewidth=0.1, color='C1')
    axes[0, 1].set_title(r'$\sigma$' + ' = {}'.format(sigma1))
    axes[0, 1].set_ylabel('y')
    axes[0, 1].set_ylim((ymin, ymax))

    #threshold and stopping criterion
    if score_function is not None:
        #score function threshold for the TAMS algorithm stopping criterion
        score_levels = np.apply_along_axis(score_function, 0,
                                           np.array([xx, yy]))
        axes[0, 1].contour(xx,
                           yy,
                           score_levels,
                           levels=[1 - score_threshold],
                           zorder=10)
        axes[1, 1].contour(xx,
                           yy,
                           score_levels,
                           levels=[1 - score_threshold],
                           zorder=10)

    if force_matrix is not None:
        ellipsoid_fun.draw_ellipsoid_2D(force_matrix,
                                        target_state,
                                        noise=sigma1)

    #solve second trajectory
    vs = schemes.Euler_Maruyama_no_stop(0,
                                        initial_state,
                                        sigma2,
                                        dt=dt,
                                        dims=2,
                                        force=force,
                                        time_array_length=len(time_array))

    axes[1, 0].plot(time_array, vs[:, 0], linewidth=0.2, label='x')
    #axes[1,0].plot(time, vs[:,1], linewidth = 0.3, label = 'y')
    axes[1, 0].set_title(r'$\sigma$' + ' = {}'.format(sigma2))
    axes[1, 0].set_xlabel('t[s]')
    axes[1, 0].set_ylabel('x')
    axes[1, 0].set_ylim((xmin, xmax))

    axes[1, 1].plot(vs[:, 0], vs[:, 1], linewidth=0.1, color='C1')
    axes[1, 1].set_title(r'$\sigma$' + ' = {}'.format(sigma2))
    axes[1, 1].set_xlabel('x')
    axes[1, 1].set_ylabel('y')
    axes[1, 1].set_xlim((xmin, xmax))
    axes[1, 1].set_ylim((ymin, ymax))

    plt.subplots_adjust(wspace=0.4)
    if save_path is not None:
        plt.savefig(save_path)
    plt.show()
Beispiel #5
0
def visualise_scorefunction(score_function,
                            initial_state,
                            target_state,
                            sigma=None,
                            nb_levels=25,
                            score_threshold=None,
                            score_thresholds=None,
                            colors=None,
                            force_matrix=None,
                            save_path=None,
                            xmin=-10,
                            xmax=10,
                            ymin=-10,
                            ymax=15,
                            new_figure=True):

    if new_figure:
        #fig = plt.figure(figsize = (1.18*3.19, 1.18*2.61))
        fig = plt.figure(figsize=(1.02 * 3.19, 1.02 * 2.61))
    #coordinates
    xx, yy = np.meshgrid(np.linspace(xmin, xmax, 200),
                         np.linspace(ymin, ymax, 200))

    #score function levels sets
    score_levels = np.apply_along_axis(score_function, 0, np.array([xx, yy]))

    #im = plt.contour(xx, yy, score_levels, 50)
    im = plt.contourf(xx,
                      yy,
                      score_levels,
                      levels=np.linspace(0, 1, nb_levels))
    plt.contour(xx,
                yy,
                score_levels,
                levels=np.linspace(0, 1, nb_levels),
                linewidths=0.4,
                colors='grey')
    plt.grid()
    cbar = plt.colorbar(im, format='%.2f')
    cbar.ax.set_title('$\phi_{ell}^Z$')

    if score_threshold is not None:
        #score function threshold for the TAMS algorithm stopping criterion
        CS = plt.contour(xx,
                         yy,
                         score_levels,
                         levels=[1 - score_threshold],
                         zorder=2,
                         linestyles='dashed',
                         colors=['red'])
        #CS.collections[0].set_label('stopping threshold')
        plt.legend()

    if score_thresholds is not None:
        for score_threshold, color in zip(score_thresholds, colors):
            #score function threshold for the TAMS algorithm stopping criterion
            CS = plt.contour(xx,
                             yy,
                             score_levels,
                             levels=[1 - score_threshold],
                             zorder=2,
                             linestyles='dashed',
                             colors=[color])
            #CS.collections[0].set_label('$\phi_{target}$ = '+str(1-score_threshold))
            plt.legend()

    if force_matrix is not None and sigma is not None:
        CS = ellipsoid_fun.draw_ellipsoid_2D(force_matrix,
                                             equilibrium_point=target_state,
                                             noise=sigma,
                                             zorder=1)
        CS.collections[0].set_label('confidence ellipsoid')
        plt.legend(loc='lower left')
    plt.scatter(initial_state[0],
                initial_state[1],
                marker='o',
                color='black',
                s=40,
                zorder=10)
    plt.scatter(target_state[0],
                target_state[1],
                marker='x',
                color='black',
                s=40,
                zorder=10)
    plt.xlabel('x')
    plt.ylabel('y')

    plt.text(-0.2,
             1,
             '(b)',
             horizontalalignment='center',
             verticalalignment='center',
             transform=plt.gca().transAxes,
             fontsize=9)

    if save_path is not None:
        plt.savefig(save_path, bbox_inches='tight')