Example #1
0
def run_simple(h0):
    T = 20
    n_groups = 20
    a = 0.5
    n = 6

    n_iter = 100
    tvec = np.arange(T + 1)
    ab = np.zeros((n_iter, T + 1))
    marks = np.zeros((n_iter, T, n_groups))
    copyh = np.zeros((n_iter, T))
    copyl = np.zeros((n_iter, T))

    # Running the agent based
    for i in range(n_iter):
        marks[i, :, :], ab[i, :], copyh[i, :], copyl[i, :] = basic(
            T, n_groups, a, n, h0)
    std = ab.std(axis=0)
    ab = ab.mean(axis=0)

    # Running analytical and numerical integration
    ana = analytical(tvec, h0, a, n)
    _, num = euler(T, a, n, step=1, h0=h0)

    # Plotting h
    fig, axes = plt.subplots(1, 2, figsize=(7, 3))
    ax = axes[0]
    ax.errorbar(tvec, ab, yerr=std, label='agent-based')
    ax.plot(ana, label='analytical')
    ax.plot(num, label='numerical')
    ax.set(xlabel="t", ylabel="h")
    ax.legend()

    plot_marks(marks, T, n_iter, axes[1])
    fig.tight_layout()
    fig.show()

    # Plotting copy evolution
    fig1, ax = plt.subplots(figsize=(7, 3))

    ax.plot(copyh.mean(axis=0), label='H copying')
    ax.plot(copyl.mean(axis=0), label='L copying')
    ax.set(xlabel="t", ylabel="n")
    ax.legend()

    # fig1.show()

    return marks
Example #2
0
def euler(T, a, n, step, h0=0.5, ax=None):
    # coeff = k(a, n)
    # print("coeff = {:.3f}".format(coeff))
    # print("range delta_t*fy: start= {:.3f}, end= {:.3f}]".format(step * (1 - 2 * h0) * coeff, step * coeff))

    h = h0
    err = []

    tvec = []
    hvec = [h0]
    anvec = []

    for t in np.arange(0, T + step, step):
        tvec.append(t)

        if t != 0:
            h = h + step * f(h, a, n)
            hvec.append(h)

        ana = analytical(t, h0, a, n)
        anvec.append(ana)

        local_error = h - ana
        err.append(abs(local_error))

        # print(str(h) + " - " + str(ana) + " = " + str(local_error))

    global_err = sum(err)
    if step == 1:
        print(global_err)

    if ax:
        # ax.plot(err)
        ax.plot(tvec, hvec, tvec, anvec)
        ax.set(title='delta_t = {:.0f}, global error = {:.3f}'.format(
            step, global_err),
               xlabel='t',
               ylabel='h')

    return global_err, hvec
