def getNextYLocation(self, increment, location): newY = location.y + increment if newY >= self.gameMap.height: return Location(location.x, 0) elif newY < 0: return Location(location.x, self.gameMap.height - 1) return Location(location.x, newY)
def getNextXLocation(self, increment, location): newX = location.x + increment if newX >= self.gameMap.width: return Location(0, location.y) elif newX < 0: return Location(self.gameMap.width - 1, location.y) return Location(newX, location.y)
def myShit(self, gameMap): i = 0 myShit = [] for y in range(gameMap.height): for x in range(gameMap.width): location = Location(x, y) if gameMap.getSite(location).owner == self.myID: myShit.append(Location(x, y)) i += 1 return i
def setUp(self): self.loc1 = Location(0, 0) self.loc2 = Location(0, 0) self.loc3 = Location(1, 1) # arbitrary self.direction = 1 self.action1 = (Action(self.loc1, self.loc3, self.direction)) self.action2 = (Action(self.loc2, self.loc3, self.direction)) self.action3 = (Action(self.loc3, self.loc1, self.direction)) self.action4 = (Action(self.loc3, self.loc2, self.direction))
def getTerritory(gameMap, userId): territory = 0 for y in range(gameMap.height): for x in range(gameMap.width): curSite = gameMap.getSite(Location(x, y)) if curSite.owner == userId: territory += 1 return territory
def getProduction(gameMap, userId): production = 0 for y in range(gameMap.height): for x in range(gameMap.width): curSite = gameMap.getSite(Location(x, y)) if curSite.owner == userId: production += curSite.production return production
def getStrength(gameMap, userId): strength = 0 for y in range(gameMap.height): for x in range(gameMap.width): curSite = gameMap.getSite(Location(x, y)) if curSite.owner == userId: strength += curSite.strength return strength
def calculate_distance_sum(gameMap, myID, newCenter): distance_sum = 0 for y in range(gameMap.height): for x in range(gameMap.width): location = Location(x, y) if gameMap.getSite(location).owner == myID: # logFile.write(str(x) + "," + str(y) + ", += " + str(location.x + gameMap.width ) + # ", " + str(location.y + gameMap.height) + "\n") distance_sum += gameMap.getDistance(location, newCenter) return distance_sum
def myCenter(gameMap, myID): # find center by adding up sum of strenght of x and y separately sumX = 0 sumY = 0 count = 0 minDistanceSum = 999999999999999999999999999999999999 for yC in range(gameMap.height): for xC in range(gameMap.width): newCenter = Location(xC, yC) if gameMap.getSite(newCenter).owner != myID: continue distanceSum = calculate_distance_sum(gameMap, myID, newCenter) if distanceSum < minDistanceSum: bestCenter = newCenter return bestCenter
def move_on(self): if self.path == None or self.target == None: raise Exception('Unable to move. This site has no path or target') old_x = self.x old_y = self.y next_dir = path.pop(0) if next_dir == NORTH: self.y = self.y - 1 elif next_dir == WEST: self.x = self.x - 1 elif next_dir == SOUTH: self.y = self.y + 1 elif next_dir == EAST: self.x = self.x + 1 return Move(Location(old_x, old_y), next_dir)
allQs = [] # Only pick one random position for easier q-learning while True: position = random.choice(np.transpose(np.nonzero(stack[0]))) area_inputs = stack_to_input(stack, position) possible_moves, Qinputs, Qs = predict_for_pos(area_inputs, model) # Epsilon greedy strategy if random.random() < 0.1: index = np.random.choice(range(len(possible_moves))) else: index = np.argmax(Qs) Q = Qs[index] move = possible_moves[index] allQs.append(Q) Qinput = Qinputs[index] sendFrame([Move(Location(position[1], position[0]), move)]) turn += 1 old_frame = frame old_Qs = Qs frame = getFrame() stack = frame_to_stack(frame, myID) area_inputs = stack_to_input(stack, position) possible_moves, Qinputs, Qs = predict_for_pos(area_inputs, model) reward = get_reward(old_frame, frame, myID, position) def handler(sig, frame): logging.info("Before exit") logging.info("Average chosen Q value: %.2f", np.array(allQs).mean()) sys.exit(0)
from helper import (getStrength, getTerritory, getProduction) sendInit('zyzo') FILE_WRITER = open('log-ANALYTICS.log', 'w') FILE_WRITER.write('') FILE_WRITER.close() MY_ID, GAME_MAP = getInit() while True: MOVES = [] GAME_MAP = getFrame() FILE_WRITER = open('log-analytics.log', 'a') ANALYTICS = {'s': 0, 'a': 0, 'm': 0} for y in range(GAME_MAP.height): for x in range(GAME_MAP.width): curLoc = Location(x, y) if GAME_MAP.getSite(curLoc).owner == MY_ID: movedPiece = False for d in CARDINALS: if (GAME_MAP.getSite(curLoc, d).owner != MY_ID and GAME_MAP.getSite(curLoc, d).strength < GAME_MAP.getSite(curLoc).strength): MOVES.append(Move(curLoc, d)) ANALYTICS['a'] += 1 movedPiece = True break if not movedPiece: MOVES.append(Move(curLoc, STILL)) ANALYTICS['s'] += 1 movedPiece = True FILE_WRITER.write(
position[1], axis=2, mode='wrap').flatten() def frame_to_stack(frame): """Converts a game frame into a stack of features""" game_map = np.array([[(x.owner, x.production, x.strength) for x in row] for row in frame.contents]) return np.array([ (game_map[:, :, 0] == myID), # 0 : owner is me ((game_map[:, :, 0] != 0) & (game_map[:, :, 0] != myID)), # 1 : owner is enemy game_map[:, :, 1] / 20, # 2 : production game_map[:, :, 2] / 255, # 3 : strength ]).astype(np.float32) networking.send_init('brianvanleeuwen') # Main game loop while True: current_stack = frame_to_stack(networking.get_frame()) positions = np.transpose(np.nonzero(current_stack[0])) output = model.predict( np.array([stack_to_input(current_stack, p) for p in positions])) networking.send_frame([ Move(Location(positions[i][1], positions[i][0]), output[i].argmax()) for i in range(len(positions)) ])
""" Return shortest path between to points of a grid in terms of strength cost """ """ A* implementation here """ # Clear log file FILE_WRITER = open('log-ANALYTICS.log', 'w') FILE_WRITER.write('') FILE_WRITER.close() # Initialize data MY_ID, GAME_MAP = getInit() mySites = {} mySitesMap = [[-1 for y in range(GAME_MAP.height)] for x in range(GAME_MAP.width)] mySiteId = 0 for x in range(GAME_MAP.width): for y in range(GAME_MAP.height): if GAME_MAP.getSite(Location(x, y)).owner == MY_ID: path, target = shortest_path(GAME_MAP, Location(x, y)) mySites[mySiteId] = MySite(mySiteId, x, y) mySites[mySiteId].set_obj(path, target) mySitesMap[x][y] = mySiteId mySiteId += 1 # Let the fun begins sendInit('zyzo') while True: MOVES = [] GAME_MAP = getFrame() FILE_WRITER = open('log-ANALYTICS.log', 'a') ANALYTICS = {'s': 0, 'a': 0, 'm': 0} for x in range(GAME_MAP.width):
for position in positions]) outputs = np.split(model.predict(inputs), len(positions)) # outputs /= sum(outputs) return possible_moves, np.split(inputs, len(positions)), [o.ravel() for o in outputs] while True: np.set_printoptions(precision=3) frame = getFrame() stack = frame_to_stack(frame, myID) positions = np.transpose(np.nonzero(stack[0])) # position = random.choice(positions) moves = [] possible_moves, allQinputs, allQs = predict_for_game(stack, positions, model) for position, Qinputs, Qs in zip(positions, allQinputs, allQs): # Sample a move following Pi(s) def softmax(x): """ Turn Q values into probabilities """ e_x = np.exp(x - np.max(x)) return e_x / e_x.sum(axis=0) # only difference def harden(x, e=2): exp = x**e return exp/exp.sum() Ps = harden(softmax(Qs.ravel())) # index = np.random.choice(range(len(possible_moves)), p=Ps) index = np.argmax(Ps) logging.info("%d Qs: %s Ps: %s", index, Qs, Ps) moves.append((position[1], position[0], possible_moves[index])) sendFrame([Move(Location(px,py), move) for (px,py,move) in moves])