Ejemplo n.º 1
0
def transonic_airfoil():
    #x0 = array([0.119087477, 0.160950359,0.203634413,0.192468212,-0.200580639, -0.126010045, 0.107256400e-18])
    x0 = array([0.17042532,0.14831629,0.14576823,0.134351,-0.15162484,-0.13875406,-0.14055989])
    tol  = 1.0e-4
    gtol = 1.0e-4
    maxIter = 50
    xDoe = read_samples('LHC_transonic_af.txt')
    aa = AirfoilAnalysis()
    aa._upd_cst(x0)
    lb = x0 - 0.075
    ub = x0 + 0.075
    err = tol+1.
    niter = 0
    gConverged = False
    xConverged = False
    delta = min([min(xu-x,x-xl) for x,xu,xl in zip(x0,ub,lb)])
    trustRegion = TrustRegionManagement(delta, 0.25, 0.75, 1.25, 0.3, 2.0)

    xDoe = denormalize(xDoe,lb,ub,0)
    fscaled = ScaledFunction(aa.fLow, aa.fHigh,0,'add')
    gscaled = ScaledFunction(aa.gLow, aa.gHigh,0,'add')

    fscaled._initialize_by_doe_points(xDoe)
    #gscaled._initialize_by_doe_points(xDoe)
    def print_header():
        print '\nx1\tx2\tf\trho\tdelta\terr\tgScaled\tgHigh'
    while err>tol:
        fscaled.construct_scaling_model(x0)
        #gscaled.construct_scaling_model(x0)
        bnds = get_bounds(x0,delta,lb,ub)
        cnstr = ({'type':'ineq','fun':aa.g3},{'type':'ineq','fun':aa.g2},)
        rslt = minimize(fscaled,x0,method='SLSQP',bounds=bnds,constraints=cnstr,tol=1e-10,jac=aa.fLowDeriv)
        #rslt = minimize(fscaled,x0,method='SLSQP',bounds=bnds,tol=1e-10)
        xnew = rslt.x
        fnew = rslt.fun
        #gScaledNew = gscaled(xnew)
        rho1, fHighNew = fscaled.get_trust_region_ratio(xnew)
        #rho2, gHighNew = gscaled.get_trust_region_ratio(xnew)
        #rho = min([rho1,rho2])
        rho = rho1
        err = np.linalg.norm([x0-xnew])

        delta = trustRegion.adjust(rho,err)
        x0 = xnew
        #print '\n%.4f\t%.4f\t%.4f\t'%(x0[0],x0[1],fnew),'%.2f\t%.2e\t'%(rho,delta),'%.2e'%err
        print '\n',fnew, rho, delta, err
        niter += 1

#        if (-gtol<=gHighNew<=gtol and gScaledNew<=gtol) or (gHighNew>=0.0 and gScaledNew>gtol):
#            gConverged = True
#        else:
#            gConverged = False
    fscaled.funcHi.display()
    fscaled.funcLo.display()
    gscaled.funcHi.display()
    gscaled.funcLo.display()
Ejemplo n.º 2
0
def numerical_example_2():
#    fHigh  = lambda x: np.exp(x[0]/3) + np.exp(x[1]/5) - x[0]
#    fLow   = lambda x: x[0] + x[1]

    
#    def fHigh(x):
#        x = 0.1*x
#        return ForresterFunc(np.linalg.norm(x)/1.2)+5.*(x[0]+x[1])
#    fLow  = lambda x: np.exp(x[0]/3) + np.exp(x[1]/5) - x[0]
    g1High = lambda x: x[0]**2*x[1]/20. - 1
    g1Low  = lambda x: x[0]**2*x[1]/20. + (x[0]+x[1])/5. - 2
    g2     = lambda x: (x[0]+x[1]-5)**2/30 + (x[0]-x[1]-12)**2/120 - 1
    
    forrester = lambda x: (5.0*x-2.0)**2.0*np.sin(12.*x-4.)
    def fHigh(x):
        x = np.array(x)*0.08
        return forrester(np.linalg.norm(x)) + 5.*np.linalg.norm(x)# + 10.*(x[0]+x[1])
    def fLow(x):
        x = np.array(x)*0.085
        return forrester(np.linalg.norm(x)) + 1.*np.linalg.norm(x)
    tol  = 1.0e-3
    gtol = 1.0e-3
    maxIter = 50
    lb   = np.array([0.0,0.0])
    ub   = np.array([10.,10.])
    x0   = np.array([5.0,5.0])
    
#    dx = 0.1
#    xmsh = ymsh = np.arange(-0.5,10.5,dx)
#    Xmsh, Ymsh = np.meshgrid(xmsh,ymsh)
#    zs1 = np.array([fHigh(np.array([x,y])) for x,y in zip(np.ravel(Xmsh),np.ravel(Ymsh))])
#    zs2 = np.array([g1High(np.array([x,y])) for x,y in zip(np.ravel(Xmsh),np.ravel(Ymsh))])
#    zs3 = np.array([g2(np.array([x,y])) for x,y in zip(np.ravel(Xmsh),np.ravel(Ymsh))])
#    Z1 = zs1.reshape(Xmsh.shape)
#    Z2 = zs2.reshape(Xmsh.shape)
#    Z3 = zs3.reshape(Xmsh.shape)
    

    #plt.show()
    #nDoe = 5 # nDoe>20 creates numerical noise and fails to converge
    
    err = tol+1.
    niter = 0
    gConverged = False
    xConverged = False
    delta = min([min(xu-x,x-xl) for x,xu,xl in zip(x0,ub,lb)])
    trustRegion = TrustRegionManagement(delta, 0.25, 0.75, 1.25, 0.3, 2.0)

    #xDoe = lhs(len(x0),nDoe)
    #xDoe = ff2n(2)
    xDoe = read_samples('2DLHC.txt')
    #xDoe = read_samples()
    xDoe = denormalize(xDoe,lb,ub)
    #print xDoe
    fscaled = ScaledFunction(fLow, fHigh,4,'add')
    gscaled = ScaledFunction(g1Low, g1High,4,'add')
    fscaled._initialize_by_doe_points(xDoe)
    gscaled._initialize_by_doe_points(xDoe)
    
    def print_header():
        print 'x1\tx2\tf\trho\tdelta\terr\tgScaled\tgHigh'
    while xConverged==False or gConverged==False:
        fscaled.construct_scaling_model(x0)
        gscaled.construct_scaling_model(x0)
        bnds = get_bounds(x0,delta,lb,ub)
        cnstr = ({'type':'ineq','fun':gscaled},{'type':'ineq','fun':g2})
        rslt = minimize(fscaled,x0,method='SLSQP',bounds=bnds,constraints=cnstr,tol=1e-10)
        xnew = rslt.x
        fnew = rslt.fun
        gScaledNew = gscaled(xnew)
        rho1, fHighNew = fscaled.get_trust_region_ratio(xnew)
        rho2, gHighNew = gscaled.get_trust_region_ratio(xnew)
        rho = min([rho1,rho2])
        err = np.linalg.norm([x0-xnew])

        delta = trustRegion.adjust(rho,err)
        x0 = xnew
        if niter%5==0:
            print_header()
        print '%.4f\t%.4f\t%.4f\t'%(x0[0],x0[1],fnew),'%.2f\t%.2e\t'%(rho,delta),'%.2e'%err,'%.4f\t%.4f\t'%(gScaledNew,gHighNew)
        niter += 1
        
        if err<=tol or niter>maxIter:
            xConverged=True
        else:
            xConverged=False

        if (-gtol<=gHighNew<=gtol and gScaledNew<=gtol) or (gHighNew>=0.0 and gScaledNew>gtol):
            gConverged = True
        else:
            gConverged = False
        xbounds = np.array([bnds[0][0],bnds[0][0],bnds[0][1],bnds[0][1],bnds[0][0]])
        ybounds = np.array([bnds[1][0],bnds[1][1],bnds[1][1],bnds[1][0],bnds[1][0]])
#        fig = plt.figure(1)
#        ax = fig.add_subplot(111)
#        ax.hold(True)
#        zs4 = np.array([fscaled(np.array([x,y])) for x,y in zip(np.ravel(Xmsh),np.ravel(Ymsh))])
#        zs5 = np.array([gscaled(np.array([x,y])) for x,y in zip(np.ravel(Xmsh),np.ravel(Ymsh))])
#        Z4 = zs4.reshape(Xmsh.shape)
#        Z5 = zs5.reshape(Xmsh.shape)
#        cs1 = ax.contour(Xmsh,Ymsh,Z1,colors='m')#,levels=[0,2,4,6,9,12,15]
#        ax.contour(Xmsh,Ymsh,Z2,colors='r',levels=[0])
#        ax.contour(Xmsh,Ymsh,Z3,colors='k',levels=[0])
#        cs2 = ax.contour(Xmsh,Ymsh,Z4,colors='g')
#        cs3 = ax.contour(Xmsh,Ymsh,Z5,colors='b',levels=[0])
#        tmpx, tmpy = [-5,-5],[1,2]
#        ax.plot(tmpx,tmpy,'m')
#        ax.plot(tmpx,tmpy,'g')
#        ax.plot(tmpx,tmpy,'r')
#        ax.plot(tmpx,tmpy,'b')
#        ax.plot(tmpx,tmpy,'k')
#        ax.plot(np.array(fscaled.xPrev)[:,0],np.array(fscaled.xPrev)[:,1],'ko')
#        ax.plot(xbounds,ybounds,'k--')
#        ax.axis([-0.5,10.5,-0.5,10.5])
#        plt.clabel(cs1, fontsize=9, inline=1)
#        plt.clabel(cs2, fontsize=9, inline=1)
#        plt.clabel(cs3, fontsize=9, inline=1)
#        ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
#        plt.legend(['Hifi objective','Lowfi objective','Hifi constraint 1','Lofi constraint 1','constraint 2','Hifi sample points','Trust region'])
#        plt.show()
#        plt.cla()
        #print xConverged, gConverged, xConverged and gConverged
    fscaled.funcHi.display()
    fscaled.funcLo.display()
    gscaled.funcHi.display()
    gscaled.funcLo.display()
    print niter
    
    xexact = np.array([ 3.11388573,  2.06264578])
    print np.linalg.norm(xexact-xnew)
    print fnew-2.5367139107528409