0

我正在编写一个模拟活动的程序,我想知道如何加快模拟时间,假设现实世界中的 1 小时等于程序中的 1 个月。

谢谢你

该程序实际上类似于您不知道顾客何时来的餐厅模拟。假设我们每隔一小时挑选一个随机数(2-10)个客户

4

5 回答 5

2

It depends on how it gets time now.

For example, if it calls Linux system time(), just replace that with your own function (like mytime) which returns speedier times. Perhaps mytime calls time and multiplies the returned time by whatever factor makes sense. 1 hr = 1 month is 720 times. Handling the origin as when the program begins should be accounted for:

time_t t0;
main ()
{
     t0 = time(NULL);    // at program initialization

     ....

     for (;;)
     {
           time_t sim_time = mytime (NULL);
           // yada yada yada
           ...
     }
}

time_t mytime (void *)
{
     return 720 * (time (NULL) - t0);   // account for time since program started
                                        // and magnify by 720, so one hour is one month
}
于 2010-08-29T06:42:33.357 回答
1

你就去做吧。您决定在一个小时的模拟时间内发生多少事件(例如,如果一个事件每秒发生一次,那么在 3600 个模拟事件之后,您已经模拟了一个小时的时间)。您的模拟无需实时运行;您可以尽可能快地计算相关数字。

于 2010-08-29T06:36:44.380 回答
1

听起来您正在实施Discrete Event Simulation。在这种情况下,您甚至不需要一个自由运行的计时器(无论您使用什么缩放)。这一切都是由事件驱动的。您有一个包含事件的优先级队列,按事件时间排序。您有一个处理循环,它将事件置于队列的头部,并将模拟时间提前到事件时间。您处理事件,这可能涉及安排更多事件。(例如,该customerArrived事件可能会导致customerOrdersDinner2 分钟后生成事件。)您可以使用random().

到目前为止,我阅读的其他答案仍然假设您需要一个连续计时器,这通常不是模拟事件驱动系统的最有效方法。您不需要将实时时间缩放到模拟时间,也不需要滴答声。让事件驱动时间!

于 2010-08-29T07:29:45.270 回答
0

If the simulation is data dependent (like a stock market program), just speed up the rate at which the data is pumped. If it is some think that depends on time() calls you will have to do some thing like wallyk's answer (assuming you have the source code).

于 2010-08-29T06:49:08.643 回答
0

如果您的模拟时间是离散的,则一种选择是构建您的程序,以便“每个滴答”都发生一些事情。一旦你这样做了,你的程序中的时间就会任意快。

真的有理由让一个月的模拟时间与现实世界中的一小时时间完全对应吗?如果是,您始终可以处理与一个月相对应的滴答数,然后暂停适当的时间以让一小时的“实时”完成。

当然,这里的一个关键变量是模拟的粒度,即模拟时间对应于多少滴答声。

于 2010-08-29T07:23:27.173 回答