How to use test_converge method in molecule

Best Python code snippet using molecule_python

integral.py

Source:integral.py Github

copy

Full Screen

1# -------------------------------------------------------------------2# Library Program:3# The following program contains various functions to solve ordinary4# integrals using various methods.5# -------------------------------------------------------------------6# PRIMITIVE RULES (SINGLE VARIABLE FUNCTIONS)7def rectangular(a,b,N,f): #removed ELs8 '''9 a ; lower-bound for integration10 b ; upper-bound for integration11 N ; number of iterations/subintervals (must be integer)12 f ; function of x (can be user-defined)13 14 The (left/right) rectangular approximation method computes the15 integral of a function by assuming that the area between the16 function and the axis over which the function is being integrated17 can be defined as the area of a series of rectangles, for a18 series of subintervals bound by successive values of x with19 constant step size ;20 xn - xn-121 For this approximation method, the left/right uppermost corner of22 a rectangle has coordinates computed using the given function,23 then calculates the rectangular area ;24 f(xn) * (xn - xn-1) [for LRAM], or25 f(xn) * (xn+1 - xn) [for RRAM]26 27 These areas are iterated for all subintervals n in N, with the28 sum of the recatangular areas approximating the integral for the29 given function between its lower/upper bounds.30 '''31 dx = (b-a)/N32 Rl = 033 Rr = 034 for n in range(0,N):35 Rl += dx * f(a+n*dx)36 for n in range(1,N+1):37 Rr += dx * f(a+n*dx)38 return Rl,Rr39def midpoint(a,b,N,f): #removed ELs40 '''41 a ; lower-bound for integration42 b ; upper-bound for integration43 N ; number of iterations/subintervals (must be integer)44 f ; function of x (can be user-defined)45 The midpoint approximation method operates similarly to the46 rectangular approximation method, in that it approximates the47 area coinciding to the integral of a given function using48 rectangles over a series of subintervals.49 Where the midpoint approximation method differs is that it50 computes the midpoint of two subinterval bounds ;51 Mn = (xn - xn-1) / 252 53 Once again, like the rectangular method a rectangular area is54 computed using two consecutive subintervals as the x bounds for55 the area and the function of the midpoint as the height ;56 A = f(Mn) * (xn - xn-1)57 58 As for all methods using shapes to approximate the integral of a59 given function within bounds, the areas of the rectangles are60 summed to give a value.61 '''62 dx = (b-a)/N63 M = 064 for n in range(1,N+1):65 M += f((a+n*dx-a+(n-1)*dx)/2) * dx66 return M67def trapezoid(a,b,N,f): #removed ELs68 '''69 a ; lower-bound for integration70 b ; upper-bound for integration71 N ; number of iterations/subintervals (must be integer)72 f ; function of x (can be user-defined)73 '''74 dx = (b-a)/N75 T = dx/2 * (f(a) + f(b))76 for n in range(1,N):77 T += dx * f(a+n*dx)78 return T79def simpson(a,b,N,f): #removed ELs80 '''81 a ; lower-bound for integration82 b ; upper-bound for integration83 N ; number of iterations/subintervals (must be integer)84 f ; function of x (can be user-defined)85 '''86 dx = (b-a)/N87 S = dx/3 * (f(a) + f(b))88 if N%2 == 0:89 for n in range(1,int(N/2)):90 S += dx/3 * (4*f(a+(2*n-1)*dx) + 2*f(a+2*n*dx))91 if N%2 != 0:92 for n in range(1,int((N+1)/2)):93 S += dx/3 * (4*f(a+(2*n-1)*dx) + 2*f(a+2*n*dx))94 return S95# LARGE UNCERTAINTY AT LOW ITERATIONS (REQUIRES HIGH ITERATIONS TO PRODUCE SIMILAR INTEGRALS TO OTHER METHODS/VERY INCONSISTENT)96def boole(a,b,N,f): #removed ELs97 '''98 a ; lower-bound for integration99 b ; upper-bound for integration100 N ; number of iterations/subintervals (must be integer)101 f ; function of x (can be user-defined)102 '''103 # SECOND TERM (32) IS 2,6,10,...,-10,-6,-2104 # THIRD TERM (12) IS 4,8,12,...,-12,-8,-4105 dx = (b-a)/N106 B = 2*dx/45 * 7*(f(a) + f(b))107 108 Ns_1 = []109 Ns_2 = []110 Ns_3 = []111 112 if N%2 == 0:113 Nrange = int(N/2)114 else:115 Nrange = int((N-1)/2 + 1)116 for n in range(1,Nrange):117 if n%2 == 1:118 Ns_1.append(n)119 if n%4 == 2:120 Ns_2.append(n)121 if n%4 == 0:122 Ns_3.append(n)123 124 for n in Ns_1:125 B += 2*dx/45 * (32*f(a+n*dx) + 32*f(b-n*dx))126 for n in Ns_2:127 B += 2*dx/45 * (12*f(a+n*dx) + 12*f(b-n*dx))128 for n in Ns_3:129 B += 2*dx/45 * (14*f(a+n*dx) + 14*f(b-n*dx))130 return B131def romberg(a,b,f): #removed ELs132 '''133 a ; lower-bound for integration134 b ; upper-bound for integration135 f ; function of x (can be user-defined)136 '''137 test_converge = 1E-9138 dx = (b-a)/2139 CRT = dx/2 * (f(a) + f(b))140 for n in range(1,3):141 CRT += dx * f(a+n*dx)142 R_matrix = [CRT]143 144 i = 2145 while i >= 2:146 dx = (b-a)/(2**(i-1))147 R_j = 0148 for n in range(0,int(2**(i-1)+1)):149 R_j += dx * f(a+n*dx)150 151 Rs = [R_j]152 for j in range(2,i+1):153 R = lambda j: (4**(j-1)*R_j - R_matrix[(j-2)]) / (4**(j-1) - 1)154 Rs.append(R(j))155 R_j = R(j)156 if abs(R(j) - R_j) <= test_converge:157 return Rs[-1]158 R_matrix = Rs159 160 i += 1161# MULTI-VARIABLE FUNCTIONS162def double_rectangular(a,b,c,d,N,M,f): #removed ELs163 '''164 a ; lower-bound for integration over x165 b ; upper-bound for integration over x166 c ; lower-bound for integration over y167 d ; upper-bound for integration over y168 N ; number of x iterations/subintervals (must be integer)169 M ; number of y iterations/subintervals (must be integer)170 f ; function of x and y (can be user-defined)171 '''172 dx = (b-a)/N173 dy = (d-c)/M174 dA = dx*dy175 DR = 0176 for n in range(1,N+1):177 for m in range(1,M+1):178 DR += dA * f(a+n*dx,c+m*dy)...

