Example #1
0
def run_test3():
    xmin, xmax = -5.0, 5.0
    ymin, ymax = -5.0, 5.0
    func = stybtang
    xDOE = mt.read_tabulated_data_without_header('LHS10.txt')
    xDOE = np.vstack([xDOE, [-5,-5]])
    yDOE = np.array([func(xi) for xi in xDOE])
    
    X1 = np.arange(xmin,xmax,0.25)
    X2 = np.arange(ymin,ymax,0.25)
    X1, X2 = np.meshgrid(X1,X2)
    
    X = np.vstack([X1.flatten(),X2.flatten()])
    X = np.transpose(X)
    Y = np.array([t.jackknifing(xDOE,yDOE,xi) for xi in X])
    Y = np.reshape(Y,X1.shape)
    
    Y2 = np.array([t.jackknifing(xDOE,yDOE,xi) for xi in xDOE])
    
    fig1 = plt.figure(1)
    ax1 = fig1.add_subplot(111, projection='3d')
    ax1.hold(True)
    ax1.plot_surface(X1,X2,Y,cmap=cm.jet,rstride=1, cstride=1)
    ax1.plot(xDOE[:,0],xDOE[:,1],Y2,'ro')
    
    plt.show()
def objective(x, X, Y, jnTarget, dMean, surrogate=None):
    jn = t.jackknifing(x, X, Y, surrogate)
    dist = np.array([np.linalg.norm(xi-x) for xi in X])
    dmin = np.sort(dist)
    
    a = 2.0*dmin[:3]/dMean
    f1 = np.exp(-8.0*a**2.0)
    
    #return -(5.0*np.sum(f1) + jn/jnTarget)
    return jn/jnTarget + np.sum(f1)
Example #3
0
def objective_old(x, X, Y, surrogate=None, nFoldSamples=1, dmin=0.05):
    f   = -t.jackknifing(x, X, Y, surrogate, nFoldSamples)
    dist = np.array([np.linalg.norm(x-xi) for xi in X])
    g = dmin - dist
    g = np.array([np.max([gi,0.0])**2.0 for gi in g])
    #phi = f + 100.0*np.sum(G)
    #G = np.min(dist)**2.0
#    g = 100.0/(1.0+1e3*dist*dist)
    G = np.sum(g)
    phi = f + G
    return phi
Example #4
0
def objective(x, X, Y, surrogate=None, p=1, k=0, dmin=0.05):
    f   = -t.jackknifing(x, X, Y, surrogate, p,k)
    dist = np.array([np.linalg.norm(x-xi) for xi in X])
    g = dmin - dist
    G = np.array([np.max([gi,0.0])**2.0 for gi in g])
#    phi = f - 100.0*np.sum(G)
#    G = np.min(dist)**2.0
#    phi = f - 100.0*G
#    g = 100.0/(1.0+1e4*dist*dist)
#    G = np.sum(g)
    phi = 10.0*f + 1e3*np.sum(G)
    return phi
Example #5
0
def get_ffd_jackknife(function, npts):
    X1 = np.linspace(0,1,npts)
    X2 = np.linspace(0,1,npts)
    X1, X2 = np.meshgrid(X1,X2)
    X = np.vstack([X1.flatten(),X2.flatten()])
    X = np.transpose(X)
    jn = np.zeros(len(X))
    Y = np.zeros(len(X))
    for i,x in enumerate(X):
        Y[i] = function(X)
    for i,x in enumerate(X):
        jn[i] = t.jackknifing(x,X,Y)
    return np.average(jn), np.max(jn)