Example #3
0
def n_analysis(ax, ax1):
    T = 45
    a = 0.5
    h0 = 0.5

    h0vec = np.array([3, 4, 5, 6])
    K = len(h0vec)

    n_iter = 100

    h8_ab = np.zeros((n_iter, K))
    h8_ana = np.zeros(K)
    h8_euler = np.zeros(K)
    teq_ab = np.zeros((n_iter, K))
    teq_ana = np.zeros(K)
    teq_euler = np.zeros(K)

    copyh = np.zeros((n_iter, K, T))
    copyl = np.zeros((n_iter, K, T))

    for k, n in enumerate(h0vec):
        n_groups = int(120 / n)
        # Running the agent based
        for i in range(n_iter):
            _, ab, copyh[i, k, :], copyl[i,
                                         k, :] = basic(T, n_groups, a, n, h0)
            h8_ab[i, k] = ab[8]
            for t, h in enumerate(ab):
                if (a * n > 1 and h < 0.01) or (a * n < 1 and h > 0.99):
                    teq_ab[i, k] = t
                    break

        # Running analytical
        h8_ana[k] = analytical(8, h0, a, n)
        if a * n > 1:
            teq_ana[k] = inverse(0.01, h0, a, n)
        else:
            teq_ana[k] = inverse(0.99, h0, a, n)

        # Running numerical
        _, hnum = euler(T, a, n, step=1, h0=h0)
        h8_euler[k] = hnum[8]
        for t, h in enumerate(hnum):
            if (a * n > 1 and h < 0.01) or (a * n < 1 and h > 0.99):
                teq_euler[k] = t
                break

    std_ch = copyh.std(axis=0)
    copyh = copyh.mean(axis=0)
    std_cl = copyl.std(axis=0)
    copyl = copyl.mean(axis=0)

    std8 = h8_ab.std(axis=0)
    h8_ab = h8_ab.mean(axis=0)
    print(teq_ab[:, 0])
    std_eq = teq_ab.std(axis=0)
    print(std_eq)
    teq_ab = teq_ab.mean(axis=0)

    # Plotting h8 with h0
    ax.errorbar(h0vec, h8_ab, yerr=std8, label='agent-based', marker="x")
    ax.plot(h0vec, h8_ana, label='analytical')
    ax.plot(h0vec, h8_euler, label='numerical', marker="x")
    ax.set(xlabel="n", ylabel="h(t=8)", ylim=[0, 0.85])
    ax.legend()
    ax.grid()

    # Plotting t_eq with h0
    ax1.errorbar(h0vec, teq_ab, yerr=std_eq, label='agent-based', marker="x")
    ax1.plot(h0vec, teq_ana, label='analytical')
    ax1.plot(h0vec, teq_euler, label='numerical', marker="x")
    ax1.legend(loc="upper right")
    ax1.set(xlabel="n", ylabel="t_eq", ylim=[0, 30])
    ax1.grid()

    # Plotting copyh, copyl with n
    fig, ax2 = plt.subplots(figsize=(10, 8))
    r = 20
    for i, n in enumerate(h0vec):
        ax2.errorbar(range(r),
                     copyh[i, :r],
                     yerr=std_ch[i, :r],
                     label="n = " + str(n))
        ax2.errorbar(range(r),
                     copyl[i, :r],
                     yerr=std_cl[i, :r],
                     label="n = " + str(n),
                     ls='--')
    ax2.legend()
Example #4
0
def a_analysis(ax, ax1):
    T = 70
    n_groups = 20
    n = 6
    h0 = 0.5

    K = 20
    h0vec = np.linspace(0, 1, K)

    n_iter = 100

    h8_ab = np.zeros((n_iter, K))
    h8_ana = np.zeros(K)
    h8_euler = np.zeros(K)
    teq_ab = np.zeros((n_iter, K))
    teq_ana = np.zeros(K)
    teq_euler = np.zeros(K)

    for k, a in enumerate(h0vec):
        # Running the agent based
        for i in range(n_iter):
            _, ab, _, _ = basic(T, n_groups, a, n, h0)
            h8_ab[i, k] = ab[8]
            for t, h in enumerate(ab):
                if (a * n > 1 and h < 0.01) or (a * n < 1 and h > 0.99):
                    teq_ab[i, k] = t
                    break

        # Running analytical
        h8_ana[k] = analytical(8, h0, a, n)
        if a * n > 1:
            teq_ana[k] = inverse(0.01, h0, a, n)
        else:
            teq_ana[k] = inverse(0.99, h0, a, n)

        # Running numerical
        _, hnum = euler(T, a, n, step=1, h0=h0)
        h8_euler[k] = hnum[8]
        for t, h in enumerate(hnum):
            if (a * n > 1 and h < 0.01) or (a * n < 1 and h > 0.99):
                teq_euler[k] = t
                break

    std8 = h8_ab.std(axis=0)
    h8_ab = h8_ab.mean(axis=0)
    print(teq_ab[:, ])
    std_eq = teq_ab.std(axis=0)
    teq_ab = teq_ab.mean(axis=0)

    # Plotting h8 with h0
    ax.errorbar(h0vec, h8_ab, yerr=std8, label='agent-based', marker="x")
    ax.plot(h0vec, h8_ana, label='analytical')
    ax.plot(h0vec, h8_euler, label='numerical', marker="x")
    ax.set(xlabel="a", ylabel="h(t=8)", ylim=[0, 0.85])
    ax.legend()
    ax.grid()

    # Plotting t_eq with h0
    ax1.errorbar(h0vec, teq_ab, yerr=std_eq, label='agent-based', marker="x")
    ax1.plot(h0vec, teq_ana, label='analytical')
    ax1.plot(h0vec, teq_euler, label='numerical', marker="x")
    ax1.set(xlabel="a", ylabel="t_eq", ylim=6)
    ax1.set_yscale("log", nonposy='clip')
    ax1.legend(loc="upper right")
    ax1.grid()