import os import traceback from .orm import Baseline, Battle, Model from evaluate import evaluate from datetime import datetime positions = ['landlord', 'landlord_up', 'landlord_front', 'landlord_down'] def battle_logic(baseline : Baseline, battle : Battle): eval_data_first = 'eval_data_200.pkl' eval_data_second = 'eval_data_800.pkl' challenger_baseline = { 'landlord_path': str(baseline.landlord_path), 'landlord_up_path': str(baseline.landlord_up_path), 'landlord_front_path': str(baseline.landlord_front_path), 'landlord_down_path': str(baseline.landlord_down_path), } challenger_baseline[battle.challenger_position + "_path"] = str(battle.challenger_path) print(str(battle.challenger_path)) landlord_wp, farmer_wp, landlord_adp, farmer_adp = \ evaluate(challenger_baseline['landlord_path'], challenger_baseline['landlord_up_path'], challenger_baseline['landlord_front_path'], challenger_baseline['landlord_down_path'], eval_data_first, 3, False, 'New') def _second_eval(landlord_wp, farmer_wp, landlord_adp, farmer_adp): landlord_wp_2, farmer_wp_2, landlord_adp_2, farmer_adp_2 = \ evaluate(challenger_baseline['landlord_path'], challenger_baseline['landlord_up_path'], challenger_baseline['landlord_front_path'], challenger_baseline['landlord_down_path'], eval_data_second, 3, False, 'New') return (landlord_wp + landlord_wp_2 * 4.0) / 5, \ (farmer_wp + farmer_wp_2 * 4.0) / 5, \ (landlord_adp + landlord_adp_2 * 4.0) / 5, \ (farmer_adp + farmer_adp_2 * 4.0) / 5 challenge_success = False if battle.challenger_position == 'landlord': if baseline.landlord_wp == 0 or landlord_wp / float(baseline.landlord_wp) > 1.15: landlord_wp, farmer_wp, landlord_adp, farmer_adp = \ _second_eval(landlord_wp, farmer_wp, landlord_adp, farmer_adp) if baseline.landlord_wp == 0 or landlord_wp / float(baseline.landlord_wp) > 1.15: challenge_success = True else: if baseline.farmer_wp == 0 or farmer_wp / float(baseline.farmer_wp) > 1.05: landlord_wp, farmer_wp, landlord_adp, farmer_adp = \ _second_eval(landlord_wp, farmer_wp, landlord_adp, farmer_adp) if baseline.farmer_wp == 0 or farmer_wp / float(baseline.farmer_wp) > 1.05: challenge_success = True if challenge_success: challenger_baseline['rank'] = baseline.rank + 1 challenger_baseline['landlord_wp'] = landlord_wp challenger_baseline['landlord_adp'] = landlord_adp challenger_baseline['farmer_wp'] = farmer_wp challenger_baseline['farmer_adp'] = farmer_adp challenger_baseline['create_time'] = datetime.now() Baseline.create(**challenger_baseline) battle.challenger_wp = landlord_wp if battle.challenger_position == 'landlord' else farmer_wp battle.challenger_adp = landlord_adp if battle.challenger_position == 'landlord' else farmer_adp battle.status = 1 if challenge_success else 2 battle.save() if not challenge_success: onnx_path = str(battle.challenger_path) + '.onnx' if os.path.exists(onnx_path): os.remove(onnx_path) os.remove(str(battle.challenger_path)) def tick(): try: battles = Battle.select().where(Battle.status == 0).order_by(Battle.id.desc()).limit(10) for battle in battles: battle.status = -1 battle.save() baselines = Baseline.select().order_by(Baseline.rank.desc()).limit(1) if len(baselines) == 0: baseline = {} for position in positions: models = Model.select().where(Model.position == position).order_by(Model.create_time.desc()).limit(1) if(len(models) > 0): baseline['%s_path' % position] = models[0].path if len(baseline.keys()) == 4: baseline['rank'] = 0 baseline['landlord_wp'] = 0 baseline['farmer_wp'] = 0 baseline['landlord_adp'] = 0 baseline['farmer_adp'] = 0 baseline['create_time'] = datetime.now() Baseline.create(**baseline) baselines = Baseline.select().order_by(Baseline.rank.desc()).limit(1) battle_logic(baselines[0], battle) else: battle.status = 3 battle.save() else: battle_logic(baselines[0], battle) except: traceback.print_exc()