Esempio n. 1
0
def iteration(l: float, r: float, eps: float, log: bool = False) -> float:
    global f_count
    global it_count
    phi = (1 + sqrt(5)) / 2
    middle_left = l + (2 - phi) * (r - l)
    middle_right = r - (2 - phi) * (r - l)
    f_middle_left = f(middle_left)
    f_count += 1
    f_middle_right = f(middle_right)
    f_count += 1
    if log:
        print(f"{0}\t{l}\t{r}\t")
    while r - l >= eps:
        it_count += 1
        old_l = l
        old_r = r
        if f_middle_left < f_middle_right:
            r = middle_right
            middle_right = middle_left
            f_middle_right = f_middle_left
            middle_left = l + (2 - phi) * (r - l)
            f_middle_left = f(middle_left)
            f_count += 1
        else:
            l = middle_left
            middle_left = middle_right
            f_middle_left = f_middle_right
            middle_right = r - (2 - phi) * (r - l)
            f_middle_right = f(middle_right)
            f_count += 1
        if log:
            print(f"{it_count}\t{l}\t{r}\t{(r - l) / (old_r - old_l)}")
    return l + (r - l) / 2
Esempio n. 2
0
def iteration(l: float, r: float, eps: float, log: bool = False) -> float:
    global f_count
    global it_count
    middle = (l + r) / 2
    f_middle = f(middle)
    f_count += 1
    f_r = f(r)
    f_count += 1
    f_l = f(l)
    f_count += 1
    if log:
        print(f"{0}\t{l}\t{r}\t")
    while r - l >= eps:
        it_count += 1
        old_l = l
        old_r = r
        u = middle - (
                (middle - l) ** 2 * (f_middle - f_r) -
                (middle - r) ** 2 * (f_middle - f_l)
        ) / (
                2 * ((middle - l) * (f_middle - f_r) -
                     (middle - r) * (f_middle - f_l))
        )
        if u > r or u < l:
            print(it_count, l, r, u)
            raise ValueError("Wrong segment")
        f_u = f(u)
        f_count += 1

        middle_left = middle
        f_middle_left = f_middle
        middle_right = u
        f_middle_right = f_u
        if middle_left > middle_right:
            middle_left, middle_right = middle_right, middle_left
            f_middle_left, f_middle_right = f_middle_right, f_middle_left

        if f_middle_left < f_middle_right:
            r = middle_right
            f_r = f_middle_right
            middle = middle_left
            f_middle = f_middle_left
        elif f_middle_left > f_middle_right:
            l = middle_left
            f_l = f_middle_left
            middle = middle_right
            f_middle = f_middle_right
        else:
            l = middle_left
            f_l = f_middle_left
            r = middle_right
            f_r = f_middle_right
            middle = (l + r) / 2
            f_middle = f(middle)
            f_count += 1

        if log:
            print(f"{it_count}\t{l}\t{r}\t{(r - l) / (old_r - old_l)}")
    return l + (r - l) / 2
Esempio n. 3
0
def iteration(l: float, r: float, eps: float, log: bool = False) -> float:
    global f_count
    global it_count
    n = 0
    while ((r - l) / eps) > get_fibonacci_number(n + 2):
        n += 1
    middle_left = l + get_fibonacci_number(n) / get_fibonacci_number(n + 2) * (
        r - l)
    middle_right = l + get_fibonacci_number(n + 1) / get_fibonacci_number(
        n + 2) * (r - l)
    f_middle_left = f(middle_left)
    f_count += 1
    f_middle_right = f(middle_right)
    f_count += 1
    if log:
        print(f"{0}\t{l}\t{r}\t")
    if f_middle_left > f_middle_right:
        l = middle_left
        temp_point = middle_right
        f_temp_point = f_middle_right
    else:
        r = middle_right
        temp_point = middle_left
        f_temp_point = f_middle_left
    while r - l >= eps:
        it_count += 1
        old_l = l
        old_r = r
        if temp_point - l < r - temp_point:
            middle_left = temp_point
            f_middle_left = f_temp_point
            middle_right = r - (middle_left - l)
            f_middle_right = f(middle_right)
            f_count += 1
        else:
            middle_right = temp_point
            f_middle_right = f_temp_point
            middle_left = l + (r - middle_left)
            f_middle_left = f(middle_left)
            f_count += 1

        if f_middle_left > f_middle_right:
            l = middle_left
            temp_point = middle_right
            f_temp_point = f_middle_right
        else:
            r = middle_right
            temp_point = middle_left
            f_temp_point = f_middle_left
        if log:
            print(f"{it_count}\t{l}\t{r}\t{(r - l) / (old_r - old_l)}")
    return l + (r - l) / 2
