NetLogo 5.3.1错误:无法在观察者上下文中使用GET-EARLYWARN,因为仅限于龟

时间:2016-07-12 07:37:10

标签: netlogo

我似乎不明白为什么我会收到此错误。我尝试在get-earlywarn程序中放置ask代理,但它似乎不起作用。它是由ask n-of n代理引起的吗?我应该把它放在一个单独的程序中吗?

以下是代码:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; FOR ALL MODELS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; VARIABLES

globals
  [
  percent-agri
  basin-area
  agri-volume
  yield-max
  water-requirement
  price-rice
  agri-cost
  month-rainfall
  drought
  month
  d
  n
  p
  total-income
  ]

breed [agents agent]

agents-own
  [
  agent-id-sw
  agent-landarea-sw
  agehh-sw
  district-sw
  pcons-sw
  earlywarn-sw
  pprob-sw
  reach
  agent-received
  agent-demand
  agent-volumestored
  agent-volumeused
  agent-yield-sw
  agent-income-sw
  y
  store-capacity
  ]

; SETUP

to setup
  clear-all
  define-month
  set month-rainfall 848.5    ;; max rainfall from 2011 to 2014 in the river basin
  define-agri-volume
  set percent-agri 0.17      ;; assumes 17% of water from river basin is allocated for agriculture use
  set basin-area 9.205e+15    ;; 9.205e+15 sq mm of surface area
  set yield-max 6.15      ;; 6.15 tons of rice/ha
  set water-requirement 528    ;; 528 cubic m/ton of rice
  set price-rice 8963.10    ;; 8,963.10 VND per ton of rice
  set agri-cost 500000      ;; 500,000 VND per cropping season
end

to define-month
    if (ticks  = 1) or (ticks = 13) or (ticks = 25) or (ticks = 37) or (ticks = 49) or (ticks = 61) or (ticks =   73) or (ticks = 85) or (ticks = 97) or (ticks = 109)       
    [ 
      set month "JANUARY" 
    ]
    if (ticks = 2) or (ticks = 14) or (ticks = 26) or (ticks = 38) or (ticks = 50) or (ticks = 62) or (ticks =   74) or (ticks = 86) or (ticks = 98) or (ticks = 110) 
    [ 
      set month "FEBRUARY" 
    ]
    if (ticks = 3) or (ticks = 15) or (ticks = 27) or (ticks = 39) or (ticks = 51) or (ticks = 63) or (ticks =   75) or (ticks = 87) or (ticks = 99) or (ticks = 111) 
    [ 
      set month "MARCH"
    ] 
    if (ticks = 4) or (ticks = 16) or (ticks = 28) or (ticks = 40) or (ticks = 52) or (ticks = 64) or (ticks =   76) or (ticks = 88) or (ticks = 100) or (ticks = 112) 
    [ 
      set month "APRIL" 
    ] 
    if (ticks = 5) or (ticks = 17) or (ticks = 29) or (ticks = 41) or (ticks = 53) or (ticks = 65) or (ticks =   77) or (ticks = 89) or (ticks = 101) or (ticks = 113) 
    [ 
      set month "MAY" 
    ] 
    if (ticks = 6) or (ticks = 18) or (ticks = 30) or (ticks = 42) or (ticks = 54) or (ticks = 66) or (ticks =   78) or (ticks = 90) or (ticks = 102) or (ticks = 114) 
    [ 
      set month "JUNE" 
    ] 
    if (ticks = 7) or (ticks = 19) or (ticks = 31) or (ticks = 43) or (ticks = 55) or (ticks = 67) or (ticks =   79) or (ticks = 91) or (ticks = 103) or (ticks = 115) 
    [ 
      set month "JULY" 
    ] 
    if (ticks = 8) or (ticks = 20) or (ticks = 32) or (ticks = 44) or (ticks = 56) or (ticks = 68) or (ticks =   80) or (ticks = 92) or (ticks = 104) or (ticks = 116) 
    [ 
      set month "AUGUST" 
    ] 
    if (ticks = 9) or (ticks = 21) or (ticks = 33) or (ticks = 45) or (ticks = 57) or (ticks = 69) or (ticks =   81) or (ticks = 93) or (ticks = 105) or (ticks = 117)  
    [ 
      set month "SEPTEMBER" 
    ] 
    if (ticks = 10) or (ticks = 22) or (ticks = 34) or (ticks = 46) or (ticks = 58) or (ticks = 70) or (ticks =   82) or (ticks = 94) or (ticks = 106) or (ticks = 118)  
    [ 
      set month "OCTOBER"
    ] 
    if (ticks = 11) or (ticks = 23) or (ticks = 35) or (ticks = 47) or (ticks = 59) or (ticks = 71) or (ticks =   83) or (ticks = 95) or (ticks = 107) or (ticks = 119) 
    [ 
      set month "NOVEMBER"
    ] 
    if (ticks = 12) or (ticks = 24) or (ticks = 36) or (ticks = 48) or (ticks = 60) or (ticks = 72) or (ticks =   84) or (ticks = 96) or (ticks = 108) or (ticks = 120) 
    [ 
      set month "DECEMBER" 
    ]
