def test_x_squared_optimization(self): def objective_func(x): return x**2 var_init = np.array([2]) optimizer = Optimizer(objective_func, var_init) min_value, var_value = optimizer.gd_optimize(tolerance=0.0000001, num_iterations=1000) self.assertAlmostEqual(min_value, 0, places=5) self.assertAlmostEqual(var_value[0], 0, places=5)
def test_x_y_z_squared_optimization(self): def objective_func(x, y, z): return x**2 + y**2 + z**2 var_init = np.array([-15, 100, -20]) optimizer = Optimizer(objective_func, var_init) min_value, var_value = optimizer.gd_optimize(tolerance=0.0000001, num_iterations=1000) self.assertAlmostEqual(min_value, 0, places=5) self.assertAlmostEqual(var_value[0], 0, places=5) self.assertAlmostEqual(var_value[1], 0, places=5) self.assertAlmostEqual(var_value[2], 0, places=5) def objective_func(x): return x[0]**2 + x[1]**2 + x[2]**2 var_init = np.array([-15, 100, -20]) optimizer = Optimizer(objective_func, var_init, scalar=False) min_value, var_value = optimizer.gd_optimize(tolerance=0.0000001, num_iterations=1000) self.assertAlmostEqual(min_value, 0, places=5) self.assertAlmostEqual(var_value[0], 0, places=5) self.assertAlmostEqual(var_value[1], 0, places=5) self.assertAlmostEqual(var_value[2], 0, places=5)