netlogo: how to make turtles stop for a set number

2019-01-28 12:08发布

I'm trying to create a model where turtles walk randomly (but with a tendency for forward movement) until they land on a yellow coloured patch which represents a baited object.

When a turtle lands on one of the yellow patches, I'd like it to stop on that patch and stay there for 15 ticks whilst it 'investigates' the bait.

After 15 ticks have elapsed I want the turtles to continue moving as usual until they encounter another yellow patch.

I've attempted to modify parts of this parked card model in the netlogo modelling commons but couldn't really make sense of it (I'm new to netlogo) http://modelingcommons.org/browse/one_model/3205#model_tabs_browse_procedures

I've also tried implementing a count-down timer as described in this thread How can one create a countdown timer in NetLogo?

However I receive a runtime error 'Only the observer can ASK the set of all turtles' when I try to run the simulation. Can anyone tell me where I'm going wrong? Probably several places! Thanks.

Here's the code that's causing the runtime error:

turtles-own [count-down]

to setup 
clear-all
ask patches with [count neighbors != 8]
[set pcolor blue]                   

create-turtles 20
ask turtles 
 [setxy random-xcor random-ycor 
 pen-down]   

ask n-of 20 patches
[ set pcolor yellow ]                   

reset-ticks
end

to go
 move-turtles
 tick
 if ticks >= 720 [stop]

 end


to move-turtles
ask turtles
  [ ifelse pcolor != yellow
  [continue]
  [stay]
  ]
 end

 to continue
ask turtles   
[rt -90 + random 181]
ask turtles
[ifelse [pcolor] of patch-ahead 1 = blue [ lt random-float 360 ]   
[fd 1]  
]
end

to stay
ask turtles 
[
setup-timer
decrement-timer
if timer-expired? [continue]
]
end

to setup-timer
set count-down 15
end

to decrement-timer
set count-down count-down - 1
end

to-report timer-expired?
report ( count-down <= 0 )
end

标签: timer netlogo
2条回答
该账号已被封号
2楼-- · 2019-01-28 12:42

This is just one example, How many ticks they should stay in the yellow area? i Assumed 15 ticks and I ask turtles to print their tick number on their label too, if it runs too fast you might miss their stay so adjust running speed for your model to see when they stay and when they move. You can have different methods to continue , in this one they just move 1 patch forward.

turtles-own [count-down]

to setup 
  clear-all
  ask patches with [count neighbors != 8]
  [set pcolor blue]                   

  create-turtles 20
  ask turtles 
  [setxy random-xcor random-ycor 
    pen-down
    set count-down 15
  ]   

  ask n-of 20 patches
  [ set pcolor yellow ]                   

  reset-ticks
end

to go
  move-turtles
  tick
  if ticks >= 720 [stop]

end


to move-turtles
  ask turtles
  [ ifelse pcolor != yellow
    [continue]
    [stay]
  ]
end

To continue   
  rt random 10
  fd 1 
end


to stay   
  set count-down count-down - 1   ;decrement-timer
  set label count-down    
  if count-down = 0 
    [
      Continue
      set label ""
      reset-count-down
    ]    

end

to reset-count-down   
  set count-down 15 
end
查看更多
你好瞎i
3楼-- · 2019-01-28 12:57

To answer just the part about "Only the observer can ASK the set of all turtles", that error message happens if you do:

ask turtles [
  ask turtles [
    do-something
  ]
]

This isn't allowed in NetLogo because it's almost always accidental rather than intentional. You probably just wanted each turtle to "do-something" once; you probably didn't mean for each turtle to "do-something" for every possible pair of two turtles.

It's less obvious that you're having all turtles ask all turtles if it's split across procedures. So for example if you write:

to go
  ask turtles [ my-procedure ]
end

to my-procedure
  ask turtles [ do-something ]
end

It's still wrong for the same reason, but it isn't as easy to see that just from glancing at it.

Your code follows this latter pattern. You have:

to move-turtles
  ask turtles [
    ...
    continue
    ...
  ]
end

to continue
  ask turtles [
    rt -90
    ...
  ]
end

I don't think you want to do ask turtles in the continue procedure. Since you call the procedure inside ask turtles, it's already a turtle procedure. I'd suggest writing it as:

to continue  ;; turtle procedure
  rt -90
  ...
end

The comment reminds you that it's intended to be run by turtles. (We follow this style in all of the models in the Models Library.)

查看更多
登录 后发表回答