def initialize_board_solution(self): from utilities.rectangle import Rectangle initial_solution_state = [[-1 for c in range(self.cols)] for r in range(self.rows)] for i in range(len(self.blocks)): initial_solution_state[self.blocks[i].row][self.blocks[i].col] = i self.solution = deepcopy(initial_solution_state) # cover the solution with all possible rectangles for all blocks for k in range(len(self.blocks)): curr_row = self.blocks[k].row curr_col = self.blocks[k].col curr_value = self.blocks[k].value for factor in self.blocks[k].factors: for i in range(curr_value // factor): for j in range(factor): top_left = Point( curr_col + i - curr_value // factor + 1, curr_row - j) bottom_right = Point(curr_col + i, curr_row + factor - 1 - j) try: rect = Rectangle(self, top_left, bottom_right, k) rect.draw_rectangle() except AssertionError: continue self._update_final_cells_values() self.solution = initial_solution_state
def test_find_color(self): board = GameBoard('tests/test_puzzle.txt') board.read_board() rect = Rectangle(board, Point(0, 0), Point(0, 0), 3) rect.draw_rectangle() rect = Rectangle(board, Point(0, 0), Point(2, 2), 4) rect.find_color() self.assertEqual(rect.color, 3)
def test_draw_rect(self): board = GameBoard('tests/test_puzzle.txt') board.read_board() rect = Rectangle(board, Point(0, 0), Point(2, 1), 4) rect.draw_rectangle() for y in range(2): for x in range(3): self.assertEqual(board.solution[y][x], 4)
def test_rectangle_init(self): board = GameBoard('tests/test_puzzle.txt') board.read_board() rect = Rectangle(board, Point(0, 1), Point(2, 2), 0) self.assertEqual(rect.board, board) self.assertEqual(rect.color, 0) self.assertEqual(rect.top_left, Point(0, 1)) self.assertEqual(rect.bottom_right, Point(2, 2))
def on_mouse_release(self, x, y, button, modifiers): if button == mouse.LEFT: self.released_cords = [(x - self.start_x) // self.cell_size, (y - self.start_y) // self.cell_size] if not self.check_mouse_movement(): return point1 = Point( min(self.pressed_cords[0], self.released_cords[0]), min(self.board.cols - self.pressed_cords[1] - 1, self.board.cols - self.released_cords[1] - 1)) point2 = Point( max(self.pressed_cords[0], self.released_cords[0]), max(self.board.cols - self.pressed_cords[1] - 1, self.board.cols - self.released_cords[1] - 1)) self.update_solution(point1, point2)
def __init__(self, bottom_left_x=0.0, bottom_left_y=0.0, init_width=0.0, init_height=0.0): self.bottom_left = Point(bottom_left_x, bottom_left_y) self.width = init_width self.height = init_height
def backtrack(self, block_pointer: int = 0): # if all blocks are visited the solution is found if block_pointer > len(self.board.blocks) - 1: return True curr_block = self.board.blocks[block_pointer] # go through all block's factors while curr_block.factor_pointer < len(curr_block.factors): curr_factor = curr_block.factors[curr_block.factor_pointer] # go through all rectangles # curr_factor * curr_block.value // curr_factor for i in range(curr_block.value // curr_factor): for j in range(curr_factor): top_left = Point( curr_block.col + i - curr_block.value // curr_factor+1, curr_block.row - j) bottom_right = Point( curr_block.col + i, curr_block.row + curr_factor - 1 - j) try: rect = Rectangle( self.board, top_left, bottom_right, block_pointer) if not rect.check_conflicts(): rect.draw_rectangle() if self._is_area_full_covered(block_pointer): # true means correct solution is found if self.backtrack(block_pointer + 1): return True # incorrect state -> reset changes rect.clear() # colorize cell self.board.solution[ curr_block.row][curr_block.col] = block_pointer except AssertionError: continue self.board.blocks[block_pointer].factor_pointer += 1 # if no rectangles are found, reset pointer if block_pointer > 0: self.board.blocks[block_pointer].factor_pointer = 0
def initialize_board_model(self): self.board.initialize_board_solution() colored_squares = 0 for y, row in enumerate(self.board.board): for x, value in enumerate(row): rev_y = self.board.cols - y - 1 color = (180, 210, 230) symbol = ' ' if value == -1 else str(value) if value != -1: self.board.solution[y][x] = colored_squares color = COLORS[int(self.board.solution[y][x]) % (len(COLORS) - 1)][0:3] colored_squares += 1 self.squares[rev_y][x] = (Square( symbol, Point(x * self.cell_size + self.start_x, rev_y * self.cell_size + self.start_y), Point( x * self.cell_size + self.cell_size + self.start_x, rev_y * self.cell_size + self.cell_size + self.start_y), color))
def test_wrong_cords_init(self): board = GameBoard('tests/test_puzzle.txt') board.read_board() self.assertRaises(AssertionError, Rectangle, board, Point(-1, 0), Point(2, 1), 0) self.assertRaises(AssertionError, Rectangle, board, Point(-5, -4), Point(4, 6), 0) self.assertRaises(AssertionError, Rectangle, board, Point(2, 2), Point(1, 1), 0)
def is_point_within_dist_of_rect(rect=Rectangle(), point=Point(), dist=0.0): if ((rect.bottom_left.x - dist) < point.x and point.x < (rect.bottom_left.x + rect.width + dist)): if (point.x < rect.bottom_left.x): a = rect.bottom_left.x - point.x y_max = rect.bottom_left.y + rect.height + math.sqrt(dist**2 - a**2) y_min = rect.bottom_left.y - math.sqrt(dist**2 - a**2) if ((y_min < point.y) and point.y < y_max): return True else: return False elif (point.x < (rect.bottom_left.x + rect.width)): y_max = rect.bottom_left.y + rect.height + dist y_min = rect.bottom_left.y - dist if ((y_min < point.y) and point.y < y_max): return True else: return False else: a = rect.bottom_left.x + rect.width - point.x y_max = rect.bottom_left.y + rect.height + math.sqrt(dist**2 - a**2) y_min = rect.bottom_left.y - math.sqrt(dist**2 - a**2) if ((y_min < point.y) and point.y < y_max): return True else: return False return False
def test_sub(self): self.assertEqual(Point(2, 3) - Point(3, 0), Point(-1, 3))
def test_clone(self): point = Point(2, 4) new_point = point.clone() self.assertEqual(point, new_point) point.move_to(0, 0) self.assertNotEqual(point, new_point)
def test_point_to_string(self): self.assertEqual(str(Point(2, 3)), '(2, 3)')
def test_change_coordinates(self): point = Point(3, 4) point.move_to(2, 0) self.assertEqual(point.x, 2) self.assertEqual(point.y, 0)
def test_sum(self): self.assertEqual(Point(2, 3) + Point(3, 0), Point(5, 3))
def test_point_init(self): self.assertEqual(Point(2, 3).x, 2) self.assertEqual(Point(2, 3).y, 3)
def build_shelf_compliance(model_response_json, shelf_compliance): # collection of brand with coordinates # sample data formate # [item_or_brand_name, x, y, h, w] brand_tags_xy_data = model_response_json["MetaData"] print_debug_detail(f"{brand_tags_xy_data}") compliance_collection = [] shelf_coordinate_object = None for each_shelf in shelf_compliance: compliance_items = each_shelf.complianceItem.split(",") print_debug_info( f"Shelf Name and Tag:- {each_shelf.shelfName, each_shelf.shelfTag}" ) #get main shelf coordinate detail for single_item_coordinate in brand_tags_xy_data: if single_item_coordinate[0] == each_shelf.shelfTag: print_debug_info( f"Actual Shelf Name is:- {single_item_coordinate[0]}") shelf_coordinate_object = single_item_coordinate break print_debug_detail(f"Shelf object -> {shelf_coordinate_object}") if shelf_coordinate_object is not None: #creat shelf Rectangle object #logger.info(f"{shelf_coordinate_object[2]} {float(shelf_coordinate_object[2]+10)}") shelf_rectangle = Rectangle2(shelf_coordinate_object[1] - 1, float(shelf_coordinate_object[2] - 1), shelf_coordinate_object[3], shelf_coordinate_object[4]) #logger.info(f"finding shelf rectangle {shelf_rectangle.x,shelf_rectangle.y,shelf_rectangle.w,shelf_rectangle.h}") find_item_inside_shelf = [] #using loop searh compliance item in the shelf for each_item_coordinate in brand_tags_xy_data: predicted_item_name = each_item_coordinate[0] print_debug_info(f"Inner item Name:- {predicted_item_name}") #creat searchable item Rectangle object #find_rectangle = Rectangle(each_item_coordinate[1],each_item_coordinate[2],each_item_coordinate[3],each_item_coordinate[4]) #logger.info(f"item object coordinate -> {find_rectangle.x,find_rectangle.y,find_rectangle.w,find_rectangle.h}") item_xy_point = Point(each_item_coordinate[1], each_item_coordinate[2]) print_debug_detail( f"Inner item x,y value {each_item_coordinate[1]}, {each_item_coordinate[2]}" ) #perform search is_rect_inside = is_point_within_dist_of_rect(shelf_rectangle, item_xy_point, dist=1) print_debug_detail(f"Item found inside:- {is_rect_inside}") if is_rect_inside: find_item_inside_shelf.append(predicted_item_name) print_debug_info( f"Inside item found length: {len(find_item_inside_shelf)}") if len(find_item_inside_shelf) > 0: #total compliance item formula using intersection of two sets comp_list_as_set = set(compliance_items) intersection = comp_list_as_set.intersection( find_item_inside_shelf) final_intersected_compliance_items = list(intersection) print_debug_info( f"compliance items list {final_intersected_compliance_items}" ) total_compliance_items_count = len( final_intersected_compliance_items) total_shelf_items_count = len(find_item_inside_shelf) total_ratio = total_compliance_items_count / total_shelf_items_count compliance_metadata = ComplianceMetaData( find_item_inside_shelf, final_intersected_compliance_items, each_shelf.shelfName, each_shelf.shelfTag, total_compliance_items_count, total_shelf_items_count, total_ratio, each_shelf.complianceLevel) compliance_collection.append(compliance_metadata) else: logger.info(f"No Compliance item found") print_debug_detail(f"loop-end") else: logger.info(f"Shelf not found") print_debug_detail(f"main-loop-end") json_string = json.dumps([ob.__dict__ for ob in compliance_collection], cls=ComplexEncoder) print_debug_detail(f"Compliance Json data") print_debug_detail(f"{json_string}") print_debug_info(f"exit from build_shelf_compliance") return json_string