Example #6
0
def run_test1():
    x = np.linspace(-0.05,1.05,100)
    f = tf.Forrester()
    y = np.array([f.high_fidelity(xi) for xi in x])
    
    #xs = np.array([0.0, 0.25, 0.5, 0.75, 1.0])
    xs = np.linspace(0.0,1.0,5)
    ys = np.array([f.high_fidelity(xi) for xi in xs])
    
    model = t.RbfMod(xs,ys)
    ym = np.array([model(xi) for xi in x])
    
    jn = np.array([t.jackknifing(xi, xs, ys, None, 1) for xi in x])
    jns = np.array([t.jackknifing(xi, xs, ys, None, 1) for xi in xs])

    plt.figure(1)
    plt.hold(True)
    plt.plot(x,y,'r-')
    plt.plot(xs,ys,'rs')
    plt.plot(x, ym, 'b--')
    plt.plot(x,ym+0.5*jn, 'k:')
    plt.plot(x,ym-0.5*jn, 'k:')
    plt.fill_between(x,ym+0.5*jn,ym-0.5*jn,color='#dddddd')
    plt.axis([min(x), max(x), None, None])
    plt.legend(['Exact function','Sample points', 'RBF Approximation', 'Jackknife Variance'],loc='upper left')
    plt.xlabel('x')
    plt.ylabel('f(x)')
    
    plt.figure(2)
    plt.hold(True)
    plt.grid(True)
    plt.plot(x,jn,'k-')
    plt.plot(xs,jns,'rs')
    plt.axis([min(x), max(x), -5, None])
    plt.legend(['Jackknife Variance', 'Sample points'],loc='upper left')
    plt.xlabel('x')
    plt.ylabel('Variance')
    plt.show()
Example #7
0
def get_jn_old(x, X, Y, surrogate=None, nFoldSamples=1):
    return t.jackknifing(x, X, Y, surrogate, nFoldSamples)
Example #8
0
def get_jn(x, X, Y, surrogate=None, p=1, k=1):
    return t.jackknifing(x, X, Y, surrogate, p, k)
Example #9
0
def objective(x, X, Y, surrogate=None):
    jn = t.jackknifing(x, X, Y, surrogate)
    return -jn
Example #10
0
def run_test1():
    fhigh = tf.ForresterND(0.40, 0.9, 2.0)
    flow  = tf.ForresterND(0.45, 1.0, 0.0)
    
    tolLow = 0.1
    tolHigh = 0.1
    
    minDistance = 0.05
    
    errHist = list()
    
    xDOE = mt.read_tabulated_data_without_header('LHS30.txt')
    xDOE += 5.0
    xDOE *= 0.1
    yl = np.array([flow(xi) for xi in xDOE])
    
    xl = np.array([0.0, 0.0])
    xu = np.array([1.0, 1.0])
    bnds = np.transpose(np.vstack([xl,xu]))
    
    X1 = np.linspace(xl[0],xu[0],50)
    X2 = np.linspace(xl[1],xu[1],50)
    X1, X2 = np.meshgrid(X1,X2)
    X = np.vstack([X1.flatten(),X2.flatten()])
    X = np.transpose(X)
    
    X1v = np.linspace(xl[0],xu[0],3)
    X2v = np.linspace(xl[1],xu[1],3)
    X1v, X2v = np.meshgrid(X1v,X2v)
    Xv = np.vstack([X1v.flatten(),X2v.flatten()])
    Xv = np.transpose(Xv)
    
    err = tolLow+1
    while err>tolLow:
        jn = np.array([t.jackknifing(xi, xDOE,yl) for xi in Xv])
        jnsortedIdx = np.argsort(jn)
        jnsortedIdx = np.flipud(jnsortedIdx)
        
        match = True
        idx = 0
        while match:
            x0 = Xv[jnsortedIdx[0]]
            #cnstr = ({'type':'ineq','fun':constraint,'args':(X,)})
            rslt = minimize(objective,x0,method='SLSQP',bounds=bnds,args=(xDOE,yl,))
             #               constraints=cnstr)
            xnew = rslt.x
            err = -rslt.fun
            
            dist = np.array([np.linalg.norm(xi-xnew) for xi in xDOE])
            dist = np.min(dist)
            
            if dist <= minDistance:
                match = True
                idx += 1
            else:
                match = False
        print err, xnew, x0, idx
        errHist.append(err)
        xDOE = np.vstack([xDOE,xnew])
        yl = np.array([flow(xi) for xi in xDOE])
    
    print len(xDOE)
    print get_avg_jackknife(xDOE,yl)
    
    Yl = np.array([flow(xi) for xi in X])
    model = mt.RbfMod(xDOE, yl)
    Ym = np.array([model(xi) for xi in X])
    Yl = np.reshape(Yl,X1.shape)
    Ym = np.reshape(Ym,X1.shape)
    
