def test_2(): """ func = F x = X vertex_tpl = [0,0] itmax = 50 tol = 1.e-15 xpt = .5 """ #xo = ia(-100.,100.) #yo = ia(-100.,100.) xo = ad(ia(-100., 100.), name='x', N=2, dim=0) yo = ad(ia(-100., 100.), name='y', N=2, dim=1) #X = interval_lagrange_tester([xo,yo]) X = [xo, yo] F = test_HC_BC_2 g = F(X) ans, i, F = BC_newton(F, X, [0, 0], tol=1.e-20) print 'test 2' print 'ans = ', ans print 'i iterations = ', i print 'F = ', F print F.grad print F.hess return
def test_problem1(): X1 = ad(ia(-1100., 1400.), name='X1', N=2, dim=0) X2 = ad(ia(-1400., 1200.), name='X2', N=2, dim=1) #X1 = ad(ia(-2.,4.), name = 'X1', N=2, dim = 0) #X2 = ad(ia(-4.,2.), name = 'X2', N=2, dim = 1) #X1 = ad(ia(-11.,14.), name = 'X1', N=2, dim = 0) #X2 = ad(ia(-14.,12.), name = 'X2', N=2, dim = 1) #X1 = ad(ia(-.11,.14), name = 'X1', N=2, dim = 0) #X2 = ad(ia(-.14,.12), name = 'X2', N=2, dim = 1) #C = ad( ia(-20.,20.), name = 'h0', N=2, dim = -1) X = [[X1], [X2]] """ func = thcf tol = 1.e-10 """ sp = UnconstrainedPaver(thcf, X, tol=1.e-10) # #sp.compute_subpaving() """ X1 = ad(ia(-4.1,4.), name = 'X1', N=2, dim = 0) X2 = ad(ia(-4.,4.), name = 'X2', N=2, dim = 1) X = [[X1],[X2]] func = thcf tol = 1.e-20 Bspline_style = True Xn, nit, y, converged = IA.compute_interval_newton_basic(thcf, X,itmax=100, tol = 1.e-25) #""" return X, sp
def use_end_curvature(self): xb = self.vertices[0, 0] x1 = self.vertices[1, 0] x2 = self.vertices[2, 0] x4 = self.vertices[-3, 0] x5 = self.vertices[-2, 0] xe = self.vertices[-1, 1] yb = self.vertices[0, 1] y1 = self.vertices[1, 1] y2 = self.vertices[2, 1] y4 = self.vertices[-3, 1] y5 = self.vertices[-2, 1] ye = self.vertices[-1, 1] ab_e = self.ab_e Cab_given = self.Cab_given Cae_given = self.Cae_given if self.alphab is None: alphab = 0. else: alphab = self.alphab if self.alphae is None: alphae = 0. else: alphae = self.alphae xb = self.vertices[0, 0] yb = self.vertices[0, 1] xe = self.vertices[-1, 0] ye = self.vertices[-1, 1] # curvature at the start: theta1 = ad( 0., 1., 0. ) #initialize alpha1 - we need alpha1 such that x2 sets the curvature = to the desired curvature theta1 = self.solveForTheta(ab_e, alphab, (xe - xb), Cab_given, -1., theta1) self.theta1 = theta1.value x2 = theta1.cos() * (3. * np.sqrt(2.) / 25.) * (xe - xb) + x1 y2 = theta1.sin() * (3. * np.sqrt(2.) / 25.) * (xe - xb) * ab_e + y1 x2 = x2.value y2 = y2.value # curvature at the end: theta4 = ad(0., 1., 0.) theta4 = solveForTheta(ab_e, alphae, (xe - xb), Cae_given, 1., theta4) self.theta4 = theta4.value x4 = theta4.cos() * (-1.) * (3. * np.sqrt(2.) / 25.) * (xe - xb) + x5 y4 = theta4.sin() * (-1.) * (3. * np.sqrt(2.) / 25.) * (xe - xb) * ab_e + y5 x4 = x4.value y4 = y4.value return np.asarray([x2, y2]), np.asarray([x4, y4])
def test_problem1(): #X1 = ad(ia(-1100.,1400.), name = 'X1', N=2, dim = 0) #X2 = ad(ia(-1400.,1200.), name = 'X2', N=2, dim = 1) #X1 = ad(ia(-2.,4.), name = 'X1', N=2, dim = 0) #X2 = ad(ia(-2.,4.), name = 'X2', N=2, dim = 1) X1 = ad(ia(-2., 4.), name='X1', N=2, dim=0) X2 = ad(ia(-4., 2.), name='X2', N=2, dim=1) #X1 = ad(ia(-4.,4.), name = 'X1', N=2, dim = 0) #X2 = ad(ia(-4.,4.), name = 'X2', N=2, dim = 1) #X1 = ad(ia(2.9,3.1), name = 'X1', N=2, dim = 0) #X2 = ad(ia(0.4,.6), name = 'X2', N=2, dim = 1) #X1 = ad(ia(-11.,14.), name = 'X1', N=2, dim = 0) #X2 = ad(ia(-14.,12.), name = 'X2', N=2, dim = 1) #X1 = ad(ia(-.11,.14), name = 'X1', N=2, dim = 0) #X2 = ad(ia(-.14,.12), name = 'X2', N=2, dim = 1) #C = ad( ia(-20.,20.), name = 'h0', N=2, dim = -1) X = [[X1], [X2]] """ func = thcf tol = 1.e-10 """ #1.0 sp = UnconstrainedPaver(thcf, X, tol=1.e-6) #16. sp = UnconstrainedPaver(schwefel1, X, tol=1.e-6) #17. #sp = UnconstrainedPaver(schwefel2,X,tol = 1.e-6) #18. sp = UnconstrainedPaver(schwefel3, X, tol=1.e-3) #20. sp = UnconstrainedPaver(booth, X, tol=1.e-3) #28 #sp = UnconstrainedPaver(schwefel32,X,tol = 1.e-3) #29 sp = UnconstrainedPaver(rosenbrock, X, tol=1.e-2) # #sp.compute_subpaving() """ X1 = ad(ia(-4.1,4.), name = 'X1', N=2, dim = 0) X2 = ad(ia(-4.,4.), name = 'X2', N=2, dim = 1) X = [[X1],[X2]] func = thcf tol = 1.e-6 Bspline_style = True Xn, nit, y, converged = IA.compute_interval_newton_basic(thcf, X,itmax=100, tol = 1.e-25) #""" return X, sp
def my_test1(): x = ad( ia(-10.,10.), N=2, dim = 0) y = ad( ia(-10.,10.), N=2, dim = 1) X = [[x],[y]] sp1 = UnconstrainedPaver(func0, X) self = sp1 #sp1.compute_subpaving() #sp1.plot_subpaving() #print self.ntboxes return X,sp1
def test_problem1(): X1 = ad(ia(-12.,10.), name = 'X1', N=2, dim = 0) X2 = ad(ia(-11.,10.3), name = 'X2', N=2, dim = 1) C = ad( ia(-20.,20.), name = 'h0', N=2, dim = -1) X = [[X1],[X2]] """ func = thcf tol = 1.e-10 """ sp = CurvePaver(thcf,X,C,tol = 0.5) #Xn, nit = IntervalAnalysis.compute_interval_newton_basic(thcf,X,itmax=100) sp.compute_subpaving() return
class AdditionTests(unittest.TestCase): N=3 Imatrix = np.identity(N) gx = ad.adObjectMaker.makeGradient(N,0)#np.matrix([1.,0.,0.]) gy = ad.adObjectMaker.makeGradient(N,1)#np.matrix([0.,1.,0.]) gz = ad.adObjectMaker.makeGradient(N,2)#np.matrix([0.,0.,1.]) H = ad.adObjectMaker.makeHessian(N)#np.matrix( np.zeros((N,N),float) ) xnew = ad.ad(3.0, gx, H) ynew = ad.ad(10., gy, H) znew = ad.ad(4.0, gz, H) x = scalarAD(3.0, gx, H) y = scalarAD(10., gy, H) z = scalarAD(4.0, gz, H) cases = ((x+y, xnew+ynew), (x+y+z, xnew+ynew+znew)) def testOne(self): for old, new in self.cases: self.failUnlessEqual(old, new)
class MultiplictionTests(unittest.TestCase): N=3 Imatrix = np.identity(N) gx = np.matrix([1.,0.,0.]) gy = np.matrix([0.,1.,0.]) gz = np.matrix([0.,0.,1.]) H = np.matrix( np.zeros((N,N),float) ) xnew = ad.ad(3.0, gx, H) ynew = ad.ad(10., gy, H) znew = ad.ad(4.0, gz, H) x = scalarAD(3.0, gx, H) y = scalarAD(10., gy, H) z = scalarAD(4.0, gz, H) cases = ((x*y, xnew*ynew), (x*y*z, xnew*ynew*znew)) def testOne(self): for old, new in self.cases: self.failUnlessEqual(old, new)
def interval_lagrange_tester(X): """ Accepts a vector of ia numbers returns a vector of ad ia numbers set up for the AD class and Lagrangian methods """ n = np.size(X) interval_ad_vector = [] for i, x in enumerate(X): number = ad(x, dim=i, N=n) interval_ad_vector.append(number) return interval_ad_vector
def var_to_ad_ia(var_inf, var_sup, N, dim, name): return ad(ia(var_inf, var_sup), name=name, N=N, dim=dim)
area=72. """ def test_HC_BC_2(V): """ constraint: f(x,y) = x^3 + 100x + 10y = 0 #""" x = V[0] y = V[1] return 10. * y + x**3 + 100. * x xo = ad(ia(-100., 100.), name='x', N=2, dim=0) yo = ad(ia(-100., 100.), name='y', N=2, dim=1) X = [xo, yo] g = test_HC_BC_2(X) print g.value print '' print g.grad print '' print g.hess print '' print 10. * yo + xo**3 + 100. * xo print '' print 3. * (xo**2) + 100. print 3. * 2. * (xo) Fthis = F_Xc(ini_CV, area=ia(50., 90.))
if __name__ == '__main__': X, sp = test_problem1() x1 = X[0][0] x2 = X[1][0] self = sp #""" self.compute_subpaving() self.plot_subpaving() for i, el in enumerate(self.boundary): F = self.f(el()) F.name = 'Minimum {}'.format(i) F.print_components() print 'at min location:' print el() X1 = ad(ia(-4., 2.), name='X1', N=2, dim=0) X2 = ad(ia(-2., 4.), name='X2', N=2, dim=1) #""" """ #X1 = ad(ia(-11.,14.), name = 'X1', N=2, dim = 0) #X2 = ad(ia(-14.,12.), name = 'X2', N=2, dim = 1) #X1 = ad(ia(-.11,.14), name = 'X1', N=2, dim = 0) #X2 = ad(ia(-.14,.12), name = 'X2', N=2, dim = 1) #C = ad( ia(-20.,20.), name = 'h0', N=2, dim = -1) func = thcf tol = 1.e-10 Bspline_style = True V = [[X1],[X2]] #"""
def test_problem2(): X1 = ad(ia(-2., 4.), name='X1', N=2, dim=0) X2 = ad(ia(-4., 2.), name='X2', N=2, dim=1) X = [[X1], [X2]] sp = UnconstrainedPaver(thcf, X, tol=1.e-6) return
class matrixMultiplicationTests(unittest.TestCase): adomg = ad.adObjectMaker.makeGradient adomh = ad.adObjectMaker.makeHessian N=3 xsup = 1. xinf = 0. ysup = 5. yinf = 3. zsup = 11. zinf = 9. gX = adomg(N,0),#np.matrix([ia(1.,1.),ia(0.,0.),ia(0.,0.)]) gY = adomg(N,1)#np.matrix([ia(0.,0.),ia(1.,1.),ia(0.,0.)]) gZ = adomg(N,2)#np.matrix([ia(0.,0.),ia(0.,0.),ia(1.,1.)]) # HX = np.matrix([[ia(0.,0.),ia(0.,0.),ia(0.,0.)], # [ia(0.,0.),ia(0.,0.),ia(0.,0.)], # [ia(0.,0.),ia(0.,0.),ia(0.,0.)]]) # ia_null = ia(0.,0.) # # m1 = np.matrix(np.zeros((N,N),float)) # HX=[] # for i in range(N): # HX.append([]) # for j in range(N): # HX[i].append(ia_null*m1[i,j]) # HX = np.matrix(HX) # # HY = copy.deepcopy(HX) # HZ = copy.deepcopy(HX) HX = adomh(N) HY = adomh(N) HZ = adomh(N) xvnew = np.asarray([ ad.ad( ia(xinf, xsup), gX, HX ), ad.ad( ia(yinf, ysup), gY, HY), ad.ad( ia(zinf, zsup), gZ, HZ)]) from AD import scalarAD from AF import fuzzyNumber Imatrix = np.identity(N) a = 0. b = .5 c = 1. mina = scalarAD( a,np.matrix(Imatrix[0]),np.matrix(np.zeros((N,N),float))) reala = scalarAD( b,np.matrix(Imatrix[0]),np.matrix(np.zeros((N,N),float))) maxa = scalarAD( c,np.matrix(Imatrix[0]),np.matrix(np.zeros((N,N),float))) x1 = fuzzyNumber(mina, reala, maxa) a = 3. b = 4. c = 5. mina = scalarAD( a,np.matrix(Imatrix[1]),np.matrix(np.zeros((N,N),float))) reala = scalarAD( b,np.matrix(Imatrix[1]),np.matrix(np.zeros((N,N),float))) maxa = scalarAD( c,np.matrix(Imatrix[1]),np.matrix(np.zeros((N,N),float))) x2 = fuzzyNumber(mina, reala, maxa) a = 9. b = 10. c = 11. mina = scalarAD( a,np.matrix(Imatrix[2]),np.matrix(np.zeros((N,N),float))) reala = scalarAD( b,np.matrix(Imatrix[2]),np.matrix(np.zeros((N,N),float))) maxa = scalarAD( c,np.matrix(Imatrix[2]),np.matrix(np.zeros((N,N),float))) x3 = fuzzyNumber(mina, reala, maxa) X = [x1,x2,x3] with Timer() as t: r = np.dot(X,X) print "=> elasped time for dot product: %s s" % t.secs xv1 = xvnew+xvnew print 'Time analysis' with Timer() as t: r = np.dot(xvnew,xvnew) print "=> elasped time for dot product: %s s" % t.secs
print 'at min location:' print el print '\n' """ """ self.compute_subpaving() for i, el in enumerate(self.boundary): F = self.f(el) F.name = 'Minimum {}'.format(i) F.value print 'at min location:' print el print '\n' """ X1 = ad(ia(-2.,4.), name = 'X1', N=2, dim = 0) X2 = ad(ia(-2.,4.), name = 'X2', N=2, dim = 1) #X1 = ad(ia(-11.,14.), name = 'X1', N=2, dim = 0) #X2 = ad(ia(-14.,12.), name = 'X2', N=2, dim = 1) #X1 = ad(ia(-.11,.14), name = 'X1', N=2, dim = 0) #X2 = ad(ia(-.14,.12), name = 'X2', N=2, dim = 1) #C = ad( ia(-20.,20.), name = 'h0', N=2, dim = -1) func = thcf #func = schwefel1 #func = schwefel2 tol = 1.e-10 Bspline_style = True #V = [[X1],[X2]] X = [[X1],[X2]] if Bspline_style:
def InitializeControlPoints(xb, yb, alphab, Cab_given, xe, ye, alphae, Cae_given, xc, yc, area, type_specifics={}): """Neglected Documentation, sorry. In general this implements Harries starting control vertices for form parameter generation of B-spline curves """ xwidth = xe - xb # Main program to find an Initial Guess of Vertex Positions alphab = np.radians(alphab) alphae = np.radians(alphae) # Immediate Interior control points: """Given the first and last vertex, the 2nd and 2nd to last vertices are linked to the known vertices as follows:""" # aspect ratio of the enclosing box: if 'SAC' in type_specifics: ab_e = type_specifics['SAC']['maxwidth'] / abs(xe - xb) elif 'DWL' in type_specifics: ab_e = type_specifics['DWL']['maxwidth'] / abs(xe - xb) else: ab_e = abs(ye - yb) / abs(xe - xb) # aspect ration of enclosing box: # ~1. for an approximately quadratic region L_polygon = (xe - xb) * np.sqrt(1. + ab_e) save1 = np.arctan2(np.tan(alphab), ab_e) save2 = np.arctan2(np.tan(alphae), ab_e) db = (2. * np.sqrt(2.) / 25.) * xwidth * np.sqrt(((np.cos(save1))**2) + (ab_e**2) * ((np.sin(save1))**2)) de = (2. * np.sqrt(2.) / 25.) * xwidth * np.sqrt(((np.cos(save2))**2) + (ab_e**2) * ((np.sin(save2))**2)) #recomended coordinates for the immediate interior points on the curve: x1 = xb + db * np.cos(alphab) x5 = xe - de * np.cos(alphae) y1 = yb + db * np.sin(alphab) y5 = ye - de * np.sin(alphae) ## Solve for the 3rd vertex from each end: # Curvature at the ends (if specified there) # determines the location of the 3rd and 3rd to last points. # Solve the non-linear equation for curvature via newton's method. # using function "solveForTheta()" # curvature at the start: theta1 = ad( 0., 1., 0. ) #initialize alpha1 - we need alpha1 such that x2 sets the curvature = to the desired curvature theta1 = solveForTheta(ab_e, alphab, (xe - xb), Cab_given, -1., theta1) x2 = theta1.cos() * (3. * np.sqrt(2.) / 25.) * (xe - xb) + x1 y2 = theta1.sin() * (3. * np.sqrt(2.) / 25.) * (xe - xb) * ab_e + y1 x2 = x2.value y2 = y2.value # curvature at the end: theta4 = ad(0., 1., 0.) theta4 = solveForTheta(ab_e, alphae, (xe - xb), Cae_given, 1., theta4) x4 = theta4.cos() * (-1.) * (3. * np.sqrt(2.) / 25.) * (xe - xb) + x5 y4 = theta4.sin() * (-1.) * (3. * np.sqrt(2.) / 25.) * (xe - xb) * ab_e + y5 x4 = x4.value y4 = y4.value #Now solve for the mid point using the integral parameters # denominator = (2.*area -x1*yb -x2*y1 +x1*y2 -x4*y2 +x2*y4 +x4*y5-x5*y4-xe*y5 ) # store1=6.*xc*area/denominator # store2=(2.*area*(x2+x4)+x1*x1*yb-x1*x2*yb-x1*x4*yb+x1*x2*y1)/denominator # store3=(-x2*x4*y1-x1*x1*y2+x1*x4*y2-x2*x5*y4+x5*x5*y4+xe*xe*y5)/denominator # store4=(-xe*x2*y5-xe*x4*y5+x2*x4*y5+xe*x5*y5-x4*x5*y5)/denominator # x3 = store1-store2-store3-store4 term1 = (1. / (6. * area)) term2 = 2. * area * (x2 + x4) + x1**2 * yb - x1 * x2 * yb - x1 * x4 * yb term3 = x1 * x2 * y1 - x2 * x4 * y1 - x1**2 * y2 + x1 * x4 * y2 - x2 * x5 * y4 + x5**2 * y4 + xe**2 * y5 term4 = -xe * x2 * y5 - xe * x4 * y5 + x2 * x4 * y5 + xe * x5 * y5 - x4 * x5 * y5 termA = term1 * (term2 + term3 + term4) termB = (1. / (6. * area)) * (2. * area - x1 * yb - x2 * y1 + x1 * y2 - x4 * y2 + x2 * y4 - x5 * y4 - xe * y5 + x4 * y5) x3 = (xc - termA) / termB if x3 > x4 or x3 < x2: x3 = (x2 + x4) / 2. #y3 = ((-2.*area+x1*yb+x2*y1-x1*y2+xe*y5+x5*y4-x4*y5)/(x2-x4)) + x3*(y2-y4)/(x2-x4) y3 = ((-2. * area + x1 * yb + x2 * y1 - x1 * y2 + xe * y5 + x5 * y4 - x4 * y5) / (x2 - x4)) y3 += x3 * (y2 - y4) / (x2 - x4) vertices = np.zeros((7, 2), float) vertices[0, 0] = xb vertices[1, 0] = x1 vertices[2, 0] = x2 vertices[3, 0] = x3 vertices[4, 0] = x4 vertices[5, 0] = x5 vertices[6, 0] = xe vertices[0, 1] = yb vertices[1, 1] = y1 vertices[2, 1] = y2 vertices[3, 1] = y3 vertices[4, 1] = y4 vertices[5, 1] = y5 vertices[6, 1] = ye #""" try: print 'checking starting vertices' xmax = type_specifics['xmax'] xmin = type_specifics['xmin'] ymax = type_specifics['ymax'] ymin = type_specifics['ymin'] for vert in vertices: if vert[0] < xmin: vert[0] = xmin if vert[0] > xmax: vert[0] = xmax if vert[1] < ymin: vert[1] = ymin if vert[1] > ymax: vert[1] = ymax print 'augmented starting vertices' except: pass #""" #""" for i in range(1, len(vertices) - 1): if vertices[i, 0] < vertices[i - 1, 0]: vertices[i, 0] = (vertices[i - 1, 0] + vertices[i + 1, 0]) / 2. #""" return vertices
print '' print el[0] print el[1] return if __name__ == '__main__': def func1(X): x = X[0] y = X[1] return (x[0]**2 + y[0]**2) # * x[0].exp() #return (x[0]**2 + x[1]**2 + y[0]**2 + y[1]**2) x = ad(ia(-10., 10.), N=2, dim=0) y = ad(ia(-10., 10.), N=2, dim=1) X = [[x], [y]] x = ad(ia(0.1, 0.3), name='x', N=2, dim=0) y = ad(ia(0.1, 0.3), name='x', N=2, dim=1) X1 = [[x], [y]] #C = ad(ia(.9,1.1), N=2, dim = -1) #C = ad(ia(4.7,5.3), N=2, dim = -1) C = ad(ia(4.0, 5.9), N=2, dim=-1) #C = ia(ia(0.,2.) sp1 = SimplePaver(func1, X, C) self = sp1 sp1.compute_subpaving() sp1.plot_subpaving()