def actions(): state_map = request.args.get('state_map') side = request.args.get('side') if not state_map or side not in ('b', 'r'): return json.dumps({'error': True, 'msg': 'invalid params', 'data': {'state_map': state_map, 'side': side}}) result = KoreanChess.get_actions(json.loads(state_map), side) return json.dumps(result)
def action(): state_map = request.args.get('state_map') side = request.args.get('side') if not state_map or side not in ('b', 'r'): return json.dumps({ 'error': True, 'msg': 'invalid params', 'data': { 'state_map': state_map, 'side': side } }) state_map = json.loads(state_map) filter_state_map(state_map) if side == 'b': reverse_state_map = KoreanChess.reverse_state_map(state_map) db_name = './q_blue.db' else: reverse_state_map = state_map db_name = './q_red.db' state_key = KoreanChess.convert_state_key(reverse_state_map) conn = sqlite3.connect(db_name) c = conn.cursor() c.execute("SELECT quality_json FROM t_quality WHERE state_key='" + state_key + "'") result = c.fetchone() reversed_state_map = KoreanChess.reverse_state_map(state_map) actions = KoreanChess.get_actions(reversed_state_map, side) if result: if result[0] == '0': action = KoreanChess.similar_action(actions, state_key, side, c) else: q_values = json.loads(result[0]) max_action = int( max(q_values.iteritems(), key=operator.itemgetter(1))[0]) if len(actions) <= max_action: action = KoreanChess.similar_action(actions, state_key, side, c) else: action = actions[max_action] else: action = KoreanChess.similar_action(actions, state_key, side, c) c.close() return json.dumps(action)
j = 0 while not blue_done and not red_done: if j >= 400: break blue_action = env.get_action_es(blue_state, kcu.BLUE) red_state, blue_reward, blue_done, is_draw = env.step(blue_action, blue_state) env.print_map(red_state, kcu.RED, i, j, blue_reward_all, red_reward_all, kcu.BLUE if blue_done else False, is_draw, blue_win_cnt, red_win_cnt) if blue_action is False and red_action is False: print('there is no action.') break if old_red_state: cur_reward = (red_reward - blue_reward * .9) red_q = KoreanChess.get_q_from_es(red_state, kcu.RED) max_action = int(max(red_q.iteritems(), key=operator.itemgetter(1))[1]) if red_q: cur_q_value = cur_reward + dis * max_action else: cur_q_value = cur_reward KoreanChess.update_es_q(old_red_state, red_action, cur_q_value) red_reward_all += (red_reward - blue_reward * .9) if blue_done and not is_draw: blue_win_cnt += 1 break red_action = env.get_action(Q_red, red_state, i, True)
key = line.strip() else: q_value = np.array(json.loads(line.strip())) if np.sum(q_value) == 0: quality_json = '0' else: quality_dict = {} for j, q in enumerate(q_value): if q != 0: quality_dict[j] = q quality_json = json.dumps(quality_dict) # Insert a row of data c.execute( "INSERT INTO t_quality VALUES " + "('%s', '%s', %d, '%s', %d)" % ((KoreanChess.compress_state_key(key), quality_json, len(q_value), datetime.datetime.now().strftime('%Y-%m-%d %H:%I:%S'), 1))) i += 1 q_file.close() # Save (commit) the changes conn.commit() # We can also close the connection if we are done with it. # Just be sure any changes have been committed or they will be lost. conn.close() conn = sqlite3.connect('./q_red.db') c = conn.cursor()
from elasticsearch import Elasticsearch as ES from env.korean_chess import KoreanChess import sys es = ES('52.79.135.2:80') q_file = open('./q_blue_with_data.txt') i = 0 key = None bulk_list = [] blue_index = {"index": {"_index": "i_irelia_state", "_type": "t_blue_state"}} for line in q_file: if i % 2 is 0: key = line.strip() decomp_key = KoreanChess.decompress_state_key(key) bulk_list.append(blue_index) bulk_list.append({"state": decomp_key}) if i % 1000 == 0 and i != 0: es.bulk(bulk_list) bulk_list = [] i += 1 q_file.close() es.bulk(bulk_list) q_file = open('./q_red_with_data.txt') i = 0 key = None bulk_list = [] red_index = {"index": {"_index": "i_irelia_state", "_type": "t_red_state"}} for line in q_file:
import sqlite3 import os import json import numpy as np import datetime from env.korean_chess import KoreanChess q_file = open('./q_blue_with_data.txt') compress_q_file = open('./q_blue_with_data_comp.txt', mode='w') i = 0 key = None for line in q_file: if i % 2 is 0: key = line.strip() compress_q_file.write(KoreanChess.compress_state_key(key) + "\n") else: compress_q_file.write(line.strip() + "\n") i += 1 compress_q_file.close() q_file.close() q_file = open('./q_red_with_data.txt') compress_q_file = open('./q_red_with_data_comp.txt', mode='w') i = 0 key = None for line in q_file: if i % 2 is 0: key = line.strip() compress_q_file.write(KoreanChess.compress_state_key(key) + "\n") else:
import numpy as np import time import json from env import korean_chess_util as kcu from env.korean_chess import KoreanChess import sys import sqlite3 conn = sqlite3.connect('q_red.db') c = conn.cursor() c.execute("SELECT quality_json FROM t_quality WHERE state_key='r6,r4,r2,r3,1,r3,r4,r2,r6,4,r7,5,r5,5,r5,1,r1,1,r1,1,r1,1,r1,1,r1,18,b1,1,b1,1,b1,1,b1,b1,2,b5,5,b5,5,b7,4,b6,b4,b2,b3,1,b3,b4,b2,b6'") result = c.fetchone() print(result) sys.exit() ret = KoreanChess.convert_state_map( 'b6,b2,b4,b3,1,b3,b2,b4,b6,4,b7,5,b5,5,b5,1,b1,1,b1,1,b1,1,b1,b1,19,r1,1,r1,1,r1,1,r1,1,r1,1,r5,5,r5,5,r7,4,r6,r4,r2,r3,1,r3,r2,r4,r6') print(ret) sys.exit() Game.register('KoreanChess', {'position_type': 'random', 'state_list': None, 'init_state': None, 'init_side': 'b'}) env = Game.make('KoreanChess') ret = env.convert_state_map( 'r6,1,r2,r3,1,r3,r2,1,r6,4,r7,8,r5,r5,r4,6,r4,r1,r1,1,r1,2,r1,14,b6,b1,2,b1,b4,b1,b1,3,b5,b4,1,b5,8,b7,4,b6,1,b2,b3,1,b3,b2,2') print(ret) # blue_state = env.reset()