Exemple #1
0
def run_test1():
    fhigh = tf.ForresterND(0.65, 0.9, 2.0)
    flow  = tf.ForresterND(0.7, 1.0, 0.0)
    xl = np.array([0,0],dtype=float)
    xu = np.array([1,1],dtype=float)
    xDOE = get_DOE_data()
    npts = 50 # for plot
    nprobes = 5 #FFD level for probe
    jnTarget = 0.1
    dTarget = 0.2
    minDistance = 0.02

    X1, X2 = t.get_2d_grid(npts, xl, xu)
    X = t.get_ffd_2d(npts, xl, xu)
    xprobe = t.get_ffd_2d(nprobes, xl, xu)
    # ---------------------
    
    yl = np.array([flow(xi) for xi in xDOE])
    model = mt.RbfMod(xDOE, yl)
    Yl = np.array([flow(xi) for xi in X])
    Yl = np.reshape(Yl,X1.shape)
    
    bnds = np.array([[-0.01,0.01],[1.01,1.01]])
    for i in range(25):
        yprobe = np.array([objective(xi, xDOE, yl, jnTarget, dTarget) for xi in xprobe])
        
        match = True
        idx = 0
        while match:
            x0idx = np.argsort(yprobe)[idx]
            x0 = xprobe[x0idx]
            rslt = minimize(objective,x0,method='SLSQP',bounds=bnds,args=(xDOE,yl,jnTarget, dTarget,))
            xnew = t.move_pt_to_bounds(rslt.x,xl,xu)
            fnew = objective(xnew, xDOE,yl,jnTarget, dTarget)
            for xx in xDOE:
                if (xx==xnew).all():
                    match = True
                    idx += 1
                    break
                else:
                    match = False
        print '%.4f\t%.4f\t%.4f\t%d'%(xnew[0],xnew[1],fnew, idx)
        xDOE = np.vstack([xDOE,xnew])
        yl = np.array([flow(xi) for xi in xDOE])
            
            
    model = mt.RbfMod(xDOE, yl)
    Ym = np.array([model(xi) for xi in X])
    Ym = np.reshape(Ym,X1.shape)

    Yobj = np.array([objective(xi, xDOE, yl, jnTarget, dTarget) for xi in X])
    Yobj = np.reshape(Yobj,X1.shape)
    yobj = np.array([objective(xi, xDOE, yl, jnTarget, dTarget) for xi in xDOE])

    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, projection='3d')
    ax2.hold(True)
    ax2.plot_wireframe(X1,X2,Yobj,color='r')
    ax2.plot(xDOE[:,0],xDOE[:,1],yobj,'ro')
    ax2.set_xlabel('X1')

    plt.show()
