コード例 #1
0
 def __init__(self,
              print_gametick=False,
              print_on_receive=False,
              create_server=True):
     self.network = Network(create_server)
     self.print_gametick = print_gametick
     self.print_on_receive = print_on_receive
     self.game_states = []
     self.player_states = {}
コード例 #2
0
 def __init__(self,
              model_path: Optional[str] = None,
              min_wait_time: float = 0.0,
              max_wait_time: float = 0.0,
              verbose: bool = False,
              gpus: Union[int, List[int]] = 0,
              max_gpu_mem_fraction: float = 1.0):
     super().__init__("LazyBug-Policy")
     self.max_wait_time = max_wait_time
     self.min_wait_time = min_wait_time
     self.__network = Network(model_path=model_path,
                              gpus=gpus,
                              max_gpu_mem_fraction=max_gpu_mem_fraction)
     # Perform first prediction to force keras to load the model
     self.__network.evaluate_positions(Positions.create_empty(1))
     self.verbose = verbose
コード例 #3
0
class EvaluatorNetwork(Evaluator):
    def __init__(self,
                 model_path: str,
                 max_gpu_mem_fraction: Optional[float] = None):
        self.__network = Network(model_path=model_path,
                                 max_gpu_mem_fraction=max_gpu_mem_fraction)

    def evaluate_positions(
            self, positions: Positions) -> Tuple[np.ndarray, np.ndarray]:
        return self.__network.evaluate_positions(positions)
コード例 #4
0
class PlayerPolicy(Player):
    def __init__(self,
                 model_path: Optional[str] = None,
                 min_wait_time: float = 0.0,
                 max_wait_time: float = 0.0,
                 verbose: bool = False,
                 gpus: Union[int, List[int]] = 0,
                 max_gpu_mem_fraction: float = 1.0):
        super().__init__("LazyBug-Policy")
        self.max_wait_time = max_wait_time
        self.min_wait_time = min_wait_time
        self.__network = Network(model_path=model_path,
                                 gpus=gpus,
                                 max_gpu_mem_fraction=max_gpu_mem_fraction)
        # Perform first prediction to force keras to load the model
        self.__network.evaluate_positions(Positions.create_empty(1))
        self.verbose = verbose

    def move(self):
        start = time()
        min_move_time = self.min_wait_time + random.random() * (
            self.max_wait_time - self.min_wait_time)
        probs, value = self.__network.evaluate_game_view(
            game_view=self.game_view)
        if self.verbose:
            print("")
            probs_sorted = sorted(probs.items(),
                                  key=lambda i: i[1],
                                  reverse=True)
            for m, p in probs_sorted:
                print("{}: {}".format(m, p))
            print("Value: {}".format(value))
        move = max(probs.items(), key=lambda x: x[1], default=[None, None])[0]
        time_diff = min_move_time - (time() - start)
        if time_diff >= 0:
            sleep(time_diff)
        return move
コード例 #5
0
    def __init__(self, print_gametick=False, print_on_receive=False):

        #self.level_creeps_spawn_timers = [1,2,3,4,5]
        #self.spawnCreeps = []

        # for i in range(0, 5):
        #    self.spawnCreeps.append(Creep.factory("Default",i))

        # game_state = MainMenu()

        self.network = Network()
        self.print_gametick = print_gametick
        self.print_on_receive = print_on_receive

        self.level_creeps_spawn_timers = [5]
        self.spawnCreeps = [Creep.factory("Default", 0)]
        initialSpawn = 0

        # 3 sets of spawns
        # Separated by initialSpawn time.
        for _ in range(0, 5):
            self.level_creeps_spawn_timers.append(initialSpawn)
            initialSpawn += 0.3

        initialSpawn += 3

        for _ in range(0, 10):
            self.level_creeps_spawn_timers.append(initialSpawn)
            initialSpawn += 0.3

        initialSpawn += 3

        for _ in range(0, 15):
            self.level_creeps_spawn_timers.append(initialSpawn)
            initialSpawn += 0.3

        for i in range(0, 30):
            self.spawnCreeps.append(Creep.factory("Default", i))

        levels = Levels(self.level_creeps_spawn_timers, self.spawnCreeps)
        self.game_state = GameplayState(
            levels, WORLD_WIDTH, WORLD_HEIGHT, 100, 100)
        self.game_state.build_tower(Tower((8, 8), 1, 1, 1, 0))
