在NetLogo中遍历两个列表:大图上下文:R扩展。将补丁值分配给R中的特定补丁

时间:2019-07-11 20:49:38

标签: r netlogo

我需要使用R将数据从NetLogo传递到R-extension,进行一些计算,然后将R的输出分配给特定的补丁。

我尝试了mapforeachNetLogo的每一种组合,这些都可以想象得到。我一直在尝试根据补丁ID更改颜色,因为它是可见的。将所有补丁设置为蓝色有效。

observer> foreach [1 2 3] [ x -> ask patches with [id = x] [ set pcolor blue ] ]

当我添加第二个列表时,没有任何尝试过。我也尝试将值分配给补丁无济于事。这使补丁4、5和6变成红色...

observer> foreach [4 5 6] [ x -> ask patches with [id = x] [ set pcolor [blue red green] ]]

下一种方法无效。

observer> set list-a (list blue red green)
observer> set list-b (list 7 8 9)
observer> ask patches with [id = list-b] [ set pcolor list-a]

尝试使用以下两个列表进行一次foreach:

observer> foreach list-b list-b ask patches with [id = list-b] [set pcolor list-a]
observer> show map list-b list-b ask patches with [id = list-b] [set pcolor list-a]

重新定义list-a,然后再次尝试原始方法会将这些补丁设置为蓝色。

observer> set list-a r:get "df1[[1]]"
observer> show list-a
observer: [1 5 8]
observer> foreach list-a [ x -> ask patches with [id = x] [ set pcolor blue ] ]

如果我仅能理解如何处理两个列表,就会被设置。

例如

list-a [1 2 3]
list-b [blue green red]

; effectively do the following

ask patch with [id = 1] [set pcolor blue]
ask patch with [id = 2] [set pcolor green]
ask patch with [id = 3] [set pcolor red]

实际的问题是我有活动变量(下面的所有补丁都在种植玉米)。

> a
   id a_corn a_crp a_soy
1   1      0     1     0
2   2      0     1     0
3   3      0     1     0
4   4      0     1     0
5   5      0     1     0
6   6      0     1     0
7   7      0     1     0
8   8      0     1     0
9   9      0     1     0
10 10      0     1     0
> 

我知道r扩展数据帧作为未命名的嵌套列表进入netlogo。因此,如果我避免嵌套:

set list-id r:get "a$id"
set list-a r:get "a$a_corn"
; then netlogo iterates through the lists:
ask patch with [id = 1] [set a_corn 0]
ask patch with [id = 2] [set a_corn 0]
; and so on

我愿意提出更好的建议。但是ID需要匹配,可能不是顺序的,并且上面的数据帧的大小和内容会有所不同。

更新: 在继续解决问题时,我正在此处记录这些步骤,以便稍后查找。

我找到了匿名命令。

observer> set procedure [[x y] -> ask patches with [id = x] [set pcolor y]]
observer> show  is-anonymous-command? procedure
observer: true
observer> set list-a (list blue red green)
observer> set list-b (list 7 8 9)
observer> (foreach list-b list-a procedure) ; does nothing

我清除了所有内容,然后重新设置,以下工作正常:

observer> ask patches with [id = 7] [set pcolor red]
observer> set procedure [[x y] -> ask patches with [id = x] [set pcolor y]]
observer> set list-b (list 7 8 9)
observer> set list-a (list blue red green)
observer> (foreach list-b list-a procedure)

现在是问题的应用部分:

observer> set procedure [[x y] -> ask patches with [id = x] [set a_corn y]]
observer> set list-a (list 1 1 1)
observer> (foreach list-b list-a procedure)

它有效!

0 个答案:

没有答案