Netlogo 存储值

Netlogo 存储值,netlogo,Netlogo,模拟教室,当学生坐在教室旁边时,电灯、风扇和ACs等设备打开。每个设备都有自己的额定功率。当设备打开时,其颜色将变为绿色,并记录打开时间,并存储打开时间的持续时间。但是如果一个学生坐在一个设备旁边,例如已经亮着的灯。不应存储时间的持续时间,因为这将是一个重复 globals[ simulation-timer to appliance-on ask students [ ask lights in-radius 4

模拟教室,当学生坐在教室旁边时,电灯、风扇和ACs等设备打开。每个设备都有自己的额定功率。当设备打开时,其颜色将变为绿色,并记录打开时间,并存储打开时间的持续时间。但是如果一个学生坐在一个设备旁边,例如已经亮着的灯。不应存储时间的持续时间,因为这将是一个重复

     globals[
          simulation-timer

     to appliance-on

           ask students [ ask lights in-radius 4
           [ifelse not already-on?
            [ set color green

            set light-on-time ticks
            set light-on-duration light-on-duration + (time - ticks)
            show (word "light on duration = " light-on-duration)
            set already-on? true] [
            set light-on-duration light-on-duration]]]

在此代码中,并非所有灯光的灯光开启持续时间都在增加。仅显示单个灯亮起持续时间。我该如何解决这个问题?谢谢大家!

我认为您遇到的是逻辑问题,而不是编码问题。当灯亮起时,无法添加持续时间,因为它尚未建立任何持续时间。这是一个完整的模型,可以打开和关闭灯光,并存储持续时间。我使用滴答声作为时间,每滴答声增加5个学生,删除5个学生。但重要的是开关灯的逻辑

globals [light-radius]

breed [students student]
students-own
[ desk
]

breed [lights light]
lights-own
[ on?
  turned-on
  duration-on
]

to setup
  clear-all
  set light-radius 3
  ask patches [ set pcolor white ]
  ask patches with [pxcor mod 3 = 0 and pycor mod 3 = 0]
  [ sprout-lights 1
    [ set size 0
      set on? false
      set pcolor gray
    ]
  ]

  reset-ticks
  ask n-of 30 patches
  [ sprout-students 1 
    [ set color blue
    ]
    ask lights in-radius light-radius [switch-light-on]
  ]
end

to go
  repeat 5 [student-arrive]
  repeat 5 [student-leave]
  ask lights with [any? students in-radius light-radius]
  [ switch-light-on
  ]
  tick
end

to student-arrive
  ask one-of patches with [not any? students-here]
  [ sprout-students 1
    [ set color blue
      ask lights in-radius light-radius with [not on?]
      [ switch-light-on
      ]
    ]
  ]
end

to switch-light-on
  set pcolor yellow
  set on? true
  set turned-on ticks
end

to student-leave
  ask one-of students
  [ die
  ]
  ask lights with [ on? and not any? students in-radius light-radius ]
  [ switch-light-off
  ]
end

to switch-light-off
  set pcolor gray
  set on? false
  type "previous duration: " print duration-on
  let how-long ticks + 1 - turned-on
  set duration-on duration-on + how-long
  type "new duration: " print duration-on
end

请注意,你实际上看不到光海龟,我正在使补丁变为黄色表示打开,灰色表示关闭。每三个补丁都有一个灯光。

谢谢,但即使在您显示的代码中,灯光也没有关闭,持续时间也没有累计。一定是复制错误了。再试一次。我还添加了一些打印输出在关闭,所以你可以看到持续时间的变化。谢谢。但这段代码并不能从一开始就把所有的准时时间加起来,如果你在最后关灯的话就会加起来。灯光关闭后,将添加每个打开的灯光会话。你需要考虑代码是如何工作的,并根据你的具体问题进行调整。因此,即使学生坐在已经亮起的灯旁边,也不会添加该灯的持续时间?因为我想避免重复。