コード例 #6
0
ファイル: tower_server.py プロジェクト: erikwijmans/Towers
class GameRunner:
    """This class runs the ENTIRE game backend, from the websocket server
    to the game engine loop to the websocket client that connects the
    game engine loop to the server."""

    def __init__(self, print_gametick=False, print_on_receive=False):
        # game_state = GameplayState(levels.level_one, WORLD_WIDTH, WORLD_HEIGHT)
        # game_state = MainMenu()
        self.network = Network()
        self.print_gametick = print_gametick
        self.print_on_receive = print_on_receive

    def run(self):
        """Run this game, along with all its network requirements, like the websocket server.
        Runs in a blocking infinite loop."""
        clock = Clock(TICK_LEN)
        clock.tick()  # tick once to initialize counter

        try:
            while True:
                dt = clock.tick()
                self.game_loop(dt)
        except KeyboardInterrupt:
            pass
        finally:
            # do any cleanup you want to do here
            pass

    def game_loop(self, dt):
        """This is the main game loop for the entire server.
        Every tick of this loop will progress the game state by
        some amount.  argument dt is the delta time since the last tick."""

        # global game_state  # pull game_state into scope from global
        if self.print_gametick:
            print("it's been " + str(dt * 1000) + " ms since last frame")
        # game_state = game_state.update(dt, client_info)
        # network.send(game_state)  # psuedocode, network doesn't exist yet

        message = self.network.receive()
        if message is not False and self.print_on_receive:
            print("gameloop got message: {}".format(message))
コード例 #7
0
 def __init__(self,
              model_path: str,
              max_gpu_mem_fraction: Optional[float] = None):
     self.__network = Network(model_path=model_path,
                              max_gpu_mem_fraction=max_gpu_mem_fraction)
コード例 #8
0
class GameRunner:
    """This class runs the ENTIRE game backend, from the websocket server
    to the game engine loop to the websocket client that connects the
    game engine loop to the server."""

    def __init__(self, print_gametick=False, print_on_receive=False):

        #self.level_creeps_spawn_timers = [1,2,3,4,5]
        #self.spawnCreeps = []

        # for i in range(0, 5):
        #    self.spawnCreeps.append(Creep.factory("Default",i))

        # game_state = MainMenu()

        self.network = Network()
        self.print_gametick = print_gametick
        self.print_on_receive = print_on_receive

        self.level_creeps_spawn_timers = [5]
        self.spawnCreeps = [Creep.factory("Default", 0)]
        initialSpawn = 0

        # 3 sets of spawns
        # Separated by initialSpawn time.
        for _ in range(0, 5):
            self.level_creeps_spawn_timers.append(initialSpawn)
            initialSpawn += 0.3

        initialSpawn += 3

        for _ in range(0, 10):
            self.level_creeps_spawn_timers.append(initialSpawn)
            initialSpawn += 0.3

        initialSpawn += 3

        for _ in range(0, 15):
            self.level_creeps_spawn_timers.append(initialSpawn)
            initialSpawn += 0.3

        for i in range(0, 30):
            self.spawnCreeps.append(Creep.factory("Default", i))

        levels = Levels(self.level_creeps_spawn_timers, self.spawnCreeps)
        self.game_state = GameplayState(
            levels, WORLD_WIDTH, WORLD_HEIGHT, 100, 100)
        self.game_state.build_tower(Tower((8, 8), 1, 1, 1, 0))

    def run(self):
        """Run this game, along with all its network requirements, like the websocket server.
        Runs in a blocking infinite loop."""
        clock = Clock(TICK_LEN)
        clock.tick()  # tick once to initialize counter

        try:
            while True:
                dt = clock.tick()
                self.game_loop(dt)

        except KeyboardInterrupt:
            pass
        finally:
            # do any cleanup you want to do here
            pass

    def process_message(self, msg):
        if msg['type'] == MSG.tower_request.name:
            # Make a new tower TODO, don't hardcode stuff
            tower = Tower(
                (msg['msg']['x'], msg['msg']['y']),
                1000,
                1,
                3,
                len(self.game_state.all_towers),
                msg['msg']['towerID']
            )
            success = self.game_state.build_tower(tower)
            if success:
                towerUpdate = {
                    'type': 'tower_update',
                    'towerAccepted': 'true',
                    'tower': tower
                }
                self.network.send_message(towerUpdate)
            else:
                # TODO, tell client request failed and why
                pass

    def game_loop(self, dt):
        # Receive and process messages from clients
        message = self.network.receive()
        if message:
            self.process_message(message)

        # Update game 1 tick and pass to clients
        tupleReturned = self.game_state.update(dt, [])

        # tupleReturned is: playerState, creepLoc, creepProgress, attacksMade
        self.network.send_message(tupleReturned)
コード例 #9
0
handler.setFormatter(formatter)
LOGGER = logging.getLogger(__name__)

is_atty = os.isatty(sys.stdout.fileno())
if not is_atty:
    root = logging.getLogger()
    root.setLevel(logging.DEBUG)
    root.handlers = []
    root.addHandler(handler)
else:
    LOGGER.addHandler(handler)

network = Network(nr_of_boards=2,
                  num_residual_blocks=args.residual_blocks,
                  optimizer=SGD(lr=0.001),
                  gpus=args.gpus,
                  verbose=is_atty,
                  value_weight=0.1,
                  policy_weight=0.9)
network.compile_model()
print(network.model.summary())

name_prefix = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
rel_model_dir = name_prefix
model_dir = os.path.join(args.model_path, rel_model_dir)
base_name = os.path.join(name_prefix + "_{:02d}_{:03d}.h5py")
os.makedirs(model_dir, exist_ok=False)
for e in range(10):
    LOGGER.info("Main epoch {}".format(e))
    if args.database_dir is not None:
        data_generator = read_database(PositionDatabase(args.database_dir))
コード例 #10
0
    root.addHandler(handler)
else:
    LOGGER.addHandler(handler)

name_prefix, model_dir, epoch, config = args.setup(args)

base_name = os.path.join(name_prefix + "_{:04d}.h5py")

tensorboard_dir = os.path.join(model_dir, "tensorboard")
os.makedirs(tensorboard_dir, exist_ok=True)
create_link(name_prefix, os.path.join(args.output_path, "latest"))

if epoch >= 1:
    model_name = base_name.format(epoch - 1)
    model_path = os.path.join(model_dir, model_name)
    network = Network(model_path=model_path)
else:
    network = Network(nr_of_boards=2,
                      num_residual_blocks=config["residual_blocks"],
                      optimizer=SGD(lr=0.001),
                      gpus=args.gpus,
                      verbose=is_atty,
                      value_weight=0.01,
                      policy_weight=0.99,
                      use_batch_normalization=True)
    network.compile_model()
print(network.model.summary())