Esempio n. 4
0
def plot_gp(gp, obs_x, obs_y, next, acquisition):
    '''
    Initial source from: https://scikit-learn.org/stable/auto_examples/gaussian_process/plot_gpr_noisy_targets.html
    '''
    if not enable_gp_plots:
        return

    xs = np.linspace(-15, 10, 100).reshape(-1, 1).tolist()
    y_pred, sigma = gp.predict(xs, return_std=True)

    global current_plot
    if current_plot not in active_plots.keys():
        current_plot += 1
        return
    plt.subplot(max_plots, 1, 1 + active_plots[current_plot])
    current_plot += 1

    plt.plot(xs, [main.f(x) for x in xs], 'r:', label=r'$f(x)$')
    plt.plot(obs_x, obs_y, 'r.', markersize=10, label=f'Observations ({len(obs_x)})')
    plt.plot(xs, y_pred, 'b-', label='GP Mean + Stddev')
    plt.fill(np.concatenate([xs, xs[::-1]]),
            np.concatenate([y_pred - sigma,
                            (y_pred + sigma)[::-1]]),
            alpha=.5, fc='b', ec='None')
    plt.axvline(x=next, c='r', ls='--', label='Next x')
    plt.plot(xs, [acquisition(x) for x in xs], 'g--', label="Acquisition")
    plt.xlabel('$x$')
    plt.ylabel('$f(x)$')
    plt.legend()
Esempio n. 5
0
def plot_target():
    xs = np.linspace(-15, 10, 100).reshape(-1, 1).tolist()
    ys = [main.f(x) for x in xs]
    plt.plot(xs, ys, 'r:')
    plt.title("Target function")
    plt.xlabel("x")
    plt.ylabel("f(x)")
    plt.savefig('target.png')
    plt.show()
Esempio n. 6
0
def iteration(l: float, r: float, eps: float) -> Tuple[float, float]:
    global f_count
    middle_left = (l + r) / 2. - eps / 4
    middle_right = (l + r) / 2. + eps / 4
    f_middle_left = f(middle_left)
    f_count += 1
    f_middle_right = f(middle_right)
    f_count += 1
    # print(l, r)
    if f_middle_left < f_middle_right:
        r = middle_right
    elif f_middle_left > f_middle_right:
        l = middle_left
    else:
        l = middle_left
        r = middle_right
    # print(l, r)
    return l, r
Esempio n. 7
0
    def test(self):
        main.FLAGS.equation = "tf.pow(x, 2) - 9"
        main.FLAGS.initial_guess = 2.0
        main.FLAGS.precision = 1e-9

        div = main.f(tf.constant(main.FLAGS.initial_guess))
        with self.test_session():
            div = div.eval()

        self.assertEqual(div, -1.25)
Esempio n. 8
0
 def test_prime_is_ok(self):
     for i in [2, 3, 5]:
         self.assertTrue(f(i))
Esempio n. 9
0
 def test_return(self):
     self.assertEqual(f(), 'qqq')
Esempio n. 10
0
def g():
    f()
    print(MyClass())
    print(path)
    print(CONST)
Esempio n. 11
0
 def test_main(self):
     self.assertEqual(main.f(), 0)
Esempio n. 12
0
def iteration(l: float, r: float, eps: float, log: bool = False) -> float:
    global f_count
    global it_count
    k = (3 - sqrt(5)) / 2
    a, c = l, r
    x = (a + c) / 2
    w, v = x, x
    fx = f(x)
    f_count += 1
    fw, fv = fx, fx
    d = c - a
    e = d
    while c - a >= eps:
        prev_a = a
        prev_c = c
        it_count += 1
        check_parabola = False
        g = e
        e = d
        if x != w and x != v and w != v and fx != fw and fx != fv and fw != fv:
            arr = sorted([(x, fx), (w, fw), (v, fv)])
            l, m, r = arr[0][0], arr[1][0], arr[2][0]
            fl, fm, fr = arr[0][1], arr[1][1], arr[2][1]
            u = parabolas(l, m, r, fl, fm, fr)
            if a <= u <= c and abs(u - x) < g / 2:
                check_parabola = True
                d = abs(u - x)
        if not check_parabola:
            if x < (a + c) / 2:
                u = x + k * (c - x)
                d = c - x
            else:
                u = x - k * (x - a)
                d = x - a

        fu = f(u)
        f_count += 1
        if fu <= fx:
            if u >= x:
                a = x
            else:
                c = x
            v = w
            w = x
            x = u
            fv = fw
            fw = fx
            fx = fu
        else:
            if u >= x:
                c = u
            else:
                a = u
            if fu <= fw or w == x:
                v = w
                w = u
                fv = fw
                fw = fu
            elif fu <= fv or v == x or v == w:
                v = u
                fv = fu
        if log:
            print(f"{it_count}\t{a}\t{c}\t{(c - a) / (prev_c - prev_a)}")
    return (a + c) / 2