将变量放入表时出错,只允许常量?

发布于 2024-09-04 16:38:28 字数 1503 浏览 8 评论 0原文

目前我正在开发一个 Netlogo 程序,我需要使用节点和链接来解决车辆路由问题。 (链接在程序中称为街道)

这里我有一些实际问题,如何在具有另一个节点的表中输入变量链接速度。像 200 等常数就可以了。我在网上找到了一些使用变量的示例,但我不知道为什么我不断收到以下错误:

期望一个常数。

(或者为什么 netlogo 需要一个常量)

这是相关的代码片段:

extensions [table]
streets-own [linkspeed linktoll] 
nodes-own [netw]

;; In another piece of code linkspeed is assigned successfully to the links

to cheapcalc

  ;; start conditions set costs very high 300000

  ;; state 3 unsearched state 2 searching state 1 searched (for later purposes)

  ask nodes [ 

    set i 0 set j count nodes set netw table:make


    while [i < j][


    table:put netw (i) [3000000 3]   set i (i + 1)]]  


  set i 0 let k 0

  ask node 35      ;; here i use node 35 as an example. 

                   ;; node 35 is connected to node 34, 36, 20 and 50

     [table:put netw (35) [0 1]   ;; node need to search costs to travel to itself 

                                   ;; putting constants is ok. 

     while [i < j]

        [ask my-links 

           [ask both-ends 

              [if (who != 35) [set color blue     

;;               set temp ([linkspeed] of street 35 who)    ;; here my real goal is to put this in stat of i. but i is easier than linkspeed. 

                 table:put netw (who) [ i 2 ]

                 ]              

           ]  ]


      set i (i + 1)] ] ;; next node for later, no it is just repetition of the same. 


end

我希望有人知道发生了什么......

Currently I am working on a Netlogo program where I need to use nodes and links for vehicle routing problem. (links are called streets in the program)

Here I have some practical problems of how to input variable linkspeed in a table with another node. Constants like 200 etc are fine. Online I found some examples where variables are used, but I do not know why I keep getting the following error:

Expected a constant.

(or why netlogo expects a constant)

Here is the relevant piece of code:

extensions [table]
streets-own [linkspeed linktoll] 
nodes-own [netw]

;; In another piece of code linkspeed is assigned successfully to the links

to cheapcalc

  ;; start conditions set costs very high 300000

  ;; state 3 unsearched state 2 searching state 1 searched (for later purposes)

  ask nodes [ 

    set i 0 set j count nodes set netw table:make


    while [i < j][


    table:put netw (i) [3000000 3]   set i (i + 1)]]  


  set i 0 let k 0

  ask node 35      ;; here i use node 35 as an example. 

                   ;; node 35 is connected to node 34, 36, 20 and 50

     [table:put netw (35) [0 1]   ;; node need to search costs to travel to itself 

                                   ;; putting constants is ok. 

     while [i < j]

        [ask my-links 

           [ask both-ends 

              [if (who != 35) [set color blue     

;;               set temp ([linkspeed] of street 35 who)    ;; here my real goal is to put this in stat of i. but i is easier than linkspeed. 

                 table:put netw (who) [ i 2 ]

                 ]              

           ]  ]


      set i (i + 1)] ] ;; next node for later, no it is just repetition of the same. 


end

I hope somebody knows what is going on...

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

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

发布评论

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

评论(1

意中人 2024-09-11 16:38:28

问题很可能不是将变量放入表中,而是将变量放入列表中(然后将其放入表中)。

将下面的行:更改

     table:put netw (who) [ i 2 ]

为:

     table:put netw (who) (list i 2)

This - (list i 2) - 允许您生成其中包含变量的列表,您不能以其他方式执行此操作 - [i 2]。

希望这有帮助。

The problem is most likely not putting a variable in a table, but putting a variable in a list (which you're then putting in a table).

Change the line below:

     table:put netw (who) [ i 2 ]

to:

     table:put netw (who) (list i 2)

This - (list i 2) - allows you to generate a list with variables in it, you can't do it the other way - [i 2].

Hope this helps.

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