def start(): print("zzy test getCardsValue") str_handcards = str(request.query.handcards) print("handcards",type(str_handcards)," len ",len(str_handcards),str_handcards) handcards = json.loads(str_handcards) cards_value, _ = CEnv.get_cards_value(Card.char2color(handcards)) return json.dumps(cards_value)
def prepare(self, cards): if not self.cards_cache: random.seed(44) random.shuffle(cards) else: cards = self.cards_cache self.agent_cards = cards[:int(len(cards) / 2)] val_self, _ = env.Env.get_cards_value(Card.char2color( self.agent_cards)) self.oppo_cards = cards[int(len(cards) / 2):] val_oppo, _ = env.Env.get_cards_value(Card.char2color(self.oppo_cards)) print(val_self, val_oppo) if (val_self < val_oppo + 5): self.prepare(cards) else: self.cards_cache = cards
def write_seq2(epochs, filename): f = open(filename, 'wb+') # origin_cards = ['3', '3', '3', '3', '4', '4', '4', '4', '5', '5', '5', '5', # '6', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '8', # '9', '9', '9', '9', '10', '10', '10', '10', 'J', 'J', 'J', 'J', # 'Q', 'Q', 'Q', 'Q', 'K', 'K', 'K', 'K', 'A', 'A', 'A', 'A', # '2', '2', '2', '2', '*', '$'] origin_cards = [ '3', '3', '3', '3', '4', '4', '4', '4', '5', '5', '5', '5', '6', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '8', '9', '9', '9', '9', '10', '10', '10', '10', 'J', 'J', 'J', 'J' ] f.write(len(origin_cards).to_bytes(2, byteorder='little', signed=False)) for i in range(epochs): cards = origin_cards.copy() enrivon = env.Env() random.shuffle(cards) for c in cards: if c == '10': c = '1' f.write(ord(c).to_bytes(1, byteorder='little', signed=False)) # print(cards) handcards = [cards[:int(len(cards) / 2)], cards[int(len(cards) / 2):]] enrivon.reset() enrivon.prepare2_manual(Card.char2color(cards)) end = False ind = 0 while not end: intention, end = enrivon.step2_auto() put_list = Card.to_cards_from_3_17(intention) try: a = next(i for i, v in enumerate(action_space) if v == put_list) except StopIteration as e: print(put_list) f.write(a.to_bytes(2, byteorder='little', signed=False)) # assert(action_space[a] == put_list) for c in put_list: handcards[ind].remove(c) ind = 1 - ind # assert((not handcards[0]) or (not handcards[1])) if i % 1000 == 0: print("writing %d..." % i) sys.stdout.flush() f.close() print("write completed with %d epochs" % epochs)
def write_seq3(epochs, filename): f = open(filename, 'wb+') origin_cards = [ '3', '3', '3', '3', '4', '4', '4', '4', '5', '5', '5', '5', '6', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '8', '9', '9', '9', '9', '10', '10', '10', '10', 'J', 'J', 'J', 'J', 'Q', 'Q', 'Q', 'Q', 'K', 'K', 'K', 'K', 'A', 'A', 'A', 'A', '2', '2', '2', '2', '*', '$' ] for i in range(epochs): cards = origin_cards.copy() enrivon = env.Env() lord_id = -1 while lord_id == -1: random.shuffle(cards) enrivon.reset() lord_id = enrivon.prepare_manual(Card.char2color(cards)) for c in cards: if c == '10': c = '1' f.write(ord(c).to_bytes(1, byteorder='little', signed=False)) f.write(lord_id.to_bytes(2, byteorder='little', signed=False)) handcards = [cards[:17], cards[17:34], cards[34:51]] extra_cards = cards[51:] handcards[lord_id] += extra_cards r = 0 ind = lord_id while r == 0: intention, r = enrivon.step_auto() put_list = Card.to_cards_from_3_17(intention) # print(put_list) try: a = next(i for i, v in enumerate(action_space) if v == put_list) except StopIteration as e: print(put_list) # raise Exception('cards error') f.write(a.to_bytes(2, byteorder='little', signed=False)) for c in put_list: handcards[ind].remove(c) ind = int(ind + 1) % 3 f.write(r.to_bytes(2, byteorder='little', signed=True)) f.close() print("write completed with %d epochs" % epochs)
def ai_play(): data = request.json print(data) pos = int(data['current_player']) player_cards = data['player_cards'] my_cards = trans_cards(player_cards.split("|")[pos]) last_move = trans_cards(data['last_move']) if int(data['last_player']) == int(data['current_player']): last_move = [] else: last_move = trans_cards(data['last_move']) intention = to_char( CEnv.step_auto_static(Card.char2color(my_cards), to_value(last_move))) res = trans_cards_reverse(intention) if res == "": res = 'P' print("result is {}".format(res)) return jsonify({'move': res})
def run(self): logger.info('simulator main loop') context = zmq.Context() sim2coord_socket = context.socket(zmq.PUSH) sim2coord_socket.setsockopt(zmq.IDENTITY, self.name.encode('utf-8')) sim2coord_socket.set_hwm(2) sim2coord_socket.connect(self.sim2coord) coord2sim_socket = context.socket(zmq.DEALER) coord2sim_socket.setsockopt(zmq.IDENTITY, self.name.encode('utf-8')) coord2sim_socket.set_hwm(2) coord2sim_socket.connect(self.coord2sim) sim2exp_sockets = [] for sim2exp in self.sim2exps: sim2exp_socket = context.socket(zmq.PUSH) sim2exp_socket.setsockopt(zmq.IDENTITY, self.name.encode('utf-8')) sim2exp_socket.set_hwm(2) sim2exp_socket.connect(sim2exp) sim2exp_sockets.append(sim2exp_socket) sim2mgr_socket = context.socket(zmq.PUSH) sim2mgr_socket.setsockopt(zmq.IDENTITY, self.name.encode('utf-8')) sim2mgr_socket.set_hwm(2) sim2mgr_socket.connect(self.sim2mgr) mgr2sim_socket = context.socket(zmq.DEALER) mgr2sim_socket.setsockopt(zmq.IDENTITY, self.name.encode('utf-8')) mgr2sim_socket.set_hwm(2) mgr2sim_socket.connect(self.mgr2sim) # while True: # time.sleep(0.3) # print(self.name) # sim2exp_sockets[1].send(dumps([self.name, 'haha'])) # print('main loop') # while True: # time.sleep(0.3) # msg = loads(coord2sim_socket.recv(copy=False).bytes) # print(msg) # sim2coord_socket.send(dumps([self.name, self.agent_names[0], np.arange(10)])) def request_screen(): sim2mgr_socket.send(dumps([self.name, SimulatorManager.MSG_TYPE.SCREEN, []])) return loads(mgr2sim_socket.recv(copy=False).bytes) def request_click(bbox): sim2mgr_socket.send(dumps([self.name, SimulatorManager.MSG_TYPE.CLICK, [(bbox[0] + bbox[2]) // 2 + self.window_rect[0] + 6, (bbox[1] + bbox[3]) // 2 + self.window_rect[1] + 46]])) return loads(mgr2sim_socket.recv(copy=False).bytes) def request_lock(): sim2mgr_socket.send(dumps([self.name, SimulatorManager.MSG_TYPE.LOCK, []])) return loads(mgr2sim_socket.recv(copy=False).bytes) def request_unlock(): sim2mgr_socket.send(dumps([self.name, SimulatorManager.MSG_TYPE.UNLOCK, []])) return loads(mgr2sim_socket.recv(copy=False).bytes) def spin_lock_on_button(): act = dict() while not act: self.current_screen = request_screen() cv2.imwrite('debug.png', self.current_screen) act = get_current_button_action(self.current_screen) if self.toggle.value == 0: break return act def discard(act, bboxes, idxs): def diff(idxs, cards): res = [] for i in range(len(cards)): if cards[i] is not None: if i in idxs: res.append(i) else: if i not in idxs: res.append(i) return res differences = diff(idxs, get_cards_bboxes(request_screen(), self.templates, bboxes=bboxes)[0]) print(differences) request_lock() while len(differences) > 0: for d in differences: request_click(bboxes[d]) # request_click(bboxes[differences[0]]) # time.sleep(0.3) differences = diff(idxs, get_cards_bboxes(request_screen(), self.templates, bboxes=bboxes)[0]) print(differences) if 'chupai' in act: request_click(act['chupai']) elif 'alone_chupai' in act: request_click(act['alone_chupai']) elif 'ming_chupai' in act: request_click(act['ming_chupai']) request_unlock() game_cnt = 0 while True: import psutil # print('memory usage is: ', psutil.virtual_memory()) if self.toggle.value == 0: time.sleep(0.2) continue print('new round') self.current_screen = request_screen() act = spin_lock_on_button() if not act: continue print(act) if 'start' in act: request_click(act['start']) continue if self.state == Simulator.State.CALLING: # state has changed if 'reverse' in act: self.state = Simulator.State.PLAYING self.current_lord_pos = who_is_lord(self.current_screen) while self.current_lord_pos < 0: self.current_screen = request_screen() self.current_lord_pos = who_is_lord(self.current_screen) print('current lord pos ', self.current_lord_pos) if self.toggle.value == 0: break continue if 'continuous defeat' in act: request_click(act['continuous defeat']) continue print('calling', act) handcards, _ = get_cards_bboxes(self.current_screen, self.templates, 0) cards_value, _ = CEnv.get_cards_value(Card.char2color(handcards)) print('cards value: ', cards_value) # assert 'jiaodizhu' in act request_click(act['bujiao']) if cards_value < 10 else request_click(act['jiaodizhu']) elif self.state == Simulator.State.PLAYING: if 'defeat' in act or 'victory' in act: request_click(act['defeat'] if 'defeat' in act else act['victory']) if self.cached_msg is None: print('other player wins in one step!!!') continue win = is_win(self.current_screen) state, action, fine_mask = self.cached_msg if win: sim2exp_sockets[self.current_lord_pos].send(dumps([[state, state], action, 1, True, False, [fine_mask, fine_mask]])) self.win_rates[self.agent_names[self.current_lord_pos]].feed(1.) else: sim2exp_sockets[self.current_lord_pos].send(dumps([[state, state], action, -1, True, False, [fine_mask, fine_mask]])) self.win_rates[self.agent_names[self.current_lord_pos]].feed(0.) game_cnt += 1 if game_cnt % 100 == 0: for agent in self.agent_names: if self.win_rates[agent].count > 0: logger.info('[last-100]{} win rate: {}'.format(agent, self.win_rates[agent].average)) self.win_rates[agent].reset() self.reset_episode() continue # test if we have cached msg not sent print('playing', act) left_cards, _ = get_cards_bboxes(self.current_screen, self.mini_templates, 1) right_cards, _ = get_cards_bboxes(self.current_screen, self.mini_templates, 2) if None in left_cards or None in right_cards: request_click(act['buchu']) time.sleep(1.) continue assert None not in left_cards assert None not in right_cards self.history[1].extend(right_cards) self.history[2].extend(left_cards) # last_cards = left_cards # if not left_cards: # last_cards = right_cards # print('last cards', last_cards) total_cards = np.ones([60]) total_cards[53:56] = 0 total_cards[57:60] = 0 handcards, bboxes = get_cards_bboxes(self.current_screen, self.templates, 0) handcards = [card for card in handcards if card is not None] remain_cards = total_cards - Card.char2onehot60(handcards + self.history[0] + self.history[1] + self.history[2]) print('current handcards: ', handcards) # left_cnt, right_cnt = get_opponent_cnts(self.current_screen, self.tiny_templates) # print('left cnt: ', left_cnt, 'right cnt: ', right_cnt) left_cnt = 17 - len(self.history[2]) right_cnt = 17 - len(self.history[1]) if self.current_lord_pos == 1: left_cnt += 3 if self.current_lord_pos == 2: right_cnt += 3 # assert left_cnt > 0 and right_cnt > 0 # to be the same as C++ side, right comes before left right_prob_state = remain_cards * (right_cnt / (left_cnt + right_cnt)) left_prob_state = remain_cards * (left_cnt / (left_cnt + right_cnt)) prob_state = np.concatenate([right_prob_state, left_prob_state]) # assert prob_state.size == 120 # assert np.all(prob_state < 1.) and np.all(prob_state >= 0.) # print(prob_state) intention, buffer_comb, buffer_fine = self.predictor.predict(handcards, [left_cards, right_cards], prob_state, self, sim2coord_socket, coord2sim_socket) if self.cached_msg is not None: state, action, fine_mask = self.cached_msg sim2exp_sockets[self.current_lord_pos].send( dumps([[state, buffer_comb[0]], action, 0, False, False, [fine_mask, buffer_comb[2]]])) sim2exp_sockets[self.current_lord_pos].send( dumps([[buffer_comb[0], buffer_fine[0]], buffer_comb[1], 0, False, True, [buffer_comb[2], buffer_fine[2]]])) self.cached_msg = buffer_fine self.history[0].extend(intention) print('intention is: ', intention) intention.sort(key=lambda k: Card.cards_to_value[k]) if len(intention) == 0: request_click(act['buchu']) else: i = 0 j = 0 to_click = [] to_click_idxs = [] while j < len(intention): if handcards[i] == intention[j]: to_click_idxs.append(i) to_click.append(bboxes[i]) i += 1 j += 1 else: i += 1 for bbox in to_click: request_click(bbox) time.sleep(0.5) request_click([1310, 760, 1310, 760]) time.sleep(1.)
def intention(self, env): intention = to_char( CEnv.step_auto_static(Card.char2color(env.get_curr_handcards()), to_value(env.get_last_outcards()))) return intention