List Netlogo将全局变量列表与数字进行比较
对于这个问题的答案可能是如此简单,我提前表示歉意,我对netlogo非常陌生,非常不了解 我试图从一个文件中读取水温,从而使我的海龟根据温度死亡/繁殖。我最终得到了要读取的文件,并将水温设置为一个全局变量,但我现在仍停留在比较部分。它不允许我将变量与数字进行比较,因为我认为变量是一个列表。出现以下错误消息List Netlogo将全局变量列表与数字进行比较,list,global,netlogo,List,Global,Netlogo,对于这个问题的答案可能是如此简单,我提前表示歉意,我对netlogo非常陌生,非常不了解 我试图从一个文件中读取水温,从而使我的海龟根据温度死亡/繁殖。我最终得到了要读取的文件,并将水温设置为一个全局变量,但我现在仍停留在比较部分。它不允许我将变量与数字进行比较,因为我认为变量是一个列表。出现以下错误消息 The > operator can only be used on two numbers, two strings, or two agents of the same type,
The > operator can only be used on two numbers, two strings, or two agents of the same type, but not on a list and a number.
error while turtle 7 running >
called by procedure REPRODUCE
called by procedure GO
called by Button 'go'
代码如下
globals [ year
month
water-temperature ]
extensions [ csv ]
to setup
ca
load-data
create-turtles 50
[ set size 1
set color red
setxy random-xcor random-ycor ]
reset-ticks
end
to go
ask turtles [ move
reproduce ]
run-temperature
end
to load-data
file-close-all
file-open "C:\\Users\\Hannah\\Documents\\Summer research project\\test3.csv"
end
to run-temperature
file-close-all
file-open "C:\\Users\\Hannah\\Documents\\Summer research project\\test3.csv"
while [ not file-at-end? ] [
set water-temperature csv:from-row file-read-line
tick ]
file-close
end
to move
rt random 50
lt random 50
fd 1
end
to reproduce
if water-temperature > 35 [ die ]
if water-temperature > 30 and water-temperature < 34 [ hatch 1 rt random-float 360 fd 1 ]
if water-temperature > 25 and water-temperature < 29 [ hatch 2 rt random-float 360 fd 1 ]
if water-temperature > 20 and water-temperature < 24 [ hatch 3 rt random-float 360 fd 1 ]
end
globals[年度]
月
水温]
扩展[csv]
设置
ca
加载数据
创造海龟50
[套装尺寸1
设置颜色为红色
setxy random xcor random ycor]
重置滴答声
终止
外带
叫海龟[移动]
复制]
运行温度
终止
加载数据
文件全部关闭
打开文件“C:\\Users\\Hannah\\Documents\\Summer research project\\test3.csv”
终止
运行温度
文件全部关闭
打开文件“C:\\Users\\Hannah\\Documents\\Summer research project\\test3.csv”
而[不是文件结尾?][
设置水温csv:从行文件读取行
[勾选]
文件关闭
终止
移动
rt随机50
lt随机50
fd 1
终止
复制
如果水温>35[死亡]
如果水温>30且水温<34[舱口1 rt随机浮动360 fd 1]
如果水温>25且水温<29[舱口2 rt随机浮动360 fd 1]
如果水温>20且水温<24[舱口3 rt随机浮动360 fd 1]
终止
我将非常感谢任何帮助
谢谢:)
汉娜欢迎来到堆栈溢出。您能否提供“test3.csv”文件前几行的示例?这将有助于对您的问题进行排序-如果您有一个标题或多个列可能会导致您的问题-多个列可能会作为列表被读入。同样,我认为您需要
文件读取
而不是文件读取行
其他一些事情-据我所知,您的加载数据
过程是不必要的(您只需要在运行温度
中进行加载)
更重要的是,你现在的代码是这样写的:“所有海龟,移动并繁殖。现在,逐行阅读整个温度文件。”问题是你的while
语句说“在到达文件末尾之前,先读一行,勾选,然后移到下一行。”此外,你的模型将每行勾选一次,海龟们什么也不做——在你的程序的最后,让你的勾选
可能更简单。在这种情况下,最好避免在go
过程中使用while
,因为它将循环直到满足while
条件
只需读取整个test.csv
并将其存储在一个变量中以便于访问,可能会更容易—以下是一个示例。使用此设置:
globals [
water-temperature
water-temperature-list
]
to setup
ca
crt 50 [
setxy random-xcor random-ycor
]
首先,告诉Netlogo水温列表是一个使用set和[]的列表。然后,执行与之前相同的文件关闭/打开操作,以准备阅读文件。然后,使用类似的while
循环,使用lput
,将温度读取到水温列表中:
set water-temperature-list []
file-close-all
file-open "test3.csv"
while [ not file-at-end? ] [
set water-temperature-list lput file-read water-temperature-list
]
file-close-all
reset-ticks
end
现在,您的模型更简单地访问这些值,因为它们直接存储在模型变量中。您可以轻松地将ticks
值与item
一起用作该列表的索引-例如,在tick 0上访问列表中的第一个元素,在tick 1上访问第二个元素,依此类推。例如:
to go
set water-temperature item ticks water-temperature-list
ask turtles [
if water-temperature > 30 [
die
]
if water-temperature <= 30 [
rt random 60
fd 1
]
]
tick
end
要走了
设置水温项目勾选水温列表
问海龟[
如果水温>30[
死亡
]
如果水温