예제 #1
0
def initialize_regiments():
    regiment1 = Regiment()
    regiment2 = Regiment()
    deploy1 = UniformIntDeploy(regiment1, nbattalion1)
    deploy2 = UniformIntDeploy(regiment2, nbattalion2)
    deploy1.deploy(attack1, health1, attackspread1, healthspread1)
    deploy2.deploy(attack2, health2, attackspread2, healthspread2)
    battle = Battlefield(regiment1, regiment2)
    return regiment1, regiment2, battle
예제 #2
0
def launch_game():
    StoryGenerator.create_story()
    button_height = 50
    battlefield = Battlefield(Battlefield.build("Battlefield/" + chosen_field + ".txt"))
    shop_state = ShopState(units, button_height, difficulty, gold, battlefield.get_enemy_spawn_count())
    shopping_screen = pygame.display.set_mode((shop_state.window_width, shop_state.window_height))
    shop_result = Shop.run(shopping_screen, shop_state)
    for unit in shop_result[0]:
        if unit not in units:
            units.append(unit)
    game_state = GameState(battlefield, button_height, units)
    battle_screen = pygame.display.set_mode((game_state.get_window_width(), game_state.get_window_height()))
    survivors = Battle.run(battle_screen, game_state)

    if is_game_over(survivors):
        return False, shop_result[1]

    return True, shop_result[1]
예제 #3
0
def launch_game():
    StoryGenerator.create_story()
    button_height = 50
    battlefield = Battlefield(Battlefield.build("Battlefield/" + chosen_field + ".txt"))
    shop_state = ShopState(units, button_height, difficulty, gold, battlefield.get_enemy_spawn_count())
    shopping_screen = pygame.display.set_mode((shop_state.window_width, shop_state.window_height))
    shop_result = Shop.run(shopping_screen, shop_state)
    for unit in shop_result[0]:
        if unit not in units:
            units.append(unit)
    game_state = GameState(battlefield, button_height, units)
    battle_screen = pygame.display.set_mode((game_state.get_window_width(), game_state.get_window_height()))
    survivors = Battle.run(battle_screen, game_state)

    if is_game_over(survivors):
        return False, shop_result[1]

    return True, shop_result[1]
예제 #4
0
def initialize_randomcommanders():
    regiment1 = Regiment()
    regiment2 = Regiment()
    deploy1 = UniformIntDeploy(regiment1, nbattalion1)
    deploy2 = UniformIntDeploy(regiment2, nbattalion2)
    deploy1.deploy(attack1, health1, attackspread1, healthspread1)
    deploy2.deploy(attack2, health2, attackspread2, healthspread2)
    battle = Battlefield(regiment1, regiment2)

    commander1 = RandomCommander(regiment1, regiment2, maxbattalion1)
    commander2 = RandomCommander(regiment2, regiment1, maxbattalion2)
    commander1.set_order_action_map()
    commander2.set_order_action_map()
    return commander1, commander2, battle
예제 #5
0
def battle_commence(health1_):
    health2 = health1_ * Nteam1 / Nteam2  # make sure total health of both team are the same
    favor = 0  # 1 if team 1 always win, -1 if team 2 always win
    for _ in range(Nbattle):
        Team1 = Regiment()
        Team2 = Regiment()

        # define rules of engagement
        Team1_ROE = fully_connected_ROE()
        Team2_ROE = fully_connected_ROE()
        # deploy teams with specified parameters
        Team1_Deploy = UniformDeploy(Team1, Nteam1)
        Team2_Deploy = UniformDeploy(Team2, Nteam2)
        Team1_Deploy.deploy(attack1, health1_, attack_spread, health_spread)
        Team2_Deploy.deploy(attack2, health2, attack_spread, health_spread)

        # define and start a battle
        Battle = Battlefield(Team1, Team2)
        Battle.commence(Team1_ROE, Team2_ROE)

        # cumulate favors
        favor += Battle.get_reward()[0]

    return favor / Nbattle
예제 #6
0
    from Deploy import UniformDeploy
    from Battlefield import Battlefield

    nbattalion1 = 5
    nbattalion2 = 5
    attack1, health1, attack1_spread, health1_spread = 5, 10, 0, 1
    attack2, health2, attack2_spread, health2_spread = 5, 10, 0, 1

    Regiment1 = Regiment()
    Regiment2 = Regiment()
    RegDeploy1 = UniformDeploy(Regiment1, nbattalion1)
    RegDeploy2 = UniformDeploy(Regiment2, nbattalion2)
    RegDeploy1.deploy(attack1, health1, attack1_spread, health1_spread)
    RegDeploy2.deploy(attack2, health2, attack2_spread, health2_spread)

    Battle = Battlefield(Regiment1, Regiment2)

    Commander1 = QCommander(Regiment1, Regiment2, 1)
    Commander2 = RandomCommander(Regiment2, Regiment1, 1)

    Commander1.set_order_action_map()
    Commander2.set_order_action_map()
    Commander1.set_model()

    Regiment1.battalions[0].set_health(-1)
    Regiment1.battalions[1].set_health(-1)
    Regiment1.battalions[2].set_health(-1)
    Regiment1.battalions[3].set_health(-1)
    Regiment1.count_KIA()
    Battle.update_state()
    print(Battle.state)
예제 #7
0
from Battlefield import Battlefield

if __name__ == '__main__':

    Battlefield().run_game()
예제 #8
0
파일: utils.py 프로젝트: jackpck/battle_sim
    def mini_batch_train(self, max_episodes, epsilon_decay_rate,
                         fraction_start_epsilon_greedy, epsilon_init,
                         batch_size):
        '''
        commander1: QCommander
        commander2: RandomCommander
        '''
        episode_rewards = []
        actions = []
        losses = []
        loss = -1  # loss of episode with less than batch_size number of samples in replay_buffer
        epsilon = epsilon_init

        for episode in range(max_episodes):
            # initialize battle
            self.deploy_regiments()
            self.commander1.set_thisRegiment(self.regiment1)
            self.commander1.set_enemyRegiment(self.regiment2)
            self.commander2.set_thisRegiment(self.regiment2)
            self.commander2.set_enemyRegiment(self.regiment1)
            Battle = Battlefield(self.regiment1, self.regiment2)

            state = Battle.state.copy()
            episode_reward = 0
            done = False

            # choose random action for the first x fractions of episodes.
            # Afterwards, choose epilson greedy with epsilon converges to zero.
            if episode >= fraction_start_epsilon_greedy * (max_episodes):
                epsilon = (1. - epsilon_decay_rate) * epsilon
            while not done:
                order1, action1 = self.commander1.order(state, eps=epsilon)
                order2, action2 = self.commander2.order(state)
                actions.append(action1)

                self.commander1.deliver_order(order1)
                self.commander2.deliver_order(order2)
                Battle.commence_round()

                Battle.update_state()
                next_state = Battle.state.copy()

                reward, done = Battle.get_reward()
                self.commander1.replay_buffer.push(state, action1, reward,
                                                   next_state, done)
                episode_reward += reward

                # start update only when there are at least batch_size number of samples in replay buffer
                if len(self.commander1.replay_buffer) > batch_size:
                    loss = self.commander1.update(batch_size)

                state = next_state

            episode_rewards.append(np.sign(episode_reward))
            losses.append(loss)
            print(
                "Episode " + str(episode) + ": " +
                str(np.sign(episode_reward)), ' loss: ', loss, ' epsilon: ',
                epsilon)

        return episode_rewards, losses, actions