【发布时间】:2018-10-22 00:53:28
【问题描述】:
我在 NetLogo 中有一个模型,其中只有当变量(平均每日温度程序)达到 18 时,我的海龟才开始移动。这个变量的值来自在设置时读取的 csv 文件(参见下面的代码)。
就目前而言,模型运行的刻度数与数据点的数量相同(365 = # 天/年)。我通过指定 if file-at-end? [stop] 将模型编写为仅运行这么长时间,因为我想测试 go 过程是否有效(并且它们确实有效)。现在,我希望模型在到达文件末尾后返回数据文件中的第一个值并继续读取它。以前没有关于 StackOverflow 或 NetLogo 的 google 组的问题,尽管对关键字、NetLogo 原语或程序进行了广泛的谷歌搜索,但已经产生了令人满意的解决方案。可能我没听懂。
我可以用什么替换 [stop] 来实现这一点?还是有什么需要改变的?
我可以扩展数据文件的长度,但我觉得我想使用的方法可能更简单,也可能更优雅。
我的代码:
`extensions
[array
csv]
globals[
river-patches ;;; water where the fish live
bank-patches ;;; patches were the fish cannot go
initial-temp ;;; temperature set at tick 0
temp-list ;;; variable containing temperature from .csv file
mintemp ;;; minimum temperature possible on any river-patch
maxtemp] ;;; maximum temperature possible on any river-patch
patches-own
[mean-daily-temp] ;;; variable for patch temperature
to setup
clear-all
file-close-all ;;; close any files
file-open "temperature.csv" ;;; contains the data for minimum and
maximum temperatures possible in each
tick
setup-constants ;;; set up constants
setup-river-patches ;;; set up river-patches
setup-bank-patches ;;; set up patches not in the river
setup-turtles ;;; set up turtles
reset-ticks ;;; set ticks to 0
end
to setup-constants
set initial-temp (22.06) ;;; initial temperature for each patch
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;; SETUP PATCHES ;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; creates patches that make up the river
to setup-river-patches
set river-patches patches with [pycor > -27 and pycor < 27]
ask river-patches
[set pcolor blue
set mean-daily-temp (initial-temp)]
;;; creates bank patches that are green
set bank-patches patches with [pycor <= -27 or pycor >= 27]
ask bank-patches
[set pcolor green]
end
;;; creates the 'fish' turtles
to setup-turtles
create-turtles 20
ask turtles [
set color white
set shape "fish"
set size 1
move-to one-of river-patches]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;; TO GO ;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to go
update-daily-temp ;;; this reads data from csv and creates the
'mintemp' and 'maxtemp' variables used in
'daily-river-temp'
daily-river-temp ;;; in each tick, this procedures sets the
individual patch temperature
move-turtles ;;; procedure asking turtles to move in response to
temperature
tick
end
;;;;;; HOW DO I ASK THE PROCEDURE update-daily-temp TO RETURN TO
;;;;;; START OF FILE, AFTER REACHING FINAL DATA POINT
;;;;;; SO THE MODEL CAN RUN FOR MORE TICKS THAN THERE
;;;;;; ARE DATA POINTS IN THE FILE? (i.e. UNTIL I CLICK GO AGAIN)
to update-daily-temp
if file-at-end? [stop]
set temp-list csv:from-row file-read-line
set mintemp item 0 temp-list
set maxtemp item 1 temp-list
end
to daily-river-temp ;;; in each tick, set the individual patch
;;; temperature
ask patches[
set mean-daily-temp random-float (maxtemp - mintemp) + mintemp
]
end
to move-turtles
ask turtles
[ifelse [mean-daily-temp] of patch-here > 18 and [pcolor] of patch
ahead 1 = blue
[forward 1 set heading random 360 ]
[set heading random 180]
if xcor > 119 and xcor < 119
[set heading random 360] pen-down]
end
谢谢你的期待!
【问题讨论】: