Douzero_Resnet/douzero/server/battle.py

100 lines
4.4 KiB
Python

import os
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)
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,
1,
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,
1,
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 landlord_wp - 0.15 > baseline.landlord_wp:
landlord_wp, farmer_wp, landlord_adp, farmer_adp = \
_second_eval(landlord_wp, farmer_wp, landlord_adp, farmer_adp)
if landlord_wp - 0.15 > baseline.landlord_wp:
challenge_success = True
else:
if farmer_wp - 0.15 > baseline.farmer_wp:
landlord_wp, farmer_wp, landlord_adp, farmer_adp = \
_second_eval(landlord_wp, farmer_wp, landlord_adp, farmer_adp)
if farmer_wp - 0.15 > baseline.farmer_wp:
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)
else:
onnx_path = str(battle.challenger_path) + '.onnx'
if os.path.exists(onnx_path):
os.remove(onnx_path)
os.remove(str(battle.challenger_path))
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()
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)
else:
battle.status = 3
battle.save()
else:
battle_logic(baselines[0], battle)