请问在netlogo运行过程中出现了you can not use PREPARE in an observer context,because PREPARE is turtle/patch-only的问题怎么解决呀,不管怎么改都没有办法正常运行,麻烦各位友友帮忙看看是哪里的问题
部分代码如下:
to go代码
to go
tick-advance (1 / 24) ;;report every hour
if ticks > 365
[ stop
file-close-all]
prepare
update-current
calculate-using-time
do-plot
clear-using-time
end
prepare代码
to prepare
set-temperature ;;which is read from temperature file
set-indoorCO2
set-indoorPM2.5
set-indoorRH
set-PM2.5
set-RH
set-WS
set classroom-have-people? false
ask tv-set1[
set shape "tv-off"
set status 0
]
ask tv-set2[
set shape "tv-off"
set status 0
]
ask window1[
set shape "window-close"
set status 0
]
ask aircon[
set shape "aircon-off"
set status 0
]
ask computer[
set shape "computer-off"
set status 0
]
ask window2[
set shape "window-close"
set status 0
]
ask light1[
set shape "light-off"
set status 0
]
ask light2[
set shape "light-off"
set status 0
]
ask light3[
set shape "light-off"
set status 0
]
end
to set-using-time 代码
to set-using-time
;;indoor
if [behavior] of students = BhvComputer[ ask computer [
set status 1
set shape "computer-on"
]
]
if [behavior] of students = BhvTV[
ask tv-set1 [
set status 1
set shape "tv-on"
]
ask tv-set2 [
set status 1
set shape "tv-on"
]
]
if classroom-have-people? = true[
if (time > 17) and (time <= 22 )[;;turn on the light at night
ask classroom [
set pcolor gray
]
ask light1[
set status 1
set shape "light-on"
]
ask light2[
set status 1
set shape "light-on"
]
ask light3[
set status 1
set shape "light-on"
]
]
if [indoorTemperature] of one-of classroom > 25 and (random 100 / 100) < 1 / (1 + 2.7 ^ (-([indoorTemperature] of one-of classroom - 31.4)))and (time > 8)[ ;;function fit from monitory data, only works when T>25
ask aircon[
set status 1
set shape "aircon-on"
]
]
if [indoorTemperature] of one-of classroom < 10 and (random 100 / 100) < (1 - [indoorTemperature] of one-of classroom / 8 ) and (time > 8)[ ;;function fit from monitory data, but now is not, only works when T<10
ask aircon[
set status -1
set shape "aircon-heat"
]
]
if (random 100 / 100) < exp(1.377 + 7.322 * [indoorRH] of one-of classroom - 2.401 * [indoorCO2] of one-of classroom + 10.902 * [Temperature] of one-of outdoor + 0.413 * [WS] of one-of outdoor - 2.46 * [PM2.5] of one-of outdoor - 3.332 * ([indoorTemperature] of one-of classroom - [Temperature] of one-of outdoor) + 5.189 * ([indoorRH] of one-of classroom - [RH] of one-of outdoor) - 10.219 * ([indoorPM2.5] of one-of classroom - [PM2.5] of one-of outdoor) + 1.653 * [abs(status)] of aircon)/(1 + exp(1.377 + 7.322 * [indoorRH] of one-of classroom - 2.401 * [indoorCO2] of one-of classroom + 10.902 * [Temperature] of one-of outdoor + 0.413 * [WS] of one-of outdoor - 2.46 * [PM2.5] of one-of outdoor - 3.332 * ([indoorTemperature] of one-of classroom - [Temperature] of one-of outdoor) + 5.189 * ([indoorRH] of one-of classroom - [RH] of one-of outdoor) - 10.219 * ([PM2.5] of one-of outdoor - [indoorPM2.5] of one-of classroom) + 1.653 * [abs(status)] of aircon))
[
ask window1[
set status 1
set shape "window-open"
]
ask window2[
set status 1
set shape "window-open"
]
]
]
end
环境参数读取代码:
to set-indoorCO2
file-open "indoorCO2.txt"
set indoorCO2 read-from-string file-read-line
end
to set-PM2.5
file-open "PM2.5.txt"
set PM2.5 read-from-string file-read-line
end
to set-indoorPM2.5
file-open "indoorPM2.5.txt"
set indoorPM2.5 read-from-string file-read-line
end
to set-RH
file-open "RH.txt"
set RH read-from-string file-read-line
end
to set-indoorRH
file-open "indoorRH.txt"
set indoorRH read-from-string file-read-line
end
to set-WS
file-open "WS.txt"
set WS read-from-string file-read-line
end
引用 皆我百晓生 小程序回复内容作答:
根据你提供的代码,出现"you can not use PREPARE in an observer context,because PREPARE is turtle/patch-only"错误是因为你将prepare
过程放在了go
过程中,而prepare
过程是用来设置turtles和patches的初始状态的,应该是在turtles-own
和patches-own
声明之后,在setup
过程调用prepare
过程。同时,你需要将所有的ask
语句从prepare
过程中移动到go
过程中,因为在prepare
过程中无法使用turtle
和patch
相关的命令。
修改后的代码如下:
turtles-own [
behavior
]
patches-own [
indoorCO2
indoorPM2.5
indoorRH
PM2.5
RH
WS
classroom-have-people?
]
to setup
clear-all
reset-ticks
prepare
end
to prepare
set-temperature ;;which is read from temperature file
set-indoorCO2
set-indoorPM2.5
set-indoorRH
set-PM2.5
set-RH
set-WS
ask turtles [
set behavior BhvComputer
]
ask tv-set1 [
set shape "tv-off"
set status 0
]
ask tv-set2 [
set shape "tv-off"
set status 0
]
ask window1 [
set shape "window-close"
set status 0
]
ask aircon [
set shape "aircon-off"
set status 0
]
ask computer [
set shape "computer-off"
set status 0
]
ask window2 [
set shape "window-close"
set status 0
]
ask light1 [
set shape "light-off"
set status 0
]
ask light2 [
set shape "light-off"
set status 0
]
ask light3 [
set shape "light-off"
set status 0
]
end
to go
tick-advance (1 / 24) ;;report every hour
if ticks > 365 [
stop
file-close-all
]
update-current
calculate-using-time
do-plot
clear-using-time
end
to set-using-time
;;indoor
if [behavior] of students = BhvComputer [
ask computer [
set status 1
set shape "computer-on"
]
]
if [behavior] of students = BhvTV [
ask tv-set1 [
set status 1
set shape "tv-on"
]
ask tv-set2 [
set status 1
set shape "tv-on"
]
]
if classroom-have-people? = true [
if (time > 17) and (time <= 22 ) [
;;turn on the light at night
ask classroom [
set pcolor gray
]
ask light1[
set status 1
set shape "light-on"
]
ask light2[
set status 1
set shape "light-on"
]
ask light3[
set status 1
set shape "light-on"
]
]
if [indoorTemperature] of one-of classroom > 25 and (random 100 / 100) < 1 / (1 + 2.7 ^ (-([indoorTemperature] of one-of classroom - 31.4))) and (time > 8) [
;;function fit from monitory data, only works when T>25
ask aircon[
set status 1
set shape "aircon-on"
]
]
if [indoorTemperature] of one-of classroom < 10 and (random 100 / 100) < (1 - [indoorTemperature] of one-of classroom / 8 ) and (time > 8) [
;;function fit from monitory data, but now is not, only works when T<10
ask aircon[
set status -1
set shape "aircon-heat"
]
]
if (random 100 / 100) < exp(1.377 + 7.322 * [indoorRH] of one-of classroom - 2.401 * [indoorCO2] of one-of classroom + 10.902 * [Temperature] of one-of outdoor + 0.413 * [WS] of one-of outdoor - 2.46 * [PM2.5] of one-of outdoor - 3.332 * ([indoorTemperature] of one-of classroom - [Temperature] of one-of outdoor) + 5.189 * ([indoorRH] of one-of classroom - [RH] of one-of outdoor) - 10.219 * ([indoorPM2.5] of one-of classroom - [PM2.5] of one-of outdoor) + 1.653 * [abs(status)] of aircon)/(1 + exp(1.377 + 7.322 * [indoorRH] of one-of classroom - 2.401 * [indoorCO2] of one-of classroom + 10.902 * [Temperature] of one-of outdoor + 0.413 * [WS] of one-of outdoor - 2.46 * [PM2.5] of one-of outdoor - 3.332 * ([indoorTemperature] of one-of classroom - [Temperature] of one-of outdoor) + 5.189 * ([indoorRH] of one-of classroom - [RH] of one-of outdoor) - 10.219 * ([PM2.5] of one-of outdoor - [indoorPM2.5] of one-of classroom) + 1.653 * [abs(status)] of aircon) [
ask window1 [
set status 1
set shape "window-open"
]
ask window2 [
set status 1
set shape "window-open"
]
]
]
end
另外,由于你只提供了部分代码,可能还存在其他问题。如果还有其他错误,请提供完整的代码以便我们查找错误。