def __init__(self): domain = build_golinski_design_domain() funs = [ golinski_volume, golinski_constraint1, golinski_constraint2, golinski_constraint3, golinski_constraint4, golinski_constraint5, golinski_constraint6, golinski_constraint7, golinski_constraint8, golinski_constraint9, golinski_constraint24, golinski_constraint25, ] grads = [ golinski_volume_grad, golinski_constraint1_grad, golinski_constraint2_grad, golinski_constraint3_grad, golinski_constraint4_grad, golinski_constraint5_grad, golinski_constraint6_grad, golinski_constraint7_grad, golinski_constraint8_grad, golinski_constraint9_grad, golinski_constraint24_grad, golinski_constraint25_grad, ] Function.__init__(self, funs, domain, grads=grads, vectorized=True)
def __init__(self): domain = build_beam_domain() funs = [ beam_area, beam_bending, beam_tip_deflect, beam_bending_stress, beam_shear_stress, beam_area_ratio, beam_twist_buckling, ] grads = [ beam_area_grad, beam_bending_grad, beam_tip_deflect_grad, beam_bending_stress_grad, beam_shear_stress_grad, beam_area_ratio_grad, beam_twist_buckling_grad, ] Function.__init__(self, funs, domain, grads=grads, vectorized=True, kwargs=BEAM)
def test_return_grad2(m=3): A = np.random.randn(m, m) A += A.T B = np.random.randn(m, m) B += B.T def func(x, return_grad = False): fx = [0.5*x.dot(A.dot(x)), 0.5*x.dot(B.dot(x))] if return_grad: grad = [A.dot(x), B.dot(x)] return fx, grad else: return fx dom = BoxDomain(-2*np.ones(m), 2*np.ones(m)) fun = Function(func, dom, return_grad = True) X = fun.domain.sample(4) fX, grads = fun(X, return_grad = True) assert fX.shape == (len(X), 2) assert grads.shape == (len(X), 2, m) for x, grad in zip(X, grads): assert np.all(np.isclose(grad, fun.grad(x)))
def test_func(): client = Client(processes = False) dom = BoxDomain(-1,1) fun = Function(func, dom, dask_client = client) X = dom.sample(5) res = fun.eval_async(X) for r, x in zip(res, X): print(r.result()) assert np.isclose(x, r.result())
def __init__(self, dask_client=None): domain = build_robot_arm_domain() funs = [robot_arm] grads = [robot_arm_grad] Function.__init__(self, funs, domain, grads=grads, vectorized=True, dask_client=dask_client)
def __init__(self, n_lower=10, n_upper=10, fraction=0.01, dask_client=None, **kwargs): domain = build_hicks_henne_domain(n_lower, n_upper, fraction=fraction) kwargs.update({'n_lower': n_lower, 'n_upper': n_upper}) Function.__init__(self, naca0012_func, domain, vectorized=False, kwargs=kwargs, dask_client=dask_client, return_grad=True)
def __init__(self, domain='deterministic', dask_client=None): assert domain in ['deterministic', 'uncertain'] if domain == 'deterministic': domain = build_borehole_domain() else: domain = build_borehole_uncertain_domain() funs = [borehole] grads = [borehole_grad] Function.__init__(self, funs, domain, grads=grads, vectorized=True, dask_client=dask_client)
def test_lambda(): dom = BoxDomain(-1,1) def f(x): return x #f = lambda x: x print('about to start client') # We use a threaded version for sanity # https://github.com/dask/distributed/issues/2515 client = Client(processes = False) print(client) fun = Function(f, dom, dask_client = client) x = dom.sample(1) res = fun.eval_async(x) print(x, res.result()) assert np.isclose(x, res.result())
def test_mult_output(M= 10,m = 5): dom = BoxDomain(-np.ones(m), np.ones(m)) X = dom.sample(M) a = np.random.randn(m) b = np.random.randn(m) def fun_a(X): return a.dot(X.T) def fun_b(X): return b.dot(X.T) def fun(X): return np.vstack([X.dot(a), X.dot(b)]).T print("Single function with multiple outputs") for vectorized in [True, False]: myfun = Function(fun, dom, vectorized = vectorized) print(fun(X)) print("vectorized", vectorized) print(myfun(X).shape) assert myfun(X).shape == (M, 2) print(myfun(X[0]).shape) assert myfun(X[0]).shape == (2,) fX = fun(X) for i, x in enumerate(X): assert np.all(np.isclose(fX[i], fun(x))) print("Two functions with a single output each") for vectorized in [True, False]: myfun = Function([fun_a, fun_b], dom, vectorized = vectorized) print(fun(X)) print("vectorized", vectorized) print(myfun(X).shape) assert myfun(X).shape == (M, 2) print(myfun(X[0]).shape) assert myfun(X[0]).shape == (2,) fX = fun(X) for i, x in enumerate(X): assert np.all(np.isclose(fX[i], fun(x)))
def test_finite_diff(): from psdr.demos.golinski import golinski_volume, build_golinski_design_domain dom = build_golinski_design_domain() fun = Function(golinski_volume, dom, fd_grad = True) x = fun.domain.sample() print(x) print(fun(x)) print(fun.grad(x)) err = check_derivative(x, fun.eval, fun.grad) print(err) assert err < 1e-5 # Check asking for multiple gradients X = fun.domain.sample(5) grads = fun.grad(X) for i, x in enumerate(X): assert np.all(np.isclose(grads[i], fun.grad(x)))
def __init__(self, truncate=1e-7, level=0, su2_maxiter=5000, workdir=None, dask_client=None, **kwargs): self.design_domain_app = buildDesignDomain(output='none', solver='CVXOPT') self.design_domain_norm = self.design_domain_app.normalized_domain() self.design_domain = self.design_domain_norm self.random_domain_app = buildRandomDomain(truncate=truncate) self.random_domain_norm = self.random_domain_app.normalized_domain() self.random_domain = self.random_domain_norm domain = self.design_domain_app * self.random_domain_app Function.__init__(self, multif, domain, vectorized=False, dask_client=dask_client, kwargs=kwargs)
def __init__(self, linear=None, constant=None, domain=None): if linear is None: if domain is not None: self.linear = np.random.randn(len(domain)) else: self.linear = np.random.randn(5) else: self.linear = np.array(linear).flatten() if constant is None: self.constant = 0 else: self.constant = float(constant) if domain is None: domain = BoxDomain(-np.ones(len(self.linear)), np.ones(len(self.linear))) Function.__init__(self, self._func, domain, grads=self._func_grad, vectorized=True)
def __init__(self, quad=None, linear=None, constant=None, domain=None): # Determine dimension of space if quad is not None: m = len(quad) elif linear is not None: m = len(linear) elif domain is not None: m = len(domain) else: m = 5 if quad is not None: self.quad = np.array(quad).reshape(m, m) else: self.quad = np.eye(m) if linear is not None: self.linear = np.array(linear).reshape(m) else: self.linear = np.zeros(m) if constant is not None: self.constant = float(constant) else: self.constant = 0. if domain is None: domain = BoxDomain(-np.ones(len(self.linear)), np.ones(len(self.linear))) Function.__init__(self, self._func, domain, grads=self._func_grad, vectorized=True)
def test_gp_fit(m=3, M=100): """ check """ dom = BoxDomain(-np.ones(m), np.ones(m)) a = np.ones(m) b = np.ones(m) b[0] = 0 f = lambda x: np.sin(x.dot(a)) + x.dot(b)**2 fun = Function(f, dom) X = dom.sample(M) fX = f(X) for structure in ['const', 'diag', 'tril']: for degree in [None, 0, 1]: gp = GaussianProcess(structure=structure, degree=degree) gp.fit(X, fX) print(gp.L) I = ~np.isclose(gp(X), fX) print(fX[I]) print(gp(X[I])) assert np.all(np.isclose( gp(X), fX, atol=1e-5)), "we should interpolate samples" _, cov = gp.eval(X, return_cov=True) assert np.all(np.isclose( cov, 0, atol=1e-3)), "Covariance should be small at samples"
def __init__(self, n_lower=10, n_upper=10): domain = build_hicks_henne_domain(n_lower, n_upper) # build_oas_design_domain() * build_oas_robust_domain() * build_oas_random_domain() Function.__init__(self, oas_func, domain, vectorized=True)
def __init__(self): domain = build_oas_design_domain() * build_oas_robust_domain( ) * build_oas_random_domain() Function.__init__(self, oas_func, domain, vectorized=True)
def __init__(self, dask_client = None): domain = build_piston_domain() funs = [piston] grads = [piston_grad] Function.__init__(self, funs, domain, grads = grads, vectorized = True, dask_client = dask_client)
def __init__(self): domain = build_hartmann_domain() funs = [u_ave, Bind] grads = [u_ave_grad, Bind_grad] Function.__init__(self, funs, domain, grads=grads, vectorized=True)
def test_return_grad(m=3): A = np.random.randn(m, m) A += A.T def func(x, return_grad = False): fx = 0.5*x.dot(A.dot(x)) if return_grad: grad = A.dot(x) return fx, grad else: return fx dom = BoxDomain(-2*np.ones(m), 2*np.ones(m)) x = dom.sample(1) fun = Function(func, dom, return_grad = True) # Check the derivative x_norm = dom.normalize(x) err = check_derivative(x_norm, fun.eval, fun.grad) assert err < 1e-5 # Check wrapping fx, grad = func(x, return_grad = True) assert np.isclose(fx, fun(x_norm)) # multiply the grad by two to correct the change of coordinates assert np.all(np.isclose(2*grad, fun.grad(x_norm))) # Check multiple outputs X = dom.sample(10) fX, grads = fun(X, return_grad = True) for i, x in enumerate(X): assert np.isclose(fun(x), fX[i]) assert np.all(np.isclose(fun.grad(x), grads[i])) # Check vectorized functions def func2(X, return_grad = False): X = np.atleast_2d(X) fX = np.vstack([0.5*x.dot(A.dot(x)) for x in X]) if return_grad: grad = X.dot(A) return fX, grad else: return fX fun2 = Function(func2, dom, vectorized = True, return_grad = True) x = fun2.domain.sample() X = fun2.domain.sample(5) assert np.isclose(fun2(x), fun(x)) assert np.all(np.isclose(fun2(X), fun(X))) assert np.all(np.isclose(fun2.grad(X), fun.grad(X))) # Check the __call__ interface fX, grad = fun2(X, return_grad = True) print(fX.shape, fun(X).shape) assert fX.shape == fun(X).shape assert np.all(np.isclose(fX, fun(X))) print(grad.shape, fun.grad(X).shape) assert grad.shape == fun.grad(X).shape assert np.all(np.isclose(grad, fun.grad(X))) fx, grad = fun2(x, return_grad = True) print(fx.shape, fun(x).shape) assert fx.shape == fun(x).shape assert np.all(np.isclose(fx, fun(x))) print(grad.shape, fun.grad(x).shape) assert grad.shape == fun.grad(x).shape assert np.all(np.isclose(grad, fun.grad(x))) fX, grad = fun(X, return_grad = True) print(fX.shape, fun(X).shape) assert fX.shape == fun(X).shape assert np.all(np.isclose(fX, fun(X))) print(grad.shape, fun.grad(X).shape) assert grad.shape == fun.grad(X).shape assert np.all(np.isclose(grad, fun.grad(X))) fx, grad = fun(x, return_grad = True) print(fx.shape, fun(x).shape) assert fx.shape == fun(x).shape assert np.all(np.isclose(fx, fun(x))) print(grad.shape, fun.grad(x).shape) assert grad.shape == fun.grad(x).shape assert np.all(np.isclose(grad, fun.grad(x)))