Exemple #2
0
def run_test1():
    flow  = tf.ForresterND(0.7, 1.0, 0.0)
    xl = np.array([0,0],dtype=float)
    xu = np.array([1,1],dtype=float)
    xDOE = get_DOE_data()
    xDOEinit = get_DOE_data()
    npts = 50 # for plot
    nprobes = 7 #FFD level for probe
    jnTarget = 0.1 # for normalization
    minDistance = 0.05
    maxDistance = 0.50

    X1, X2 = t.get_2d_grid(npts, xl, xu)
    X      = t.get_ffd_2d(npts, xl, xu)
    xprobe = t.get_ffd_2d(nprobes, xl, xu)
    # ---------------------
    
    yl    = np.array([flow(xi) for xi in xDOE])
    model = mt.RbfMod(xDOE, yl)
    Yl    = np.array([flow(xi) for xi in X])
    Yl    = np.reshape(Yl,X1.shape)
    
    bnds = np.array([[-0.01,0.01],[1.01,1.01]])
    
    tol = 0.5
    err = tol+1.0
    while err>=tol:
        yprobe = np.array([objective(xi, xDOE, yl) for xi in xprobe])

        x0idx = np.argsort(yprobe)[0]
        x0    = xprobe[x0idx]
        rslt = minimize(objective,x0,method='SLSQP',bounds=bnds,args=(xDOE,yl,))
        xnew = t.move_pt_to_bounds(rslt.x,xl,xu) #SLSQP bugfix
        fnew = objective(xnew, xDOE,yl)
        
        match = True
        jnbest = 0
        while match:
            for xx in xDOE:
                if (xx==xnew).all():
                    match = True
                    for xi in xDOE:
                        d = np.linalg.norm(xi-xnew)
                        if minDistance<= d <=maxDistance:
                            if objective(xi, xDOE, yl)<=jnbest:
                                xmove = xi
                    xnew = move_pt_dir(xnew, xmove)
                    break
                else:
                    match = False

        print '%d\t%.4f\t%.4f\t%.4f\t%.4f'%(len(xDOE),xnew[0],xnew[1],-fnew,err)
        xDOE = np.vstack([xDOE,xnew])
        yl = np.array([flow(xi) for xi in xDOE])
        jnavg = np.array([objective(xi, xDOE, yl) for xi in xDOE])
        err = -np.average(jnavg)
            
      
    ylinit = np.array([flow(xi) for xi in xDOEinit])      
    model = mt.RbfMod(xDOE, yl)
    Ym = np.array([model(xi) for xi in X])
    Ym = np.reshape(Ym,X1.shape)

    Yobj = np.array([objective(xi, xDOE, yl) for xi in X])
    Yobj = np.reshape(Yobj,X1.shape)
    yobj = np.array([objective(xi, xDOE, yl) for xi in xDOE])

    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.plot(xDOEinit[:,0],xDOEinit[:,1],ylinit,'bs')
    ax1.set_xlabel('X1')

    fig2 = plt.figure(2)
    ax2 = fig2.add_subplot(111, projection='3d')
    ax2.hold(True)
    ax2.plot_wireframe(X1,X2,Yobj,color='r')
    ax2.plot(xDOE[:,0],xDOE[:,1],yobj,'ro')
    ax2.set_xlabel('X1')

    plt.show()
Exemple #3
0
def main():
    #flow  = tf.ForresterND(1.0, 0.9, 2.0)
    flow = tf.stybtang
    #flow  = tf.ForresterND(0.7, 1.0, 0.0)
    xDOEinit = get_DOE_data('LHS30.txt')
    xDOEinit = xDOEinit*10.0-5.0
    xDOE0    = t.get_ffd_2d(11, [-5.0,-5.0], [5.0, 5.0])
    nTry = 10
    nFoldSamples = 1
    k = 10
    #xDOE0    = get_DOE_data('LHS11.txt')
