修改 c++ 中的模拟时间

发布于 2024-09-16 09:26:25 字数 134 浏览 8 评论 0原文

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

谢谢,

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

i am writing a program which simulates an activity, i am wondering how to speed up time for the simulation, let say 1 hour in the real world is equal to 1 month in the program.

thank you

the program is actually similar to a restaurant simulation where you dont really know when customer come. let say we pick a random number (2-10) customer every one hour

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

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

发布评论

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

评论(5

娇俏 2024-09-23 09:26:25

这取决于现在如何获得时间。

例如,如果它调用 Linux 系统 time(),只需将其替换为您自己的函数(如 mytime)即可返回更快的时间。也许 mytime 调用 time 并将返回的时间乘以任何有意义的因子。 1小时=1个月就是720次。应考虑在程序开始时处理原点:

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
}

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
}
酒几许 2024-09-23 09:26:25

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

You just do it. You decide how many events take place in an hour of simulation time (eg., if an event takes place once a second, then after 3600 simulated events you've simulated an hour of time). There's no need for your simulation to run in real time; you can run it as fast as you can calculate the relevant numbers.

别忘他 2024-09-23 09:26:25

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

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

It sounds like you are implementing a Discrete Event Simulation. You don't even need to have a free-running timer (no matter what scaling you may use) in such a situation. It's all driven by the events. You have a priority queue containing events, ordered by the event time. You have a processing loop which takes the event at the head of the queue, and advances the simulation time to the event time. You process the event, which may involve scheduling more events. (For example, the customerArrived event may cause a customerOrdersDinner event to be generated 2 minutes later.) You can easily simulate customers arriving using random().

The other answers I've read thus far are still assuming you need a continuous timer, which is usually not the most efficient way of simulating an event-driven system. You don't need to scale real time to simulation time, or have ticks. Let the events drive time!

谁许谁一生繁华 2024-09-23 09:26:25

如果模拟依赖于数据(如股票市场程序),只需加快数据抽取的速度即可。如果有人认为这取决于 time() 调用,您将不得不做一些类似 wallyk 的回答的事情(假设您有源代码)。

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).

网名女生简单气质 2024-09-23 09:26:25

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

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

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

If time in your simulation is discrete, one option is to structure your program so that something happens "every tick".
Once you do that, time in your program is arbitrarily fast.

Is there really a reason for having a month of simulation time correspond exactly to an hour of time in the real world ? If yes, you can always process the number of ticks that correspond to a month, and then pause the appropriate amount of time to let an hour of "real time" finish.

Of course, a key variable here is the granularity of your simulation, i.e. how many ticks correspond to a second of simulated time.

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