How to use create_random_row_with_constraints method in avocado

Best Python code snippet using avocado_python Github


Full Screen

...29 Creation of the first solution. This solution is the start of searching30 for the best solution31 :return: solution matrix (list(list))32 """33 self.final_matrix = [self.create_random_row_with_constraints()]34 self.combination_matrix.cover_solution_row(self.final_matrix[0])35 while self.combination_matrix.total_uncovered != 0:36 if self.combination_matrix.total_uncovered < self.combination_matrix.total_covered_more_than_ones:37 new_row = self.compute_row()38 else:39 new_row = self.compute_row_using_hamming_distance()40 self.combination_matrix.cover_solution_row(new_row)41 self.final_matrix.append(new_row)42 return self.final_matrix43 def compute(self):44 """45 Searching for the best solution. It creates one solution and from that,46 it tries to create smaller solution. This searching process is limited47 by ITERATIONS_SIZE. When ITERATIONS_SIZE is 0 the last found solution is48 the best solution.49 :return: The best solution50 """51 self.final_matrix = self.final_matrix_init()52 matrix = [x[:] for x in self.final_matrix]53 iterations = ITERATIONS_SIZE54 step_size = 155 deleted_rows = []56 while step_size != 0:57 for i in range(step_size):58 delete_row = matrix.pop(random.randint(0, len(matrix) - 1))59 self.combination_matrix.uncover_solution_row(delete_row)60 deleted_rows.append(delete_row)61 LOG.debug("I'm trying solution with size %s and %s iterations",62 len(matrix), iterations)63 matrix, is_better_solution = self.find_better_solution(iterations, matrix)64 if is_better_solution:65 self.final_matrix = matrix[:]66 deleted_rows = []67 step_size *= 268 LOG.debug("-----solution with size %s was found-----\n",69 len(matrix))70 iterations = ITERATIONS_SIZE71 else:72 LOG.debug("-----solution with size %s was not found-----\n",73 len(matrix))74 for i in range(step_size):75 self.combination_matrix.cover_solution_row(deleted_rows[i])76 matrix.append(deleted_rows[i])77 if step_size > 1:78 step_size = 179 else:80 step_size = 081 return self.final_matrix82 def find_better_solution(self, counter, matrix):83 """84 Changing the matrix to cover all combinations85 :param counter: maximum number of changes in the matrix86 :param matrix: matrix to be changed87 :return: new matrix and is changes have been successful?88 """89 while self.combination_matrix.total_uncovered != 0:90 LOG.debug(self.__throbber.render(), extra={"skip_newline": True})91 solution, row_index, _ = self.use_random_algorithm(matrix)92 if len(solution) != 0:93 self.combination_matrix.uncover_solution_row(matrix[row_index])94 self.combination_matrix.cover_solution_row(solution)95 matrix[row_index] = solution96 if counter == 0:97 return matrix, False98 counter -= 199 return matrix, True100 def use_random_algorithm(self, matrix):101 """102 Applies one of these algorithms to the matrix.103 It chooses algorithm by random in proportion 1:1:8104 :param matrix: matrix to be changed105 :return: new row of matrix, index of row inside matrix and parameters which has been changed106 """107 switch = random.randint(0, 9)108 if switch == 0:109 solution, row_index, parameters = self.change_one_value(matrix)110 elif switch == 1:111 solution, row_index, parameters = self.change_one_column(matrix)112 else:113 solution, row_index, parameters = self.cover_missing_combination(matrix)114 return solution, row_index, parameters115 def compute_row(self):116 """117 Computation of one row which covers most of combinations118 :return: new solution row119 """120 is_valid_row = False121 while not is_valid_row:122 possible_parameters = list(self.combination_matrix.uncovered_rows)123 row = [-1] * len( while len(possible_parameters) != 0:125 # finding uncovered combination126 combination_parameters_index = random.randint(0, len(possible_parameters) - 1)127 combination_parameters = possible_parameters[combination_parameters_index]128 del possible_parameters[combination_parameters_index]129 combination_row = self.combination_matrix.get_row(combination_parameters)130 possible_combinations = list(combination_row.get_all_uncovered_combinations())131 combination_index = random.randint(0, len(possible_combinations) - 1)132 combination = possible_combinations[combination_index]133 is_parameter_used = False134 # Are parameters already used in row?135 for i in combination_parameters:136 if row[i] != -1:137 is_parameter_used = True138 break139 if is_parameter_used:140 continue141 row_copy = row.copy()142 # Is combination matches the constraints?143 for index, parameter in enumerate(combination_parameters):144 row_copy[parameter] = combination[index]145 if self.combination_matrix.is_valid_solution(row_copy):146 row = row_copy147 # Filling in of free spaces inside the row148 for index, r in enumerate(row):149 if r == -1:150 is_valid = False151 while not is_valid:152 row[index] = random.randint(0,[index] - 1)153 is_valid = self.combination_matrix.is_valid_solution(row)154 is_valid_row = self.combination_matrix.is_valid_solution(row)155 return row156 def cover_missing_combination(self, matrix):157 """158 Randomly finds one missing combination. This combination puts into each159 row of the matrix. The row with the best coverage is the solution160 :param matrix: matrix to be changed161 :return: solution, index of solution inside matrix and parameters which has been changed162 """163 parameters, combination = self.get_missing_combination_random()164 best_uncover = float("inf")165 best_solution = []166 best_row_index = 0167 for row_index in range(len(matrix)):168 solution = [x for x in matrix[row_index]]169 for index, item in enumerate(parameters):170 solution[item] = combination[index]171 if self.combination_matrix.is_valid_combination(solution, parameters):172 self.combination_matrix.uncover_combination(matrix[row_index], parameters)173 self.combination_matrix.cover_combination(solution, parameters)174 if self.combination_matrix.total_uncovered < best_uncover:175 best_uncover = self.combination_matrix.total_uncovered176 best_solution = solution177 best_row_index = row_index178 self.combination_matrix.uncover_combination(solution, parameters)179 self.combination_matrix.cover_combination(matrix[row_index], parameters)180 if best_uncover == 0:181 break182 return best_solution, best_row_index, parameters183 def get_missing_combination_random(self):184 """185 Randomly finds one missing combination.186 :return: parameter of combination and values of combination187 """188 possible_parameters = list(self.combination_matrix.uncovered_rows)189 combination_parameters_index = random.randint(0, len(possible_parameters) - 1)190 combination_parameters = possible_parameters[combination_parameters_index]191 combination_row = self.combination_matrix.get_row(combination_parameters)192 possible_combinations = list(combination_row.get_all_uncovered_combinations())193 combination_index = random.randint(0, len(possible_combinations) - 1)194 combination = possible_combinations[combination_index]195 return combination_parameters, combination196 def change_one_column(self, matrix):197 """198 Randomly choose one column of the matrix. In each cell of this column199 changes value. The row with the best coverage is the solution.200 :param matrix: matrix to be changed201 :return: solution, index of solution inside matrix and parameters which has been changed202 """203 column_index = random.randint(0, len( - 1)204 best_uncover = float("inf")205 best_solution = []206 best_row_index = 0207 for row_index in range(len(matrix)):208 try:209 solution, row_index, parameters = self.change_one_value(matrix, row_index, column_index)210 except ValueError:211 continue212 self.combination_matrix.uncover_combination(matrix[row_index], parameters)213 self.combination_matrix.cover_combination(solution, parameters)214 if self.combination_matrix.total_uncovered < best_uncover:215 best_uncover = self.combination_matrix.total_uncovered216 best_solution = solution217 best_row_index = row_index218 self.combination_matrix.uncover_combination(solution, parameters)219 self.combination_matrix.cover_combination(matrix[row_index], parameters)220 if best_uncover == 0:221 break222 return best_solution, best_row_index, [column_index]223 def change_one_value(self, matrix, row_index=None, column_index=None):224 """225 Change one cell inside the matrix226 :param matrix: matrix to be changed227 :param row_index: row inside matrix. If it's None it is chosen randomly228 :param column_index: column inside matrix. If it's None it is chosen randomly229 :return: solution, index of solution inside matrix and parameters which has been changed230 """231 is_cell_chosen = True232 if row_index is None:233 is_cell_chosen = False234 row_index = random.randint(0, len(matrix) - 1)235 row = [x for x in matrix[row_index]]236 if column_index is None:237 is_cell_chosen = False238 column_index = random.randint(0, len(row) - 1)239 possible_numbers = list(range(0, row[column_index])) + list(240 range(row[column_index] + 1,[column_index]))241 row[column_index] = random.choice(possible_numbers)242 while not self.combination_matrix.is_valid_combination(row, [column_index]):243 possible_numbers.remove(row[column_index])244 if len(possible_numbers) == 0:245 if is_cell_chosen:246 raise ValueError("Selected cell can't be changed")247 column_index = random.randint(0, len(row) - 1)248 row_index = random.randint(0, len(matrix) - 1)249 row = [x for x in matrix[row_index]]250 possible_numbers = list(range(0, row[column_index])) + list(251 range(row[column_index] + 1,[column_index]))252 row[column_index] = random.choice(possible_numbers)253 return row, row_index, [column_index]254 def compute_row_using_hamming_distance(self):255 """256 :return: row with the biggest hamming distance from final matrix257 """258 row_1 = self.create_random_row_with_constraints()259 row_2 = self.create_random_row_with_constraints()260 if self.compute_hamming_distance(row_1) >= self.compute_hamming_distance(row_2):261 return row_1262 else:263 return row_2264 def compute_hamming_distance(self, row):265 """266 :return: hamming distance of row from final matrix267 """268 distance = 0269 for final_row in self.final_matrix:270 for index, cell in enumerate(final_row):271 if row[index] != cell:272 distance += 1273 return distance274 def create_random_row_with_constraints(self):275 """276 Create a new test-case random row, and the row meets the constraints.277 :return: new random row278 :rtype: list279 """280 data_size = len( row = [-1]*data_size282 for parameter in random.sample(range(data_size), data_size):283 possible_values = self.solver.get_possible_values(row, parameter)284 value_choice = random.choice(possible_values)285 row[parameter] = value_choice...

Full Screen

Full Screen Github


Full Screen

...19 parameters = [3, 3, 3, 3]20 constraints = {((0, 0), (2, 0)), ((0, 1), (1, 1), (2, 0)), ((0, 2), (3, 2))}21 t_value = 222 self.cit = Cit(parameters, t_value, constraints)23 def test_create_random_row_with_constraints(self):24 for _ in range(0, 10):25 row = self.cit.create_random_row_with_constraints()26 with self.subTest(random_row=row):27 self.assertTrue(self.cit.combination_matrix.is_valid_solution(row), "New random row is not valid")28 def test_compute_hamming_distance(self):29 self.cit.final_matrix.append([1, 0, 1, 2])30 self.cit.final_matrix.append([2, 1, 1, 0])31 row = [2, 0, 3, 2]32 expected_distance = 533 self.assertEqual(expected_distance, self.cit.compute_hamming_distance(row), "Wrong hamming distance")34 def test_final_matrix_init(self):35 combination_matrix = copy(self.cit.combination_matrix)36 final_matrix = self.cit.final_matrix_init()37 expected_total_uncovered = 038 expected_uncovered_rows = {}39 self.assertEqual(expected_total_uncovered, self.cit.combination_matrix.total_uncovered,...

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:


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

Run avocado 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?