#    xDOE0    = np.vstack([xDOE0,[[0,0],[0,1],[1,0],[1,1]]])
    xl = np.array([-5.0,-5],dtype=float)
    xu = np.array([5,5.0],dtype=float)
    minDistance = 0.1
    tol = 0.01
    runBudget = 75

    npts = 50 # for plot only
     # larger design space: slsqp bug
    xDOE = xDOEinit

    # plot only
    ylinit    = np.array([flow(xi) for xi in xDOEinit])
    X1, X2 = t.get_2d_grid(npts, xl, xu)
    X      = t.get_ffd_2d(npts, xl, xu)
    yl    = np.array([flow(xi) for xi in xDOE])
    model = mt.RbfMod(xDOE, yl)
    Ym0   = np.array([model(xi) for xi in X])
    Ym0   = np.reshape(Ym0,X1.shape)

    # main body
    err = tol+1
    itr = 0
    yl  = np.array([flow(xi) for xi in xDOE])
    bnds = np.array([[-0.005,-0.005],[1.005,1.005]])
    #for zz in range(10):
    npts = len(xDOE)
    while err>tol and runBudget>npts:
        itr += 1
        jn0 = np.array([objective(xi, xDOE, yl, None, nFoldSamples) for xi in xDOE0])
        xsort = xDOE0[np.argsort(jn0)]
        xopt = np.zeros([nTry,xDOE.shape[1]])
        pointFound = False
        i = 0
        while not pointFound:
            x0 = xsort[i]
            i += 1
            rslt = minimize(objective,x0,method='SLSQP',bounds=bnds,args=(xDOE,yl,None,nFoldSamples,k,minDistance,))
            xopt = t.move_pt_to_bounds(rslt.x,xl,xu) #SLSQP bugfix
            #fopt = get_jn(xopt, xDOE,yl,None,nFoldSamples)
            pointFound = check_point(xopt, xDOE, minDistance)
            
        #xopt  = filter_results(xopt,fopt,0.1)
        xDOE = mt.remove_duplicates(np.vstack([xopt,xDOE]))
        npts = len(xDOE)
        yl   = np.array([flow(xi) for xi in xDOE])
        err = t.RRMSE(xDOE,yl)
        #err  = np.average(np.array([get_jn(xi, xDOE, yl,None,nFoldSamples) for xi in xDOE]))
        print '%d\t%d\t%.4e'%(itr, len(xDOE), err)


    yl    = np.array([flow(xi) for xi in xDOE])
    model = mt.RbfMod(xDOE, yl)
    Ym1   = np.array([model(xi) for xi in X])
    Ym1   = np.reshape(Ym1,X1.shape)
    
    Yl    = np.array([flow(xi) for xi in X])
    Yl    = np.reshape(Yl,X1.shape)
    
    Yobj = np.array([objective(xi, xDOE, yl,None,nFoldSamples) for xi in X])
    Yobj = np.reshape(Yobj,X1.shape)
    yobj = np.array([get_jn(xi, xDOE, yl,None,nFoldSamples) for xi in xDOE])
    
    Yobj2 = np.array([get_jn(xi, xDOE, yl,None,nFoldSamples) for xi in X])
    Yobj2 = np.reshape(Yobj2,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,Ym0,color='b')
    ax1.plot_wireframe(X1,X2,Ym1,color='b')
    ax1.plot(xDOE[:,0],xDOE[:,1],yl,'ro')
    ax1.plot(xDOEinit[:,0],xDOEinit[:,1],ylinit,'bs')
    ax1.set_xlabel('X1')
    ax1.set_ylabel('X2')

    fig2 = plt.figure(2)
    ax2 = fig2.add_subplot(111, projection='3d')
    ax2.hold(True)
    ax2.plot_wireframe(X1,X2,Yobj,color='r')
    ax2.plot(xDOE[:,0],xDOE[:,1],yobj,'ro')
    ax2.plot_wireframe(X1,X2,Yobj2,color='b')
    ax2.set_xlabel('X1')
    #ax2.set_zlim([0,16])
    
    
    lvls = np.linspace(-100,250,20)
    
    fig3 = plt.figure(3)
    ax3 = fig3.add_subplot(111)
    ax3.hold(True)
    ax3.plot(xDOE[:,0],xDOE[:,1],'ro')
    ax3.plot(xDOEinit[:,0],xDOEinit[:,1],'bs')
    CS = ax3.contourf(X1,X2, Yl, levels=lvls)
    plt.colorbar(CS, ticks=lvls, format='%.4f')
    ax3.set_xlabel('X1')
    ax3.set_ylabel('X2')
    
    fig4 = plt.figure(4)
    ax4 = fig4.add_subplot(111)
    ax4.hold(True)
    ax4.plot(xDOE[:,0],xDOE[:,1],'ro')
    ax4.plot(xDOEinit[:,0],xDOEinit[:,1],'bs')
    CS1 = ax4.contourf(X1,X2, Ym1, levels=lvls)
    plt.colorbar(CS1, ticks=lvls, format='%.4f')
    ax4.set_xlabel('X1')
    ax4.set_ylabel('X2')
    #ax3.clabel(CS, inline=1, fontsize=10)
    
    

    plt.show()
def create_aero_db():
    af = MyTools.airfoil.airfoil.Airfoil()
    
    af.read_txt('Clark-Y.txt')
    #af.create_naca4()
    
    #
    flow = AeroModel('ClarkY-adaptive.txt')
    flow.plot()
