sjz commited on
Commit
d8f258e
1 Parent(s): d97a106

fix AI vs AI bug

Browse files
Files changed (2) hide show
  1. const.py +6 -1
  2. pages/AI_VS_AI.py +75 -50
const.py CHANGED
@@ -36,6 +36,11 @@ _PLAYER_COLOR = {
36
  _BLANK: "Blank",
37
  _BLACK: "YOU HUMAN",
38
  }
 
 
 
 
 
39
  _HORIZONTAL = np.array(
40
  [
41
  [0, 0, 0, 0, 0],
@@ -87,6 +92,6 @@ _ROOM_COLOR = {
87
 
88
  _MODEL_PATH = {
89
  "AlphaZero": "Gomoku_MCTS/checkpoint/2023-12-14-18-17-07_test_teaching_learning_collect_epochs=1000_size=9_model=normal/best_policy.model",
90
- "duel": "/Users/husky/GomokuDemo/Gomoku_MCTS/checkpoint/2023-12-14-10-22-12_test_teaching_learning_collect_epochs=1000_size=9_model=duel/best_policy.model",
91
  "Gumbel AlphaZero": "Gomoku_MCTS/checkpoint/2023-12-14-21-19-40_selfplay_epochs=1000_size=9_model=gumbel/best_policy.model",
92
  }
 
36
  _BLANK: "Blank",
37
  _BLACK: "YOU HUMAN",
38
  }
39
+ _PLAYER_COLOR_AI_VS_AI = {
40
+ _WHITE: "WHITE",
41
+ _BLANK: "Blank",
42
+ _BLACK: "BLACK",
43
+ }
44
  _HORIZONTAL = np.array(
45
  [
46
  [0, 0, 0, 0, 0],
 
92
 
93
  _MODEL_PATH = {
94
  "AlphaZero": "Gomoku_MCTS/checkpoint/2023-12-14-18-17-07_test_teaching_learning_collect_epochs=1000_size=9_model=normal/best_policy.model",
95
+ "duel": "Gomoku_MCTS/checkpoint/2023-12-14-10-22-12_test_teaching_learning_collect_epochs=1000_size=9_model=duel/best_policy.model",
96
  "Gumbel AlphaZero": "Gomoku_MCTS/checkpoint/2023-12-14-21-19-40_selfplay_epochs=1000_size=9_model=gumbel/best_policy.model",
97
  }
pages/AI_VS_AI.py CHANGED
@@ -24,7 +24,7 @@ from const import (
24
  _BLACK, # 1, for human
25
  _WHITE, # 2 , for AI
26
  _BLANK,
27
- _PLAYER_COLOR,
28
  _PLAYER_SYMBOL1,
29
  _PLAYER_SYMBOL2,
30
  _ROOM_COLOR,
@@ -438,38 +438,50 @@ def gomoku():
438
  in session_state.ROOM.COORDINATE_1D
439
  ):
440
  if i == gpt_i and j == gpt_j:
441
- BOARD_PLATE[i][j].button(
442
- session_state.Symbol[_NEW],
443
- key=f"{i}:{j}",
444
- args=(i, j),
445
- on_click=forbid_click,
 
 
 
446
  )
447
  else:
448
  # disable click for GPT choices
449
- BOARD_PLATE[i][j].button(
450
- session_state.Symbol[cell],
451
- key=f"{i}:{j}",
452
- args=(i, j),
453
- on_click=forbid_click
 
 
 
454
  )
455
  else:
456
  if session_state.USE_AIAID and i * _BOARD_SIZE + j in top_five_acts and not \
457
  session_state.ROOM.BOARD.game_end()[0]:
458
  # enable click for other cells available for human choices
459
  prob = top_five_probs[top_five_acts.index(i * _BOARD_SIZE + j)]
460
- BOARD_PLATE[i][j].button(
461
- session_state.Symbol[cell] + f"({round(prob, 2)})",
462
- key=f"{i}:{j}",
463
- on_click=forbid_click,
464
- args=(i, j),
 
 
 
465
  )
466
  else:
467
  # enable click for other cells available for human choices
468
- BOARD_PLATE[i][j].button(
469
- session_state.Symbol[cell],
470
- key=f"{i}:{j}",
471
- on_click=forbid_click,
472
- args=(i, j),
 
 
 
473
  )
474
 
475
  message.markdown(
@@ -512,7 +524,6 @@ def gomoku():
512
  print("AI's move is located at ({}, {}) :".format(gpt_i, gpt_j))
513
  move = session_state.ROOM.BOARD.location_to_move((gpt_i, gpt_j))
514
  print("Location to move: ", move)
515
- # print("Location to move: ", move)
516
  # MCTS BOARD
517
  session_state.ROOM.BOARD.do_move(move)
518
  # Gomoku Bot BOARD
@@ -541,38 +552,50 @@ def gomoku():
541
  in session_state.ROOM.COORDINATE_1D
542
  ):
543
  if i == gpt_i and j == gpt_j:
544
- BOARD_PLATE[i][j].button(
545
- session_state.Symbol[_NEW],
546
- key=f"{i}:{j}",
547
- args=(i, j),
548
- on_click=forbid_click,
 
 
 
549
  )
550
  else:
551
  # disable click for GPT choices
552
- BOARD_PLATE[i][j].button(
553
- session_state.Symbol[cell],
554
- key=f"{i}:{j}",
555
- args=(i, j),
556
- on_click=forbid_click
 
 
 
557
  )
558
  else:
559
  if session_state.USE_AIAID and i * _BOARD_SIZE + j in top_five_acts and not \
560
  session_state.ROOM.BOARD.game_end()[0]:
561
  # enable click for other cells available for human choices
562
  prob = top_five_probs[top_five_acts.index(i * _BOARD_SIZE + j)]
563
- BOARD_PLATE[i][j].button(
564
- session_state.Symbol[cell] + f"({round(prob, 2)})",
565
- key=f"{i}:{j}",
566
- on_click=forbid_click,
567
- args=(i, j),
 
 
 
568
  )
569
  else:
570
  # enable click for other cells available for human choices
571
- BOARD_PLATE[i][j].button(
572
- session_state.Symbol[cell],
573
- key=f"{i}:{j}",
574
- on_click=forbid_click,
575
- args=(i, j),
 
 
 
576
  )
577
 
578
  message.markdown(
@@ -623,21 +646,23 @@ def gomoku():
623
  # Infos
624
  def update_info() -> None:
625
  # Additional information
626
- SCORE_PLATE[0].metric("Gomoku-Agent", session_state.ROOM.HISTORY[0])
627
- SCORE_PLATE[1].metric("Black", session_state.ROOM.HISTORY[1])
628
  if session_state.ROOM.WINNER != _BLANK:
629
  st.balloons()
630
  ROUND_INFO.write(
631
- f"#### **{_PLAYER_COLOR[session_state.ROOM.WINNER]} WIN!**\n**Click buttons on the left for more plays.**"
632
  )
633
  st.markdown("<br>", unsafe_allow_html=True)
634
  st.markdown("<br>", unsafe_allow_html=True)
635
  chart_data = pd.DataFrame(session_state.ROOM.ai_simula_time_list, columns=["Simulation Time"])
636
- st.line_chart(chart_data)
637
-
638
- game_control()
639
- update_info()
640
-
 
 
 
 
641
 
642
  if __name__ == "__main__":
643
  gomoku()
 
24
  _BLACK, # 1, for human
25
  _WHITE, # 2 , for AI
26
  _BLANK,
27
+ _PLAYER_COLOR_AI_VS_AI,
28
  _PLAYER_SYMBOL1,
29
  _PLAYER_SYMBOL2,
30
  _ROOM_COLOR,
 
438
  in session_state.ROOM.COORDINATE_1D
439
  ):
440
  if i == gpt_i and j == gpt_j:
441
+ # BOARD_PLATE[i][j].button(
442
+ # session_state.Symbol[_NEW],
443
+ # key=f"{i}:{j}",
444
+ # args=(i, j),
445
+ # on_click=forbid_click,
446
+ # )
447
+ BOARD_PLATE[i][j].write(
448
+ session_state.Symbol[_NEW]
449
  )
450
  else:
451
  # disable click for GPT choices
452
+ # BOARD_PLATE[i][j].button(
453
+ # session_state.Symbol[cell],
454
+ # key=f"{i}:{j}",
455
+ # args=(i, j),
456
+ # on_click=forbid_click
457
+ # )
458
+ BOARD_PLATE[i][j].write(
459
+ session_state.Symbol[cell]
460
  )
461
  else:
462
  if session_state.USE_AIAID and i * _BOARD_SIZE + j in top_five_acts and not \
463
  session_state.ROOM.BOARD.game_end()[0]:
464
  # enable click for other cells available for human choices
465
  prob = top_five_probs[top_five_acts.index(i * _BOARD_SIZE + j)]
466
+ # BOARD_PLATE[i][j].button(
467
+ # session_state.Symbol[cell] + f"({round(prob, 2)})",
468
+ # key=f"{i}:{j}",
469
+ # on_click=forbid_click,
470
+ # args=(i, j),
471
+ # )
472
+ BOARD_PLATE[i][j].write(
473
+ session_state.Symbol[cell]
474
  )
475
  else:
476
  # enable click for other cells available for human choices
477
+ # BOARD_PLATE[i][j].button(
478
+ # session_state.Symbol[cell],
479
+ # key=f"{i}:{j}",
480
+ # on_click=forbid_click,
481
+ # args=(i, j),
482
+ # )
483
+ BOARD_PLATE[i][j].write(
484
+ session_state.Symbol[cell]
485
  )
486
 
487
  message.markdown(
 
524
  print("AI's move is located at ({}, {}) :".format(gpt_i, gpt_j))
525
  move = session_state.ROOM.BOARD.location_to_move((gpt_i, gpt_j))
526
  print("Location to move: ", move)
 
527
  # MCTS BOARD
528
  session_state.ROOM.BOARD.do_move(move)
529
  # Gomoku Bot BOARD
 
552
  in session_state.ROOM.COORDINATE_1D
553
  ):
554
  if i == gpt_i and j == gpt_j:
555
+ # BOARD_PLATE[i][j].button(
556
+ # session_state.Symbol[_NEW],
557
+ # key=f"{i}:{j}",
558
+ # args=(i, j),
559
+ # on_click=forbid_click,
560
+ # )
561
+ BOARD_PLATE[i][j].write(
562
+ session_state.Symbol[_NEW]
563
  )
564
  else:
565
  # disable click for GPT choices
566
+ # BOARD_PLATE[i][j].button(
567
+ # session_state.Symbol[cell],
568
+ # key=f"{i}:{j}",
569
+ # args=(i, j),
570
+ # on_click=forbid_click
571
+ # )
572
+ BOARD_PLATE[i][j].write(
573
+ session_state.Symbol[cell]
574
  )
575
  else:
576
  if session_state.USE_AIAID and i * _BOARD_SIZE + j in top_five_acts and not \
577
  session_state.ROOM.BOARD.game_end()[0]:
578
  # enable click for other cells available for human choices
579
  prob = top_five_probs[top_five_acts.index(i * _BOARD_SIZE + j)]
580
+ # BOARD_PLATE[i][j].button(
581
+ # session_state.Symbol[cell] + f"({round(prob, 2)})",
582
+ # key=f"{i}:{j}",
583
+ # on_click=forbid_click,
584
+ # args=(i, j),
585
+ # )
586
+ BOARD_PLATE[i][j].write(
587
+ session_state.Symbol[cell]
588
  )
589
  else:
590
  # enable click for other cells available for human choices
591
+ # BOARD_PLATE[i][j].button(
592
+ # session_state.Symbol[cell],
593
+ # key=f"{i}:{j}",
594
+ # on_click=forbid_click,
595
+ # args=(i, j),
596
+ # )
597
+ BOARD_PLATE[i][j].write(
598
+ session_state.Symbol[cell]
599
  )
600
 
601
  message.markdown(
 
646
  # Infos
647
  def update_info() -> None:
648
  # Additional information
 
 
649
  if session_state.ROOM.WINNER != _BLANK:
650
  st.balloons()
651
  ROUND_INFO.write(
652
+ f"#### **{_PLAYER_COLOR_AI_VS_AI[session_state.ROOM.WINNER]} WIN!**\n**Click buttons on the left for more plays.**"
653
  )
654
  st.markdown("<br>", unsafe_allow_html=True)
655
  st.markdown("<br>", unsafe_allow_html=True)
656
  chart_data = pd.DataFrame(session_state.ROOM.ai_simula_time_list, columns=["Simulation Time"])
657
+ chart_placeholder.line_chart(chart_data)
658
+
659
+
660
+ chart_placeholder = st.empty()
661
+ while session_state.ROOM.WINNER == _BLANK:
662
+ game_control()
663
+ update_info()
664
+ SCORE_PLATE[0].metric("Gomoku-Agent", session_state.ROOM.HISTORY[0])
665
+ SCORE_PLATE[1].metric("Black", session_state.ROOM.HISTORY[1])
666
 
667
  if __name__ == "__main__":
668
  gomoku()