#    fig1 = plt.figure(1)
#    ax1 = fig1.add_subplot(111, projection='3d')
#    ax1.hold(True)
#    ax1.plot_wireframe(X1,X2,Yl,color='r')
#    ax1.plot_wireframe(X1,X2,Ym,color='b')
#    ax1.plot(xDOE[:,0],xDOE[:,1],yl,'ro')
#    ax1.set_xlabel('X1')
#    
#    fig2 = plt.figure(2)
#    ax2 = fig2.add_subplot(111)
#    ax2.plot(range(len(errHist)),errHist,'rs-')
#    ax2.set_yscale('log')
#    plt.show()
#    
    
    # variable fidelity model construction
    flowApprox = mt.RbfMod(xDOE,yl)
    
    xDOEh = mt.read_tabulated_data_without_header('LHS20.txt')
    xDOEh += 5.0
    xDOEh *= 0.1
    #xDOEh = np.array([[0.0,0.0], [0, 1], [1,1], [1,0], [0.5,0.5]])
    yh = np.array([fhigh(xi) for xi in xDOEh])
    yl = np.array([flowApprox(xi) for xi in xDOEh])
    gamma = yh - yl
    
    fscaling = HybridScaling(xDOEh,yh,yl,flowApprox,0.0)
    
    
    # --- vf DOE ---
    
    err = tolHigh+1.0
    while err>tolHigh:
        jn = np.array([t.jackknifing(xi, xDOEh,gamma) for xi in Xv])
        jnsortedIdx = np.argsort(jn)
        jnsortedIdx = np.flipud(jnsortedIdx)
        
        match = True
        idx = 0
        while match:
            x0 = Xv[jnsortedIdx[idx]]
            rslt = minimize(objective,x0,method='SLSQP',bounds=bnds,args=(xDOEh,gamma))
            xnew = rslt.x
            err = -rslt.fun
            
            dist = np.array([np.linalg.norm(xi-xnew) for xi in xDOEh])
            dist = np.min(dist)
            
            if dist <= minDistance:
                match = True
                idx += 1
            else:
                match = False
        print err, xnew, x0, idx
        errHist.append(err)
        xDOEh = np.vstack([xDOEh,xnew])
        yl = np.array([flow(xi) for xi in xDOEh])
        yh = np.array([fhigh(xi) for xi in xDOEh])
        gamma = yh-yl
    
    fscaling = HybridScaling(xDOEh,yh,yl,flowApprox,1.0)
    print len(xDOEh)
    
    
    # --- plot ---
    Yl = np.array([fhigh(xi) for xi in X])
    Ym = np.array([fscaling(xi) for xi in X])
    Yl = np.reshape(Yl,X1.shape)
    Ym = np.reshape(Ym,X1.shape)
    
    print get_avg_jackknife(xDOEh,gamma)

    fig1 = plt.figure(1)
    ax1 = fig1.add_subplot(111, projection='3d')
    ax1.hold(True)
    ax1.plot_wireframe(X1,X2,Yl,color='r')
    ax1.plot_wireframe(X1,X2,Ym,color='b')
    ax1.plot(xDOEh[:,0],xDOEh[:,1],yh,'ro')
    ax1.legend(['high','scaled','samples-high'])
    ax1.set_xlim(0,1)
    ax1.set_ylim(0,1)
    ax1.set_zlim(0,5)
    ax1.set_xlabel('X1')
    ax1.set_ylabel('X2')
    plt.show()
Example #11
0
def get_avg_jackknife(X,Y):
    jn = np.zeros(len(X))
    for i,x in enumerate(X):
        jn[i] = t.jackknifing(x,X,Y)
    return np.average(jn), np.max(jn)