Full Screen

Full Screen

test_jacobian.py

Source:test_jacobian.py Github

copy

Full Screen

...13 input_vector = (0, 0, 0)))14 npt.assert_almost_equal(matrix, f_solver._jacobian_matrix(15 input_vector = (1, 2, 3)))16class JacobianInverseSolverTestCase(unittest.TestCase):17 def test_converge(self):18 matrix = ((1, 0, 3), (0, 2, 2), (1, 2, 1))19 f = lambda x: np.dot(matrix, x)20 f_solver = JacobianInverseSolver(function = f)21 # For a linear map the unconstrained solver reaches the target22 # output vector after a single iteration.23 npt.assert_almost_equal((1, 1, 0), f_solver.converge(24 input_vector = (0, 0, 0),25 target_output_vector = (1, 2, 3)))26 def test_converge_max_input_fix(self):27 matrix = ((1, 0, 3), (0, 2, 2), (1, 2, 1))28 f = lambda x: np.dot(matrix, x)29 f_solver = JacobianInverseSolver(30 function = f,31 max_input_fix = 0.5)32 # The input vector has to go from (0, 0, 0) to (1, 1, 0). The solver33 # input fix is limited to 0.5 per component. The solver gets halfway34 # there after a single iteration.35 input_vector = (0, 0, 0)36 input_vector = input_vector = f_solver.converge(37 input_vector = input_vector,38 target_output_vector = (1, 2, 3))39 npt.assert_almost_equal((0.5, 0.5, 0), input_vector)40 # And reaches the goal after a second iteration.41 input_vector = f_solver.converge(42 input_vector = input_vector,43 target_output_vector = (1, 2, 3))44 npt.assert_almost_equal((1, 1, 0), input_vector)45 def test_converge_max_output_error(self):46 matrix = ((1, 0, 3), (0, 2, 2), (1, 2, 1))47 f = lambda x: np.dot(matrix, x)48 f_solver = JacobianInverseSolver(49 function = f,50 max_output_error = np.sqrt(14) / 2)51 # The output vector has to go from (0, 0, 0) to (1, 2, 3). The52 # initial output vector error norm is hence sqrt(14). The solver53 # output error norm is limited to sqrt(14)/2. The solver gets54 # halfway there after a single iteration.55 input_vector = (0, 0, 0)56 input_vector = f_solver.converge(57 input_vector = input_vector,58 target_output_vector = (1, 2, 3))59 npt.assert_almost_equal((0.5, 0.5, 0), input_vector)60 # And reaches the goal after a second iteration.61 input_vector = f_solver.converge(62 input_vector = input_vector,63 target_output_vector = (1, 2, 3))64 npt.assert_almost_equal((1, 1, 0), input_vector)65class DampedLeastSquaresSolverTest(unittest.TestCase):66 def test_converge(self):67 matrix = ((1, 0, 3), (0, 2, 2), (1, 2, 1))68 f = lambda x: np.dot(matrix, x)69 # For a linear map the solver reaches the target output vector70 # after enough iterations.71 f_solver = DampedLeastSquaresSolver(function = f, constant = 1)72 input_vector = (0, 0, 0)73 for n in xrange(50):74 input_vector = f_solver.converge(75 input_vector = input_vector,76 target_output_vector = (1, 2, 3))...

Full Screen

Full Screen

test1.py

Source:test1.py Github

copy

Full Screen

...4from .newsvendor import *5class MyTestCase(unittest.TestCase):6 def setUp(self):7 pass8 def test_converge(self):9 self.news = newsvendormodel()10 status = solve_default(self.news,11 iteration_limit=20,12 cut_selection_frequency=10,13 simulation=MonteCarloSimulation(14 frequency=10,15 steps=list(range(10, 501, 10))16 ),17 bound_stalling=BoundStalling(18 iterations=5,19 atol=1e-320 )21 )22 self.assertEqual(status, Staus.stalling_convergence)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run molecule automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful