From 1a48af157db84b32733087a26c7655c54eedbfa0 Mon Sep 17 00:00:00 2001 From: Daochen Date: Wed, 13 May 2020 13:08:00 -0500 Subject: [PATCH] Refinement --- .gitignore | 2 +- server/media/uploaded_agents/example_model.py | 84 ------------------- server/tournament/views.py | 14 ++-- 3 files changed, 10 insertions(+), 90 deletions(-) delete mode 100644 server/media/uploaded_agents/example_model.py diff --git a/.gitignore b/.gitignore index 219814a..497982b 100644 --- a/.gitignore +++ b/.gitignore @@ -25,4 +25,4 @@ yarn-error.log* db.sqlite3 __pycache__ *.swp -/uploaded_agents +uploaded_agents diff --git a/server/media/uploaded_agents/example_model.py b/server/media/uploaded_agents/example_model.py deleted file mode 100644 index e5010a4..0000000 --- a/server/media/uploaded_agents/example_model.py +++ /dev/null @@ -1,84 +0,0 @@ -''' Leduc Hold 'em rule model -''' -import rlcard -from rlcard.models.model import Model - -class LeducHoldemRuleAgentV2(object): - ''' Leduc Hold 'em Rule agent version 2 - ''' - def __init__(self): - self.use_raw = True - - def step(self, state): - ''' Predict the action when given raw state. A simple rule-based AI. - Args: - state (dict): Raw state from the game - - Returns: - action (str): Predicted action - ''' - legal_actions = state['raw_legal_actions'] - state = state['raw_obs'] - hand = state['hand'] - public_card = state['public_card'] - action = 'fold' - ''' - When having only 2 hand cards at the game start, choose fold to drop terrible cards: - Acceptable hand cards: - Pairs - AK, AQ, AJ, AT - A9s, A8s, ... A2s(s means flush) - KQ, KJ, QJ, JT - Fold all hand types except those mentioned above to save money - ''' - if public_card: - if public_card[1] == hand[1]: - action = 'raise' - else: - action = 'fold' - else: - if hand[0] == 'K': - action = 'raise' - elif hand[0] == 'Q': - action = 'check' - else: - action = 'fold' - - #return action - if action in legal_actions: - return action - else: - if action == 'raise': - return 'call' - if action == 'check': - return 'fold' - if action == 'call': - return 'raise' - else: - return action - - def eval_step(self, state): - return self.step(state), [] - -class LeducHoldemRuleModelV2(Model): - ''' Leduc holdem Rule Model version 2 - ''' - - def __init__(self): - ''' Load pretrained model - ''' - env = rlcard.make('leduc-holdem') - rule_agent = LeducHoldemRuleAgentV2() - self.rule_agents = [rule_agent for _ in range(env.player_num)] - - @property - def agents(self): - ''' Get a list of agents for each position in a the game - - Returns: - agents (list): A list of agents - - Note: Each agent should be just like RL agent with step and eval_step - functioning well. - ''' - return self.rule_agents diff --git a/server/tournament/views.py b/server/tournament/views.py index beb8779..4f9cc5f 100644 --- a/server/tournament/views.py +++ b/server/tournament/views.py @@ -16,7 +16,7 @@ from .models import Game, Payoff, UploadedAgent from .tournament import Tournament -def reset_model_ids(): +def _reset_model_ids(): from .rlcard_wrap import rlcard, MODEL_IDS agents = UploadedAgent.objects.all() for agent in agents: @@ -69,8 +69,12 @@ def query_payoff(request): @transaction.atomic def launch(request): if request.method == 'GET': - eval_num = int(request.GET['eval_num']) - game = request.GET['name'] + try: + eval_num = int(request.GET['eval_num']) + game = request.GET['name'] + except: + return HttpResponse(json.dumps({'value': -1, 'info': 'parameters error'})) + games_data, payoffs_data = Tournament(game, MODEL_IDS[game], eval_num).launch() Game.objects.filter(name=game).delete() Payoff.objects.filter(name=game).delete() @@ -103,7 +107,7 @@ def upload_agent(request): a = UploadedAgent(name=name, game=game, f=f, entry=entry) a.save() - reset_model_ids() + _reset_model_ids() return HttpResponse(json.dumps({'value': 0, 'info': 'success'})) def delete_agent(request): @@ -113,7 +117,7 @@ def delete_agent(request): return HttpResponse(json.dumps({'value': -1, 'info': 'name not exists'})) UploadedAgent.objects.filter(name=name).delete() - reset_model_ids() + _reset_model_ids() return HttpResponse(json.dumps({'value': 0, 'info': 'success'})) def list_agents(request):