Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ブラウザでプレイできるエージェント #1021

Open
wants to merge 28 commits into
base: master
Choose a base branch
from

Conversation

OkanoShinri
Copy link
Collaborator

@OkanoShinri OkanoShinri commented Nov 12, 2021

cf. #1012

# serve.py
import agent
rulebased_agent = agent.RuleBasedAgent()
rulebased_agent.serve("127.0.0.1:9090", 1, 0, 0)
# serve2.py
import agent
human_control_agent = agent.HumanControlAgentOnBrowser()
human_control_agent.serve("127.0.0.1:9091", 1, 0, 0)
# run.py
import mjx
agents = {
    "player_0": "127.0.0.1:9091",  # human control
    "player_1": "127.0.0.1:9090",
    "player_2": "127.0.0.1:9090",
    "player_3": "127.0.0.1:9090",
}
mjx.run(agents, 1, 1, 1)
  1. python serve.py
  2. python serve2.py
  3. python run.py
  4. YOUR TURNと表示されたら http://127.0.0.1:5000/ へアクセス

@OkanoShinri
Copy link
Collaborator Author

demo

@OkanoShinri
Copy link
Collaborator Author

OkanoShinri commented Nov 12, 2021

修正予定

  • ShowPagesのクラス変数はインスタンス変数でもよいはず 無理っぽかったのでそのまま
  • Queue周りの遅延を調べ、sleepを減らす
  • ダミーは表示しないようにする
  • コメント消す
  • htmlをもう少しいい感じにする(特にフォント)
  • 選択肢をソート

@OkanoShinri
Copy link
Collaborator Author

OkanoShinri commented Nov 16, 2021

  • 赤牌の区別

@OkanoShinri
Copy link
Collaborator Author

進捗&デザイン

Screenshot 2021-11-16 at 21-46-50 Mjx Visualizer

@sotetsuk
Copy link
Collaborator

動きました! @OkanoShinri
選択肢のデザインは色々選択肢があるかと思うのですが、ひとまずこれでいい気はします
(あとは右隣とかに縦に列挙するとかがあるかなとは思います)

理想をいうと、選択肢は現在の選択がハイライトされていて、十字キーなどで選んでエンターで選ばれるのがいい気がします。
クリック時の挙動は現在のままでいいと思います!

@sotetsuk
Copy link
Collaborator

あと、リソース系(index.html, ttf, css)は全部同じ一箇所にまとめてくくれると楽なのでお願いします

  • static/index.html
  • static/xx.css
  • static/xx.tff

など

@OkanoShinri
Copy link
Collaborator Author

あと、リソース系(index.html, ttf, css)は全部同じ一箇所にまとめてくくれると楽なのでお願いします

* static/index.html

* static/xx.css

* static/xx.tff

など

これFlaskの仕様で、htmlとかcssをこの通りに置かないと動かないのです…

@OkanoShinri
Copy link
Collaborator Author

赤牌の区別

Screenshot 2021-11-17 at 20-15-59 Mjx Visualizer

@OkanoShinri
Copy link
Collaborator Author

demo

@sotetsuk
Copy link
Collaborator

いい感じですね!あとは細かい点しかないかもしれない

  • ツモ切りが一番上にくると嬉しいか
  • キーの長押しで連続で動けると嬉しいか
  • エンター押したあと、とりあえず選択肢が消えると安心するか(今は選ばれてるのか一瞬疑問に思う)

@sotetsuk
Copy link
Collaborator

Screenshot from 2021-11-17 21-24-43

赤が反映されてない?

@OkanoShinri
Copy link
Collaborator Author

OkanoShinri commented Nov 17, 2021

TODO

  • ツモ切り一番上
  • キーの長押し
  • 決定後選択肢を消す
  • 鳴きの赤牌

@OkanoShinri
Copy link
Collaborator Author

OkanoShinri commented Nov 24, 2021

次回までに

  • Post/Redirect/Get (PRG) パターンの実装

流れ

@OkanoShinri
Copy link
Collaborator Author

処理の流れ

