def case_6(): '''Find stationary point of Easom's function. Stationary points include (pi, pi), (pi/2, pi/2).''' def f(variables): x, y = variables return -np.cos(x) * np.cos(y) * np.exp(-((x - np.pi)**2 + (y - np.pi)**2)) f_string = 'f(x, y) = -\cos(x)\cos(y)\exp(-((x-\pi)^2 + (y-\pi)^2))' x0 = 2.5 y0 = 2.5 init_vars = [x0, y0] solution, xy_path, f_path = opt.BFGS(f, init_vars, iters=10000) opt.plot_results(f, xy_path, f_path, f_string, threedim=True, bfgs=True, hide=True) xn, yn = solution.val assert (np.allclose(xn, np.pi) and np.allclose(yn, np.pi)) plt.close('all')
def case_7(): '''Find stationary point of Himmelblau's function.''' def f(variables): x, y = variables return (x**2 + y - 11)**2 + (x + y**2 - 7)**2 f_string = 'f(x, y) = (x^2 + y - 11)^2 + (x + y^2 - 7)^2' x0 = 5 y0 = 5 init_vars = [x0, y0] solution, xy_path, f_path = opt.BFGS(f, init_vars, iters=10000) opt.plot_results(f, xy_path, f_path, f_string, x_lims=(-10, 10), y_lims=(-10, 10), threedim=True, animate=True, bfgs=True, hide=True) xn, yn = solution.val assert ((np.allclose(xn, 3) and np.allclose(yn, 2)) or (np.allclose(xn, -2.805118) and np.allclose(yn, 3.131312)) or (np.allclose(xn, -3.779310) and np.allclose(yn, -3.283186)) or (np.allclose(xn, 3.584428) and np.allclose(yn, -1.848126))) plt.close('all')
def case_5(): '''Find stationary point of bowl function: f(x, y) = x^2 + y^2 + 2. The global minimum is 2 when (x, y) = (0, 0). ''' def f(variables): x, y = variables return x**2 + y**2 + 2 f_string = 'f(x, y) = x^2 + y^2 + 2' for x_val, y_val in [[2., 3.], [-2., 5.]]: x0 = x_val y0 = y_val init_vars = [x0, y0] solution, xy_path, f_path = opt.BFGS(f, init_vars) xn, yn = solution.val opt.plot_results(f, xy_path, f_path, f_string, x_lims=(-7.5, 7.5), y_lims=(-7.5, 7.5), threedim=True, animate=True, bfgs=True, hide=True) minimum = [0, 0] assert np.allclose([xn, yn], minimum) der = [0, 0] assert np.allclose(solution.der, der) plt.close('all')
def case_3(): '''Find stationary point of f(x) = sin(x).''' def f(x): return np.sin(x) f_string = 'f(x) = sin(x)' for x_val in [-3, -1, 1, 3]: # Test case when 1D input is a list x0 = [da.Var(x_val)] solution, x_path, f_path = opt.BFGS(f, x0) opt.plot_results(f, x_path, f_path, f_string, x_lims=(-2 * np.pi, 2 * np.pi), y_lims=(-1.5, 1.5), bfgs=True, hide=True) # BFGS finds a stationary point, which are every pi offset from pi/2 first = solution.val - (np.pi / 2) multiple_of_one_half_pi = ((abs(solution.val) - (np.pi / 2)) % (np.pi)) np.testing.assert_array_almost_equal(multiple_of_one_half_pi, [0]) np.testing.assert_array_almost_equal(solution.der, [0]) plt.close('all')
def case_1(): '''Simple quadratic function with stationary point at x = 0.''' def f(x): return x**2 f_string = 'f(x) = x^2' for x_val in [-4, -2, 0, 2, 4]: x0 = da.Var(x_val) solution, x_path, f_path = opt.BFGS(f, x0) opt.plot_results(f, x_path, f_path, f_string, bfgs=True, hide=True) assert np.allclose(solution.val, [0]) assert np.allclose(solution.der, [0]) plt.close('all')
def case_4(): '''Find stationary point of Rosenbrock function: f(x, y) = 4(y - x^2)^2 + (1 - x)^2. The global minimum is 0 when (x, y) = (1, 1). ''' def f(variables): x, y = variables return 4 * (y - (x**2))**2 + (1 - x)**2 f_string = 'f(x, y) = 4(y - x^2)^2 + (1 - x)^2' for x_val, y_val in [[-6., -6], [2., 3.], [-2., 5.]]: x0 = da.Var(x_val, [1, 0]) y0 = da.Var(y_val, [0, 1]) init_vars = [x0, y0] solution, xy_path, f_path = opt.BFGS(f, init_vars, iters=25000) xn, yn = solution.val opt.plot_results(f, xy_path, f_path, f_string, x_lims=(-7.5, 7.5), y_lims=(-7.5, 7.5), threedim=True, animate=True, bfgs=True, hide=True) minimum = [1, 1] assert np.allclose([xn, yn], minimum) # dfdx = 2(200x^3 - 200xy + x - 1) # dfdy = 200(y - x^2) der_x = 2 * (8 * (xn**3) - 8 * xn * yn + xn - 1) der_y = 8 * (yn - (xn**2)) assert np.allclose(solution.der, [der_x, der_y]) plt.close('all')
def case_2(): '''Stationary point (minimum) at x = 1.''' def f(x): return (x - 1)**2 + 3 f_string = 'f(x) = (x - 1)^2 + 3' for x_val in [-3, -1, 1, 3, 5]: # Test initial guess without using da.Var type solution, x_path, f_path = opt.BFGS(f, x_val) opt.plot_results(f, x_path, f_path, f_string, x_lims=(-3, 5), y_lims=(2, 10), animate=True, bfgs=True, hide=True) assert np.allclose(solution.val, [1]) assert np.allclose(solution.der, [0]) plt.close('all')