def run_agent(registration_name, port, token): """ Starts a server that hosts an agent. """ agent = ffai.make_bot(registration_name) server = PythonSocketServer(agent, port, token) server.run()
def worker(remote, parent_remote, env, worker_id): parent_remote.close() steps = 0 tds = 0 tds_opp = 0 next_opp = ffai.make_bot('random') while True: command, data = remote.recv() if command == 'step': steps += 1 action, dif = data[0], data[1] obs, reward, done, info = env.step(action) tds_scored = info['touchdowns'] - tds tds = info['touchdowns'] tds_opp_scored = info['opp_touchdowns'] - tds_opp tds_opp = info['opp_touchdowns'] reward_shaped = reward_function(env, info, shaped=True) ball_carrier = env.game.get_ball_carrier() # PPCG if dif < 1.0: if ball_carrier and ball_carrier.team == env.game.state.home_team: extra_endzone_squares = int((1.0 - dif) * 25.0) distance_to_endzone = ball_carrier.position.x - 1 if distance_to_endzone <= extra_endzone_squares: #reward_shaped += rewards_own[OutcomeType.TOUCHDOWN] env.game.state.stack.push( Touchdown(env.game, ball_carrier)) if done or steps >= reset_steps: # If we get stuck or something - reset the environment if steps >= reset_steps: print( "Max. number of steps exceeded! Consider increasing the number." ) done = True env.opp_actor = next_opp obs = env.reset() steps = 0 tds = 0 tds_opp = 0 remote.send((obs, reward, reward_shaped, tds_scored, tds_opp_scored, done, info)) elif command == 'reset': dif = data steps = 0 tds = 0 tds_opp = 0 env.opp_actor = next_opp obs = env.reset() # set_difficulty(env, dif) remote.send(obs) elif command == 'render': env.render() elif command == 'swap': next_opp = data elif command == 'close': break
def run_game(nbr_of_games, enable_forward_model): config = ffai.load_config("gym-11") config.fast_mode = True ruleset = ffai.load_rule_set(config.ruleset) home = ffai.load_team_by_filename("human", ruleset) away = ffai.load_team_by_filename("human", ruleset) away_agent = Agent("Human 1", human=True, agent_id=1) home_agent = Agent("Human 2", human=True, agent_id=2) seed = 0 random_agent = ffai.make_bot('random') random_agent.rnd = np.random.RandomState(seed) for _ in range(nbr_of_games): game = Game(seed, home, away, home_agent, away_agent, config) game.init() if enable_forward_model: game.enable_forward_model() while not game.state.game_over: game.step(random_agent.act(game))
# Uncomment to this to evaluate the bot against the random baseline # Load configurations, rules, arena and teams config = ffai.load_config("bot-bowl-iii") config.competition_mode = False config.pathfinding_enabled = True # config = get_config("gym-7.json") # config = get_config("gym-5.json") # config = get_config("gym-3.json") ruleset = ffai.load_rule_set(config.ruleset, all_rules=False) # We don't need all the rules arena = ffai.load_arena(config.arena) home = ffai.load_team_by_filename("human", ruleset) away = ffai.load_team_by_filename("human", ruleset) # Play 10 games for i in range(10): home_agent = ffai.make_bot('scripted') home_agent.name = "Scripted Bot" away_agent = ffai.make_bot('random') away_agent.name = "Random Bot" config.debug_mode = False game = ffai.Game(i, home, away, home_agent, away_agent, config, arena=arena, ruleset=ruleset) game.config.fast_mode = True print("Starting game", (i+1)) start = time.time() game.init() end = time.time() print(end - start)
import ffai import mybot import minigrod import argparse parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('--agent', help='FFAI registered name of agent', required=True) parser.add_argument('--token', help='Secret communication token', required=True) parser.add_argument('--port', help='Port to listen on', type=int, default=5000) args = parser.parse_args() agent = ffai.make_bot(args.agent) server = ffai.ai.PythonSocketServer(agent, args.port, args.token) server.run()
def __init__(self): super().__init__("Scripted bot", ffai.make_bot('scripted'))
if __name__ == "__main__": # Load configurations, rules, arena and teams config = ffai.load_config("bot-bowl-ii") ruleset = ffai.load_rule_set(config.ruleset) arena = ffai.load_arena(config.arena) home = ffai.load_team_by_filename("human", ruleset) away = ffai.load_team_by_filename("human", ruleset) config.competition_mode = False config.debug_mode = False # Play 10 games game_times = [] for i in range(10): away_agent = ffai.make_bot("my-random-bot") home_agent = ffai.make_bot("my-random-bot") game = ffai.Game(i, home, away, home_agent, away_agent, config, arena=arena, ruleset=ruleset) game.config.fast_mode = True print("Starting game", (i + 1)) game.init() print("Game is over")
# Register the bot to the framework ffai.register_bot('search-bot', SearchBot) # Load configurations, rules, arena and teams config = ffai.load_config("bot-bowl-iii") ruleset = ffai.load_rule_set(config.ruleset) arena = ffai.load_arena(config.arena) home = ffai.load_team_by_filename("human", ruleset) away = ffai.load_team_by_filename("human", ruleset) config.competition_mode = False config.debug_mode = False config.fast_mode = True config.pathfinding_enabled = False # Play a game bot_a = ffai.make_bot("search-bot") bot_b = ffai.make_bot("search-bot") game = ffai.Game(1, home, away, bot_a, bot_b, config, arena=arena, ruleset=ruleset) print("Starting game") game.init() print("Game is over")
def worker(remote, parent_remote, env, worker_id): parent_remote.close() steps = 0 tds = 0 tds_opp = 0 next_opp = ffai.make_bot('almost-random') prev_super_shaped = None prev_pos_reward = [None, None] while True: if debug_thread: print(f"worker {worker_id} - stuck on receive") command, data = remote.recv() if debug_thread: print(f"worker {worker_id} - after receive: '{command}'") if command == 'step': steps += 1 action, dif, lecture = data[0], data[1], data[2] # s = "in worker, action is " + str(action) #print(s) obs, reward, done, info = env.step(action) tds_scored = info['touchdowns'] - tds tds = info['touchdowns'] tds_opp_scored = info['opp_touchdowns'] - tds_opp tds_opp = info['opp_touchdowns'] reward_shaped, prev_super_shaped, prev_pos_reward = reward_function( env, info, shaped=True, obs=obs, prev_super_shaped=prev_super_shaped, prev_pos_reward=prev_pos_reward) ball_carrier = env.game.get_ball_carrier() # PPCG if dif < 1.0 and env.lecture is None: if ball_carrier and ball_carrier.team == env.game.state.home_team: extra_endzone_squares = int((1.0 - dif) * 25.0) distance_to_endzone = ball_carrier.position.x - 1 if distance_to_endzone <= extra_endzone_squares: #reward_shaped += rewards_own[OutcomeType.TOUCHDOWN] env.game.state.stack.push( Touchdown(env.game, ball_carrier)) if done or steps >= reset_steps: # If we get stuck or something - reset the environment if steps >= reset_steps: print( "Max. number of steps exceeded! Consider increasing the number." ) done = True env.opp_actor = next_opp obs = env.reset(lecture) steps = 0 prev_pos_reward = [None, None] prev_super_shaped = None tds = 0 tds_opp = 0 if debug_thread: print(f"worker {worker_id} - stuck on send") remote.send((obs, reward, reward_shaped, tds_scored, tds_opp_scored, done, info)) if debug_thread: print(f"worker {worker_id} - stuck somewhere else") elif command == 'reset': dif, lecture = data[0], data[1] steps = 0 tds = 0 tds_opp = 0 env.opp_actor = next_opp prev_super_shaped = None prev_pos_reward = [None, None] obs = env.reset(lecture) # set_difficulty(env, dif) remote.send(obs) elif command == 'render': #env.render() pass elif command == 'swap': next_opp = data elif command == 'close': break
from scripted_bot_example import * import ffai as ffai import time as time config = ffai.load_config("bot-bowl-ii") config.competition_mode = False ruleset = ffai.load_rule_set(config.ruleset, all_rules=False) # We don't need all the rules arena = ffai.load_arena(config.arena) home = ffai.load_team_by_filename("human", ruleset) away = ffai.load_team_by_filename("human", ruleset) # Play 10 games for i in range(10): home_agent = ffai.make_bot('grodbot') home_agent.set_verbose(True) home_agent.set_debug(False) home_agent.name = "Grod" away_agent = ffai.make_bot('scripted') away_agent.name = "RandomBot" config.debug_mode = False game = ffai.Game(i, home, away, home_agent, away_agent, config, arena=arena, ruleset=ruleset) game.config.fast_mode = True