我正在构建一个多智能体模拟使用Akka,因此,希望运行比实时更快的模拟。具体来说,我想配置Akka调度器,这样调度器就可以从一个调度事件前进到另一个调度事件(这显然可能涉及到事件之间的时间步骤),而不是通过一些底层的固定时间步骤进行推进。
换句话说,我希望调度程序的行为就好像它是一种优先级队列,在这种队列中,优先级由事件的模拟时间戳提供。
明白了吗?如果是这样的话,我是否可以使用Actor系统的默认调度程序来执行?如果这是不可能的,那么我将如何使用现有的Akka组件来滚动我自己的调度程序来完成这个任务。
发布于 2015-11-25 13:12:03
我不认为这是可能的阿克卡调度程序。来自文档 (重点雷):
有时候,在未来发生事情的需要会出现,那么你会去哪里呢?只看ActorSystem!在这里,您可以找到返回akka.actor.Scheduler实例的调度程序方法,这个实例在每个ActorSystem中都是唯一的,并在内部中用于调度在特定时间点发生的事情。
但是,您始终可以使用递归函数完成相同的任务。假设您的“实时”功能如下所示:
def periodicFunction() : Unit = ??? //whatever you're doing to Agents
//periodicFunction is called every 10 seconds
actorSystem.scheduler().schedule(0 seconds, 10 seconds)(periodicFunction())您的模拟代码可以简单地是:
@scala.annotation.tailrec
def fasterThanRealTimeLoop(n : Int) =
if(n > 0) {
periodicFunction()
fasterThanRealTimeLoop(n-1)
}然后,您可以模拟20次运行
fasterThanRealTimeLoop(20)可以进一步封装此功能,以封装这两种可能性:
val realtimeMode : Boolean = ??? //some configuration setting
val periodicArgs : Either[FiniteDuration, Int] =
if(realtimeMode) Left(10 Seconds) else Right(20)
periodicArgs.left.foreach { period =>
actorSystem.scheduler().schedule(0 seconds, period)(periodicFunction())
}
periodicArgs.right.foreach { count =>
fasterThanRealTimeLoop(count)
}此代码现在将根据配置设置调用正确类型的循环(定时的或尽可能快的)。
https://stackoverflow.com/questions/33915856
复制相似问题