我正在尝试实现一个从视图导出的输出。我从此链接获得了帮助:如何导出 NetLogo 6.2 的世界,仅对出生海龟的补丁进行着色? 我非常感谢 Luke C 的帮助,因为我成功实现了我想要的部分内容。
但是,我仍然没有成功地通过海龟轮廓生成图像。我有 16 个海龟档案(请参阅 ValidHabs)。
例如,我想为 16 个海龟配置文件中的每一个导出图像。
例如海龟档案1(只能在栖息地覆盖物1中出生和孵化),所以:
At tick 0, the world started with: 1 turtle no (patch 15 -4),
At tick 1, there are 2 turtles in patches (patch 15 -4) and (patch 14 -8) and
At tick 2, there are 4 turtles in patches (patch 15 -4), (patch 14 -8), (patch 12 -5) and (patch 17 -1).
因此,对于海龟轮廓 1,您将得到一个白色导出图像,其中色块(色块 15 -4)、(色块 14 -8)、(色块 12 -5)和(色块 17 -1)被涂成洋红色。 然后我会将图像导出到海龟 2 的个人资料等...直到我到达只能在栖息地覆盖物 4 和 5 中生成的海龟 16 的个人资料。
我尝试使用 foreach 将图像导出到每个配置文件,但发生的情况是打开单个 .png 文件并在该单个文件中生成结果(“覆盖”)。我想要的是为每个海龟配置文件生成 1 个 .png 格式的文件。我认为使用 foreach 并小心地我可以生成文件。但到目前为止我还没能做到。
代码:
globals [ ListProfiles ]
patches-own [ turtle-born-here ]
turtles-own [ all-code metabolism reproduction code-metabolism code-reproduction ]
to setup
ca
set ListProfiles [ [1] [2] [3] [4] [5] ]
ask patches [ set turtle-born-here false ]
ask n-of 5 patches [
sprout 1
setup-turtles
set turtle-born-here true
]
reset-ticks
end
to go
ask turtles [
rt random 60 - 30
fd 1
if random-float 1 < 0.05 [
hatch 1
ask patch-here [ set turtle-born-here true]
]
]
end
to setup-turtles
ask turtles [
(
ifelse
metabolism = 2 [set code-metabolism "M1"]
)
(
ifelse
reproduction = 5 [set code-reproduction "R1"]
)
set all-code ( word code-metabolism code-reproduction )
]
end
to example-export-image
setup
; Some example go runs
repeat 50 [ go ]
; This is the export-view component
cd
ask turtles [
ht
]
ask patches [
ifelse turtle-born-here
[ set pcolor magenta ]
[ set pcolor white ]
]
carefully
[ file-delete ( "View.png" ) ]
[ ]
foreach ListProfiles
[
y ->
let k turtles with [ all-code = y ]
ask k [
export-view ( "View.png" )
]
]
end
就目前情况而言,您提供的代码远远超过了问题本身实际需要的代码,对我来说,这可以归结为:“如何更改多个实验/模拟的文件名?”这个问题之前已经被回答过 以不同的方式 可能适合您的需求。
对于一个独立的示例,这实际上只是上面第一个链接中 Charles 答案的改进:
patches-own [ hab-type turtle-born-here ]
globals [ profiles ]
to setup
ca
resize-world 0 29 0 29
set profiles [ "a" "b" "c" ]
ask patches [
( ifelse pxcor < ( max-pxcor / 3) [
set hab-type "a"
] pxcor < ( max-pxcor * 2 / 3 ) [
set hab-type "b"
] [
; else
set hab-type "c"
]
)
]
end
to fake-simulate
foreach profiles [ profile ->
ask turtles [ die ]
ask patches [
set pcolor black
set turtle-born-here false
]
ask n-of 5 patches with [ hab-type = profile ] [
sprout 1
set turtle-born-here true
]
reset-ticks
repeat 50 [
ask turtles [
rt random 60 - 30
fd 1
if random-float 1 < 0.1 and [ hab-type ] of patch-here = profile [
hatch 1
ask patch-here [ set turtle-born-here true]
]
]
tick
]
export-image profile
]
end
to export-image [ unique-id ]
; Prepare the view for export
ask turtles [ ht ]
ask patches [
ifelse turtle-born-here
[ set pcolor magenta ]
[ set pcolor white ]
]
; Use word to create a file name
let out-file ( word "example_view_export_" unique-id "_.png" )
export-view out-file
end
输出类似: