Ben
Ben

Reputation: 31

NetLogo: Moving percent of turtles

I created 1000 turtles in NetLogo which move randomly, but I want only thirty percent of them to move forward one step and the other seventy percent to move forward five steps.

to setup
  ca
  setup-turtles
  setup-patches
  reset-ticks
end

to go
  move-turtles
  tick
end

to setup-patches
  ask patches [set pcolor brown]
end

to setup-turtles
  create-turtles number
   ask turtles [set shape "person" set size 1] 
   ask turtles [setxy random-xcor random-ycor] ;; posicionar las personas en un punto al azar
end

to move-turtles
  ask turtles [
    right random 360]
end

Upvotes: 3

Views: 1139

Answers (1)

Nicolas Payette
Nicolas Payette

Reputation: 14972

One simple and fast way to approach this would be to do it probabilistically:

ask turtles [ fd ifelse-value (random 100 <= 30) [ 1 ] [ 5 ] ]

...so that each turtle would move one step with a probability of 30% and five steps with a probability of 70%. But it wouldn't guarantee that exactly 30% of your turtles move one step. It would just average to 30% in the long run.

If you want exact numbers, one way to do it would be:

let small-movers n-of (count turtles * 0.3) turtles
let big-movers turtles with [ not member? self small-movers ]
ask small-movers [ fd 1 ]
ask big-movers [ fd 5 ]

But this will be slowish because of the member? check.

A much faster way would be to use a turtle variable. Assuming you have:

turtles-own [ step-size ]

Then you can do:

ask turtles [ set step-size 5 ]
ask n-of (count turtles * 0.3) turtles [ set step-size 1 ]    
ask turtles [ fd step-size ]

Upvotes: 3

Related Questions