def analyze(design): """ Gets a design object from p6_tool Do breath first search to find path for every position, Save a dictionary of parent states. """ global ANALYSIS sim = Simulator(design) init = sim.get_initial_state() ANALYSIS = {init: (None, None)} queue = [] queue.append(init) while queue: curr_state = queue.pop( 0) # list.pop(0) gets the first item, pop() gets the last one adj_list = get_adj(sim, curr_state) for adj in adj_list: if adj not in ANALYSIS: queue.append(adj) ANALYSIS[adj] = curr_state return
def analyze(design): sim = Simulator(design) specials = design['specials'] init = sim.get_initial_state() frontier = [] frontier.append(init) global ANALYSIS ANALYSIS = {init: (None, None)} # Reset the ANALYSIS dictionary to scrub out old data. while len(frontier) > 0: current = frontier.pop(0) for neighbor in _get_neighbors(sim, current): if neighbor not in ANALYSIS: # Record the unvisited state frontier.append(neighbor) ANALYSIS[neighbor] = current # Check if goal coordinate (Special 5) is reachable reach = False for state in ANALYSIS: position, _ = state if specials.get(position, 0) == 5: reach = True break if reach: print "Special 5 is reachable!" else: print "Special 5 is NON reachable!"
def analyze(design): sim = Simulator(design) # TODO: fill in this function, populating the ANALYSIS dict init = sim.get_initial_state() global ANALYSIS queue = [] ANALYSIS = {init: (None, None)} queue.append(init) while queue: curr_state = queue.pop(0) adj_list = get_adj(sim, curr_state) for adj in adj_list: if adj not in ANALYSIS: queue.append(adj) ANALYSIS[adj] = curr_state print ANALYSIS return
def analyze(design): sim = Simulator(design) init = sim.get_initial_state() #states are position, abilities ANALYSIS = {init: None} queue = Q.PriorityQueue() queue.put((0, init[0], init[1])) while not queue.empty(): curr_state = queue.get() moves = sim.get_moves() states = [] for m in moves: print curr_state if sim.get_next_state((curr_state[1], curr_state[2]), m) != None: pos, abs = sim.get_next_state((curr_state[1], curr_state[2]), m) state = (curr_state[0] + 1, pos, abs) states.append(state) for s in states: this = (s[1], s[2]) if this not in ANALYSIS: ANALYSIS[this] = (curr_state[1], curr_state[2]) queue.put(s) return ANALYSIS
def analyze(design): sim = Simulator(design) init = sim.get_initial_state() # next_state = sim.get_next_state(init, moves[0]) # position, abilities = next_state # or None if character dies # i, j = position #BFS prev = {init: None} q = [init] while q: curr_state = q.pop(0) # print (str(curr_state[0])) moves = sim.get_moves() neighbors = [] for move in moves: neighbor = sim.get_next_state(curr_state, move) if neighbor is not None: neighbors.append(neighbor) for next_state in neighbors: if next_state not in prev: prev[next_state] = curr_state q.append(next_state) ANALYSIS['prev'] = prev print('analysis complete')
def analyze(design): sim = Simulator(design) init = sim.get_initial_state() #BFS ANALYSIS = {init: None} q = Q.PriorityQueue() q.put((0, init[0], init[1])) while not q.empty(): node = q.get() #no goal state required as we're doing an exhaustive search moves = sim.get_moves() #see all possible states for each move states = [] for move in moves: #NONETYPE not iterable error #print node if sim.get_next_state((node[1],node[2]), move) != None: pos, abil = sim.get_next_state((node[1],node[2]), move) newState = (node[0] + 1, pos, abil) states.append(newState) #Use ANALYSIS like a prev dict, only each key now has states so the solution will be unique for each set of abilities for state in states: curr = (state[1],state[2]) if curr not in ANALYSIS: ANALYSIS[curr] = (node[1],node[2]) q.put(state) return ANALYSIS
def analyze(design): sim = Simulator(design) init = sim.get_initial_state() moves = sim.get_moves() ANALYSIS.clear() queue = Queue.Queue() visited = [] queue.put(init) ANALYSIS[init] = None visited.append(init) while not queue.empty(): curr_state = queue.get() for move in moves: next_state = sim.get_next_state(curr_state, move) if next_state not in visited and next_state is not None: ANALYSIS[next_state] = curr_state queue.put(next_state) visited.append(next_state)
def analyze(design): ANALYSIS.clear() sim = Simulator(design) init = sim.get_initial_state() moves = sim.get_moves() next_state = sim.get_next_state(init, moves[0]) queue = deque([]) visited = [] ANALYSIS[init] = None queue.append(init) visited.append(init) while len(queue) > 0: current_state = queue.popleft() if current_state[0] == (8,2): print "Goal is reachable with the following ability combination:" for ability in current_state[1]: print ability if len(current_state[1]) == 0: print "Does not require any abilities!" for move in moves: next_state = sim.get_next_state(current_state, move) #position, abilities = next_state if next_state == None: continue if next_state not in visited: ANALYSIS[next_state] = current_state queue.append(next_state) visited.append(next_state)
def analyze(design): visited = [] prev = {} queve = deque() abilities = {} # visited = [] sim = Simulator(design) init = sim.get_initial_state() init_pos, init_ability = init abilities[init_pos] = init_ability # print ANALYSIS[init_pos] visited.append(init_pos) queve.append(init) # # while queve is not empty while queve: current_state = queve.popleft() current_position, current_abilities = current_state # # if node has reached its destination cell it will quit while loop # # calls the get_steps function stored in adj moves = sim.get_moves() for next_move in moves: if next_move == "NOTHING": continue next_state = sim.get_next_state(current_state, next_move) if next_state == None: # ANALYSIS[next_position] = None continue # if in the current state the character dies ignore this state next_position, next_abilities = next_state if next_position == current_position: continue # stores new ability into Analysis for later use when it comes to drawing out the lines if next_position not in visited: visited.append(next_position) abilities[next_position] = current_abilities prev[next_position] = current_position queve.append(next_state) elif next_position in visited: for ability in next_abilities: if ability not in abilities[next_position]: abilities[next_position] = current_abilities ANALYSIS.append(prev) prev.clear() prev[current_position] = None prev[next_position] = current_position queve.append(next_state) ANALYSIS[str(len(abilities))] = prev # ANALYSIS.reverse() print current_abilities
def analyze(design): sim = Simulator(design) specials = design['specials'] init = sim.get_initial_state() frontier = [] frontier.append(init) global ANALYSIS ANALYSIS = { init: (None, None) } # Reset the ANALYSIS dictionary to scrub out old data. while len(frontier) > 0: current = frontier.pop(0) for neighbor in _get_neighbors(sim, current): if neighbor not in ANALYSIS: # Record the unvisited state frontier.append(neighbor) ANALYSIS[neighbor] = current # Check if goal coordinate (Special 5) is reachable reach = False for state in ANALYSIS: position, _ = state if specials.get(position, 0) == 5: reach = True break if reach: print "Special 5 is reachable!" else: print "Special 5 is NON reachable!"
def analyze(design): sim = Simulator(design) init = sim.get_initial_state() for i in range(0, design['width']): # prepare list of lists for j in range(0, design['height']): ANALYSIS[(i,j)] = [] queue = [init] # search setup parent = {init: None} while queue: # BFS current = queue.pop(0) position, abilities = current if parent[current] is not None: # add new path to current location previous, junk = parent[current] path = ANALYSIS[previous][len(ANALYSIS[previous])-1] + [position] else: # initial state case path = [position] ANALYSIS[position].append(path) moves = sim.get_moves() # add all new possibilities to queue for move in moves: next_state = sim.get_next_state(current, move) if next_state is not None and next_state not in parent: # if next_state is valid and new parent[next_state] = current queue.append(next_state)
def analyze(design): sim = Simulator(design) ANALYSIS = {} sim = Simulator(design) init = sim.get_initial_state() moves = sim.get_moves() next_state = sim.get_next_state(init, moves[0]) position, abilities = next_state # or None if character dies i, j = position
def analyze(design): global prev_state global abil prev_state = {} abil = {} sim = Simulator(design) init = sim.get_initial_state() moves = sim.get_moves() init_pos, init_abil = init # a state is a tuple (position, abilities) prev_state[init] = None abil[init_pos] = [init_abil] states = [init] for state in states: for move in moves: next_state = sim.get_next_state(state, move) if next_state is None: continue if next_state == state: continue if next_state in prev_state: continue position, abilities = next_state prev_state[next_state] = state if position in abil: duplicate = False for abil_set in abil[position]: if not abil_set.symmetric_difference(abilities): duplicate = True if not duplicate: # if this ability set is novel for this position, # add it to the list of ability sets at this position, # and add this new state to the state list, # otherwise, discard this state abil[position].append(abilities) states.append(next_state) else: # if this position has never been reached, # enter it into the abilities-by-position table, # and add this new state to the state list. abil[position] = [abilities] states.append(next_state) debug = "an assignment to break on"
def analyze(design): global ANALYSIS ANALYSIS.clear() ANALYSIS = {} #Use this dict to construct a Simulator object. sim = Simulator(design) abilities = sim.get_abilities() for ability in abilities: sim = Simulator(design) path = _ability_path(sim, ability) if path[0] and path[1]: ANALYSIS[ability] = path
def analyze(design): ANALYSIS.clear() sim = Simulator(design) init = sim.get_initial_state() moves = sim.get_moves() queue = [init] ANALYSIS[init] = None while queue: node = queue.pop(0) for nextNode in graph(sim, node, moves): if nextNode not in ANALYSIS: queue.append(nextNode) ANALYSIS[nextNode] = node
def analyze(design): sim = Simulator(design) queue = [] prev = [] init = sim.get_initial_state() queue.append((init, [init])) prev.append(init) global ANALYSIS ANALYSIS = {} while queue: (state, path) = queue.pop(0) for next_move in sim.get_moves(): next_state = sim.get_next_state(state, next_move) if next_state is not None and next_state not in ANALYSIS and next_state not in prev: ANALYSIS[next_state] = path queue.append((next_state, path + [next_state])) prev.append(next_state)
def analyze(design): global ANALYSIS ANALYSIS = {} sim = Simulator(design) queue = [] visited = [] init = sim.get_initial_state() queue.append((init, [init])) visited.append(init) while queue: (state, path) = queue.pop(0) for next in sim.get_moves(): next_state = sim.get_next_state(state, next) if next_state is not None and next_state not in ANALYSIS: if next_state not in visited: ANALYSIS[next_state] = path queue.append((next_state, path + [next_state])) visited.append(next_state)
def analyze(design): sim = Simulator(design) init = sim.get_initial_state() moves = sim.get_moves() next_state = sim.get_next_state(init, moves[0]) queue = Queue.Queue() queue.put(next_state) ANALYSIS[next_state] = None while not queue.empty(): current = queue.get() for i in range(0, 4): next = sim.get_next_state(current, moves[i]) if next == None: continue if next not in ANALYSIS: queue.put(next) ANALYSIS[next] = current
def analyze(design): PREV.clear() sim = Simulator(design) init = sim.get_initial_state() moves = sim.get_moves() queue = [] queue.append(init) PREV[init] = None while queue: current_state = queue.pop(0) for move in moves: next_state = sim.get_next_state(current_state, move) if next_state != None and next_state not in PREV: queue.append(next_state) PREV[next_state] = current_state
def analyze(design): sim = Simulator(design) queue = [] visited = [] state = sim.get_initial_state() queue.append((state, [state])) visited.append(state) while queue: (prevstate, path) = queue.pop(0) for next in sim.get_moves(): newstate = sim.get_next_state(prevstate, next) if (newstate is not None and newstate not in ANALYSIS): if newstate not in visited: ANALYSIS[newstate] = path queue.append((newstate, path + [newstate])) visited.append(newstate)
def analyze(design): global ANALYSIS ANALYSIS = {} sim = Simulator(design) queue = [] visited = [] # TODO: fill in this function, populating the ANALYSIS dict init = sim.get_initial_state() moves = sim.get_moves() next_state = sim.get_next_state(init, moves[0]) queue.append((init, [init])) visited.append(init) while queue: (prev,path) = queue.pop(0) for each in moves: next_state = sim.get_next_state(prev, each) if next_state not in ANALYSIS and next_state is not None: if next_state not in visited: ANALYSIS[next_state] = path queue.append((next_state, path + [next_state])) visited.append(next_state)
def analyze(design): sim = Simulator(design) init = sim.get_initial_state() moves = sim.get_moves() next_state = sim.get_next_state(init, moves[0]) position, abilities = init # or None if character dies # ANALYSIS[init] = init queue = [] visited = [] queue.append((init, [])) visited.append(init) # print queue while queue: current_state, p = queue.pop(0) # if current_state != sim.get_end_goal(): # break for m in sim.get_moves(): next_state = sim.get_next_state(current_state, m) if next_state is not None and next_state not in visited and next_state not in ANALYSIS: ANALYSIS[next_state] = p visited.append(next_state) queue.append((next_state, p + [next_state]))
def analyze(design): sim = Simulator(design) queue = [] visited = [] init = sim.get_initial_state() moves = sim.get_moves() next_state = sim.get_next_state(init, moves[0]) position, abilities = next_state # or None if character dies i, j = position queue.append((init, [init])) visited.append(init) while queue: (prevstate, path) = queue.pop(0) for next in sim.get_moves(): newstate = sim.get_next_state(prevstate, next) if (newstate is not None and newstate not in ANALYSIS): if newstate not in visited: ANALYSIS[newstate] = path queue.append((newstate, path + [newstate])) visited.append(newstate)
def analyze(design): ANALYSIS.clear() queve = deque() sim = Simulator(design) init = sim.get_initial_state() #init_pos, init_ability = init #ANALYSIS = {next_state:current_state} ANALYSIS[init] = None queve.append(init) while queve: current_state = queve.popleft() #current_position,current_abilities = current_state moves = sim.get_moves() for next_move in moves: next_state = sim.get_next_state(current_state,next_move) if next_state == None: continue #next_position, next_abilities = next_state if next_state not in ANALYSIS: ANALYSIS[next_state] = current_state queve.append(next_state)
def analyze(design): ANALYSIS.clear() queve = deque() sim = Simulator(design) init = sim.get_initial_state() #init_pos, init_ability = init #ANALYSIS = {next_state:current_state} ANALYSIS[init] = None queve.append(init) while queve: current_state = queve.popleft() #current_position,current_abilities = current_state moves = sim.get_moves() for next_move in moves: next_state = sim.get_next_state(current_state, next_move) if next_state == None: continue #next_position, next_abilities = next_state if next_state not in ANALYSIS: ANALYSIS[next_state] = current_state queve.append(next_state)
def analyze(design): """ Gets a design object from p6_tool Do breath first search to find path for every position, Save a dictionary of parent states. """ global ANALYSIS sim = Simulator(design) init = sim.get_initial_state() ANALYSIS = {init: (None, None)} queue = [] queue.append(init) while queue: curr_state = queue.pop(0) # list.pop(0) gets the first item, pop() gets the last one adj_list = get_adj(sim, curr_state) for adj in adj_list: if adj not in ANALYSIS: queue.append(adj) ANALYSIS[adj] = curr_state return
def analyze(design): sim = Simulator(design) # TODO: fill in this function, populating the ANALYSIS dict queue = [sim.get_initial_state()] ANALYSIS.clear() PREV.clear() ANALYSIS[sim.get_initial_state()[0]] = [sim.get_initial_state()] PREV[sim.get_initial_state()] = None while (queue): current_state = queue.pop() for move in sim.get_moves(): next_state = sim.get_next_state(current_state, move) if next_state: if next_state not in PREV: position, _ = next_state # or None if character dies if position not in ANALYSIS: ANALYSIS[position] = [] ANALYSIS[position].append(next_state) PREV[next_state] = current_state queue.append(next_state)
def analyze(design): sim = Simulator(design) # TODO: fill in this function, populating the ANALYSIS dict queue = [sim.get_initial_state()] ANALYSIS.clear() PREV.clear() ANALYSIS[sim.get_initial_state()[0]] = [sim.get_initial_state()] PREV[sim.get_initial_state()] = None while queue: current_state = queue.pop() for move in sim.get_moves(): next_state = sim.get_next_state(current_state, move) if next_state: if next_state not in PREV: position, _ = next_state # or None if character dies if position not in ANALYSIS: ANALYSIS[position] = [] ANALYSIS[position].append(next_state) PREV[next_state] = current_state queue.append(next_state)
def analyze(design): visited = {} prev = {} queve = deque() # visited = [] sim = Simulator(design) init = sim.get_initial_state() init_pos, init_ability = init ANALYSIS[init_pos] = None # print ANALYSIS[init_pos] visited[init_pos] = init_ability queve.append(init) # # while queve is not empty while queve: current_state = queve.popleft() current_position,current_abilities = current_state # # if node has reached its destination cell it will quit while loop # # calls the get_steps function stored in adj moves = sim.get_moves() for next_move in moves: if next_move == "NOTHING": continue next_state = sim.get_next_state(current_state,next_move) if next_state == None: # ANALYSIS[next_position] = None continue # if in the current state the character dies ignore this state next_position, next_abilities = next_state if next_position == current_position: continue # stores new ability into Analysis for later use when it comes to drawing out the lines if next_position not in visited: visited[next_position] = current_abilities ANALYSIS[next_position] = current_position queve.append(next_state) elif next_position in visited: for ability in next_abilities: if ability not in visited[next_position]: visited[next_position] = current_abilities # ANALYSIS[next_position] = current_position queve.append(next_state) # elif next_position in visited: # ANALYSIS[next_position] = current_position # queve.append(next_state) # elif next_position in visited: # ANALYSIS[next_position] = current_position # queve.append(next_state) # ANALYSIS.reverse() print current_abilities
def analyze_specific(thing, goal): startTime = time.time() tempDesign = copy.deepcopy(thing) sim = Simulator(tempDesign) init = sim.get_initial_state() path = [] ANALYSIS = {init: None} startingNode = (init[0], init[1], 0) # (x,y), ability, turn) ANALYSIS[startingNode] = None q = [(0, init[0], init[1], 0, tempDesign, sim)] # distance from start, (point), abilities, turn number, newDesign) while q: node = heappop(q) #print (node[1], node[2], int(node[3] / turns_to_move)) #Node structure: (distance from start, (point), abilities, turn number, newDesign) # not an exhaustive search, so stop as soon as you find the goal platform, which moves tempGoal = (goal[0], goal[1] - turns_number(node[3]) ) if node[1] == tempGoal: currNode = (node[1], node[2], turns_number(node[3])) # build path while ANALYSIS[currNode] != None: # print currNode path.append(currNode[0]) currNode = ANALYSIS[currNode] break # simulate first, THEN check the moves so that we don't get invalid moves tempSim = Simulator(node[4]) moves = tempSim.get_moves() #see all possible states for each move states = [] for move in moves: if tempSim.get_next_state((node[1],node[2]), move) != None: pos, abil = tempSim.get_next_state((node[1], node[2]), move) tempResult = p6_tool.take_turn(copy.deepcopy(node[4]), node[3], pos) #check if it's moved diagonally downward and add distance accordingly cost = get_state_cost(pos, node[1]) #print " -- ", tempResult[1]['specials'].keys()[0] newState = (node[0] + cost, tempResult[1]['specials'].items()[0][0], abil, tempResult[0], tempResult[1]) states.append(newState) #Use ANALYSIS like a prev dict, only each key now has states so the solution will be unique for each set of abilities for state in states: # ignore nodes that are stalling; they already have no valid path if is_falling(init[0][1], state[1][1]) and not is_miss_the_goal(state[1][1], tempGoal[1]): curr = (state[1], state[2], turns_number(state[3])) tent_score = state[0] + distance_heuristic(state[1], tempGoal) if (curr not in ANALYSIS or tent_score < node[0]) : #need to make nodes contain data on design movements or else they can't move to the same place twice ANALYSIS[curr] = (node[1], node[2], turns_number(node[3])) state = (tent_score, state[1], state[2], state[3], state[4]) heappush(q, state) print "Time taken: ", time.time() - startTime return path