模拟异步游戏环境

发布于 2024-12-07 10:34:45 字数 761 浏览 0 评论 0原文

这个想法是为代理建立一个环境模型。在最基本的情况下,它看起来像这样:

  1. 系统要求代理执行下一步操作
  2. 代理响应(例如“向左移动!”)
  3. 系统将代理移动到适当的状态

但是,我在以异步方式实现这一点时遇到了麻烦(使用线程等)。

目前我的系统看起来像这样:

void Start(){
   while(true && !gameOver){
       askAgent()
       moveAgent()

       if(agentState == terminalState){ 
          gameOver = True;
       }

   }
 }

显然,这会阻塞正在运行的线程。 (更尴尬的是我正在使用 OSGi,所以任何单个包都不应该占用所有处理时间!)

另外,我希望系统能够对环境中出现的新代理做出反应,并与它们交互(我的运行时,OSGi ,已经具备了在系统中出现或消失某些内容时通知我的功能),例如:

void setAgent(Agent agent){
       system.addAgentToEnvironment(agent);
       system.simulateAgent(agent);
}

而不是直接从 main 运行...

我知道这非常令人困惑,而且我不确定我是否提出了这个问题正确 - 所以任何我非常感谢有关我可以查看的架构或方法的建议。

The idea is to model an environment for agents. In the most basic case it looks likes this:

  1. System asks the agent for the next action
  2. Agent responds (e.g. "move left!")
  3. System moves the agent to the appropriate state

However, I am having trouble implementing this in an asynchronous manner (with threading and such).

Currently my system looks like this:

void Start(){
   while(true && !gameOver){
       askAgent()
       moveAgent()

       if(agentState == terminalState){ 
          gameOver = True;
       }

   }
 }

Clearly, this blocks the thread this is running on.
(What's more embarrassing is I am using OSGi, so any single bundle should not be hogging all the processing time!)

Also, I would like the system to react to new agents appearing in the environment, and engage with them (my runtime, OSGi, already has the facility of notifying me if something appears or disappears from the system) something like:

void setAgent(Agent agent){
       system.addAgentToEnvironment(agent);
       system.simulateAgent(agent);
}

Instead of just running from main straight away...

I know this is very confusing, and I am not sure if I am even posing the question correctly - so any tips on the architecture or approaches I can look at are greatly appreciated.

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

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

发布评论

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

评论(2

墨落成白 2024-12-14 10:34:45

您肯定需要一些数据保护(可能在代理主列表上,以及对每个单独代理及其数据的某种保护)。

除此之外,我会遵循这种模型:

while (waiting for events)
  spawn thread to respond to event // add agent, calculate and perform move, etc.
  // even better would be to enqueue the event into a thread pool

  if (terminal)
    break // end game

HTH

You will definitely need some data protection (perhaps on a master list of agents, and some kind of protection on each individual agent and its data).

Other than that, I would follow this kind of model:

while (waiting for events)
  spawn thread to respond to event // add agent, calculate and perform move, etc.
  // even better would be to enqueue the event into a thread pool

  if (terminal)
    break // end game

HTH

多孤肩上扛 2024-12-14 10:34:45

为了帮助思考应用程序的未来,我强烈建议您使用两个循环。

long then = System.currentTimeMillis();
for(Agent a : agents) {
    agent.calcuateNextMove(getEnvironment());
}

for(Agent a : agents) {
    agent.performNextMove(getEnvironment());
}
long now = System.currentTimeMillis();
sleep(TIME_STEP_DURATION + now - then); // give you a steady frame rate in relation to real time

这个片段给了你两件事。

  1. 移动是独立于同一步骤上的其他移动进行的。这样,您当前的行动就不会受到那些碰巧在您之前行动过的人的影响。
  2. 智能体只是存在,只是被告知根据你给它的环境计算他的下一步行动。这使得改变状态、将代理复制到多个环境中以及给人一种环境与实际不同的错觉变得异常容易。例如,您可能有一个 filterFor(Environment e, Agent a) 来为该特定代理制作环境的模拟版本。比如戴醉酒护目镜或眼罩之类的。

In order to help think about the future of the application, I would urge you to use two loops.

long then = System.currentTimeMillis();
for(Agent a : agents) {
    agent.calcuateNextMove(getEnvironment());
}

for(Agent a : agents) {
    agent.performNextMove(getEnvironment());
}
long now = System.currentTimeMillis();
sleep(TIME_STEP_DURATION + now - then); // give you a steady frame rate in relation to real time

This snippet gives you two things.

  1. Moves are made independently of other moves on the same step. This way you do not have your current move influenced by those who happened to move before you.
  2. An agent merely exists, and is simply told to calculate his next move based on the environment you give it. This makes it incredibly easy to change states, copy agents into multiple environments, and give the illusion that the environment is different than it really is. For example, you may have a filterFor(Environment e, Agent a) that makes a mocked up version of the environment for that particular agent. Like wearing drunk-goggles or a blindfold or something.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文