修复BUG,添加metrics接口

This commit is contained in:
ZaneYork 2021-12-25 22:41:25 +08:00
parent f7b0f2d7f4
commit e2879b44a8
3 changed files with 71 additions and 30 deletions

View File

@ -1,4 +1,5 @@
import os import os
import traceback
from .orm import Baseline, Battle, Model from .orm import Baseline, Battle, Model
from evaluate import evaluate 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) challenger_baseline[battle.challenger_position + "_path"] = str(battle.challenger_path)
print(str(battle.challenger_path))
landlord_wp, farmer_wp, landlord_adp, farmer_adp = \ landlord_wp, farmer_wp, landlord_adp, farmer_adp = \
evaluate(challenger_baseline['landlord_path'], evaluate(challenger_baseline['landlord_path'],
challenger_baseline['landlord_up_path'], challenger_baseline['landlord_up_path'],
@ -43,16 +45,16 @@ def battle_logic(baseline : Baseline, battle : Battle):
challenge_success = False challenge_success = False
if battle.challenger_position == 'landlord': 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 = \ landlord_wp, farmer_wp, landlord_adp, farmer_adp = \
_second_eval(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 challenge_success = True
else: 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 = \ landlord_wp, farmer_wp, landlord_adp, farmer_adp = \
_second_eval(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 challenge_success = True
if challenge_success: if challenge_success:
challenger_baseline['rank'] = baseline.rank + 1 challenger_baseline['rank'] = baseline.rank + 1
@ -73,27 +75,32 @@ def battle_logic(baseline : Baseline, battle : Battle):
battle.save() battle.save()
def tick(): def tick():
battles = Battle.select().where(Battle.status == 0).order_by(Battle.id.desc()) try:
for battle in battles: battles = Battle.select().where(Battle.status == 0).order_by(Battle.id.desc()).limit(2)
baselines = Baseline.select().order_by(Baseline.rank.desc()).limit(1) for battle in battles:
if len(baselines) == 0: battle.status = -1
baseline = {} battle.save()
for position in positions: baselines = Baseline.select().order_by(Baseline.rank.desc()).limit(1)
models = Model.select().where(Model.position == position).order_by(Model.create_time.desc()).limit(1) if len(baselines) == 0:
if(len(models) > 0): baseline = {}
baseline['%s_path' % position] = models[0].path for position in positions:
if len(baseline.keys()) == 4: models = Model.select().where(Model.position == position).order_by(Model.create_time.desc()).limit(1)
baseline['rank'] = 0 if(len(models) > 0):
baseline['landlord_wp'] = 0 baseline['%s_path' % position] = models[0].path
baseline['farmer_wp'] = 0 if len(baseline.keys()) == 4:
baseline['landlord_adp'] = 0 baseline['rank'] = 0
baseline['farmer_adp'] = 0 baseline['landlord_wp'] = 0
baseline['create_time'] = datetime.now() baseline['farmer_wp'] = 0
Baseline.create(**baseline) baseline['landlord_adp'] = 0
baselines = Baseline.select().order_by(Baseline.rank.desc()).limit(1) baseline['farmer_adp'] = 0
battle_logic(baselines[0], battle) 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: else:
battle.status = 3 battle_logic(baselines[0], battle)
battle.save() except:
else: traceback.print_exc()
battle_logic(baselines[0], battle)

View File

@ -1,6 +1,11 @@
# -*- coding:utf8 -*- # -*- coding:utf8 -*-
from peewee import * 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 BaseModel(Model):
class Meta: class Meta:

View File

@ -1,5 +1,3 @@
from douzero.evaluation.simulation import evaluate
from douzero.server.orm import Model, Battle, Baseline from douzero.server.orm import Model, Battle, Baseline
from douzero.server.battle import tick from douzero.server.battle import tick
from flask import Flask, jsonify, request from flask import Flask, jsonify, request
@ -38,6 +36,37 @@ def upload():
threadpool.submit(tick) threadpool.submit(tick)
return jsonify({'status': 0, 'message': 'success', 'result': ''}) 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__': if __name__ == '__main__':
import argparse import argparse
parser = argparse.ArgumentParser(description='DouZero evaluation backend') parser = argparse.ArgumentParser(description='DouZero evaluation backend')