How to make an equal distribution of turtles for each profile type using NetLogo 6.2?
I have no idea how to solve the following problem:
I have 9 turtle profiles which are:
profile1: M1R1 profile2: M1R2 profile3: M1R3 profile4: M2R1 profile5: M2R2 profile6: M2R3 profile7: M3R1 profile8: M3R2 profile9: M3R3
M= metabolism and R = reproduction.
I would like the world to have an exact number of turtles born in each of these profiles. For example:
profile1: 2 turtles profile2: 2 turtles profile3: 2 turtles profile4: 2 turtles profile5: 2 turtles profile6: 2 turtles profile7: 2 turtles profile8: 2 turtles profile9: 2 turtles
It turns out that I'm just getting to have variable numbers between the profiles. is it possible for me to distribute exact amount of turtle breeding for each profile? If yes, could anyone suggest some sort of solution? Well, I've been trying to solve this for a long time!!! :)
Thanks in advance
globals [ AvailablePatch ]
turtles-own [ metabolism reproduction code-metabolism code-reproduction all-code ]
patches-own [ turtle-count ]
to setup
clear-all
let list1 ( list 2 4 8 )
let list2 ( list 5 10 15 )
let n 2 ;; 20 meters away each turtle will be from another turtle
set AvailablePatch patches with [ ( pxcor mod ( n + 1 ) = 0 ) and ( pycor mod ( n + 1 ) = 0 ) ]
ask AvailablePatch
[
sprout 1
[
set metabolism item 0 ( n-of 1 list1 )
set reproduction item 0 ( n-of 1 list2 )
setup-turtles who
]
set turtle-count ( turtle-count + 1 )
]
end
to setup-turtles [ whichTurtle? ]
ask turtle who [
(
ifelse
metabolism = 2 [
set code-metabolism "M1"
(
ifelse
reproduction = 5 [
set code-reproduction "R1"
]
reproduction = 10 [
set code-reproduction "R2"
]
reproduction = 15 [
set code-reproduction "R3"
]
)
]
metabolism = 4 [
set code-metabolism "M2"
(
ifelse
reproduction = 5 [
set code-reproduction "R1"
]
reproduction = 10 [
set code-reproduction "R2"
]
reproduction = 15 [
set code-reproduction "R3"
]
)
]
metabolism = 8 [
set code-metabolism "M3"
(
ifelse
reproduction = 5 [
set code-reproduction "R1"
]
reproduction = 10 [
set code-reproduction "R2"
]
reproduction = 15 [
set code-reproduction "R3"
]
)
]
)
set all-code ( word code-metabolism code-reproduction )
]
end
Solution 1:
What you could do, is to:
- use 2
foreach
loops. That way, you get all combinations of listitems:
let list1 ( list 2 4 8 )
let list2 ( list 5 10 15 )
foreach list1
[
this_metabolism ->
foreach list2
[
this_reproduction ->
show word this_metabolism this_reproduction
;... other procedures
]
]
- Now you want to create a turtle (or more) for each of this combinations, but only on a
AvailablePatch
:
ask one-of AvailablePatch
[
sprout 1
[
set metabolism this_metabolism
set reproduction this_reproduction
setup-turtle
]
set turtle-count turtles-here
set AvailablePatch other AvailablePatch ;this patch is no longer available
]
If you want more than one turtle per combination, instead of ask one-of AvailablePatch
use for example ask n-of 3 AvailablePatch
.
I changed some more things on your code:
- there is a reporter, that reports an agentset containing all the turtles on the patch
set turtle-count count turtles-here
. You don't even have to update it there, but just callcount turtles-here
instead ofturtle-count
, when you need it. That way you don't have to add and substract turtles everytime you create new ones or some die. - inside
sprout
its already "turtle code". Meaning that you don't have to call the procedure with a turtle'swho
number, but you can write the code as it was inside anask turtle [...]
block. - the
ifelse
cases insidesetup-turtles
are independed, so it can be simplified.
Full code:
globals [ AvailablePatch ]
turtles-own [ metabolism reproduction code-metabolism code-reproduction all-code ]
patches-own [ turtle-count ]
to setup
clear-all
let list1 ( list 2 4 8 )
let list2 ( list 5 10 15 )
let n 2 ;; 20 meters away each turtle will be from another turtle
set AvailablePatch patches with [ ( pxcor mod ( n + 1 ) = 0 ) and ( pycor mod ( n + 1 ) = 0 ) ]
(
foreach list1
[
this_metabolism ->
foreach list2
[
this_reproduction ->
ask one-of AvailablePatch
[
sprout 1
[
set metabolism this_metabolism
set reproduction this_reproduction
setup-turtle
]
set turtle-count count turtles-here
set AvailablePatch other AvailablePatch
]
]
]
)
end
to setup-turtle ;turtle procedure
(
ifelse
metabolism = 2 [set code-metabolism "M1"]
metabolism = 4 [set code-metabolism "M2"]
metabolism = 8 [set code-metabolism "M3"]
)
(
ifelse
reproduction = 5 [set code-reproduction "R1"]
reproduction = 10 [set code-reproduction "R2"]
reproduction = 15 [set code-reproduction "R3"]
)
set all-code ( word code-metabolism code-reproduction )
set color reproduction
set pcolor metabolism
end