(黒丸:mjx側、白丸:flask側)

  • act関数実行時にobservationが渡される
  • observationをQueueに入れ、task_doneまでブロック
    • Queueからobservationを取り出し、task_done
    • 表示用ページへリダイレクト(≒ページ表示)
  • Queueからactionを取り出すまでブロック
    • POST経由でactionを受け取る
    • actionをQueueに入れ、task_doneまでブロック
  • Queueからactionを取り出し、task_done
  • actionをreturnしてact関数を抜ける
    • Queueからobservationを取り出すまでブロック

何回か計測した感じではHTTPリクエストから表示すべきobservation取得までは0.1秒以下で済んでいるので、あとは通信速度を速める等か

case "ArrowUp":
case "ArrowLeft":
case "j":
keydown_counters[0]++;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この keydown_counters っていります?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

連続で押したときのため?

super().__init__()
self.q = Queue()

self.sub = threading.Thread(target=self.flask, args=(self.q,))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

threadingよりmultiprocessingの方がよいと思う(PythonのthreadはGILで止まると思う)

mjx/agent.py Outdated
page2 = PostPage()
ShowPage.q = q
app = Flask(__name__)
app.config["SECRET_KEY"] = "8888"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SECRET_KEYは何のためにある?

@OkanoShinri
Copy link
Collaborator Author

revertがうまく行かなかったのでresetで戻してしまいました。

mjx/agent.py Outdated
@@ -156,6 +165,113 @@ def act(self, observation: Observation) -> Action: # type: ignore
)


class ShowPage(View):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このクラスは mjx/visualizer 以下とかに移しますか

@@ -0,0 +1,93 @@
<!doctype html>
<html lang="ja">
Copy link
Collaborator

@sotetsuk sotetsuk Dec 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これもstatic以下に移しましょう

mjx/templates/index.html => mjx/static/index.html

@sotetsuk
Copy link
Collaborator

sotetsuk commented Dec 8, 2021

あとはmasterをこのPRにマージして、動作に問題ないか確認もお願いします

@sotetsuk
Copy link
Collaborator

sotetsuk commented Dec 10, 2021

なんか常に一段目までで止まっちゃうんですけど、そんなことないです? @OkanoShinri

@sotetsuk
Copy link
Collaborator

sotetsuk commented Dec 15, 2021

やっぱり固まるなあ。。。(タブがローディングで止まってる)

image

YOUR TURNは表示されている

image

@OkanoShinri
Copy link
Collaborator Author

実行が遅いとか固まる系の問題はmultiprocessingを導入することで解決されるかもしれないですが、よく分からないエラーが出て止まってしまいます。

from multiprocessing import Pipe, Process
class MinimumWebAgent(Agent):  # type: ignore
    def __init__(self) -> None:
        super().__init__()
        self.conn, child_conn = Pipe()
        self.sub = Process(target=self.hoge, args=(child_conn,), daemon=True)

    def act(self, observation: Observation) -> Action:
        print("main: YOUR TURN")
        self.sub.start()
        print("main: Sub prosess started")
        self.conn.send(observation)
        action = self.conn.recv()
        return action

    def hoge(self, conn):
        print("sub: Waiting...")
        observation = conn.recv()
        print("sub: Recieved!")
        action = Action()
        conn.send(action)

に対して他のAgentと同じように動かすと

127.0.0.1:9091
main: YOUR TURN
main: Sub prosess started
sub: Waiting...
terminate called without an active exception
Aborted

sub: Recieved!が出ないので、プロセス間で何かをやり取りするとダメみたいです

@OkanoShinri
Copy link
Collaborator Author

  • threading x Queue 動くが遅い&固まる(条件不明)
  • threading x multiprocessing.Pipe 動かない(Agentのコンストラクタは抜けているが、actが呼び出されない?)
  • multiprocessing x Queue 動かない(actまで呼び出されるが、プロセス間でデータをやり取りできない)
  • multiprocessing x multiprocessing.Pipe 上と同じ

@OkanoShinri
Copy link
Collaborator Author

あとterminate called without an active exceptionでググってもC++ばっかり出てくる(std::threadのエラーコードみたい)ので、mjxのC++部分が関係しているんだろうなあというざっくりした考えです

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants