def main(): parser = argparse.ArgumentParser() parser.add_argument("--config", help="A yaml config file. Must always be specified.", required=True) parser.add_argument("--loop", help="If set, then runs in an endless loop.", required=False, action="store_true") args = parser.parse_args() do_loop = args.loop is True # Init logging and adjust log levels for some classes. init_logging() logger = get_named_logger("{}.main".format( os.path.splitext(os.path.basename(__file__))[0])) get_class_logger(GameController).setLevel( logging.INFO) # Don't log specifics of a single game # Load config and check experiment dir. logger.info(f'Loading config from "{args.config}"...') config = load_config(args.config) experiment_dir = config["experiment_dir"] while not os.path.exists(experiment_dir): logger.warn( f'The experiment dir specified in the config does yet not exist: "{experiment_dir}" - waiting...' ) sleep(10) agent_checkpoint_paths = { i: os.path.join(experiment_dir, name) for i, name in config["training"]["agent_checkpoint_names"].items() } while True: # Wait until a ".for_eval" checkpoint exists (for any of possibly multiple agents). Then rename it to ".in_eval.[uniqueid]". # In this way, multiple eval scripts can run in parallel. # When the evaluation is done, we will rename it to ".{score}". for i_agent, cp_path in agent_checkpoint_paths.items(): # If multiple agents are specified in the config, evaluate all of them. checkpoint_path_in = f"{os.path.splitext(cp_path)[0]}.for_eval.h5" checkpoint_path_tmp = f"{os.path.splitext(cp_path)[0]}.in_eval.pid{os.getpid()}.h5" if os.path.exists(checkpoint_path_in): # Load the latest checkpoint and evaluate it try: os.rename(checkpoint_path_in, checkpoint_path_tmp) logger.info('Found a new checkpoint, evaluating...') # Create agent agent_type = config["training"]["player_agents"][i_agent] if agent_type == "DQNAgent": alphasheep_agent = DQNAgent(0, config=config, training=False) else: raise ValueError( f"Unknown agent type specified: {agent_type}") alphasheep_agent.load_weights(checkpoint_path_tmp) # Eval agent current_perf = eval_agent(alphasheep_agent) # Now we know the performance. Find best-performing previous checkpoint that exists on disk logger.info( "Comparing performance to previous checkpoints...") splitext = os.path.splitext(cp_path) checkpoints = glob.glob("{}-*{}".format( splitext[0], splitext[1])) best_perf = 0. for cp in checkpoints: perf_str = re.findall( r"{}-(.*){}".format(os.path.basename(splitext[0]), splitext[1]), cp) if len(perf_str) > 0: p = float(perf_str[0]) if p > best_perf: best_perf = p if best_perf > 0: logger.info( "Previously best checkpoint has performance {}". format(best_perf)) else: logger.info("Did not find any previous results.") if current_perf > best_perf: best_perf = current_perf logger.info("Found new best-performing checkpoint!") cp_best = "{}-{}{}".format(splitext[0], str(best_perf), splitext[1]) os.rename(checkpoint_path_tmp, cp_best) except OSError: # Probably a concurrent rename by another worker; continue and try again. logger.exception("Could not rename checkpoint!") logger.info("Waiting...") sleep(10) if not do_loop: # Run only once. return
from common.send_email import send_emails from flask.views import View, MethodView from flask_login import current_user, login_required from common.Dingtalk import send_ding from common.mysqldatabasecur import * from common.dubbo_feng import DubboInterface from config import Config_daoru_xianzhi, redis_host, redis_port, redis_save_result_db, save_duration, \ system_request_toke, test_fail_try_num from common.excet_excel import create_interface_case from common.hebinglist import listmax from common.pyredis import ConRedisOper import logging from utils.log_util import init_logging from config import DEBUG init_logging(__name__, level=DEBUG) case = Blueprint('case', __name__) def save_result(key, value): m = ConRedisOper(host=redis_host, port=redis_port, db=redis_save_result_db) m.sethase(key, value, save_duration) def get_result(key): m = ConRedisOper(host=redis_host, port=redis_port, db=redis_save_result_db) results = m.getset(key) return results def get_projects_and_models():
def main(): parser = argparse.ArgumentParser() parser.add_argument( "--p0-agent", type=str, choices=['static', 'rule', 'random', 'alphasheep', 'user'], required=True) parser.add_argument( "--alphasheep-checkpoint", help="Checkpoint for AlphaSheep, if --p0-agent=alphasheep.", required=False) parser.add_argument( "--agent-config", help="YAML file, containing agent specifications for AlphaSheep.", required=False) args = parser.parse_args() agent_choice = args.p0_agent as_checkpoint_path = args.alphasheep_checkpoint as_config_path = args.agent_config if agent_choice == "alphasheep" and (not as_checkpoint_path or not as_config_path): raise ValueError( "Need to specify --alphasheep-checkpoint and --agent-config if --p0_agent=alphasheep." ) # Init logging and adjust log levels for some classes. init_logging() logger = get_named_logger("{}.main".format( os.path.splitext(os.path.basename(__file__))[0])) get_class_logger(GameController).setLevel( logging.DEBUG) # Log every single card. get_class_logger(Gui).setLevel(logging.DEBUG) # Log mouse clicks. get_class_logger(RuleBasedAgent).setLevel( logging.DEBUG) # Log decisions by the rule-based players. # Create the agent for Player 0. if agent_choice == "alphasheep": # Load config. We ignore the "training" and "experiment" sections, but we need "agent_config". logger.info(f'Loading config from "{as_config_path}"...') config = load_config(as_config_path) get_class_logger(DQNAgent).setLevel(logging.DEBUG) # Log Q-values. alphasheep_agent = DQNAgent(0, config=config, training=False) alphasheep_agent.load_weights(as_checkpoint_path) p0 = Player("0-AlphaSheep", agent=alphasheep_agent) elif agent_choice == "user": p0 = Player("0-User", agent=GUIAgent(0)) elif agent_choice == "rule": p0 = Player("0-Hans", agent=RuleBasedAgent(0)) elif agent_choice == "static": p0 = Player("0-Static", agent=StaticPolicyAgent(0)) else: p0 = Player("0-RandomGuy", agent=RandomCardAgent(0)) # Players 1-3 are RuleBasedAgents. players = [ p0, Player("1-Zenzi", agent=RuleBasedAgent(1)), Player("2-Franz", agent=RuleBasedAgent(2)), Player("3-Andal", agent=RuleBasedAgent(3)) ] # Rig the game so Player 0 has the cards to play a Herz-Solo. # Also, force them to play it. game_mode = GameMode(GameContract.suit_solo, trump_suit=Suit.herz, declaring_player_id=0) controller = GameController(players, dealing_behavior=DealWinnableHand(game_mode), forced_game_mode=game_mode) # The GUI initializes PyGame and registers on events provided by the controller. Everything single-threaded. # # The controller runs the game as usual. Whenever the GUI receives an event, it can block execution, so the controller must wait # for the GUI to return control. Until then, it can draw stuff and wait for user input (mouse clicks, card choices, ...). logger.info("Starting GUI.") with Gui(controller.game_state) as gui: # Run an endless loop of single games. logger.info("Starting game loop...") try: while True: controller.run_game() except UserQuitGameException: # Closing the window or pressing [Esc] logger.info("User quit game.") logger.info("Shutdown.")