2021-12-25 19:06:34 +08:00
|
|
|
import os
|
2021-12-25 22:41:25 +08:00
|
|
|
import traceback
|
2021-12-25 19:06:34 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-12-25 22:41:25 +08:00
|
|
|
print(str(battle.challenger_path))
|
2021-12-25 19:06:34 +08:00
|
|
|
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':
|
2021-12-25 22:41:25 +08:00
|
|
|
if baseline.landlord_wp == 0 or landlord_wp / float(baseline.landlord_wp) > 1.2:
|
2021-12-25 19:06:34 +08:00
|
|
|
landlord_wp, farmer_wp, landlord_adp, farmer_adp = \
|
|
|
|
_second_eval(landlord_wp, farmer_wp, landlord_adp, farmer_adp)
|
2021-12-25 22:41:25 +08:00
|
|
|
if baseline.landlord_wp == 0 or landlord_wp / float(baseline.landlord_wp) > 1.2:
|
2021-12-25 19:06:34 +08:00
|
|
|
challenge_success = True
|
|
|
|
else:
|
2021-12-25 22:41:25 +08:00
|
|
|
if baseline.farmer_wp == 0 or farmer_wp / float(baseline.farmer_wp) > 1.2:
|
2021-12-25 19:06:34 +08:00
|
|
|
landlord_wp, farmer_wp, landlord_adp, farmer_adp = \
|
|
|
|
_second_eval(landlord_wp, farmer_wp, landlord_adp, farmer_adp)
|
2021-12-25 22:41:25 +08:00
|
|
|
if baseline.farmer_wp == 0 or farmer_wp / float(baseline.farmer_wp) > 1.2:
|
2021-12-25 19:06:34 +08:00
|
|
|
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():
|
2021-12-25 22:41:25 +08:00
|
|
|
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()
|
2021-12-25 19:06:34 +08:00
|
|
|
else:
|
2021-12-25 22:41:25 +08:00
|
|
|
battle_logic(baselines[0], battle)
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|