Beispiel #1
0
    def cost(x, axis=None, samples=Ns):
        """lower bound on expected model output

    Inputs:
        x: list of NoisyModel hyperparameters
        axis: int, the index of y on which to find bound (all, by default)
        samples: int, number of samples, for a non-deterministic OUQ model

    Returns:
        lower bound on expected value of model output
        """
        # build a model, F(x|a), and tune "a" for optimal F.
        kwds = dict(mu=x[0], sigma=0.0, zmu=x[1], zsigma=0.0,
        #           uid=False, cached=True) # active updates enabled
                    uid=True, cached=False) # active updates disabled
        #print('building model F(x|a)...')
        model = NoisyModel('model', model=toy, nx=nx, ny=ny, **kwds)

        rnd = Ns if model.rnd else None
        #print('building UQ objective of expected model output...')
        b = ExpectedValue(model, bnd, constraint=scons, cvalid=is_cons, samples=rnd)
        i = counter.count()
        #print('solving for lower bound on expected model output...')
        solver = b.lower_bound(axis=axis, id=i, **param)
        if type(solver) is not tuple:
            solver = (solver,) #FIXME: save solver to DB (or pkl)
        if axis is None:
            results = tuple(-s.bestEnergy for s in solver) #NOTE: -1 for GLB
            #print('[id: %s] %s' % (i, tuple(s.bestSolution for s in solver)))
        else:
            results = -solver[axis].bestEnergy #NOTE: -1 for GLB
            #print('[id: %s] %s' % (i, solver[axis].bestSolution))
        return results
    model = NoisyModel('model', model=toy, nx=nx, ny=ny, **approx)
    #print('building estimator G(x) from truth data...')
    kwds = dict(smooth=0.0, noise=0.0, method='thin_plate', extrap=False)
    surrogate = InterpModel('surrogate', nx=nx, ny=ny, data=truth, **kwds)
    #print('building UQ model of model error...')
    error = ErrorModel('error', model=model, surrogate=surrogate)

    rnd = Ns if error.rnd else None
    #print('building UQ objective of expected model error...')
    b = ExpectedValue(error,
                      bnd,
                      constraint=scons,
                      cvalid=is_cons,
                      samples=rnd)
    #print('solving for lower bound on expected model error...')
    b.lower_bound(axis=None, id=0, **param)
    print("lower bound per axis:")
    for axis, solver in b._lower.items():
        print("%s: %s @ %s" % (axis, solver.bestEnergy, solver.bestSolution))

    #print('solving for upper bound on expected model error...')
    param['opts']['termination'] = COG(1e-10, 200)  #NOTE: short stop?
    param['npop'] = 160  #NOTE: increase if poor convergence
    param['stepmon'] = VerboseLoggingMonitor(1,
                                             20,
                                             filename='log.txt',
                                             label='upper')
    b.upper_bound(axis=None, id=1, **param)
    print("upper bound per axis:")
    for axis, solver in b._upper.items():
        print("%s: %s @ %s" % (axis, -solver.bestEnergy, solver.bestSolution))
    # build a model that approximates 'truth'
    #print('building model F(x|a) of truth...')
    approx = dict(mu=-.05, sigma=0., zmu=.05, zsigma=0.)
    model = NoisyModel('model', model=toy, nx=nx, ny=ny, **approx)
    #print('building UQ model of model error...')
    error = ErrorModel('error', model=truth, surrogate=model)

    rnd = Ns if error.rnd else None
    #print('building UQ objective of expected model error...')
    b = ExpectedValue(error,
                      bnd,
                      constraint=scons,
                      cvalid=is_cons,
                      samples=rnd)
    #print('solving for lower bound on expected model error...')
    solver = b.lower_bound(axis=None, **param)
    if type(solver) is not tuple:
        solver = (solver, )
    for s in solver:
        print("lower bound:\n%s @ %s" % (s.bestEnergy, s.bestSolution))

    #print('solving for upper bound on expected model error...')
    param['opts']['termination'] = COG(1e-10, 200)  #NOTE: short stop?
    param['npop'] = 160  #NOTE: increase if poor convergence
    param['stepmon'] = VerboseLoggingMonitor(1,
                                             20,
                                             filename='log.txt',
                                             label='upper')
    solver = b.upper_bound(axis=None, id=1, **param)
    if type(solver) is not tuple:
        solver = (solver, )