training_database = PositionDatabase(args.training_database_dir)
validation_database = PositionDatabase(args.validation_database_dir)
extra_databases = {
コード例 #11
0
ファイル: tower_server.py プロジェクト: erikwijmans/Towers
 def __init__(self, print_gametick=False, print_on_receive=False):
     # game_state = GameplayState(levels.level_one, WORLD_WIDTH, WORLD_HEIGHT)
     # game_state = MainMenu()
     self.network = Network()
     self.print_gametick = print_gametick
     self.print_on_receive = print_on_receive
コード例 #12
0
class GameRunner:
    """This class runs the ENTIRE game backend, from the websocket server
    to the game engine loop to the websocket client that connects the
    game engine loop to the server."""
    def __init__(self,
                 print_gametick=False,
                 print_on_receive=False,
                 create_server=True):
        self.network = Network(create_server)
        self.print_gametick = print_gametick
        self.print_on_receive = print_on_receive
        self.game_states = []
        self.player_states = {}

    def add_player(self, player_id):
        """Add a player to the game by giving them their own state."""

        levels = Levels.createLevel(5, 0.5, 10, 5, 3, "Default")
        state = GameplayState(levels, WORLD_WIDTH, WORLD_HEIGHT, 100, 10000,
                              player_id)

        self.game_states.append(state)
        self.player_states[player_id] = state
        print('added player, and state for player {}'.format(player_id))

    def remove_player(self, player_id):
        if player_id in self.player_states:
            state = self.player_states[player_id]
            del self.player_states[player_id]
            self.game_states.remove(state)
            print('removing player and state for player {}'.format(player_id))

    def run(self):
        # wait until a request comes in to start the game, then start the game
        while True:
            message = self.network.receive()
            if message:
                print('game received message: {}'.format(message))
            if message and message['type'] == MSG.game_start_request.name:
                self.start_game()
                break
            elif message and message['type'] == MSG.instance_request.name:
                self.spawn_new_game()
            elif message and message['type'] == MSG.game_add_player.name:
                player_id = message['player_id']
                self.add_player(player_id)
            elif message and message['type'] == MSG.game_remove_player.name:
                player_id = message['player_id']
                self.remove_player(player_id)

    def spawn_new_game(self):
        print('spawning new game instance')
        new_game = GameRunner(create_server=False)
        t = threading.Thread(target=new_game.run)
        t.daemon = True
        t.start()

    def start_game(self):
        print('starting game.')
        clock = Clock(TICK_LEN)
        clock.tick()  # tick once to initialize counter

        try:
            while True:
                dt = clock.tick()
                self.game_loop(dt)

        except KeyboardInterrupt:
            pass
        finally:
            # do any cleanup you want to do here
            pass

    def process_message(self, msg):
        if msg['type'] == MSG.tower_request.name:
            player_id = msg['player_id']
            state = self.player_states[player_id]
            x, y = msg['msg']['x'], msg['msg']['y']
            if msg['msg']['towerID'] == 'delete_tower':
                # request to delete a tower that's already present
                state.delete_tower((x, y))
                self.network.send_message({
                    'type': 'tower_update',
                    'towerDeleted': True,
                    'x': x,
                    'y': y,
                    'player_id': player_id
                })
            else:
                tower = state.build_tower((x, y), msg['msg']['towerID'])
                towerUpdate = None
                if tower:
                    towerUpdate = {
                        'type': 'tower_update',
                        'towerAccepted': 'true',
                        'tower': tower,
                        'player_id': player_id
                    }
                else:
                    towerUpdate = {
                        'type': 'tower_update',
                        'towerAccepted': 'false',
                        'reason': 'TODO',
                        'player_id': player_id
                    }
                self.network.send_message(towerUpdate)
        elif msg['type'] == MSG.instance_request.name:
            self.spawn_new_game()
        elif msg['type'] == MSG.game_add_player.name:
            player_id = msg['player_id']
            self.add_player(player_id)
        elif msg['type'] == MSG.game_remove_player.name:
            player_id = msg['player_id']
            self.remove_player(player_id)
        elif msg['type'] == MSG.creep_request.name:
            player_id = msg['player_id']
            creep_type = msg['msg']['creepID']
            for player in self.player_states:
                if player != player_id:
                    state = self.player_states[player]
                    state.spawn_creep(creep_type)

    def game_loop(self, dt):
        # Receive and process messages from clients
        message = self.network.receive()
        if message:
            self.process_message(message)

        # Update game 1 tick and pass to clients
        for player in self.player_states:
            state = self.player_states[player]
            data = state.update(dt, [])
            if state.is_dead():
                self.player_states[player] = LoseState()

            if data is not None:  # will be None if the player is dead
                self.network.send_message(data)
コード例 #13
0
ファイル: test_model.py プロジェクト: TimSchneider42/lazybug
import numpy as np

from chess.variant import BughouseBoards
from engine.data import read_batches, transform_move_id_to_chessmove, PositionDatabase
from engine.data.position_sequence import PositionSequence
from engine.network import Network

MODEL_PATH = "../models/current"

network = Network(model_path=MODEL_PATH)
network.compile_model()

# TODO: this is inaccurate
dummy_boards = BughouseBoards()
database = PositionDatabase(
    "/mnt/linux_data_slow/bughouse_data/databases/validation_full")
position_sequence = PositionSequence(database, batch_size=256 * 4)
out_list = []
for i, (x_data, y_data) in enumerate(position_sequence):
    print("")
    print("Batch {}".format(i))
    num = len(x_data)
    output = network.model.test_on_batch(x_data, y_data)
    out_list.append(output)
    for n, o in zip(network.model.metrics_names, output):
        print("{}: {}".format(n, o))

    probs, value = network.model.predict([x[0:1] for x in x_data])
    probs = probs[0]
    value = value[0][0]
    print("Example: ")
コード例 #14
0
ファイル: tower_game.py プロジェクト: zlailari/Towers
 def __init__(self, print_gametick=False, print_on_receive=False, create_server=True):
     self.network = Network(create_server)
     self.print_gametick = print_gametick
     self.print_on_receive = print_on_receive
     self.game_states = []
     self.player_states = {}
コード例 #15
0
ファイル: tower_game.py プロジェクト: zlailari/Towers
class GameRunner:
    """This class runs the ENTIRE game backend, from the websocket server
    to the game engine loop to the websocket client that connects the
    game engine loop to the server."""

    def __init__(self, print_gametick=False, print_on_receive=False, create_server=True):
        self.network = Network(create_server)
        self.print_gametick = print_gametick
        self.print_on_receive = print_on_receive
        self.game_states = []
        self.player_states = {}

    def add_player(self, player_id):
        """Add a player to the game by giving them their own state."""

        levels = Levels.createLevel(5, 0.5, 10, 5, 3, "Default")
        state = GameplayState(levels, WORLD_WIDTH,
                              WORLD_HEIGHT, 100, 10000, player_id)

        self.game_states.append(state)
        self.player_states[player_id] = state

    def remove_player(self, player_id):
        if player_id in self.player_states:
            state = self.player_states[player_id]
            del self.player_states[player_id]
            self.game_states.remove(state)

            print('removing player and state for player {}'.format(player_id))

    def run(self):
        # wait until a request comes in to start the game, then start the game
        while True:
            message = self.network.receive()
            if message and message['type'] == MSG.game_start_request.name:
                self.start_game()
                break
            elif message and message['type'] == MSG.instance_request.name:
                self.spawn_new_game()
            elif message and message['type'] == MSG.game_add_player.name:
                player_id = message['player_id']
                self.add_player(player_id)
            elif message and message['type'] == MSG.game_remove_player.name:
                player_id = message['player_id']
                self.remove_player(player_id)

    def spawn_new_game(self):
        new_game = GameRunner(create_server=False)
        t = threading.Thread(target=new_game.run)
        t.daemon = True
        t.start()

    def start_game(self):
        clock = Clock(TICK_LEN)
        clock.tick()  # tick once to initialize counter

        try:
            while True:
                dt = clock.tick()
                self.game_loop(dt)

        except KeyboardInterrupt:
            pass
        finally:
            # do any cleanup you want to do here
            pass

    def process_message(self, msg):
        if msg['type'] == MSG.tower_request.name:
            player_id = msg['player_id']
            state = self.player_states[player_id]
            x, y = msg['msg']['x'], msg['msg']['y']
            if msg['msg']['towerID'] == 'delete_tower':
                # request to delete a tower that's already present
                state.delete_tower((x, y))
                self.network.send_message(
                    {
                        'type': 'tower_update',
                        'towerDeleted': True,
                        'x': x,
                        'y': y,
                        'player_id': player_id
                    }
                )
            elif msg['msg']['towerID'] == 'upgrade_tower':
                tower_upgraded = state.upgrade_tower((x,y))
                self.network.send_message(
                    {
                        'type': 'tower_update',
                        'towerUpgraded': tower_upgraded,
                        'x': x,
                        'y': y,
                        'player_id': player_id
                    }
                )

            else:
                tower = state.build_tower((x, y), msg['msg']['towerID'])
                towerUpdate = None
                if tower:
                    towerUpdate = {
                        'type': 'tower_update',
                        'towerAccepted': True,
                        'tower': tower,
                        'player_id': player_id
                    }
                else:
                    towerUpdate = {
                        'type': 'tower_update',
                        'towerAccepted': False,
                        'reason': 'TODO',
                        'player_id': player_id
                    }
                self.network.send_message(towerUpdate)
        elif msg['type'] == MSG.instance_request.name:
            self.spawn_new_game()
        elif msg['type'] == MSG.game_add_player.name:
            player_id = msg['player_id']
            self.add_player(player_id)
        # elif msg['type'] == MSG.game_remove_player.name:
        #     player_id = msg['player_id']
        #     self.remove_player(player_id)
        elif msg['type'] == MSG.creep_request.name:
            player_id = msg['player_id']
            creep_type = msg['msg']['creepID']

            if((self.player_states[player_id].gold)>= creep_costs[creep_type]):
                for player in self.player_states:
                    if player != player_id:
                        state = self.player_states[player]
                        state.spawn_creep(creep_type)
                        self.player_states[player_id].spawned_creeps +=1
                        self.player_states[player_id].gold=self.player_states[player_id].gold-creep_costs[creep_type]


    def game_loop(self, dt):
        # Receive and process messages from clients
        message = self.network.receive()
        if message:
            self.process_message(message)

        # Update game 1 tick and pass to clients
        dead_players = []
        for player in self.player_states:
            state = self.player_states[player]
            data = state.update(dt, [])
            if state.is_dead():
                dead_players.append(player)

            self.network.send_message(data)

        for player in dead_players:
            self.player_states.pop(player, None)  # remove dead players