在月底执行每月任务,并使用netlogo时间:schedule-repeating-event-with-period

问题描述 投票:0回答:1

我需要每月在月底运行一次程序。我还没有找到具体的原语,所以我找到了这段代码。

我可以通过在界面中输入日期来开始我的模拟,全局是

start-date-default

因此,我实例化模型并将其锚定到该开始时间。

;; holds last day for each month
set month-end table:make
table:put month-end 1 31
table:put month-end 2 28
table:put month-end 3 31
table:put month-end 4 30
table:put month-end 5 31
table:put month-end 6 30
table:put month-end 7 31
table:put month-end 8 31
table:put month-end 9 30
table:put month-end 10 31
table:put month-end 11 30
table:put month-end 12 31

set start-date time:create-with-format start-date-default "dd-MM-YYYY"
set tick-date time:anchor-to-ticks start-date 1 "day"
time:anchor-schedule start-date 1 "day"

;; to find end of month for the select start month I do
let month time:get "month" tick-date
let day table:get month-end month
let year time:get "year" tick-date
let end-of-month time:create-with-format (word day ifelse-value month < 10 ["-0"]["-"] month "-" year) "dd-MM-YYYY"

此时发生了一些奇怪的事情。如果我每两个月运行一次该程序,例如:

time:schedule-repeating-event-with-period (one-of org) [ -> pay-bills ] end-of-month 2 "month"

如果我运行了 11 个月,我正确地得到了以下输出:

(org 0): "monthly pay-bills{{time:logotime 31-01-2023}}"
(org 0): "monthly pay-bills{{time:logotime 31-03-2023}}"
(org 0): "monthly pay-bills{{time:logotime 31-05-2023}}"
(org 0): "monthly pay-bills{{time:logotime 31-07-2023}}"
(org 0): "monthly pay-bills{{time:logotime 30-09-2023}}"
(org 0): "monthly pay-bills{{time:logotime 30-11-2023}}"

但是,如果我每月运行该程序,例如:

time:schedule-repeating-event-with-period (one-of org) [ -> pay-bills ] end-of-month 1 "month"

然后我有这个奇怪的输出:

(org 0): "monthly pay-bills{{time:logotime 31-01-2023}}"
(org 0): "monthly pay-bills{{time:logotime 28-02-2023}}"
(org 0): "monthly pay-bills{{time:logotime 28-03-2023}}"
(org 0): "monthly pay-bills{{time:logotime 28-04-2023}}"
(org 0): "monthly pay-bills{{time:logotime 28-05-2023}}"
(org 0): "monthly pay-bills{{time:logotime 28-06-2023}}"
(org 0): "monthly pay-bills{{time:logotime 28-07-2023}}"
(org 0): "monthly pay-bills{{time:logotime 28-08-2023}}"
(org 0): "monthly pay-bills{{time:logotime 28-09-2023}}"
(org 0): "monthly pay-bills{{time:logotime 28-10-2023}}"
(org 0): "monthly pay-bills{{time:logotime 28-11-2023}}"

我想我错过了一些东西...我不明白为什么这一切都发生在第二个月后的28...

netlogo
1个回答
0
投票

我不知道为什么一个月的安排不起作用;如果没有更多信息,它看起来可能是计划重复事件中的错误。

您可以通过每天执行一条语句来解决这个问题,如下所示:

if (time:difference-between last-pay-time tick-date "months") = 2
[
 pay-bills
 set last-pay-time time:copy tick-date
]
© www.soinside.com 2019 - 2024. All rights reserved.