Abdullah Al Momin
Abdullah Al Momin

Reputation: 39

Storing a value

A classroom is simulated where appliances (e.g lights Fans and ACs) turn on when a student sits next to it. Each appliance has its own wattage rating. When an appliance is turned on its color changes to green and the on-time is noted and the duration on-time is stored. But if a student sits next to a appliance (e.g light) that is already on. The duration-on-time should not be stored as it would be a repetition.

     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]]]

In this code the light-on-duration is not adding for all of the lights. Only individual light-on-duration is shown. How should I fix this? Thank you!

Upvotes: 0

Views: 59

Answers (1)

JenB
JenB

Reputation: 17678

I think you have a logic problem rather than a coding problem. You can't add to duration when the light turns on because it hasn't yet built up any duration. Here is a complete model that turns lights on and off and stores duration. I am using ticks as the time, and each tick it adds 5 students and removes 5 students. But what's important is the logic of turning the lights on and off.

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

Note that you can't actually see the light turtles, I am making the patch turn yellow for on and grey for off. Every third patch has a light.

Upvotes: 1

Related Questions