def bfs_map(value): """ YOUR CODE HERE """ return_list = [value] children = Sliding.children(HEIGHT, WIDTH, Sliding.hash_to_board(WIDTH, HEIGHT, value[0])) for child in children: return_list.append((Sliding.board_to_hash(WIDTH, HEIGHT, child), value[1]+1)) return return_list
def bfs_map(value): """ YOUR CODE HERE """ result = [] if value[1] == level - 1: result = Sliding.children(WIDTH, HEIGHT, Sliding.hash_to_board(WIDTH, HEIGHT, value[0])) for i in range(0, len(result)): result[i] = (Sliding.board_to_hash(WIDTH, HEIGHT, result[i]), level) result.append(value) return result
def bfs_map(value): #value is the (puzzle, level) tuple """ YOUR CODE HERE """ lst = [(value)] if value[1] == level: children = Sliding.children(WIDTH,HEIGHT,Sliding.hash_to_board(WIDTH, HEIGHT, value[0])) for child in children: lst.append((Sliding.board_to_hash(WIDTH, HEIGHT, child),level+1)) return lst return lst
def bfs_map(value): """ YOUR CODE HERE """ if (value[1] != (level - 1)): return [value] else: children = Sliding.children(WIDTH, HEIGHT, Sliding.hash_to_board(WIDTH,HEIGHT,value[0])) childList = [value] for child in children: childList.append((Sliding.board_to_hash(WIDTH,HEIGHT,child), level)) return childList
def bfs_map(value): """ YOUR CODE HERE """ mapVal = [] mapVal.append((value[0], value[1])) if value[1] == level: pos = Sliding.hash_to_board(WIDTH, HEIGHT, value[0]) for cpos in Sliding.children(WIDTH, HEIGHT, pos): cpos2 = Sliding.board_to_hash(WIDTH, HEIGHT, cpos) mapVal.append((cpos2,level+1)) return mapVal
def bfs_map(value): items = [] if value[1] < level: items.append((value[0],value[1])) if value[1] == level-1: children_board = Sliding.hash_to_board(WIDTH, HEIGHT, value[0]) children = Sliding.children(WIDTH, HEIGHT, children_board) for child in children: items.append((Sliding.board_to_hash(WIDTH, HEIGHT, child), value[1] + 1)) return items
def bfs_map(value): """ YOUR CODE HERE """ result = [] if value[1] == level - 1: result = Sliding.children( WIDTH, HEIGHT, Sliding.hash_to_board(WIDTH, HEIGHT, value[0])) for i in range(0, len(result)): result[i] = (Sliding.board_to_hash(WIDTH, HEIGHT, result[i]), level) result.append(value) return result
def bfs_map(value): #value is the (puzzle, level) tuple """ YOUR CODE HERE """ lst = [(value)] if value[1] == level: children = Sliding.children( WIDTH, HEIGHT, Sliding.hash_to_board(WIDTH, HEIGHT, value[0])) for child in children: lst.append((Sliding.board_to_hash(WIDTH, HEIGHT, child), level + 1)) return lst return lst
def bfs_map(value): """ YOUR CODE HERE """ if (value[1] != (level - 1)): return [value] else: children = Sliding.children( WIDTH, HEIGHT, Sliding.hash_to_board(WIDTH, HEIGHT, value[0])) childList = [value] for child in children: childList.append((Sliding.board_to_hash(WIDTH, HEIGHT, child), level)) return childList
def bfs_map(arg): """ YOUR CODE HERE """ if arg[1] == level: children = Sliding.children( WIDTH, HEIGHT, Sliding.hash_to_board(WIDTH, HEIGHT, arg[0])) toreturn = [arg] for position in children: toreturn.append((Sliding.board_to_hash(WIDTH, HEIGHT, position), level + 1)) return toreturn else: return [arg]
def bfs_map(value): """ Takes in a key, value pair of (board state, level), creates all of the children of that board state if is on the same level as the global level, and returns them in a list. """ """ YOUR CODE HERE """ child_list = [] #Check if we are at the right level, so then we can make children of only those boards if value[1] == level: temp = Sliding.hash_to_board(WIDTH, HEIGHT, value[0]) iter_list = Sliding.children(WIDTH, HEIGHT, temp) for child in iter_list: child_list += [(Sliding.board_to_hash(WIDTH,HEIGHT, child), level + 1)] #Spark map only lets us return a list if we want multiple things. #Unlike Hadoop I believe which allows us to emit return child_list
def bfs_map(value): """ YOUR CODE HERE """ prev = [(value[0], value[1])] if value[1] == level: #convert from int to board # do I save it, not sure? ask Manny , using hashID since it was declared down? hashID = Sliding.board_to_hash(WIDTH, HEIGHT, prev[0][0]) #value[0] currBoard = Sliding.hash_to_board(WIDTH, HEIGHT, hashID) # ask manny if this is the correct to call this method # also what would number be? children = Sliding.children(WIDTH, HEIGHT, currBoard) # not sure value[0], currBoard #nextID = Sliding.board_to_hash(WIDTH, HEIGHT, children) curr = [] for i in range(0, len(children)): curr.append((children[i], level+1)) return prev + curr #nextID = Sliding.board_to_hash(WIDTH, HEIGHT, children[0]) #children[0] return prev
def solve_sliding_puzzle(master, output, height, width): """ Solves a sliding puzzle of the provided height and width. master: specifies master url for the spark context output: function that accepts string to write to the output file height: height of puzzle width: width of puzzle """ # Set up the spark context. Use this to create your RDD sc = SparkContext(master, "python") # Global constants that will be shared across all map and reduce instances. # You can also reference these in any helper functions you write. global HEIGHT, WIDTH, level # Initialize global constants HEIGHT=height WIDTH=width level = 0 # this "constant" will change, but it remains constant for every MapReduce job # The solution configuration for this sliding puzzle. You will begin exploring the tree from this node sol = Sliding.solution(WIDTH, HEIGHT) """ YOUR MAP REDUCE PROCESSING CODE HERE """ myRdd = sc.parallelize([(sol, level)]) # myRdd = [(('A', 'B', 'C', '-'), 0)] myRdd = myRdd.flatMap(bfs_flat_map).reduceByKey(bfs_reduce) prev_num = 0 pos_num = myRdd.count() while prev_num != pos_num: level+=1 prev_num = pos_num myRdd = myRdd.flatMap(bfs_flat_map) if level%4==0: myRdd = myRdd.partitionBy(16) myRdd = myRdd.reduceByKey(bfs_reduce) pos_num = myRdd.count() """ YOUR OUTPUT CODE HERE """ # myRdd = myRdd.map(lambda a: (a[1], a[0])).sortByKey().collect() # myRdd becomes a list # for each in myRdd: # output(str(each[0]) + " " + str(each[1])) myRdd = myRdd.map(lambda a: (Sliding.hash_to_board(WIDTH, HEIGHT, a[1]), a[0])).sortByKey() sc.stop()
def bfs_map(value): """ value: Taken an element from RDD bfs_map function only applies children() to each element at the last level in RDD; return :If an element is not at the last level, then it will be put in an empty list and return; return :If an element is at the last level, then its children and the element will be put into an empty list and return; """ lst = [] lst.append(value) value = (Sliding.hash_to_board(WIDTH,HEIGHT,value[0]), value[1]) if (value[1] < level): return lst children = Sliding.children(WIDTH, HEIGHT, value[0]) for each in children: lst.append(((Sliding.board_to_hash(WIDTH, HEIGHT, tuple(each))), value[1]+1)) return lst
def bfs_map(value): """ Takes in a key, value pair of (board state, level), creates all of the children of that board state if is on the same level as the global level, and returns them in a list. """ """ YOUR CODE HERE """ child_list = [] #Check if we are at the right level, so then we can make children of only those boards if value[1] == level: temp = Sliding.hash_to_board(WIDTH, HEIGHT, value[0]) iter_list = Sliding.children(WIDTH, HEIGHT, temp) for child in iter_list: child_list += [(Sliding.board_to_hash(WIDTH, HEIGHT, child), level + 1)] #Spark map only lets us return a list if we want multiple things. #Unlike Hadoop I believe which allows us to emit return child_list
def bfs_flat_map(value): """ YOUR CODE HERE """ # childrenLst=[] # childrenLst.append(value) # for child in Sliding.children(WIDTH,HEIGHT,Sliding.hash_to_board(WIDTH, HEIGHT, value[0])): # pair=[] # pair.append(Sliding.board_to_hash(WIDTH, HEIGHT, child)) # pair.append(level) # childrenLst.append(tuple(pair)) # return childrenLst childrenLst = [] childrenLst.append((value[0], value[1])) if(value[1] == level - 1): for child in Sliding.children(WIDTH, HEIGHT, Sliding.hash_to_board(WIDTH,HEIGHT, value[0])): childrenLst.append((Sliding.board_to_hash(WIDTH,HEIGHT,child), value[1]+1)) return childrenLst
def solve_sliding_puzzle(master, output, height, width): """ Solves a sliding puzzle of the provided height and width. master: specifies master url for the spark context output: function that accepts string to write to the output file height: height of puzzle width: width of puzzle """ # Set up the spark context. Use this to create your RDD sc = SparkContext(master, "python") # Global constants that will be shared across all map and reduce instances. # You can also reference these in any helper functions you write. global HEIGHT, WIDTH, level # Initialize global constants HEIGHT = height WIDTH = width level = 0 # this "constant" will change, but it remains constant for every MapReduce job # The solution configuration for this sliding puzzle. You will begin exploring the tree from this node sol = Sliding.solution(WIDTH, HEIGHT) """ YOUR MAP REDUCE PROCESSING CODE HERE """ myRdd = sc.parallelize([(sol, level) ]) # myRdd = [(('A', 'B', 'C', '-'), 0)] myRdd = myRdd.flatMap(bfs_flat_map).reduceByKey(bfs_reduce) prev_num = 0 pos_num = myRdd.count() while prev_num != pos_num: level += 1 prev_num = pos_num myRdd = myRdd.flatMap(bfs_flat_map) if level % 4 == 0: myRdd = myRdd.partitionBy(16) myRdd = myRdd.reduceByKey(bfs_reduce) pos_num = myRdd.count() """ YOUR OUTPUT CODE HERE """ # myRdd = myRdd.map(lambda a: (a[1], a[0])).sortByKey().collect() # myRdd becomes a list # for each in myRdd: # output(str(each[0]) + " " + str(each[1])) myRdd = myRdd.map(lambda a: (Sliding.hash_to_board(WIDTH, HEIGHT, a[1]), a[ 0])).sortByKey() sc.stop()
def bfs_map(value): """ value: Taken an element from RDD bfs_map function only applies children() to each element at the last level in RDD; return :If an element is not at the last level, then it will be put in an empty list and return; return :If an element is at the last level, then its children and the element will be put into an empty list and return; """ lst = [] lst.append(value) value = (Sliding.hash_to_board(WIDTH, HEIGHT, value[0]), value[1]) if (value[1] < level): return lst children = Sliding.children(WIDTH, HEIGHT, value[0]) for each in children: lst.append(((Sliding.board_to_hash(WIDTH, HEIGHT, tuple(each))), value[1] + 1)) return lst
def bfs_map(value): """ YOUR CODE HERE """ prev = [(value[0], value[1])] if value[1] == level: #convert from int to board # do I save it, not sure? ask Manny , using hashID since it was declared down? hashID = Sliding.board_to_hash(WIDTH, HEIGHT, prev[0][0]) #value[0] currBoard = Sliding.hash_to_board( WIDTH, HEIGHT, hashID) # ask manny if this is the correct to call this method # also what would number be? children = Sliding.children(WIDTH, HEIGHT, currBoard) # not sure value[0], currBoard #nextID = Sliding.board_to_hash(WIDTH, HEIGHT, children) curr = [] for i in range(0, len(children)): curr.append((children[i], level + 1)) return prev + curr #nextID = Sliding.board_to_hash(WIDTH, HEIGHT, children[0]) #children[0] return prev
def bfs_flat_map(value): """ YOUR CODE HERE """ # childrenLst=[] # childrenLst.append(value) # for child in Sliding.children(WIDTH,HEIGHT,Sliding.hash_to_board(WIDTH, HEIGHT, value[0])): # pair=[] # pair.append(Sliding.board_to_hash(WIDTH, HEIGHT, child)) # pair.append(level) # childrenLst.append(tuple(pair)) # return childrenLst childrenLst = [] childrenLst.append((value[0], value[1])) if (value[1] == level - 1): for child in Sliding.children( WIDTH, HEIGHT, Sliding.hash_to_board(WIDTH, HEIGHT, value[0])): childrenLst.append((Sliding.board_to_hash(WIDTH, HEIGHT, child), value[1] + 1)) return childrenLst
def solve_puzzle(master, output, height, width, slaves): global HEIGHT, WIDTH, level HEIGHT=height WIDTH=width level = 0 sc = SparkContext(master, "python") sol = Sliding.solution(WIDTH, HEIGHT) frontierRDD = sc.parallelize([(Sliding.board_to_hash(WIDTH, HEIGHT, sol), 0)]) boardsRDD = sc.parallelize([(Sliding.board_to_hash(WIDTH, HEIGHT, sol), 0)]) #while frontierRDD.count() != 0: while True: level += 1 # get all frontier nodes as a flattened list of ONLY (key), NOT (key, value) frontierRDD = frontierRDD.flatMap(lambda v: Sliding.children(WIDTH, HEIGHT, Sliding.hash_to_board(WIDTH, HEIGHT, v[0]))) # add new (chilq, level) pairs to all boards boardsRDD = boardsRDD + frontierRDD.map(lambda v: (Sliding.board_to_hash(WIDTH, HEIGHT, v), level)) #boardsRDD = boardsRDD.partitionBy(8, partitionFunc) # only keep board seen at lowest level boardsRDD = boardsRDD.reduceByKey(lambda v1, v2: min(v1, v2)) # frontier is only the boards that have the current level frontierRDD = boardsRDD.filter(lambda v: v[1] == level) # magic voodoo that it doesn't work without boardsRDD = boardsRDD.partitionBy(slaves, lambda v: v) frontierRDD = frontierRDD.partitionBy(slaves, lambda v: v) if level % 4 == 0 and frontierRDD.count() == 0: break boardsRDD.coalesce(slaves).saveAsTextFile(output) sc.stop()
def revert_back(value): return str(value[1]) + " " + str( Sliding.hash_to_board(WIDTH, HEIGHT, value[0]))
def get_board(state): """Abstraction for obtaining the board of a state representation """ return Sliding.hash_to_board(WIDTH, HEIGHT, state[0])
def get_board(state): """Abstraction for obtaining the board of a state representation """ return Sliding.hash_to_board(WIDTH, HEIGHT, state[0])
def hash_to_board(state): return Sliding.hash_to_board(width, height, state)
def revert_back(value): return str(value[1]) + " " + str(Sliding.hash_to_board(WIDTH, HEIGHT, value[0]))