Best Python code snippet using molecule_python
test_criteria.py
Source:test_criteria.py
...56 # change tolerance57 t.tolerance[x] = 1e-558 self.assertAlmostEqual(t.tolerance[x], 1e-5)59 self.assertAlmostEqual(t.tolerance.default, 1e-8)60 def test_converged(self):61 """Test converged method."""62 x = relentless.variable.DesignVariable(value=3.0)63 q = QuadraticObjective(x=x)64 t = relentless.optimize.GradientTest(tolerance=1e-8, variables=x)65 self.assertFalse(t.converged(result=q.compute(x)))66 x.value = 0.99999999967 self.assertTrue(t.converged(result=q.compute(x)))68 # test at high69 x.value = -2.070 x.high = 2.071 self.assertFalse(t.converged(result=q.compute(x)))72 x.value = 0.073 x.high = 0.074 self.assertTrue(t.converged(result=q.compute(x)))75 # test at low76 x.high = None77 x.value = 0.078 x.low = 0.079 self.assertFalse(t.converged(result=q.compute(x)))80 x.value = 2.081 x.low = 2.082 self.assertTrue(t.converged(result=q.compute(x)))83class test_ValueTest(unittest.TestCase):84 """Unit tests for relentless.optimize.ValueTest"""85 def test_init(self):86 """Test creation with data."""87 x = relentless.variable.DesignVariable(value=3.0)88 # test default values89 t = relentless.optimize.ValueTest(value=2.5)90 self.assertAlmostEqual(t.absolute, 1e-8)91 self.assertAlmostEqual(t.relative, 1e-5)92 self.assertAlmostEqual(t.value, 2.5)93 # non-default values94 t = relentless.optimize.ValueTest(absolute=1e-7, relative=1e-4, value=3.0)95 self.assertAlmostEqual(t.absolute, 1e-7)96 self.assertAlmostEqual(t.relative, 1e-4)97 self.assertAlmostEqual(t.value, 3.0)98 # change parameters99 t.absolute = 1e-9100 t.relative = 1e-5101 t.value = 1.5102 self.assertAlmostEqual(t.absolute, 1e-9)103 self.assertAlmostEqual(t.relative, 1e-5)104 self.assertAlmostEqual(t.value, 1.5)105 def test_converged(self):106 """Test converged method."""107 x = relentless.variable.DesignVariable(value=3.0)108 q = QuadraticObjective(x=x)109 t = relentless.optimize.ValueTest(absolute=0.2, relative=0.2, value=1.0)110 self.assertFalse(t.converged(result=q.compute(x)))111 x.value = 1.999999999112 self.assertTrue(t.converged(result=q.compute(x)))113 t = relentless.optimize.ValueTest(value=9.0)114 self.assertFalse(t.converged(result=q.compute(x)))115 x.value = 3.9999999999116 self.assertTrue(t.converged(result=q.compute(x)))117class AnyTest(unittest.TestCase):118 """Unit tests for relentless.optimize.AnyTest"""119 def test_init(self):120 """Test creation with data."""121 x = relentless.variable.DesignVariable(value=3.0)122 t1 = relentless.optimize.GradientTest(tolerance=1e-8, variables=x)123 t2 = relentless.optimize.ValueTest(value=2.0)124 t3 = relentless.optimize.ValueTest(value=1.0)125 t = relentless.optimize.AnyTest(t1,t2,t3)126 self.assertCountEqual(t.tests, (t1,t2,t3))127 def test_converged(self):128 """Test converged method."""129 x = relentless.variable.DesignVariable(value=3.0)130 q = QuadraticObjective(x=x)131 t1 = relentless.optimize.GradientTest(tolerance=1e-8, variables=x)132 t2 = relentless.optimize.ValueTest(value=1.0)133 t3 = relentless.optimize.ValueTest(value=0.0)134 t = relentless.optimize.AnyTest(t1,t2,t3)135 self.assertFalse(t.converged(result=q.compute(x)))136 x.value = 2.00000001137 self.assertTrue(t.converged(result=q.compute(x)))138 x.value = 1.00000001139 self.assertTrue(t.converged(result=q.compute(x)))140class AllTest(unittest.TestCase):141 """Unit tests for relentless.optimize.AllTest"""142 def test_init(self):143 """Test creation with data."""144 x = relentless.variable.DesignVariable(value=3.0)145 t1 = relentless.optimize.GradientTest(tolerance=1e-8, variables=x)146 t2 = relentless.optimize.ValueTest(value=1.0)147 t3 = relentless.optimize.ValueTest(value=0.0)148 t = relentless.optimize.AllTest(t1,t2,t3)149 self.assertCountEqual(t.tests, (t1,t2,t3))150 def test_converged(self):151 """Test converged method."""152 x = relentless.variable.DesignVariable(value=3.0)153 q = QuadraticObjective(x=x)154 t1 = relentless.optimize.GradientTest(tolerance=1e-8, variables=x)155 t2 = relentless.optimize.ValueTest(value=1.0)156 t3 = relentless.optimize.ValueTest(value=0.0)157 t = relentless.optimize.AllTest(t1,t2,t3)158 self.assertFalse(t.converged(result=q.compute(x)))159 x.value = 1.999999999160 self.assertFalse(t.converged(result=q.compute(x)))161 t2.value = 0.0162 x.value = 0.999999999163 self.assertTrue(t.converged(result=q.compute(x)))164class OrTest(unittest.TestCase):165 """Unit tests for relentless.optimize.OrTest"""166 def test_init(self):167 """Test creation with data."""168 x = relentless.variable.DesignVariable(value=3.0)169 t1 = relentless.optimize.GradientTest(tolerance=1e-8, variables=x)170 t2 = relentless.optimize.ValueTest(value=1.0)171 t = relentless.optimize.OrTest(t1,t2)172 self.assertCountEqual(t.tests, (t1,t2))173 def test_converged(self):174 """Test converged method."""175 x = relentless.variable.DesignVariable(value=3.0)176 q = QuadraticObjective(x=x)177 t1 = relentless.optimize.GradientTest(tolerance=1e-8, variables=x)178 t2 = relentless.optimize.ValueTest(value=1.0)179 t = relentless.optimize.OrTest(t1,t2)180 self.assertFalse(t.converged(result=q.compute(x)))181 x.value = 1.9999999999182 self.assertTrue(t.converged(result=q.compute(x)))183 x.value = 0.9999999999184 self.assertTrue(t.converged(result=q.compute(x)))185class AndTest(unittest.TestCase):186 """Unit tests for relentless.optimize.AndTest"""187 def test_init(self):188 """Test creation with data."""189 x = relentless.variable.DesignVariable(value=3.0)190 t1 = relentless.optimize.GradientTest(tolerance=1e-8, variables=x)191 t2 = relentless.optimize.ValueTest(value=1.0)192 t = relentless.optimize.AndTest(t1,t2)193 self.assertCountEqual(t.tests, (t1,t2))194 def test_converged(self):195 """Test converged method."""196 x = relentless.variable.DesignVariable(value=3.0)197 q = QuadraticObjective(x=x)198 t1 = relentless.optimize.GradientTest(tolerance=1e-8, variables=x)199 t2 = relentless.optimize.ValueTest(value=1.0)200 t = relentless.optimize.AndTest(t1,t2)201 self.assertFalse(t.converged(result=q.compute(x)))202 x.value = 1.999999999203 self.assertFalse(t.converged(result=q.compute(x)))204 t2.value = 0.0205 x.value = 0.999999999206 self.assertTrue(t.converged(result=q.compute(x)))207if __name__ == '__main__':208 unittest.main()
dynamic_programming.py
Source:dynamic_programming.py
...73 continue74 x = [i,j]75 PI[i,j] = greedy_policy_improvement(x, V, gamma, cost_fn)76 return PI77def test_converged(arg1, arg2):78 """79 Computes the maximum norm between arg1 and arg2 and80 checks whether the result is smaller than the global variable delta.81 """82 return np.max(np.abs(arg1-arg2)) < c.delta83def Value_Iteration(V, gamma, cost_fn):84 """85 Value iteration algorithm. Stops after convergence of max_steps.86 """87 max_steps = 100000088 for i in range(max_steps):89 V_prev = V90 V = value_iteration_step(V, gamma, cost_fn)91 if test_converged(V, V_prev):92 return V93 else:94 print(f"failed to converge after {max_steps} steps.")95 96def Policy_Evaluation(V, PI, gamma, cost_fn, eval_steps = None):97 """98 Policy evaluation used for the policy iteration. It stops afer eval_step.99 If no value for eval_steps is given, Policy_Evaluation runs until100 convergence or until max_steps is reached.101 """102 if eval_steps != None:103 max_steps = eval_steps104 else:105 max_steps = 1000000106 107 if eval_steps != None:108 for i in range(max_steps):109 V_prev = V110 V = policy_evaluation_step(V, PI, gamma, cost_fn)111 if test_converged(V, V_prev):112 return V113 else: return V114 115 else:116 for i in range(max_steps):117 V_prev = V118 V = policy_evaluation_step(V, PI, gamma, cost_fn)119 if test_converged(V, V_prev):120 return V121 else:122 print(f"failed to converge after {max_steps} steps.")123 124def Policy_Iteration(V, PI, gamma, cost_fn, eval_steps = None):125 """126 This function computes the Policy_Iteration of no value for eval_steps is given.127 If eval_steps is given, it computes the optistic policy128 iteration with that many policy evaluation steps.129 """130 max_steps = 1000000131 for i in range(max_steps):132 V_prev = V133 V = Policy_Evaluation(V, PI, gamma, cost_fn, eval_steps)134 PI_prev = PI135 PI = policy_improvement_step(V, gamma, cost_fn, PI)136 if test_converged(V, V_prev):137 return PI, V138 else:...
matrix_test.py
Source:matrix_test.py
...38 result_matrix = [[2, 1, 0],39 [1, 2, 3],40 [1, 1, 2]]41 self.assertEqual(add_self_loops(test_matrix, 2), result_matrix)42 def test_converged(self):43 test_matrix = [[0.03, 1, 0],44 [1, 0, 3],45 [0.0008, 1, 1]]46 self.assertEqual(converged(test_matrix, test_matrix), True)47if __name__ == '__main__':...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!