def SchemeUniform_OptInner(u, SB, f, bc, oracle=None): # Use the oracle, if available, to select the active superbases only if not (oracle is None): SB = np.take_along_axis(SB, np.broadcast_to( oracle, SB.shape[:2] + (1, ) + oracle.shape), axis=2) d2u = bc.Diff2(u, SB) d2u[..., bc.not_interior] = 0. # Placeholder value to silent NaN warnings # Generate the parameters for the low dimensional optimization problem Q = 0.5 * np.array([[0, 1, 1], [1, 0, 1], [1, 1, 0]]) dim = 2 l = -d2u m = lp.dot_VV(SB, SB) m = bc.as_field(m) from agd.FiniteDifferences import as_field Q = as_field(Q, m.shape[1:]) dim = 2 alpha = dim * f**(1 / dim) mask = (alpha == 0) Q = Q * np.where(mask, 1., alpha**2) # Evaluate the non-linear functional using dense-sparse composition residue = ad.apply(ConstrainedMaximize, Q, l, m, shape_bound=u.shape).copy() residue[:, mask] = np.max(l / m, axis=0)[:, mask] return ad.max_argmax(residue, axis=0)
def SchemeMALBR_Opt(u,SB,f,bc): # Evaluate using the envelope theorem result,_ = ad.apply(SchemeMALBR_OptInner, u,bc.as_field(SB),bc, envelope=True) # Boundary conditions return np.where(bc.interior, f - result, u-bc.grid_values)
def SchemeSampling_Opt(u, diffs, beta, bc): # Evaluate the operator using the envelope theorem result, _ = ad.apply(SchemeSampling_OptInner, u, bc.as_field(diffs), bc, envelope=True) # Boundary conditions return np.where(bc.interior, beta - result, u - bc.grid_values)
def SchemeUniform_Opt(u, SB, f, bc): # Evaluate the maximum over the superbases using the envelope theorem residue, _ = ad.apply(SchemeUniform_OptInner, u, bc.as_field(SB), f, bc, envelope=True) return ad.where(bc.interior, residue, u - bc.grid_values)
def SchemeMALBR_OptInner(u,SB,bc,oracle=None): # If the active superbases are known, then take only these if not(oracle is None): SB = np.take_along_axis(SB,np.broadcast_to(oracle,SB.shape[:2]+(1,)+oracle.shape),axis=2) d2u = bc.Diff2(u,SB) d2u[...,bc.not_interior] = 0. # Placeholder value to silent NaN warnings # Evaluate the complex non-linear function using dense - sparse composition result = ad.apply(MALBR_H,d2u,shape_bound=u.shape) return ad.min_argmin(result,axis=0)
def SchemeConsistent_Opt(u, alpha, beta, bc): value, _ = ad.apply(MinimizeTrace_Opt, u, alpha, bc, envelope=True) residue = beta - value return np.where(bc.interior, residue, u - bc.grid_values)