NETLOGO - Option with a certain probability - probability

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.

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.

Related

How to make turtles / agents go faster or slower when being on a certain patch?

I have to make turtles go faster when being on a green patch, and when they are on a blue patch the speed should go down. I added part of the code that I tried, but it doesn't work. Could someone please help me?
Thanks in advance!!
turtles-own [speed]
to go
ask turtles [
left 60
right random 60
forward 1
if any? neighbors with [pcolor = green]
[
set speed speed + 1
]
if any? neighbors with [pcolor = blue]
[
set speed speed + 0.1
]
]
reset-ticks
I think what user2901351 was getting at was that you are using neighbors in your example code. If you look at the dictionary entry for neighbors, you'll see that it references the 8 patches around the current patch. If you instead want the turtle to check the patch it's currently on, you can use the patch-here primitive or, as a shortcut, just ask the turtle to check a patch-owned variable directly. Below is a toy example that shows an example of this at work- more details in the comments.
turtles-own [speed]
to setup
ca
crt 5 [ set speed 1 ]
ask n-of 20 patches [
ifelse random-float 1 < 0.5
[ set pcolor green ]
[ set pcolor blue ]
]
reset-ticks
end
to go
ask turtles [
; Since 'pcolor' is a patch variable, if the turtle queries pcolor
; it will check the patch's variable directly.
if pcolor = green [ set speed speed + 0.5 ]
if pcolor = blue [ set speed speed - 0.5 ]
; Make sure that the turtles are moving forward by their speed value,
; rather than the hard-coded value of "1" in the example code. Also,
; I've included a check here so the turtles don't either stop moving
; or start moving backwards.
if speed < 1 [ set speed 1 ]
rt random-float 60 - 30
fd speed
show speed
]
tick
end

How to create two different random color groups in NetLogo 6.1.1?

How to create two different random color groups in NetLogo 6.1.1?
I am trying to create two different groups from the group of 250 turtles. The starting situation is that all 250 turtles are gray and then they will turn to yellow and pink groups one by one.
This code of mine makes all turtles in the beginning to be gray and then they will all turn to pink. I do not want this but I want two randomly made groups where pink turtles is typically either larger or smaller group of turtles than the yellow group of turtles in the end of the code run.
I just started to code with NetLogo 6.1.1. Thank you for understanding and all help and have a nice day.
[
time
person
]
turtles-own [ x ]
to setup
clear-all
reset-ticks
set time 0
create-turtles 250
[
setxy random-xcor random-ycor
]
ask turtles
[
set shape "person"
set size 1
set color gray
]
end
to go
ask turtles
[
show random 2 = x
if x = 1 [set color yellow]
if x = 0 [set color pink]
]
end ```
I don't see any place where the values for the turtle variable x are set, so they will always have the default value of 0. In NetLogo = is used to check equality, not for assignment, so show random 2 = x is just going to print true or false depending on if random 2 is 0 or not (in case you thought that was assignment). You'd want something like this:
to go
ask turtles
[
set x random 2
if x = 1 [set color yellow]
if x = 0 [set color pink]
]
end
Or you could move the set x random 2 to the setup procedure if you just want to set the value once to use later in go.

NetLogo: moving turtle until it finds the final patch in 1 tick?

I would like to let my turtle wander until its energy level < [totalattract] of patch-here. The code here works fine:
to move-turtles
ifelse ([totalattract] of patch-here < energy)
[ rt random 90 lt random 90
jump random 3
]
[move-to max-one-of patches in-radius 3 [totalattract]
]
if energy = 0 [die]
end
However, I want to let it wander within 1 tick - to start with the wandering (jumping) and at the end of jumping (when its energy < [totalattract] of patch-here) move-it to patch with highest [totalattract] value in splotch in-radius X. I was trying to implement the while condition or to repeat, however for repeat I need a specific number of movement and this one depends of turtle's energy and patch's [totalattract]. How can I deal with? I will really appreciate every help or advises !!
If you want all of the turtles to do their procedures in 1 tick you want to put the tick statement inside your go procedure. Like this:
to go
ask turtles [move-turtles]
;Some other code here...
tick
end
If you want only one turtle to do its procedure in 1 tick you want to put the tick statement inside your move-tutle procedure. Like this:
to move-turtles
ifelse ([totalattract] of patch-here < energy)
[ rt random 90 lt random 90
jump random 3
]
[move-to max-one-of patches in-radius 3 [totalattract]
]
if energy = 0 [die]
tick
end

Ask a random number of turtles to do something?

I have two procedures for turtles but I need a random number of turtles to do one of the procedures and another number of random turtles to do the other procedure in equivalent parts.
So suppose i have a population of 40 turtles and I have the following procedures:
to move
ask turtles [
rt random-float 90 - random-float 90
fd 1
]
end
to bounce
ask turtles
[
if [pcolor] of patch-at dx 400 = white [
set heading (- heading)
]
if [pcolor] of patch-at 400 dy = white [
set heading (180 - heading)
]
]
end
What I want to do is to have 20 random turtles do the procedure "to bounce" and the other 20 random turtles do the procedure "to move". Or the 40 turtles will do any of the 2 procedures randomly but without any turtle doing the 2 processes at once.
I know this is kind of difficult but I think it can be done. Any suggestions?
This is the easiest way I can think of:
let bouncers n-of 20 turtles
ask turtles [
ifelse member? self bouncers
[ bounce ]
[ move ]
]
There's lots of interpretations of the phrase random number of turtles. Seth's method allows you to control exactly how many turtles do each of the two procedures. That is, a fixed number of random turtles.
If you want it always to be approximately, but not necessarily equal to, some proportion of turtles (0.4 in the example below), so each turtle randomly decides with a given probability, then you can do:
ask turtles [
ifelse-value random-float 1 < 0.4
[ bounce ]
[ move ]
]
If you want the number of turtles to be random, then you can change the first line of Seth's code to:
let bouncers n-of (1 + random count turtles) turtles
Note: none of this code is tested so please comment (or edit) if it throws an error.

Creating another kind of turtles in go button according to slider percentage

I am trying to create land use model in a city. Every GO or tick x percent (according to slider) migrants (turtles) will be sprouted in random patches which there are no turtles on it.
Currently I am still using below code, it doesn't use slider but specific number 1000 > 9 which is close to 2% according to number of turtles I created in setup.
to go
ask patches with [pcolor = green and any? turtles-here = false]
[ if random 1000 < 9 [sprout-migrants 1 [
set color red
set shape "default"
set size 1
set-income
find-residence]]]
tick
end
Assuming you have a slider named x that you want to control the percent change, then replace:
random 1000 < 9
with
random 100 < x
If the slider can take on non-integer values, then do
random-float 100 < x
you could also use this code. this way you don't need to run the if-statement to all of the patch.
assuming the slider is x as percent and turtle-numbers is contain the number of turtles you setup
let migrants-to-sprout ((x / 100) * turtle-numbers)
you set the number of turtles you want to sprout first and then make it as the loop indicator
repeat migrants-to-sprout [
ask patches with [pcolor = green and not any? turtles-here]
[
sprout-migrants 1 [...]
]
]

Resources