如何在 NetLogo 6.2 中仅从初始刻度和最终刻度生成包含海龟数量值的输出?
How to generate an output with values of the amount of turtles only from the initial tick and from the final tick in NetLogo 6.2?
我想生成一个包含三列的输出。一列海龟的个人资料代码(code_profile)。另一列表示该配置文件的初始海龟数量(否)。另一列告诉该配置文件的最终海龟数量 (nf),最后是刻度总数 (total_ticks)。例如:
但是,我无法使用这些信息生成这些列。仅包含有关海龟代码、海龟数量和刻度的信息的列。有人可以帮我吗?
提前致谢!
globals [ edge-size listProfiles ValidHabs balance ]
patches-own [ resources turtle-count ]
turtles-own [ metabolism resource-turtle code turtle-profiles turtle-code ]
to setup
ca
random-seed 1
set ValidHabs [ [1] [2] [3] [4] [5] ]
set edge-size 90
set-patch-size 12
ask patches [ set resources random 100 ]
set listProfiles [ ]
setup-patches
reset-ticks
prepare1
prepare1.2
end
to setup-patches
let list1 ( list 4 8 )
(
foreach ValidHabs [
this-profile ->
foreach list1
[
this-metabolism ->
ask n-of 1 patches
[
sprout 1
[
set turtle-profiles this-profile
set metabolism this-metabolism
setup-turtles who
]
set turtle-count count turtles-here
]
]
]
)
end
to setup-turtles [which?]
ask turtle who [
if metabolism = 4 [ set code "M1" ]
if metabolism = 8 [ set code "M2" ]
set turtle-code ( word turtle-profiles code )
print ( word "turtle-codes: " turtle-code )
]
set resource-turtle [ resources ] of patch-here
end
to go
listProfiles-turtles
ask turtles [
right random 360
fd 1
turtle-eat
reproduction
if resource-turtle >= 30 [ die ]
output
]
if ticks >= 8 [ stop ]
tick
end
to listProfiles-turtles
ask turtles [
set listProfiles lput turtle-code listProfiles
set listProfiles remove-duplicates listProfiles
]
end
to turtle-eat
set resource-turtle (resources - metabolism )
end
to reproduction
if ( resource-turtle > 30 ) [
hatch 1
set resource-turtle 0
]
end
to prepare1
carefully
[ file-delete ( word "output.csv" ) ]
[ ]
file-open ( word "output.csv" )
file-print ( word "code_profile,quantity_turtle_per_profile,tick" )
let list-profiles [ ]
ask turtles [
set list-profiles lput turtle-code list-profiles
]
set list-profiles remove-duplicates list-profiles
foreach list-profiles
[
x ->
let n count turtles with [ turtle-code = x ]
file-print ( word x "," n "," ticks".initial" )
]
file-close
end
to prepare1.2
file-open ( word "output.csv" )
let list-profiles [ ]
ask turtles [
set list-profiles lput turtle-code list-profiles
]
set list-profiles remove-duplicates list-profiles
foreach list-profiles
[
x ->
let n count turtles with [ turtle-code = x ]
file-print ( word x "," n "," ticks )
]
end
to output
file-open ( word "output.csv" )
foreach listProfiles
[
x ->
let n count turtles with [ turtle-code = x ]
; print ( word "code_profile:" x "," "quantity_turtle_per_profile:" n "," "tick: " ticks )
file-print ( word x "," n "," ticks )
]
end
一种方法是:
- 在给定的 运行
开始时定义新陈代谢列表(此处为 code
s)
- 达到停止条件后更新该列表
我复制了您的大部分代码,但添加了 csv
扩展以简化 write-out。我还将输出从海龟过程更改为仅在停止条件下发生,但您可以更改此示例以更符合您的实际需要。代码注释中的更多详细信息:
extensions [ csv ]
globals [ edge-size listProfiles ValidHabs balance profile-detail-list ]
patches-own [ resources turtle-count ]
turtles-own [ metabolism resource-turtle code turtle-profiles turtle-code ]
to setup
ca
random-seed 1
set ValidHabs [ [1] [2] [3] [4] [5] ]
set edge-size 90
set-patch-size 12
ask patches [ set resources random 100 ]
set listProfiles [ ]
setup-patches
set profile-detail-list []
foreach remove-duplicates [code] of turtles [
cur_met ->
set profile-detail-list lput ( list cur_met ( count turtles with [ code = cur_met ] ) ) profile-detail-list
]
reset-ticks
end
to setup-patches
let list1 ( list 4 8 )
(
foreach ValidHabs [
this-profile ->
foreach list1
[
this-metabolism ->
ask n-of 1 patches
[
sprout 1
[
set turtle-profiles this-profile
set metabolism this-metabolism
setup-turtles who
]
set turtle-count count turtles-here
]
]
]
)
end
to setup-turtles [which?]
ask turtle who [
if metabolism = 4 [ set code "M1" ]
if metabolism = 8 [ set code "M2" ]
set turtle-code ( word turtle-profiles code )
print ( word "turtle-codes: " turtle-code )
]
set resource-turtle [ resources ] of patch-here
end
to go
listProfiles-turtles
ask turtles [
right random 360
fd 1
turtle-eat
reproduction
if resource-turtle >= 30 [ die ]
]
if ticks >= 8 [
foreach ( range 0 ( length profile-detail-list) ) [ i ->
; Grab the metabolism / first-n pair
let met_n item i profile-detail-list
; Extract the metabolism and first-n values for later listing
let cur_met first met_n
let first_n last met_n
; Get the count of turtles with a matching metabolism
let final_n count turtles with [ code = cur_met ]
let replacement_list ( list cur_met first_n final_n ticks )
; Replace the item in the profile-detail-list
set profile-detail-list replace-item i profile-detail-list replacement_list
]
; Add "header" values to the profile-detail-list
set profile-detail-list fput ( list "code_profile" "no" "nf" "total_ticks" ) profile-detail-list
; Output
let out_file "output_example.csv"
csv:to-file out_file profile-detail-list
print word "Exported to " out_file
stop
]
tick
end
to listProfiles-turtles
ask turtles [
set listProfiles lput turtle-code listProfiles
set listProfiles remove-duplicates listProfiles
]
end
to turtle-eat
set resource-turtle (resources - metabolism )
end
to reproduction
if ( resource-turtle > 30 ) [
hatch 1
set resource-turtle 0
]
end
类似这样的输出是一个格式如下的 csv:
我想生成一个包含三列的输出。一列海龟的个人资料代码(code_profile)。另一列表示该配置文件的初始海龟数量(否)。另一列告诉该配置文件的最终海龟数量 (nf),最后是刻度总数 (total_ticks)。例如:
但是,我无法使用这些信息生成这些列。仅包含有关海龟代码、海龟数量和刻度的信息的列。有人可以帮我吗?
提前致谢!
globals [ edge-size listProfiles ValidHabs balance ]
patches-own [ resources turtle-count ]
turtles-own [ metabolism resource-turtle code turtle-profiles turtle-code ]
to setup
ca
random-seed 1
set ValidHabs [ [1] [2] [3] [4] [5] ]
set edge-size 90
set-patch-size 12
ask patches [ set resources random 100 ]
set listProfiles [ ]
setup-patches
reset-ticks
prepare1
prepare1.2
end
to setup-patches
let list1 ( list 4 8 )
(
foreach ValidHabs [
this-profile ->
foreach list1
[
this-metabolism ->
ask n-of 1 patches
[
sprout 1
[
set turtle-profiles this-profile
set metabolism this-metabolism
setup-turtles who
]
set turtle-count count turtles-here
]
]
]
)
end
to setup-turtles [which?]
ask turtle who [
if metabolism = 4 [ set code "M1" ]
if metabolism = 8 [ set code "M2" ]
set turtle-code ( word turtle-profiles code )
print ( word "turtle-codes: " turtle-code )
]
set resource-turtle [ resources ] of patch-here
end
to go
listProfiles-turtles
ask turtles [
right random 360
fd 1
turtle-eat
reproduction
if resource-turtle >= 30 [ die ]
output
]
if ticks >= 8 [ stop ]
tick
end
to listProfiles-turtles
ask turtles [
set listProfiles lput turtle-code listProfiles
set listProfiles remove-duplicates listProfiles
]
end
to turtle-eat
set resource-turtle (resources - metabolism )
end
to reproduction
if ( resource-turtle > 30 ) [
hatch 1
set resource-turtle 0
]
end
to prepare1
carefully
[ file-delete ( word "output.csv" ) ]
[ ]
file-open ( word "output.csv" )
file-print ( word "code_profile,quantity_turtle_per_profile,tick" )
let list-profiles [ ]
ask turtles [
set list-profiles lput turtle-code list-profiles
]
set list-profiles remove-duplicates list-profiles
foreach list-profiles
[
x ->
let n count turtles with [ turtle-code = x ]
file-print ( word x "," n "," ticks".initial" )
]
file-close
end
to prepare1.2
file-open ( word "output.csv" )
let list-profiles [ ]
ask turtles [
set list-profiles lput turtle-code list-profiles
]
set list-profiles remove-duplicates list-profiles
foreach list-profiles
[
x ->
let n count turtles with [ turtle-code = x ]
file-print ( word x "," n "," ticks )
]
end
to output
file-open ( word "output.csv" )
foreach listProfiles
[
x ->
let n count turtles with [ turtle-code = x ]
; print ( word "code_profile:" x "," "quantity_turtle_per_profile:" n "," "tick: " ticks )
file-print ( word x "," n "," ticks )
]
end
一种方法是:
- 在给定的 运行 开始时定义新陈代谢列表(此处为
- 达到停止条件后更新该列表
code
s)
我复制了您的大部分代码,但添加了 csv
扩展以简化 write-out。我还将输出从海龟过程更改为仅在停止条件下发生,但您可以更改此示例以更符合您的实际需要。代码注释中的更多详细信息:
extensions [ csv ]
globals [ edge-size listProfiles ValidHabs balance profile-detail-list ]
patches-own [ resources turtle-count ]
turtles-own [ metabolism resource-turtle code turtle-profiles turtle-code ]
to setup
ca
random-seed 1
set ValidHabs [ [1] [2] [3] [4] [5] ]
set edge-size 90
set-patch-size 12
ask patches [ set resources random 100 ]
set listProfiles [ ]
setup-patches
set profile-detail-list []
foreach remove-duplicates [code] of turtles [
cur_met ->
set profile-detail-list lput ( list cur_met ( count turtles with [ code = cur_met ] ) ) profile-detail-list
]
reset-ticks
end
to setup-patches
let list1 ( list 4 8 )
(
foreach ValidHabs [
this-profile ->
foreach list1
[
this-metabolism ->
ask n-of 1 patches
[
sprout 1
[
set turtle-profiles this-profile
set metabolism this-metabolism
setup-turtles who
]
set turtle-count count turtles-here
]
]
]
)
end
to setup-turtles [which?]
ask turtle who [
if metabolism = 4 [ set code "M1" ]
if metabolism = 8 [ set code "M2" ]
set turtle-code ( word turtle-profiles code )
print ( word "turtle-codes: " turtle-code )
]
set resource-turtle [ resources ] of patch-here
end
to go
listProfiles-turtles
ask turtles [
right random 360
fd 1
turtle-eat
reproduction
if resource-turtle >= 30 [ die ]
]
if ticks >= 8 [
foreach ( range 0 ( length profile-detail-list) ) [ i ->
; Grab the metabolism / first-n pair
let met_n item i profile-detail-list
; Extract the metabolism and first-n values for later listing
let cur_met first met_n
let first_n last met_n
; Get the count of turtles with a matching metabolism
let final_n count turtles with [ code = cur_met ]
let replacement_list ( list cur_met first_n final_n ticks )
; Replace the item in the profile-detail-list
set profile-detail-list replace-item i profile-detail-list replacement_list
]
; Add "header" values to the profile-detail-list
set profile-detail-list fput ( list "code_profile" "no" "nf" "total_ticks" ) profile-detail-list
; Output
let out_file "output_example.csv"
csv:to-file out_file profile-detail-list
print word "Exported to " out_file
stop
]
tick
end
to listProfiles-turtles
ask turtles [
set listProfiles lput turtle-code listProfiles
set listProfiles remove-duplicates listProfiles
]
end
to turtle-eat
set resource-turtle (resources - metabolism )
end
to reproduction
if ( resource-turtle > 30 ) [
hatch 1
set resource-turtle 0
]
end
类似这样的输出是一个格式如下的 csv: