SVMBot.py
1 2 # Adapted from: https://halite.io/learn-programming-challenge/tutorials/ml-svm 3 #!/usr/bin/env python3 4 5 import os 6 from collections import defaultdict 7 8 import hlt 9 import model 10 from hlt import constants 11 12 13 class SVMBot: 14 def __init__(self, weights): 15 # Get the initial game state 16 game = hlt.Game() 17 game.ready("SVM-" + os.path.basename(weights)) 18 19 # During init phase: initialize the model and compile it 20 my_model = model.HaliteModel(weights=weights) 21 22 self.my_model = my_model 23 self.game = game 24 25 def run(self): 26 # Some minimal state to say when to go home 27 go_home = defaultdict(lambda: False) 28 while True: 29 self.game.update_frame() 30 me = self.game.me # Here we extract our player metadata from the game state 31 game_map = self.game.game_map # And here we extract the map metadata 32 other_players = [p for pid, p in self.game.players.items() if pid != self.game.my_id] 33 34 command_queue = [] 35 36 for ship in me.get_ships(): # For each of our ships 37 # Did not machine learn going back to base. Manually tell ships to return home 38 if ship.position == me.shipyard.position: 39 go_home[ship.id] = False 40 elif go_home[ship.id] or ship.halite_amount == constants.MAX_HALITE: 41 go_home[ship.id] = True 42 movement = game_map.get_safe_move(game_map[ship.position], game_map[me.shipyard.position]) 43 if movement is not None: 44 game_map[ship.position.directional_offset(movement)].mark_unsafe(ship) 45 command_queue.append(ship.move(movement)) 46 else: 47 ship.stay_still() 48 continue 49 50 # Use machine learning to get a move 51 ml_move = self.my_model.predict_move(ship, game_map, me, other_players, self.game.turn_number) 52 if ml_move is not None: 53 movement = game_map.get_safe_move(game_map[ship.position], 54 game_map[ship.position.directional_offset(ml_move)]) 55 if movement is not None: 56 game_map[ship.position.directional_offset(movement)].mark_unsafe(ship) 57 command_queue.append(ship.move(movement)) 58 continue 59 ship.stay_still() 60 61 # Spawn some more ships 62 if me.halite_amount >= constants.SHIP_COST and \ 63 not game_map[me.shipyard].is_occupied and len(me.get_ships()) == 0: 64 command_queue.append(self.game.me.shipyard.spawn()) 65 66 if self.game.turn_number == 299: 67 with open("SVM.txt","a") as file: 68 file.write(str(me.halite_amount - 4000) + "\n") 69 70 71 self.game.end_turn(command_queue) # Send our moves back to the game environment