def cost_rotated_subpart(some_part, goal_part): ''' Determine whether the part 'some_part' appears in another part 'goal_part' as a rotated subpart. If yes, return the number of 'rotate90' needed, if no return 'np.inf' The definition of appearance is the same as in the function 'appear_as_subpart'. @param some_part: a tuple representation of a tetris part goal_part: a tuple representation of another tetris part @return the number of rotation needed to see 'some_part' appear in 'goal_part' np.inf if no rotated version of 'some_part' appear in 'goal_part' ''' # Create TetrisPart object of some_part tuple, to access TetrisPart functions. make_object_part = TetrisPart(some_part) # Rotate above tetris part 90 degrees clockwise, 4 times. for rot in list(range(1, 5)): make_object_part.rotate90() # Get tuple of tetris part matrix_part_rotated = make_object_part.get_frozen() # Call appear_as_subpart and If rotated some_part appears in goal_part, # return current iteration value as it is the number of rotations. if appear_as_subpart(matrix_part_rotated, goal_part): return rot # Return np.inf if no solution found. return np.inf
def result(self, state, action): """ Return the state that results from executing the given action in the given state. The action must be one of self.actions(state). The action can be a drop or rotation. """ currentState = list(state) if (action[0] == "rotate"): #Check if the action is a rotation action piece = TetrisPart(action[1]) piece.rotate90() currentState.append( piece.get_frozen()) #Add new rotated piece to the state currentState.remove(action[1]) #remove old piece else: newPiece = TetrisPart(action[0], action[1], action[2]) currentState.append( newPiece.get_frozen()) #Add the new piece to the state currentState.remove(action[0]) currentState.remove(action[1]) #Remove old pieces currentState = make_state_canonical(currentState) return currentState
def actions(self, state): """Return the actions that can be executed in the given state. The result would typically be a list, but if there are many actions, consider yielding them one at a time in an iterator, rather than building them all at once. Rotations are allowed, but no filtering out the actions that lead to doomed states. The actions that lead to rotation are defined as a tuple of the form: action = (rotated(piece),index(piece),magic_num) """ # First the drop of one piece into other valid_moves1 = AssemblyProblem_2.actions(self, state) # Rotation of one of the pieces of state valid_moves2 = [] # Check if all elements are equal. If all pieces are the same it does not matter which one we rotate are_equal = False if state[1:] == state[:-1]: are_equal = True for i in range(0, len(state)): tetris_piece = TetrisPart(state[i]) tetris_piece.rotate90() piece = tetris_piece.get_frozen() valid_moves2.append((piece, i, self.magic_num)) if are_equal: break valid_moves = valid_moves1 + valid_moves2 return valid_moves
def result(self, state, action): """ Return the state that results from executing the given action in the given state. The action must be one of self.actions(state). The action can be a drop or rotation. """ # Here a workbench state is a frozenset of parts #raise NotImplementedError part_list = list(state) pa, pu, offset = action if pu == None: part_list.remove(pa) rotated_part = TetrisPart(pa) rotated_part.rotate90() part_list.append(rotated_part.get_frozen()) return make_state_canonical(part_list) else: part_list.remove(pa) part_list.remove(pu) new_part = TetrisPart(pa, pu, offset) part_list.append(new_part.get_frozen()) return make_state_canonical(part_list)
def cost_rotated_subpart(some_part, goal_part): #DONE ''' Determine whether the part 'some_part' appears in another part 'goal_part' as a rotated subpart. If yes, return the number of 'rotate90' needed, if no return 'np.inf' The definition of appearance is the same as in the function 'appear_as_subpart'. -> call this function for all rotations of some_part @param some_part: a tuple representation of a tetris part goal_part: a tuple representation of another tetris part @return the number of rotation needed to see 'some_part' appear in 'goal_part' 0, 1, 2, 3 or infinity (np.inf) np.inf if no rotated version of 'some_part' appear in 'goal_part' ''' #USING HIS METHOD # Make a tetris part of all parts, rotate some part n times, test # appear_as_subpart for each rotation, if true return n. If all rotations # tested and returned false, return inf because no solution. sp = TetrisPart(some_part) gp = TetrisPart(goal_part) for num_rot in range(0,4): if appear_as_subpart(sp.get_frozen(), gp.get_frozen()): return num_rot sp.rotate90() return np.inf
def test3(): initial_state = load_state('workbenches/wb_09_i.txt') c = initial_state[0] print(initial_state) print(c) d = TetrisPart(c) d.rotate90() print(d.get_frozen()) display_state((d.get_frozen(), c))
def cost_rotated_subpart(some_part, goal_part): ''' Determine whether the part 'some_part' appears in another part 'goal_part' as a rotated subpart. If yes, return the number of 'rotate90' needed, if no return 'np.inf' The definition of appearance is the same as in the function 'appear_as_subpart'. @param some_part: a tuple representation of a tetris part goal_part: a tuple representation of another tetris part @return the number of rotation needed to see 'some_part' appear in 'goal_part' np.inf if no rotated version of 'some_part' appear in 'goal_part' ''' ''' compare subpart to goal_part do you need to rotate? no? -> sub_part == goal part rotate_counter=np.inf yes-> rotate-> (assignment_one) -> assignment_one.rotate90() rotate_counter=rotate_counter+1 loop until sub_part == goal_part OR rotate_counter == 4 (360deg) ''' # raise NotImplementedError rotate_counter = 0 while rotate_counter < 4: ps = np.array(some_part) # pg = np.array(goal_part) psT = TetrisPart(ps) pgT = TetrisPart(pg) if appear_as_subpart(ps,pg) == False: psT.rotate90() rotate_counter += 1 #do rotation #return rotate_counter # if counter exits loop, there is no match; set value to infinity and exit rotate_counter=np.inf return rotate_counter
def result(self, state, action): """ Return the state that results from executing the given action in the given state. The action must be one of self.actions(state). The action can be a drop or rotation. """ # Store action[0] - [2] in corresponding part and offset variables (pa = part above, pu = part under). pa, pu, offset = action # Resulting list of combined state. Currently lists all parts in state. state_to_make_canonical = list(state) final_state = "" # Conditional statement to check if there is a part to drop onto or not. if pu is not None: # Make new part with pa and pu. make_object = TetrisPart(pa, pu, offset) # Get tuple of tetris part. returned_state = make_object.get_frozen() # Remove existing parts from list state_to_make_canonical.remove(pa) state_to_make_canonical.remove(pu) else: # Make new part with pa, rotate and get tuple of new part. make_object = TetrisPart(pa) make_object.rotate90() returned_state = make_object.get_frozen() # Remove old part above from state list. state_to_make_canonical.remove(pa) # Append new state to resulting list, make canonical and return. state_to_make_canonical.append(returned_state) final_state = make_state_canonical(state_to_make_canonical) return final_state
def result(self, state, action): #DONE """ Return the state that results from executing the given action in the given state. The action must be one of self.actions(state). The action can be a drop or rotation. """ # Here a workbench state is a frozenset of parts assert(action in self.actions(state)) #defense part_list = list(state) if len(action)==2: #THIS IS A ROTATION part, rot = action assert (rot in range(1,4)) #defense # ROTATE PART NUM_ROT TIMES 90 DEGREES # REMOVE PART FROM PART LIST # APPEND ROTATED PART TO PART LIST new_part = TetrisPart(part) for i in range(0, rot): new_part.rotate90() new_part_tuple = new_part.get_frozen() part_list.remove(part) part_list.append(new_part_tuple) elif len(action) == 3: # THIS IS A DROP # USE THE ACTION GIVEN TO MAKE A NEW PART FROM PU AND PA # REMOVE PA AND PU FROM STATE, REPLACE WITH NEW PART # COMPUTE AND RETURN NEW STATE pa, pu, offset = action new_part = TetrisPart(pa,pu,offset) new_part_tuple = new_part.get_frozen() part_list.remove(pu) part_list.remove(pa) part_list.append(new_part_tuple) return make_state_canonical(part_list)
def cost_rotated_subpart(some_part, goal_part): ''' Determine whether the part 'some_part' appears in another part 'goal_part' as a rotated subpart. If yes, return the number of 'rotate90' needed, if no return 'np.inf' The definition of appearance is the same as in the function 'appear_as_subpart'. @param some_part: a tuple representation of a tetris part goal_part: a tuple representation of another tetris part @return the number of rotation needed to see 'some_part' appear in 'goal_part' np.inf if no rotated version of 'some_part' appear in 'goal_part' ''' # Create a TetrisPart to rotate some_part some_part_tetris = TetrisPart(some_part) # Check if the array has just a single element. if np.array(some_part).size == 1: # Check if the element is in goal. if some_part[0] in goal_part: return 0 else: return np.inf # Check the four possible rotations possible_rotations = 4 for rot in range(0, possible_rotations): if rot == 0: some_part_tetris.get_frozen() if appear_as_subpart(some_part_tetris.frozen, goal_part): return rot else: some_part_tetris.rotate90() some_part_tetris.get_frozen() if appear_as_subpart(some_part_tetris.frozen, goal_part): return rot return np.inf
def cost_rotated_subpart(some_part, goal_part): ''' Determine whether the part 'some_part' appears in another part 'goal_part' as a rotated subpart. If yes, return the number of 'rotate90' needed, if no return 'np.inf' The definition of appearance is the same as in the function 'appear_as_subpart'. @param some_part: a tuple representation of a tetris part goal_part: a tuple representation of another tetris part @return the number of rotation needed to see 'some_part' appear in 'goal_part' np.inf if no rotated version of 'some_part' appear in 'goal_part' ''' piece = TetrisPart(some_part) for rotation in range(0, 4): if (appear_as_subpart(piece.get_frozen(), goal_part)): return rotation # return the first number of rotation needed to appear in a goal part piece.rotate90() return np.inf # return np.inf if part can't be found in a goal piece