#    fhigh = AeroModel('aero_data_jfoil.txt')
    #flow.plot()
    #fhigh.plot()
    #fhigh = CFDrun(af)

    #fhigh.modelCL.rbf.epsilon = 1e-8
    #flow.plot()
    
    #fhigh.plot()
    
#    def func(x):
#        cl0, cd0, cm0 = flow(x)
#        cl1, cd1, cm1 = fhigh(x)
#        return cl1-cl0, cd1-cd0, cm1-cm0
    
    func = CFDrun(af)
    func.path = 'ClarkY-adaptive.txt'
#    func = AeroModel('ClarkY-C81.txt')
#    func.plot()
#    fhigh = AeroModel('ClarkY-adaptive.txt')
#    fhigh.plot()

    Mach = np.array([0.2, 0.8])
    alpha = np.array([-20.0, 20.0])
    DOEpath = 'LHS30.txt'
    tol          = np.array([0.05, 0.001, 0.01])
    runBudget    = 100
    minDistance  = 0.025
    kFold        = 1
    pOut         = 5
    xDOE0        = t.get_ffd_2d(51, [-1.0, -1], [1.,1.]) # trial initial points
    nptsPlot     = 50
    
    # --- ---
    
    bnds = np.array([[-0.0001,-0.0001],[1.0001,1.0001]])
    _xl = np.array([-1.0, -1.0])
    _xu = np.array([1.0, 1.0])
    xl = np.array([Mach[0], alpha[0]])
    xu = np.array([Mach[1], alpha[1]])
    
    norm = mt.Normalization(xl, xu)
    
    xDOE = mt.read_tabulated_data_without_header(DOEpath)
    #xDOE = t.get_ffd_2d(3,[-1.0, -1], [1.,1.])
    
    #get_aero_data(xDOE[10], af, norm)
    
    data = np.zeros([len(xDOE),3])
    dataNew = np.zeros(3)
    for i,xx in enumerate(xDOE):
        data[i,0], data[i,1], data[i,2] = func(xx)
        xreal = func.norm.denormalize(xx)
        out2 = '%.6f\t%.6f\t%.8f\t%.8f\t%.8f\n'%(xreal[0],xreal[1],data[i,0],data[i,1],data[i,2])
        fid2 = open('ClarkY-ad.txt','at')
        fid2.write(out2)
        fid2.close()
#    data = mt.read_tabulated_data_without_header(func.path)[:,2:]

    itr = 0
    err = tol+1.0
    npts = len(xDOE)
    while np.all(err>tol) and runBudget>npts:
        itr += 1
        dataSetIdx = itr%3
        ds = data[:,dataSetIdx]
        jn0 = np.array([objective(xi, xDOE, ds, None, pOut, kFold) for xi in xDOE0])
        xsort = xDOE0[np.argsort(jn0)]
        #xopt = np.zeros([nTrial,xDOE.shape[1]])
        pointFound = False
        i = 0
        while not pointFound:
            x0 = xsort[i]
            i += 0
            rslt = minimize(objective,x0,method='SLSQP',bounds=bnds,
                            args=(xDOE,ds,None,pOut, kFold,minDistance,))
            xopt = t.move_pt_to_bounds(rslt.x,_xl,_xu) #SLSQP bugfix
            pointFound = check_point(xopt, xDOE, minDistance)
        xDOE = mt.remove_duplicates(np.vstack([xopt,xDOE]))
        npts = len(xDOE)
        dataNew[0], dataNew[1], dataNew[2]   = func(xopt)
        data = np.vstack([dataNew, data])
        err1 = t.RRMSE(xDOE,data[:,0])
        err2 = t.RRMSE(xDOE,data[:,1])
        err3 = t.RRMSE(xDOE,data[:,2])
