def safeMoves(self, loc=None): if loc == None: loc = self._pawns['X'].getLocation() if loc == -1: raise BaseAIProtocolError( "Cannot determine safe X moves when X location is unknown") dets = ['Red', 'Yellow', 'Green', 'Blue', 'Black'] detLocs = [self._pawns[d].getLocation() for d in dets] # log.msg("DETETIVE LOCATIONS: "+detLocs) # f = open("abc.txt", "a") # f.write("Now the file has more content!") # f.close() allMoves = path.possible_destinations(loc, 1, eliminate=[sets.Set(detLocs)]) distances = [ path.distance(self._pawns[d].getLocation(), tickets=self._pawns[d]._tickets) for d in dets ] def findSafe(threshold): safe = allMoves.copy() for dest in allMoves: for dist in distances: if dist[dest] < threshold: safe.remove(dest) break return safe return [ findSafe(threshold) for threshold in range(1, map.MAX_DISTANCE + 1) ]
def safeMoves(self, loc=None): if loc == None: loc = self._pawns['X'].getLocation() if loc == -1: raise BaseAIProtocolError( "Cannot determine safe X moves when X location is unknown") dets = ['Red', 'Yellow', 'Green', 'Blue', 'Black'] detLocs = [self._pawns[d].getLocation() for d in dets] allMoves = path.possible_destinations(loc, 1, eliminate=[sets.Set(detLocs)]) distances = [ path.distance(self._pawns[d].getLocation(), tickets=self._pawns[d]._tickets) for d in dets ] def findSafe(threshold): safe = allMoves.copy() for dest in allMoves: for dist in distances: if dist[dest] < threshold: safe.remove(dest) break return safe return [ findSafe(threshold) for threshold in range(1, map.MAX_DISTANCE + 1) ]
def doTurn(self, pawnName): # Taxis are best, because they hide position to some degree. # Underground is relatively expensive because it tells detectives where # you might be. black is very expensive because supplies are limited. def cost(ticket_amounts, ticket): if ticket == 'taxi': return 1 elif ticket == 'bus': return 1.6 elif ticket == 'underground': return 3 elif ticket == 'black': return 5 all_paths = path.cheapest_path(self._pawns['X'].getLocation(), tickets=self._pawns['X']._tickets, cost=cost) # look at the available moves in order from most safe to least safe safe = self.safeMoves() safe.reverse() bestMove = None bestTransport = None bestCost = 1000000 for moves in safe: moves_list = list(moves) random.shuffle(moves_list) for move in moves_list: p = all_paths[move] dest, transport = p[0] if cost(self._pawns['X']._tickets, transport) < bestCost: bestMove = move bestTransport = transport bestCost = cost(self._pawns['X']._tickets, transport) if bestMove != None: log.msg("found a safe move at this threshold") break else: log.msg("could not find a safe move at this threshold") # if no safe moves can be found, then move randomly if bestMove == None: log.msg("detectives have me trapped--moving randomly") moves = path.possible_destinations(self._pawns['X'].getLocation, 1, tickets=self._pawns['X']._tickets)[0] selected_move = random.choice(list(moves)) bestMove, bestTransport = all_paths[selected_move] log.msg("making a move") self.makeMove(['x', str(bestMove), str(bestTransport)])
def doTurn(self, pawnName): # Taxis are best, because they hide position to some degree. # Underground is relatively expensive because it tells detectives where # you might be. black is very expensive because supplies are limited. def cost(ticket_amounts, ticket): if ticket == 'taxi': return 1 elif ticket == 'bus': return 1.6 elif ticket == 'underground': return 3 elif ticket == 'black': return 5 all_paths = path.cheapest_path(self._pawns['X'].getLocation(), tickets=self._pawns['X']._tickets, cost=cost) # look at the available moves in order from most safe to least safe safe = self.safeMoves() safe.reverse() bestMove = None bestTransport = None bestCost = 1000000 for moves in safe: moves_list = list(moves) random.shuffle(moves_list) for move in moves_list: p = all_paths[move] dest, transport = p[0] if cost(self._pawns['X']._tickets, transport) < bestCost: bestMove = move bestTransport = transport bestCost = cost(self._pawns['X']._tickets, transport) if bestMove != None: log.msg("found a safe move at this threshold") break else: log.msg("could not find a safe move at this threshold") # if no safe moves can be found, then move randomly if bestMove == None: log.msg("detectives have me trapped--moving randomly") moves = path.possible_destinations(self._pawns['X'].getLocation, 1, tickets=self._pawns['X']._tickets)[0] selected_move = random.choice(list(moves)) bestMove, bestTransport = all_paths[selected_move] log.msg("\n\n\n") log.msg("Mr. X location: ", self._pawns['X'].getLocation()) log.msg("Mr. X Tickets: "+ str(self._pawns['X']._tickets)+"\n") dets = ['Red', 'Yellow', 'Green', 'Blue', 'Black'] detLocs = [self._pawns[d].getLocation() for d in dets] log.msg("DETETIVE LOCATIONS: "+ str(detLocs)) det_tickets = [self._pawns[d]._tickets for d in dets] log.msg("DETECTIVE TICKETS: "+ str(det_tickets)+"\n") log.msg("Turn number: "+ str(self._turnNum)+"\n") # self.turnNo += 1 # log.msg("Turn No: "+ str(self.turnNo)+"\n") self.generate_feature_space() # log.msg("Mr. x options are: ", self.safeMoves()) move_file = "/Users/shreyasi/Desktop/LondonLaw/move.txt" f = open(move_file, "w") f.write(str(bestMove)+" , "+bestTransport+"\n\n") f.close() log.msg("Best move is from: ", self._pawns['X'].getLocation(), "to : ", bestMove, "using: ", bestTransport) self.makeMove(['x', str(bestMove), str(bestTransport)]) log.msg("making a move " +"\n\n\n")
def doTurn(self, pawnName): pawn = self._pawns[pawnName] def cost(ticket_amounts, ticket): if ticket == 'taxi': if ticket_amounts['taxi'] > 2: return 1 else: return 2 elif ticket == 'bus': if ticket_amounts['bus'] > 2: return 1.2 else: return 2 elif ticket == 'underground': if ticket_amounts['underground'] > 1: return 3 else: return 5 elif ticket == 'black': return 1000000 all_paths = path.cheapest_path(pawn.getLocation(), tickets=pawn._tickets, cost=cost) detective_locs = [self._pawns[p].getLocation() for p in self._pawns if p != 'X'] #log.msg("Detectives Bot") self.generate_feature_space(pawnName) if self._turnNum < 3: # Before X has surfaced, just try to get to high-mobility locations. # Compute locations we can get to by the end of turn 2, with the constraint # that we don't spend any undergrounds t = pawn._tickets.copy() t['underground'] = 0 e = [sets.Set()] * (3 - self._turnNum) e[0].union_update(detective_locs) locs = list(path.possible_destinations(pawn.getLocation(), 3 - self._turnNum, tickets=t, eliminate=e)) random.shuffle(locs) # evaluate the mobility of each location bestLoc = None bestMobility = 0 for loc in locs: # how well-connected is this location? mobility = len(path.possible_destinations(loc, 1, pawn._tickets)) if mobility > bestMobility: bestLoc = loc bestMobility = mobility if bestLoc != None and all_paths[bestLoc] != None: self.makeMove([pawnName.lower(), str(all_paths[bestLoc][0][0]), all_paths[bestLoc][0][1]]) log.msg("Best Move for ", pawnName.lower(), "detective is to", str(all_paths[bestLoc][0][0]), "using", all_paths[bestLoc][0][1]) else: # shouldn't happen raise DetectiveSimpleAIProtocolError("failed to find a move for turnNum < 3") else: # after X has surfaced, just try to go somewhere where he *might* be (use game # history to eliminate detective positions as possible locations) lastXLoc = self._history[self._lastXSurfacingTurn]['X'][0] xTransports = [] e = [] for i in range(self._lastXSurfacingTurn + 1, self._turnNum + 1): xTransports.append(self._history[i]['X'][1]) detPos = [] for p in self._pawns: if p != 'X' and p in self._history[i-1].keys(): detPos.append(self._history[i-1][p][0]) e.append(sets.Set(detPos)) #log.msg('x transports = ' + str(xTransports)) xLocs = path.possible_locations(lastXLoc, xTransports, eliminate=e) #log.msg('x possible locations = ' + str(xLocs)) # determine which of these locations is closest with a path that is # attainable minDist = 1000000 bestLoc = None for loc in xLocs: if (all_paths[loc] != None and len(all_paths[loc]) < minDist and len(all_paths[loc]) > 0 and all_paths[loc][0][0] not in detective_locs): bestLoc = loc if bestLoc != None and all_paths[bestLoc] != None: self.makeMove([pawnName.lower(), str(all_paths[bestLoc][0][0]), all_paths[bestLoc][0][1]]) log.msg("Best Move for ", pawnName.lower(), "detective is to", str(all_paths[bestLoc][0][0]), "using", all_paths[bestLoc][0][1]) else: # if we can't find anything good, make a random move log.msg("moving randomly") availableMoves = list(path.possible_destinations(pawn.getLocation(), 1, tickets=pawn._tickets, eliminate = [sets.Set(detective_locs)])) move = random.choice(availableMoves) self.makeMove([pawnName.lower(), str(all_paths[move][0][0]), all_paths[move][0][1]]) log.msg("Best Move for ", pawnName.lower(), "detective is to", str(all_paths[move][0][0]), "using", all_paths[move][0][1])