如何在 Haskell 中创建两个具有内部状态的共同生产者/消费者?

发布于 2024-07-18 13:38:40 字数 273 浏览 4 评论 0原文

我有一个代理,它接受状态并返回操作,同时保留状态/操作对的效用的内部表示。 我还有一个接受操作并返回状态/奖励对的环境。

我需要能够将代理设置为启动状态,然后连续从代理 -(action)-> 环境-(状态,奖励)-> 代理-(动作)->... 但是,内部状态(每次迭代都需要更新)需要保持私有(即在代理或环境内)。 这意味着我不能简单地将环境作为代理内的函数调用,使用状态和操作作为参数。

我有点像 Haskell 菜鸟,所以我什至不确定这是否可能。

I've got an agent that takes in states and returns actions, while keeping an internal representation of the utility of state/action pairs. I've also got an environment that takes in actions and returns state/reward pairs.

I need to be able to set the agent up with a start state and then continuously go from agent -(action)-> environment -(state, reward)-> agent -(action)->...
However, the internal states (which need to be updated every iteration) need to stay private (that is, within the agent or the environment). This means that I can't simply call environment as a function within the agent using state and action as arguments.

I'm somewhat of a Haskell noobie, so I'm not even sure if this is possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二智少女 2024-07-25 13:38:40

两个问题:

  • 如果代理必须使用状态来计算操作,那么您如何期望对代理保密状态的表示?

  • 如果环境期望在给定一个动作的情况下产生一个状态(和奖励),那么您如何期望对环境保密状态的表示?

这两种方法都是可能的,但每种方法都必须涉及某种用于查询状态或创建状态的抽象。 我对这个设计没有什么好感。

将有助于澄清问题

  • 通过

    为感兴趣的函数提供类型签名

  • 识别您想要表示状态的函数不 要公开。

PS 这些困难与 Haskell 完全分离,并且无论选择何种实现语言(只要实现语言支持某种形式的隐私)都会成为问题。

Two questions:

  • If the agent must use a state to compute an action, then how do you expect to keep the representation of states secret from the agent?

  • If the environment expects to produce a state (and a reward) given an action, how do you expect to keep the representation of states secret from the environment?

Both of these are possible, but each must involve some sort of abstraction for querying states or creating states. I don't have a good feeling about this design.

It would help to clarify the question by

  • Providing type signatures for the functions of interest

  • Identifying to which functions you want the representation of states not to be exposed.

P.S. These difficulties are quite separable from Haskell and would be at issue regardless of the choice of implementation language (provided that the implementation language supports some form of privacy).

浮光之海 2024-07-25 13:38:40

您需要决定代理和环境中的哪一个位于“顶部” - 让我们假设这个答案的其余部分是顶部的环境调用代理,因为这通常是最有意义的。

您可以使用模块系统将代理的数据表示形式保持为私有 - 只需导出数据类型名称而不包含其任何内部信息。

module Agent (AgentState, other_stuff) where

与如果代理还需要传递环境的状态相反

module Agent (AgentState(..), other_stuff) where

(尽管我看不出有任何理由这是必要的,因为环境可以自己跟踪它),那么使代理函数具有多态性,以便它们可以传递任何状态类型 - 那么环境可以传递它喜欢的任何内容,而无需暴露其表示形式。

还应该可以使用状态单子来实现对状态发生的情况的更多控制,例如防止环境复制代理提供的状态并使用相同的状态重复调用代理,但如果您不熟悉Haskell 最好先获得一些没有 monad 的经验。 (并不是说单子特别可怕或者什么,但它们确实向你隐藏了细节,所以很难看到正在发生的事情。)

You'll need to decide which of the agent and the environment sits "on top" - let's suppose for the rest of this answer that it's the environment on top calling into the agent since that generally makes most sense.

You can keep data representations of the Agent private using the module system - just export the datatype name without any of its internals.

module Agent (AgentState, other_stuff) where

as opposd to

module Agent (AgentState(..), other_stuff) where

If the agent also needs to be passed the environment's state (though I can't see any reason this would be necessary as the environment can keep track of it for itself), then make the agent functions polymorphic so that they can be passed any state type - then the environment can pass whatever it likes in without its representation being exposed.

It should also be possible to use state monads to achieve more control over what happens to the state, for example preventing the environment from duplicating a state the agent gives it and calling the agent repeatedly with that same state, but if you're new to Haskell it's probably best to get a bit of experience without monads first. (Not that monads are particularly scary or anything, but they do hide detail from you so it's harder to see what's going on.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文