#        err1  = np.average(np.array([get_jn(xi, xDOE, data[:,0],None, pOut, kFold) for xi in xDOE]))
#        err2  = np.average(np.array([get_jn(xi, xDOE, data[:,1],None, pOut, kFold) for xi in xDOE]))
#        err3  = np.average(np.array([get_jn(xi, xDOE, data[:,2],None, pOut, kFold) for xi in xDOE]))
        err = np.array([err1,err2,err3])
        out = '%d\t%d\t%.4e\t%.4e\t%.4e\n'%(itr, len(xDOE), err[0], err[1], err[2])
        xreal = func.norm.denormalize(xDOE[0])
        out2 = '%.6f\t%.6f\t%.8f\t%.8f\t%.8f\n'%(xreal[0],xreal[1],dataNew[0],dataNew[1],dataNew[2])
        print out
        fid1 = open('aDOE_results.txt','at')
        fid1.write(out)
        fid1.close()
        fid2 = open('ClarkY-ad.txt','at')
        fid2.write(out2)
        fid2.close()

   #--- plot area
    iz = 0

    X1, X2 = t.get_2d_grid(nptsPlot, [-1.0, -1.0], [1.0, 1.0])
    X      = t.get_ffd_2d(nptsPlot, [-1.0, -1.0], [1.0, 1.0])
    X1r, X2r = t.get_2d_grid(nptsPlot, xl, xu)
    #Yl    = np.array([func(xi)[iz] for xi in X])
    #Yl    = np.reshape(Yl,X1.shape)
    xDOEreal = np.zeros(xDOE.shape)
    for i,xd in enumerate(xDOE):
        xDOEreal[i] = norm.denormalize(xd)
    
    Yobj = np.array([objective(xi, xDOE, data[:,iz],None,1, kFold) for xi in X])
    Yobj = np.reshape(Yobj,X1.shape)
    yobj = np.array([objective(xi, xDOE, data[:,iz],None,1, kFold) for xi in xDOE])

    model = mt.RbfMod(xDOE, data[:,iz])
    Ym1   = np.array([model(xi) for xi in X])
    Ym1   = np.reshape(Ym1,X1.shape)

    fig1 = plt.figure(1)
    ax1 = fig1.add_subplot(111, projection='3d')
    ax1.hold(True)
    #.plot_wireframe(X1r,X2r,Yl,color='r')
    #ax1.plot_wireframe(X1,X2,Ym0,color='b')
    ax1.plot_wireframe(X1r,X2r,Ym1,color='b')
    ax1.plot(xDOEreal[:,0],xDOEreal[:,1],data[:,iz],'ro')
    #ax1.plot(xDOEinit[:,0],xDOEinit[:,1],ylinit,'bs')
    ax1.set_xlabel('X1')
    ax1.set_ylabel('X2')
    ax1.set_zlim([-1,1])

    fig2 = plt.figure(2)
    ax2 = fig2.add_subplot(111, projection='3d')
    ax2.hold(True)
    ax2.plot_wireframe(X1,X2,Yobj,color='r')
    ax2.plot(xDOE[:,0],xDOE[:,1],yobj,'ro')
    ax2.set_xlabel('X1')
    
    
    #lvls = np.linspace(-30,20,20)
    
    fig3 = plt.figure(3)
    ax3 = fig3.add_subplot(111)
    ax3.hold(True)
    ax3.plot(xDOE[:,0],xDOE[:,1],'ro')
    #ax3.plot(xDOEinit[:,0],xDOEinit[:,1],'bs')
    #CS = ax3.contourf(X1,X2, Yl, levels=lvls)
    #plt.colorbar(CS, ticks=lvls, format='%.4f')
    ax3.set_xlabel('X1')
    ax3.set_ylabel('X2')
    
    fig4 = plt.figure(4)
    ax4 = fig4.add_subplot(111)
    ax4.hold(True)
    ax4.plot(xDOE[:,0],xDOE[:,1],'ro')
    #ax4.plot(xDOEinit[:,0],xDOEinit[:,1],'bs')
    #CS1 = ax4.contourf(X1,X2, Ym1, levels=lvls)
    #plt.colorbar(CS1, ticks=lvls, format='%.4f')
    ax4.set_xlabel('X1')
    ax4.set_ylabel('X2')
    #ax3.clabel(CS, inline=1, fontsize=10)
    
    plt.show()