# How to use is_valid_solution method in avocado

Best Python code snippet using avocado_python

inverse.py

Source:inverse.py

1import math2import numpy as np3from kinematics.forward import ForwardKinematics4from kinematics.kinematics import Kinematics5from kinematics.solution import InverseKinematicsShoulderSolution, InverseKinematicsSpecificSolution, \6 InverseKinematicsSolution, InverseKinematicsWristSolution7class InverseKinematics(Kinematics):8 def __init__(self):9 super().__init__()10 self.forward_kinematics = ForwardKinematics()11 def __clamp_cos_sin_within_threshold(self, cos_or_sin):12 new_val = cos_or_sin13 if 1 < new_val <= 1.2:14 new_val = 1.015 elif -1.2 <= new_val < -1:16 new_val = -1.017 return new_val18 def __compute_solution_for_theta_1(self, T06, theta_1, debug=False):19 wrist_solution = InverseKinematicsWristSolution()20 # Theta 521 P06 = T06[:, 3]22 theta_5_1 = None23 theta_5_2 = None24 theta_5_cos = (P06[0] * math.sin(theta_1) - P06[1] * np.cos(25 theta_1) - self.joint4_dh.d) / self.joint6_dh.d26 theta_5_cos = self.__clamp_cos_sin_within_threshold(theta_5_cos)27 if -1 <= theta_5_cos <= 1:28 theta_5_1 = math.acos(theta_5_cos)29 theta_5_2 = -math.acos(theta_5_cos)30 sigma = 0.0000131 if theta_5_1 is not None and not -sigma <= math.sin(theta_5_1) <= sigma:32 wrist_solution.solution_wrist_up = self.__compute_solution_for_wrist(theta_1, theta_5_1, T06)33 else:34 wrist_solution.solution_wrist_up.is_valid_solution = False35 if theta_5_2 is not None and not -sigma <= math.sin(theta_5_2) <= sigma:36 wrist_solution.solution_wrist_down = self.__compute_solution_for_wrist(theta_1, theta_5_2, T06)37 else:38 wrist_solution.solution_wrist_down.is_valid_solution = False39 if not wrist_solution.solution_wrist_up.is_valid_solution and not wrist_solution.solution_wrist_down.is_valid_solution:40 wrist_solution.is_valid_solution = False41 if debug:42 print(f"Theta 5: {theta_5_1:.3f}, {theta_5_2:.3f}")43 return wrist_solution44 def __compute_solution_for_wrist(self, theta_1, theta_5, T06, debug=False):45 shoulder_solution = InverseKinematicsShoulderSolution()46 # Theta 647 T60 = np.linalg.inv(T06)48 X60 = T60[:, 0]49 Y60 = T60[:, 1]50 theta_6_cos = (X60[0] * math.sin(theta_1) - Y60[0] * math.cos(theta_1)) / math.sin(51 theta_5) # only using one of the theta 5's for now..52 theta_6_sin = (-X60[1] * math.sin(theta_1) + Y60[1] * math.cos(theta_1)) / math.sin(53 theta_5) # only using one of the theta 5's for now..54 theta_6 = math.atan2(theta_6_sin, theta_6_cos)55 if debug:56 print(f"Theta 6: {theta_6:.3f}")57 tm_dict = {}58 # Theta 359 T01 = self.compute_transformation_matrix(theta_1, self.joint1_dh)60 T45 = self.compute_transformation_matrix(theta_5, self.joint5_dh)61 T56 = self.compute_transformation_matrix(theta_6, self.joint6_dh)62 T46 = np.matmul(T45, T56)63 T64 = np.linalg.inv(T46)64 T10 = np.linalg.inv(T01)65 T14 = np.matmul(np.matmul(T10, T06), T64)66 P14 = T14[:, 3]67 tm_dict["T06"] = T0668 tm_dict["T01"] = T0169 tm_dict["T45"] = T4570 tm_dict["T56"] = T5671 tm_dict["T64"] = T6472 tm_dict["T10"] = T1073 tm_dict["T14"] = T1474 tm_dict["P14"] = P1475 theta_3_cos = (math.sqrt(76 P14[0] ** 2 + P14[2] ** 2) ** 2 - self.joint3_dh.a ** 2 - self.joint4_dh.a ** 2) / (77 2 * (-self.joint3_dh.a) * (-self.joint4_dh.a))78 if debug:79 print("theta3_cos: ", theta_3_cos)80 theta_3_cos = self.__clamp_cos_sin_within_threshold(theta_3_cos)81 if not -1 <= theta_3_cos <= 1:82 shoulder_solution.is_valid_solution = False83 return shoulder_solution84 theta_3_up = math.acos(theta_3_cos)85 theta_3_down = -math.acos(theta_3_cos)86 if debug:87 print(f"Theta 3: Up: {theta_3_up:.3f} Down: {theta_3_down:.3f}")88 shoulder_solution.solution_elbow_up = self.__compute_specific_solution(theta_1, theta_3_up, theta_5, theta_6, tm_dict)89 shoulder_solution.solution_elbow_down = self.__compute_specific_solution(theta_1, theta_3_down, theta_5, theta_6, tm_dict)90 return shoulder_solution91 def __compute_specific_solution(self, theta_1, theta_3, theta_5, theta_6, tm_dict, debug=False):92 specific_solution = InverseKinematicsSpecificSolution()93 P14 = tm_dict["P14"]94 phi_1 = math.atan2(-P14[2], -P14[0])95 phi_2 = math.asin((-self.joint4_dh.a * math.sin(theta_3)) / math.sqrt(P14[0]**2 + P14[2]**2))96 theta_2 = phi_1 - phi_297 if debug:98 print(f"Theta 2: {theta_2:.3f}")99 T01 = tm_dict["T01"]100 T12 = self.compute_transformation_matrix(theta_2, self.joint2_dh)101 T23 = self.compute_transformation_matrix(theta_3, self.joint3_dh)102 T45 = tm_dict["T45"]103 T56 = tm_dict["T56"]104 T06 = tm_dict["T06"]105 T03 = np.matmul(np.matmul(T01, T12), T23)106 T30 = np.linalg.inv(T03)107 T64 = tm_dict["T64"]108 T34 = np.matmul(np.matmul(T30, T06), T64)109 X34 = T34[:, 0]110 theta_4 = math.atan2(X34[1], X34[0])111 if debug:112 print(f"Theta 4: {theta_4:.3f}")113 specific_solution.thetas = [theta_1, theta_2, theta_3, theta_4, theta_5, theta_6]114 return specific_solution115 def __print_all_solutions(self, solution):116 print("Inverse Solutions:")117 if solution.solution_shoulder_left.is_valid_solution:118 if solution.solution_shoulder_left.solution_wrist_up.is_valid_solution:119 if solution.solution_shoulder_left.solution_wrist_up.solution_elbow_up.is_valid_solution:120 print(121 f"Shoulder left, wrist up, elbow up: {solution.solution_shoulder_left.solution_wrist_up.solution_elbow_up.thetas}")122 if solution.solution_shoulder_left.solution_wrist_up.solution_elbow_down.is_valid_solution:123 print(124 f"Shoulder left, wrist up, elbow down: {solution.solution_shoulder_left.solution_wrist_up.solution_elbow_down.thetas}")125 if solution.solution_shoulder_left.solution_wrist_down.is_valid_solution:126 if solution.solution_shoulder_left.solution_wrist_down.solution_elbow_up.is_valid_solution:127 print(128 f"Shoulder left, wrist down, elbow up: {solution.solution_shoulder_left.solution_wrist_down.solution_elbow_up.thetas}")129 if solution.solution_shoulder_left.solution_wrist_down.solution_elbow_down:130 print(131 f"Shoulder left, wrist down, elbow down: {solution.solution_shoulder_left.solution_wrist_down.solution_elbow_down.thetas}")132 if solution.solution_shoulder_right.is_valid_solution:133 if solution.solution_shoulder_right.solution_wrist_up.is_valid_solution:134 if solution.solution_shoulder_right.solution_wrist_up.solution_elbow_up.is_valid_solution:135 print(136 f"Shoulder right, wrist up, elbow up: {solution.solution_shoulder_right.solution_wrist_up.solution_elbow_up.thetas}")137 if solution.solution_shoulder_right.solution_wrist_up.solution_elbow_down.is_valid_solution:138 print(139 f"Shoulder right, wrist up, elbow down: {solution.solution_shoulder_right.solution_wrist_up.solution_elbow_up.thetas}")140 if solution.solution_shoulder_right.solution_wrist_down.is_valid_solution:141 if solution.solution_shoulder_right.solution_wrist_down.solution_elbow_up.is_valid_solution:142 print(143 f"Shoulder right, wrist down, elbow up: {solution.solution_shoulder_right.solution_wrist_down.solution_elbow_up.thetas}")144 if solution.solution_shoulder_right.solution_wrist_down.solution_elbow_down.is_valid_solution:145 print(146 f"Shoulder right, wrist down, elbow down: {solution.solution_shoulder_right.solution_wrist_down.solution_elbow_up.thetas}")147 def compute_joint_angles(self, T06, debug=False):148 solution = InverseKinematicsSolution()149 #Theta 1150 P05 = np.dot(T06, [0, 0, -self.joint6_dh.d, 1])151 phi_1 = math.atan2(P05[1], P05[0])152 phi_2_cos = self.joint4_dh.d / math.sqrt(P05[0]**2 + P05[1]**2)153 phi_2 = math.acos(phi_2_cos)154 theta_1_1 = phi_1 + phi_2 + (np.pi / 2)155 theta_1_2 = phi_1 - phi_2 + (np.pi / 2)156 if debug:157 print(f"Theta 1: {theta_1_1:.3f}, {theta_1_2:.3f}")158 if not math.isnan(theta_1_1):159 solution.solution_shoulder_left = self.__compute_solution_for_theta_1(T06, theta_1_1, debug)160 else:161 solution.solution_shoulder_left = InverseKinematicsWristSolution().is_valid_solution = False162 if not math.isnan(theta_1_2):163 solution.solution_shoulder_right = self.__compute_solution_for_theta_1(T06, theta_1_2, debug)164 else:165 solution.solution_shoulder_right = InverseKinematicsWristSolution().is_valid_solution = False166 if debug:167 self.__print_all_solutions(solution)168 return solution169 def get_solution_for_config_id(self, solution, config_id):170 if config_id == 0:171 return solution.solution_shoulder_left.solution_wrist_up.solution_elbow_up.thetas172 elif config_id == 1:173 return solution.solution_shoulder_left.solution_wrist_up.solution_elbow_down.thetas174 elif config_id == 2:175 return solution.solution_shoulder_left.solution_wrist_down.solution_elbow_up.thetas176 elif config_id == 3:177 return solution.solution_shoulder_left.solution_wrist_down.solution_elbow_down.thetas178 elif config_id == 4:179 return solution.solution_shoulder_right.solution_wrist_up.solution_elbow_up.thetas180 elif config_id == 5:181 return solution.solution_shoulder_right.solution_wrist_up.solution_elbow_down.thetas182 elif config_id == 6:183 return solution.solution_shoulder_right.solution_wrist_down.solution_elbow_up.thetas184 elif config_id == 7:185 return solution.solution_shoulder_right.solution_wrist_down.solution_elbow_down.thetas186 else:187 raise Exception("invalid config solution id")188 def get_best_solution_for_config_id(self, T06, config_id):189 solution = self.compute_joint_angles(T06)190 if self.is_valid_solution_by_config_id(solution, config_id):191 return self.get_solution_for_config_id(solution, config_id)192 else:193 index = config_id + 1194 checked_all = False195 while not checked_all:196 if index >= 8:197 index = 0198 if index == config_id:199 print('Found no valid solutions..')200 return None201 if self.is_valid_solution_by_config_id(solution, index):202 return self.get_solution_for_config_id(solution, index)203 index += 1204 def is_valid_solution_by_config_id(self, solution, config_id):205 if 0 <= config_id < 4 and solution.solution_shoulder_left.is_valid_solution:206 if 0 <= config_id < 2 and solution.solution_shoulder_left.solution_wrist_up.is_valid_solution:207 if config_id == 0 and solution.solution_shoulder_left.solution_wrist_up.solution_elbow_up.is_valid_solution:208 return True209 if config_id == 1 and solution.solution_shoulder_left.solution_wrist_up.solution_elbow_down.is_valid_solution:210 return True211 if 2 <= config_id < 4 and solution.solution_shoulder_left.solution_wrist_down.is_valid_solution:212 if config_id == 2 and solution.solution_shoulder_left.solution_wrist_down.solution_elbow_up.is_valid_solution:213 return True214 if config_id == 3 and solution.solution_shoulder_left.solution_wrist_down.solution_elbow_down:215 return True216 if 4 <= config_id < 8 and solution.solution_shoulder_right.is_valid_solution:217 if 4 <= config_id < 6 and solution.solution_shoulder_right.solution_wrist_up.is_valid_solution:218 if config_id == 4 and solution.solution_shoulder_right.solution_wrist_up.solution_elbow_up.is_valid_solution:219 return True220 if config_id == 5 and solution.solution_shoulder_right.solution_wrist_up.solution_elbow_down.is_valid_solution:221 return True222 if 6 <= config_id < 8 and solution.solution_shoulder_right.solution_wrist_down.is_valid_solution:223 if config_id == 6 and solution.solution_shoulder_right.solution_wrist_down.solution_elbow_up.is_valid_solution:224 return True225 if config_id == 7 and solution.solution_shoulder_right.solution_wrist_down.solution_elbow_down.is_valid_solution:226 return True227 else:228 return False229 def get_current_configuration_id(self, joint_angles):230 T06 = self.forward_kinematics.compute_0_to_6_matrix(joint_angles)231 solution = self.compute_joint_angles(T06)232 differences = np.full(8, 1000)233 if solution.solution_shoulder_left.is_valid_solution:234 if solution.solution_shoulder_left.solution_wrist_up.is_valid_solution:235 if solution.solution_shoulder_left.solution_wrist_up.solution_elbow_up.is_valid_solution:236 differences[0] = 0237 if solution.solution_shoulder_left.solution_wrist_up.solution_elbow_down.is_valid_solution:238 differences[1] = 0239 if solution.solution_shoulder_left.solution_wrist_down.is_valid_solution:240 if solution.solution_shoulder_left.solution_wrist_down.solution_elbow_up.is_valid_solution:241 differences[2] = 0242 if solution.solution_shoulder_left.solution_wrist_down.solution_elbow_down:243 differences[3] = 0244 if solution.solution_shoulder_right.is_valid_solution:245 if solution.solution_shoulder_right.solution_wrist_up.is_valid_solution:246 if solution.solution_shoulder_right.solution_wrist_up.solution_elbow_up.is_valid_solution:247 differences[4] = 0248 if solution.solution_shoulder_right.solution_wrist_up.solution_elbow_down.is_valid_solution:249 differences[5] = 0250 if solution.solution_shoulder_right.solution_wrist_down.is_valid_solution:251 if solution.solution_shoulder_right.solution_wrist_down.solution_elbow_up.is_valid_solution:252 differences[6] = 0253 if solution.solution_shoulder_right.solution_wrist_down.solution_elbow_down.is_valid_solution:254 differences[7] = 0255 for i in range(6):256 if solution.solution_shoulder_left.is_valid_solution:257 if solution.solution_shoulder_left.solution_wrist_up.is_valid_solution:258 if solution.solution_shoulder_left.solution_wrist_up.solution_elbow_up.is_valid_solution:259 differences[0] += abs(joint_angles[i] - solution.solution_shoulder_left.solution_wrist_up.solution_elbow_up.thetas[i])260 if solution.solution_shoulder_left.solution_wrist_up.solution_elbow_down.is_valid_solution:261 differences[1] += abs(joint_angles[i] - solution.solution_shoulder_left.solution_wrist_up.solution_elbow_down.thetas[i])262 if solution.solution_shoulder_left.solution_wrist_down.is_valid_solution:263 if solution.solution_shoulder_left.solution_wrist_down.solution_elbow_up.is_valid_solution:264 differences[2] += abs(joint_angles[i] - solution.solution_shoulder_left.solution_wrist_down.solution_elbow_up.thetas[i])265 if solution.solution_shoulder_left.solution_wrist_down.solution_elbow_down:266 differences[3] += abs(joint_angles[i] - solution.solution_shoulder_left.solution_wrist_down.solution_elbow_down.thetas[i])267 if solution.solution_shoulder_right.is_valid_solution:268 if solution.solution_shoulder_right.solution_wrist_up.is_valid_solution:269 if solution.solution_shoulder_right.solution_wrist_up.solution_elbow_up.is_valid_solution:270 differences[4] += abs(joint_angles[i] - solution.solution_shoulder_right.solution_wrist_up.solution_elbow_up.thetas[i])271 if solution.solution_shoulder_right.solution_wrist_up.solution_elbow_down.is_valid_solution:272 differences[5] += abs(joint_angles[i] - solution.solution_shoulder_right.solution_wrist_up.solution_elbow_down.thetas[i])273 if solution.solution_shoulder_right.solution_wrist_down.is_valid_solution:274 if solution.solution_shoulder_right.solution_wrist_down.solution_elbow_up.is_valid_solution:275 differences[6] += abs(joint_angles[i] - solution.solution_shoulder_right.solution_wrist_down.solution_elbow_up.thetas[i])276 if solution.solution_shoulder_right.solution_wrist_down.solution_elbow_down.is_valid_solution:277 differences[7] += abs(joint_angles[i] - solution.solution_shoulder_right.solution_wrist_down.solution_elbow_down.thetas[i])278 print(differences)...

