diff --git a/douzero/server/battle.py b/douzero/server/battle.py index a583324..8826f19 100644 --- a/douzero/server/battle.py +++ b/douzero/server/battle.py @@ -1,4 +1,5 @@ import os +import traceback from .orm import Baseline, Battle, Model from evaluate import evaluate @@ -17,6 +18,7 @@ def battle_logic(baseline : Baseline, battle : Battle): } 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'], @@ -43,16 +45,16 @@ def battle_logic(baseline : Baseline, battle : Battle): challenge_success = False if battle.challenger_position == 'landlord': - if landlord_wp / baseline.landlord_wp > 1.2: + if baseline.landlord_wp == 0 or landlord_wp / float(baseline.landlord_wp) > 1.2: landlord_wp, farmer_wp, landlord_adp, farmer_adp = \ _second_eval(landlord_wp, farmer_wp, landlord_adp, farmer_adp) - if landlord_wp / baseline.landlord_wp > 1.2: + if baseline.landlord_wp == 0 or landlord_wp / float(baseline.landlord_wp) > 1.2: challenge_success = True else: - if farmer_wp / baseline.farmer_wp > 1.2: + if baseline.farmer_wp == 0 or farmer_wp / float(baseline.farmer_wp) > 1.2: landlord_wp, farmer_wp, landlord_adp, farmer_adp = \ _second_eval(landlord_wp, farmer_wp, landlord_adp, farmer_adp) - if farmer_wp / baseline.farmer_wp > 1.2: + if baseline.farmer_wp == 0 or farmer_wp / float(baseline.farmer_wp) > 1.2: challenge_success = True if challenge_success: challenger_baseline['rank'] = baseline.rank + 1 @@ -73,27 +75,32 @@ def battle_logic(baseline : Baseline, battle : Battle): battle.save() def tick(): - battles = Battle.select().where(Battle.status == 0).order_by(Battle.id.desc()) - for battle in battles: - 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) + try: + battles = Battle.select().where(Battle.status == 0).order_by(Battle.id.desc()).limit(2) + 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.status = 3 - battle.save() - else: - battle_logic(baselines[0], battle) + battle_logic(baselines[0], battle) + except: + traceback.print_exc() diff --git a/douzero/server/orm.py b/douzero/server/orm.py index 8b2874d..29f8bde 100644 --- a/douzero/server/orm.py +++ b/douzero/server/orm.py @@ -1,6 +1,11 @@ # -*- coding:utf8 -*- from peewee import * -db = SqliteDatabase('model.db') +db = SqliteDatabase('model.db', pragmas={ + 'journal_mode': 'wal', + 'cache_size': -1 * 64000, # 64MB + 'foreign_keys': 1, + 'ignore_check_constraints': 0, + 'synchronous': 0}) class BaseModel(Model): class Meta: diff --git a/evaluate_server.py b/evaluate_server.py index 9ed1355..c7da9b7 100644 --- a/evaluate_server.py +++ b/evaluate_server.py @@ -1,5 +1,3 @@ -from douzero.evaluation.simulation import evaluate - from douzero.server.orm import Model, Battle, Baseline from douzero.server.battle import tick from flask import Flask, jsonify, request @@ -38,6 +36,37 @@ def upload(): threadpool.submit(tick) return jsonify({'status': 0, 'message': 'success', 'result': ''}) +@app.route('/battle_tick', methods=['POST']) +def battle_tick(): + tick() + return jsonify({'status': 0, 'message': 'success', 'result': ''}) + +@app.route('/metrics', methods=['GET']) +def metrics(): + type = request.args.get('type') + baselines = Baseline.select().order_by(Baseline.rank.desc()).limit(3) + end_time = datetime.now() + metrics = {} + for i in range(len(baselines)): + baseline = baselines[i] + baseline_metric = {} + models = Model.select(Model.frame, Model.path).where( + Model.type == type, + Model.create_time >= baseline.create_time, + Model.create_time >= end_time + ).order_by(Model.create_time.asc()) + end_time = baseline.create_time + for model in models: + battle = Battle.get_or_none(Battle.challenger_path == model.path, Battle.status > 0, Battle.status != 3) + if battle is not None: + baseline_metric[model.frame] = { + 'position': battle.challenger_position, + 'wp': '%.4f' % float(battle.challenger_wp), + 'adp': '%.4f' % float(battle.challenger_adp) + } + metrics[baseline.rank] = baseline_metric + return jsonify({'status': 0, 'message': 'success', 'result': metrics}) + if __name__ == '__main__': import argparse parser = argparse.ArgumentParser(description='DouZero evaluation backend')