"""Fixed-kernel tiny Gaussian-process surrogate and two actual lower-bound selections."""
import math,json
def solve(matrix,rhs):
    original=[r[:] for r in matrix];b0=rhs[:];n=len(rhs);a=[list(r)+[v] for r,v in zip(matrix,rhs)]
    for col in range(n):
        pivot=max(range(col,n),key=lambda i:abs(a[i][col]));a[col],a[pivot]=a[pivot],a[col]
        if abs(a[col][col])<1e-12:raise ValueError('singular teaching system')
        d=a[col][col];a[col]=[x/d for x in a[col]]
        for row in range(n):
            if row!=col:
                d=a[row][col];a[row]=[x-d*y for x,y in zip(a[row],a[col])]
    out=[r[-1] for r in a];assert max(abs(sum(original[i][j]*out[j] for j in range(n))-b0[i]) for i in range(n))<1e-10
    return out
def kernel(a,b):return math.exp(-0.5*(a-b)**2)
def posterior(observed,x):
    xs=[p[0] for p in observed];ys=[p[1] for p in observed];n=len(xs)
    matrix=[[kernel(a,b)+(0.01 if i==j else 0) for j,b in enumerate(xs)] for i,a in enumerate(xs)]
    k=[kernel(x,a) for a in xs];alpha=solve(matrix,ys);v=solve(matrix,k)
    mu=sum(a*b for a,b in zip(k,alpha));variance=1-sum(a*b for a,b in zip(k,v));assert variance>=-1e-10
    sd=math.sqrt(max(variance,0));return {'x':x,'mean':mu,'latent_variance':max(variance,0),'latent_sd':sd,'acquisition_mean_minus_sd':mu-sd}
def demo():
    domain=[0.0,0.5,1.0,1.5,2.0];calls=[]
    def objective(x):calls.append(x);return (x-1)**2
    observed=[(0.0,objective(0.0))];rounds=[]
    for number in [1,2]:
        choices=[posterior(observed,x) for x in domain if x not in [a for a,b in observed]]
        selected=min(choices,key=lambda r:(r['acquisition_mean_minus_sd'],r['x']))
        before=observed[:];y=objective(selected['x']);observed.append((selected['x'],y))
        rounds.append({'round':number,'observations_before':before,'candidate_posterior_before':choices,'selected_x':selected['x'],'actual_function_observed_y':y,'posterior_after_at_all_candidates':[posterior(observed,x) for x in domain]})
    assert calls==[0.0,2.0,1.0] and observed[-1]==(1.0,0.0)
    return {'objective':'minimize (x-1)^2 on fixed five-point domain','domain':domain,'prior_mean':0,'kernel':'exp(-0.5*(a-b)^2), amplitude=1, lengthscale=1 fixed','diagonal_noise_variance_assumption':0.01,'actual_objective_noise_added':False,'acquisition':'mean - 1*latent_sd; smallest x breaks exact tie','rounds':rounds,'objective_calls':calls,'total_function_evaluations_including_initial':3,'hyperparameters_fitted':False,'BoTorch_or_GP_library_executed':False}
if __name__=='__main__':print(json.dumps(demo(),ensure_ascii=False,indent=2))