sudoku_sol_validator.py

Source:sudoku_sol_validator.py

1"""2Sudoku Background3Sudoku is a game played on a 9x9 grid. The goal of the game is to fill all cells of the grid with digits from 1 to 9,4so that each column, each row, and each of the nine 3x3 sub-grids (also known as blocks)5contain all of the digits from 1 to 9.6(More info at: http://en.wikipedia.org/wiki/Sudoku)7Sudoku Solution Validator8Write a function validSolution/ValidateSolution/valid_solution() that accepts a 2D array representing a Sudoku board,9and returns true if it is a valid solution, or false otherwise.10The cells of the sudoku board may also contain 0's, which will represent empty cells.11Boards containing one or more zeroes are considered to be invalid solutions.12The board is always 9 cells by 9 cells, and every cell only contains integers from 0 to 9.13Examples14validSolution([15 [5, 3, 4, 6, 7, 8, 9, 1, 2],16 [6, 7, 2, 1, 9, 5, 3, 4, 8],17 [1, 9, 8, 3, 4, 2, 5, 6, 7],18 [8, 5, 9, 7, 6, 1, 4, 2, 3],19 [4, 2, 6, 8, 5, 3, 7, 9, 1],20 [7, 1, 3, 9, 2, 4, 8, 5, 6],21 [9, 6, 1, 5, 3, 7, 2, 8, 4],22 [2, 8, 7, 4, 1, 9, 6, 3, 5],23 [3, 4, 5, 2, 8, 6, 1, 7, 9]24]); // => true25validSolution([26 [5, 3, 4, 6, 7, 8, 9, 1, 2],27 [6, 7, 2, 1, 9, 0, 3, 4, 8],28 [1, 0, 0, 3, 4, 2, 5, 6, 0],29 [8, 5, 9, 7, 6, 1, 0, 2, 0],30 [4, 2, 6, 8, 5, 3, 7, 9, 1],31 [7, 1, 3, 9, 2, 4, 8, 5, 6],32 [9, 0, 1, 5, 3, 7, 2, 1, 4],33 [2, 8, 7, 4, 1, 9, 6, 3, 5],34 [3, 0, 0, 4, 8, 1, 1, 7, 9]35]); // => false36"""37def valid_solution(board) -> bool:38 is_valid_solution = True39 if not board or len(board) != 9:40 is_valid_solution = False41 squares = [[] for _ in board]42 line_idx = 043 # form squares44 for idx, line in enumerate(board, 1):45 # check each line46 if len(set(line)) != 9:47 is_valid_solution = False48 squares[line_idx].append(line[:3])49 squares[line_idx + 1].append(line[3:6])50 squares[line_idx + 2].append(line[6:])51 if idx % 3 == 0:52 line_idx += 353 # check each square54 for square in squares:55 for sub_line in square:56 if not all(sub_line):57 is_valid_solution = False58 lists_extension = set(square[0] + square[1] + square[2])59 if len(lists_extension) != 9:60 is_valid_solution = False61 columns = list()62 # check each column63 for i in range(len(board)):64 sub_column = list()65 for j in range(len(board[i])):66 sub_column.append(board[j][i])67 if len(set(sub_column)) != 9:68 is_valid_solution = False69 columns.append(sub_column)70 return is_valid_solution71print(valid_solution([72 [1, 3, 2, 5, 7, 9, 4, 6, 8],73 [4, 9, 8, 2, 6, 1, 3, 7, 5],74 [7, 5, 6, 3, 8, 4, 2, 1, 9],75 [6, 4, 3, 1, 5, 8, 7, 9, 2],76 [5, 2, 1, 7, 9, 3, 8, 4, 6],77 [9, 8, 7, 4, 2, 6, 5, 3, 1],78 [2, 1, 4, 9, 3, 5, 6, 8, 7],79 [3, 6, 5, 8, 1, 7, 9, 2, 4],80 [8, 7, 9, 6, 4, 2, 1, 3, 5]]))81"""82Other Solutions831.84def validSolution(board):85 blocks = [[board[x+a][y+b] for a in (0, 1, 2) for b in (0, 1, 2)] for x in (0, 3, 6) for y in (0, 3, 6)]86 return not filter(lambda x: set(x) != set(range(1, 10)), board + zip(*board) + blocks)872.88def validSolution(board):89 boxes = validate_boxes(board)90 cols = validate_cols(board)91 rows = validate_rows(board)92 return boxes and cols and rows93def validate_boxes(board):94 for i in range(0, 9, 3):95 for j in range(0, 9, 3):96 nums = board[i][j:j+3] + board[i+1][j:j+3] + board[i+2][j:j+3]97 if not check_one_to_nine(nums):98 return False99 return True100def validate_cols(board):101 transposed = zip(*board)102 for row in transposed:103 if not check_one_to_nine(row):104 return False105 return True106 107def validate_rows(board):108 for row in board:109 if not check_one_to_nine(row):110 return False111 return True112 113def check_one_to_nine(lst):114 check = range(1,10)115 return sorted(lst) == check116 1173.118import numpy as np119from itertools import chain120nums = set(range(1, 10))121def validSolution(board):122 a = np.array(board)123 r = range(0, 9, 3)124 return all(set(v.flatten()) == nums for v in chain(a, a.T, (a[i:i+3, j:j+3] for i in r for j in r)))125 1264.127def validSolution(board):128 for x in range(9):129 arr = [board[y][x] for y in range(9)]130 arr2 = [board[x][y] for y in range(9)]131 arr3 = [board[i][y] for y in range(((x%3)*3),(((x%3)*3)+3)) for i in range((int(x/3)*3),(int(x/3)*3)+3)]132 for i in range(9):133 if arr[i] in arr[(i+1):]: return False134 if arr2[i] in arr2[(i+1):]: return False135 if arr3[i] in arr3[(i+1):]: return False136 return True...

## 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.