def setUp(self): avail_funcs = ['3h-camel', 'beale', 'mc-cormick', 'rosenbrock'] # Instancing the available functions to test optimizer self.funcs = {} for fun_id in avail_funcs: self.funcs[fun_id] = CFunction.create(fun_id)
def test_2D(self): """Plot of a 2D example.""" grid_limits = [(-4, 4), (-4, 4)] A = CArray.eye(2, 2) b = CArray.zeros(2).T circle = CFunction.create('quadratic', A, b, 0) self._test_2D(circle, grid_limits, levels=[16])
def _quadratic_fun(d): """Creates a quadratic function in d dimensions.""" def _quadratic_fun_min(A, b): from scipy import linalg min_x_scipy = linalg.solve((2 * A).tondarray(), -b.tondarray(), sym_pos=True) return CArray(min_x_scipy).ravel() A = CArray.eye(d, d) b = CArray.ones((d, 1)) * 2 discr_fun = CFunction.create('quadratic', A, b, c=0) min_x = _quadratic_fun_min(A, b) min_val = discr_fun.fun(min_x) discr_fun.global_min = lambda: min_val discr_fun.global_min_x = lambda: min_x return discr_fun
def setUp(self): self.fun = CFunction.create('mc-cormick')
def setUp(self): self.fun = CFunction.create('rosenbrock')
def setUp(self): self.fun = CFunction.create('3h-camel')
def setUp(self): self.fun = CFunction.create('beale')
def setUp(self): A = CArray.eye(2, 2) b = CArray.zeros((2, 1)) c = 0 self.fun = CFunction.create('quadratic', A, b, c)
def setUp(self): self.test_funcs = dict() # Instancing the available functions to test optimizer self.test_funcs['3h-camel'] = { 'fun': CFunction.create('3h-camel'), 'x0': CArray([1, 1]), 'grid_limits': [(-2, 2), (-2, 2)], 'vmin': 0, 'vmax': 5 } self.test_funcs['beale'] = { 'fun': CFunction.create('beale'), 'x0': CArray([0, 0]), 'grid_limits': [(-1, 4.5), (-1, 1.5)], 'vmin': 0, 'vmax': 1 } self.test_funcs['mc-cormick'] = { 'fun': CFunction.create('mc-cormick'), 'x0': CArray([0, 1]), 'grid_limits': [(-2, 3), (-3, 1)], 'vmin': -2, 'vmax': 2 } self.test_funcs['rosenbrock'] = { 'fun': CFunction.create('rosenbrock'), 'x0': CArray([-1, -1]), 'grid_limits': [(-2.1, 1.1), (-2.1, 1.1)], 'vmin': 0, 'vmax': 10 } quad = self._quadratic_fun(2) self.test_funcs['quad-2'] = { 'fun': quad, 'x0': CArray([4, -4]), 'grid_limits': [(-5, 5), (-5, 5)], 'vmin': None, 'vmax': None } n = 100 quad = self._quadratic_fun(n) self.test_funcs['quad-100-sparse'] = { 'fun': quad, 'x0': CArray.zeros((n, ), dtype=int).tosparse(dtype=int), } n = 2 poly = self._create_poly(d=n) self.test_funcs['poly-2'] = { 'fun': poly, 'x0': CArray.ones((n, )) * 2, 'vmin': -10, 'vmax': 5, 'grid_limits': [(-1, 1), (-1, 1)] } n = 100 # x0 is a sparse CArray and the solution is a zero vector poly = self._create_poly(d=n) self.test_funcs['poly-100-int'] = { 'fun': poly, 'x0': CArray.ones((n, ), dtype=int) * 2 } n = 100 poly = self._create_poly(d=n) self.test_funcs['poly-100-int-sparse'] = { 'fun': poly, 'x0': CArray.ones((n, ), dtype=int).tosparse(dtype=int) * 2 }