end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;STORE WATER MODEL
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; RUN MODEL

to go-sw
  tick
  define-agents-sw
  define-agri-volume
  get-earlywarn
  define-received
  define-demand
  store-water
  use-storedwater
  define-yield-sw
  define-income-sw
  define-total-income
  if ticks = 121 [stop]
end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; INPUT DATA

to define-agents-sw

  create-agents 111
  ask agents [hide-turtle]

  random-seed new-seed

  file-open "agent_id_sw.txt"
  foreach sort agents [ask ? [set agent-id-sw file-read] ]
  file-close

  file-open "landarea_sw.txt"
  foreach sort agents [ask ? [set agent-landarea-sw file-read] ]
  file-close

  file-open "agehh_sw.txt"
  foreach sort agents [ask ? [set agehh-sw file-read] ]
  file-close

  file-open "district_sw.txt" 
  foreach sort agents [ask ? [set district-sw file-read] ]
  file-close

  file-open "pcons_sw.txt" 
  foreach sort agents [ask ? [set pcons-sw file-read] ]
  file-close

  file-open "earlywarn_sw.txt"
  foreach sort agents [ask ? [set earlywarn-sw file-read] ]
  file-close

  file-open "pprob_sw.txt" 
  foreach sort agents [ask ? [set pprob-sw file-read] ]
  file-close

end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; STORE WATER PROCESS

to define-agri-volume
  set d random 0.9676        ;; or set d random 1
  ifelse d >= 0.8998
    [
    set agri-volume (month-rainfall * (1 - d) * basin-area * percent-agri) 
    set drought "DROUGHT"
    ]
    [
    set agri-volume (month-rainfall * (1 - d) * basin-area * percent-agri) 
    set drought "NODROUGHT"
    ]
end

to get-earlywarn
  if drought = "DROUGHT"
    [
    ifelse (ticks  = 1) or (ticks = 13) or (ticks = 25) or (ticks = 37) or (ticks = 49) or (ticks = 61) or (ticks =   73) or (ticks = 85)     or (ticks = 97) or (ticks = 109) or (ticks = 7) or (ticks = 19) or (ticks = 31) or (ticks = 43) or (ticks = 55) or (ticks = 67) or     (ticks = 79) or (ticks = 91) or (ticks = 103) or (ticks = 115) 
      [
      set n random 111
      ;; n of agents reached by early warning system decided at the start of each cropping season
      set p random 4
      ]
      [
      set n n
      set p p
      ]
    ask n-of n agents 
      [
      set pprob-sw pprob-sw + p
      set y (-0.591 * agehh-sw) + (-0.605 * district-sw) + (-0.249 * pcons-sw) + (2.223 * earlywarn-sw) + (-0.615 * pprob-sw)      ;; y is computed y-hat from logistic regression
      ;; constant numbers are estimates from logistic regression
      ]
    ifelse agents = n-of n agents
      [set reach "REACHED"]
      [set reach "NOTREACHED"]
    ifelse reach = "REACHED"
      [
      ifelse y >= 0.5
        [
        store-water
        use-storedwater        
        ]
        [
        ifelse agent-received > agent-demand
          [set agent-volumeused agent-demand]
          [set agent-volumeused agent-received]
        set agent-volumestored 0
        ]
      ] 
      [
      ifelse agent-received > agent-demand
        [set agent-volumeused agent-demand]
        [set agent-volumeused agent-received]
      set agent-volumestored 0
      ]
    ]
  if drought = "NODROUGHT"
    [
    ifelse agent-received > agent-demand
      [set agent-volumeused agent-demand]
      [set agent-volumeused agent-received]
    set agent-volumestored 0
    ]
end

to define-received
      set agent-received agri-volume * agent-landarea-sw / sum [agent-landarea-sw] of agents
end

to define-demand
      set agent-demand yield-max * water-requirement * agent-landarea-sw
end

to store-water
  set store-capacity random 1000
  ifelse agent-received > agent-demand
  [
  let dif1 agent-received - agent-demand    ;; assumes that stored water only comes from excess of received
         ifelse dif1 >= store-capacity
      [set agent-volumestored agent-volumestored + store-capacity]
             [set agent-volumestored agent-volumestored + dif1]
  ]
      [set agent-volumestored agent-volumestored]
end

to use-storedwater
  let dif2 agent-demand - agent-received
  ifelse agent-received < agent-demand
      [
  ifelse agent-volumestored > dif2 
         [
           set agent-volumeused agent-received + dif2
    set agent-volumestored agent-volumestored - dif2
    ]
          [
    if agent-volumestored > 0
              [
      set agent-volumeused agent-received + agent-volumestored
             set agent-volumestored 0
      ]
    ]
      ]
      [
  set agent-volumeused agent-received
  set agent-volumestored agent-volumestored - 0
  ]
end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; GET OUTPUT

to define-yield-sw
  ask agents
  [
  if (ticks = 6) or (ticks = 18) or (ticks = 30) or (ticks = 42) or (ticks = 54) or (ticks = 66) or (ticks =   78) or (ticks = 90) or (ticks =   102) or (ticks = 114) or (ticks = 12) or (ticks = 24) or (ticks = 36) or (ticks = 48) or (ticks = 60) or (ticks = 72) or (ticks =   84) or   (ticks = 96) or (ticks = 108) or (ticks = 120)      ;; assumes 2 cropping seasons per year
  [ifelse agent-volumeused >= agent-demand
     [set agent-yield-sw agent-yield-sw + (yield-max * agent-landarea-sw) / 6]
     [
     set agent-yield-sw agent-yield-sw + 
     (6.15 * agent-landarea-sw * sum [agent-volumeused] of agents / sum [agent-demand] of agents) / 6
     ]
  ]
  ]
end

to define-income-sw
  ask agents
  [
  if (ticks = 6) or (ticks = 18) or (ticks = 30) or (ticks = 42) or (ticks = 54) or (ticks = 66) or (ticks =   78) or (ticks = 90) or (ticks =   102) or (ticks = 114) or (ticks = 12) or (ticks = 24) or (ticks = 36) or (ticks = 48) or (ticks = 60) or (ticks = 72) or (ticks =   84) or   (ticks = 96) or (ticks = 108) or (ticks = 120) 
    [set agent-income-sw (agent-yield-sw * price-rice) - agri-cost]
  ]
end

to define-total-income
  if (ticks = 6) or (ticks = 18) or (ticks = 30) or (ticks = 42) or (ticks = 54) or (ticks = 66) or (ticks =   78) or (ticks = 90) or (ticks =   102) or (ticks = 114) or (ticks = 12) or (ticks = 24) or (ticks = 36) or (ticks = 48) or (ticks = 60) or (ticks = 72) or (ticks =   84) or   (ticks = 96) or (ticks = 108) or (ticks = 120)
    [set total-income sum [agent-income-sw] of agents]
end

1 个答案:

答案 0 :(得分:1)

您正在获取错误代码&#34;无法在观察者环境中使用XXX,因为只有海龟&#34;因为代码中的某个地方你只要求一只乌龟命令(例如 前进 设定年龄X 当年龄由乌龟拥有时)但不直接与乌龟拥有。就像我做一个go命令说:

to go
   fd 1
end

而不是:

to go
   ask turtles [fd 1]
end

至于你的代码,在没有看到谁拥有什么以及全局变量是什么的情况下,我猜测它是你所要求的代码(关于get-earlywarn的中途):

 ifelse reach = "REACHED"
      [
        ifelse y >= 0.5
        [
          store-water
          use-storedwater        
        ]....................

我认为你不会在那时向任何特工询问。如果您提出问问代理人[事先应该帮助。

P.S。如果你给出完整的代码,请确保包括globals / etc,如果可以的话。或者,将问题缩小范围要好得多。

编辑:是的。既然你把整个代码放了,它肯定是我认为的问题。你问ifelse到达....没有要求任何海龟。