0

I try to make my first ABM using NETLOGO. I would like to show in an easy way how an election works.

So I create 3 types of turtles : young people, adults and senior (I create this because of candidate's preferences are not the same, one's are more social than others more liberal...).

So I would like to make them move, and changing the p-color of the patch into they move with probability. For example in 2012 young people vote (in a simply way) 30% social (color 136), 30% liberal (color 97), 20% extreme-right (color 104) and 10% extreme-left (red).

So, I would like in my code to introduce probability when turtles move to patches and change color.

This is the interesting part of my code :

to chose-color-young

ask jeunes

[if pcolor = white [set pcolor one-of [136 97 104 15]]]

end

I would like to do something like that 136 with probability = 0.3 ;97 with probability = 0.3 ; 104 with probability = 0.20 and 15 with probability = 0.10.

rene
  • 41,474
  • 78
  • 114
  • 152
Marc
  • 71
  • 7
  • Possible duplicate of [Netlogo: Assign variable using probabilities](http://stackoverflow.com/questions/41901313/netlogo-assign-variable-using-probabilities) – Alan Jan 28 '17 at 22:00

1 Answers1

2

The rnd extension does exactly what you need:

let probs [[136 0.3] [97 0.3] [104 0.20] [15 0.10]]
ask jeunes [
  if pcolor = white [
    set pcolor first rnd:weighted-one-of-list probs last
  ]
]

See this other answer for more explanations on the rnd extension.

Community
  • 1
  • 1
Nicolas Payette
  • 14,847
  • 1
  • 27
  • 37