How to make an equitable distribution of turtles using NetLogo 6.2? - netlogo

I have a problem that I asked for help here: How to make an equal distribution of turtles for each profile type using NetLogo 6.2?
And Lena helped me a lot :)
But, I was not very precise in what I would like and it was partially solved. I tried to adjust how Lena responded and I couldn't, because I get stuck in the very similar problem that I still don't understand how to deal in the code.
So my problem is the following:
I have 31 profiles of turtles that refer to combinations of 5 types of habitats. For example:
profile1: turtles are only born in habitat 1
profile2: turtles are only born in habitat 2
profile3: turtles are only born in habitat 3
profile4: turtles are only born in habitat 4
profile5: turtles are only born in habitat 5
profile6: turtles are only born in habitats 1 and 2
profile6: turtles are only born in habitats 1 and 3
... until you reach profile 31 where the turtles are born in habitats 1, 2, 3, 4 and 5
The problem is that I have 2 more variables (metabolism (M) and reproduction (R)), each with 3 levels:
M1R1
M1R2
M1R3
M2R1
M2R2
M2R3
M3R1
M3R2
M3R3
I would like to have exactly these nine combinations for the 31 turtle profiles. For example:
Perfil1 (turtles are only born in habitat 1):
M1R1
M1R2
M1R3
M2R1
M2R2
M2R3
M3R1
M3R2
M3R3
Perfil2 (turtles are only born in habitat 2):
M1R1
M1R2
M1R3
M2R1
M2R2
M2R3
M3R1
M3R2
M3R3
... until you reach profile 31 where the turtles are born in habitats 1, 2, 3, 4 and 5:
M1R1
M1R2
M1R3
M2R1
M2R2
M2R3
M3R1
M3R2
M3R3
The issue is that I get the following return:
profile21: M1R1
profile17: M1R2
profile20: M1R3
profile17: M2R1
profile6: M2R2
profile26: M2R3
profile30: M3R1
profile7: M3R2
profile27: M3R3
But what I would like is to have this combination of metabolism and reproduction variables for each of the 31 profiles. Like for example:
profile1: M1R1
profile1: M1R2
profile1: M1R3
profile1: M2R1
profile1: M2R2
profile1: M2R3
profile1: M3R1
profile1: M3R2
profile1: M3R3
profile2: M1R1
profile2: M1R2
profile2: M1R3
profile2: M2R1
profile2: M2R2
profile2: M2R3
profile2: M3R1
profile2: M3R2
profile2: M3R3
... until you reach profile 31
profile31: M1R1
profile31: M1R2
profile31: M1R3
profile31: M2R1
profile31: M2R2
profile31: M2R3
profile31: M3R1
profile31: M3R2
profile31: M3R3
Could anyone help me understand how I can solve this?
Thanks in advance :)
The code, below:
globals [ AvailablePatch UnassignedProfileCountList ValidHabs ]
turtles-own [ metabolism reproduction code-metabolism code-reproduction all-code turtle-profiles-habitat ]
patches-own [ turtle-count habitatcover ]
to setup
clear-all
random-seed 1
resize-world 69 * 0 ( 69 * 1 ) ( 69 * -1 ) 69 * 0
read
setup-patches
reset-ticks
foreach sort turtles
[
t ->
ask t
[
print ( word "I am turtle:" " " who " " "my profile type:" " " turtle-profiles-habitat " " "my code:" " " all-code " " "my code reproduction level:" " " code-reproduction " " "my code metabolism level:" " " code-metabolism )
]
]
end
to read
set ValidHabs [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ]
end
to-report get-any-incomplete-profile [ habtype ]
let kkk 0
let shortlist [ ]
let validhablist [];
repeat 30
[
set kkk ( kkk + 1 )
set validhablist item kkk ValidHabs
if (
(( item kkk UnassignedProfileCountList > 0 ) and ( true = member? habtype validhablist ))
)
[
set shortlist lput kkk shortlist
]
]
let mypick -1
ifelse ( 0 < length shortlist )
[
set mypick item 0 ( n-of 1 shortlist )
let oldcount item mypick UnassignedProfileCountList
let newcount ( oldcount - 1 )
set UnassignedProfileCountList replace-item mypick UnassignedProfileCountList newcount
]
[
set mypick -1
]
report mypick
end
to setup-patches
let n 2
set AvailablePatch patches with [
( pxcor mod ( n + 1 ) = 0 ) and ( pycor mod ( n + 1 ) = 0 ) ]
set UnassignedProfileCountList [ 0 ]
repeat 30
[
set UnassignedProfileCountList lput 9 UnassignedProfileCountList
]
let list1 ( list 2 4 8 )
let list2 ( list 5 10 15 )
(
foreach list1
[
this-metabolism ->
foreach list2
[
this-reproduction ->
ask one-of AvailablePatch
[
sprout 1
[
set turtle-profiles-habitat oneprofile
set metabolism this-metabolism
set reproduction this-reproduction
setup-turtles
]
set turtle-count count turtles-here
set AvailablePatch other AvailablePatch
]
]
]
]
]
)
end
to setup-turtles
(
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

Again, you have a list and want to create a specific number of turtles for each combination of this list with the other lists. So, you can use a foreach loop:
foreach ValidHabs [
this-profile ->
;code that should be run for every entry of ValidHabs
]
The whole setup-patches function:
to setup-patches
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 ) ]
set UnassignedProfileCountList [ 0 ] ;; effectively start from item 1 not zero
repeat 30
[
set UnassignedProfileCountList lput 9 UnassignedProfileCountList
]
let list1 ( list 2 4 8 )
let list2 ( list 5 10 15 )
(
foreach ValidHabs [
this-profile ->
foreach list1
[
this-metabolism ->
foreach list2
[
this-reproduction ->
ask one-of AvailablePatch
[
sprout 1
[
set turtle-profiles-habitat this-profile
set metabolism this-metabolism
set reproduction this-reproduction
setup-turtles
]
set turtle-count count turtles-here
set AvailablePatch other AvailablePatch ;this patch is no longer available
]
]
]
]
)
end

Related

How can I use foreach inside a code in NetLogo?

I'm new to NetLogo and I'm having a hard time understanding the logic of using foreach and joining two parts into a single code. Can anyone help me understand and solve my problem?
My problem:
I have 31 profiles of turtles that refer to combinations of 5 types of habitats. For example:
profile1: turtles are only born in habitat 1
profile2: turtles are only born in habitat 2
profile3: turtles are only born in habitat 3
profile4: turtles are only born in habitat 4
profile5: turtles are only born in habitat 5
profile6: turtles are only born in habitats 1 and 2
profile7: turtles are only born in habitats 1 and 3
... until you reach profile31 where the turtles are born in habitats 1, 2, 3, 4 an 5
I also have 2 variables with 3 levels each. The reproduction variable (R) and the metabolism variable (M), which results in 9 combinations, that is:
R1M1
R1M2
R1M3
R2M1
R2M2
R2M3
R3M1
R3M2
R3M3
I would like to have these 9 combinations for each of the 31 turtle profiles. For example:
Profile1:
R1M1
R1M2
R1M3
R2M1
R2M2
R2M3
R3M1
R3M2
R3M3
Profile2:
R1M1
R1M2
R1M3
R2M1
R2M2
R2M3
R3M1
R3M2
R3M3
And so on until you reach profile 31 having these 9 levels (
combinations of the 3 levels of the variables reproduction (R) and metabolism (M)). And therefore, generating 279 turtles in the world (31 profiles * 9 = 279)
I had help on this link: How to make an equitable distribution of turtles using NetLogo 6.2? and thank you very much
However, I'm not able to understand the logic and put two pieces of code to work together. I have this part of the code that has to exist for the following code structure to work:
ask AvailablePatch
[ let oneprofile 99 ;;dummy value
while [ ( count turtles-here < 1 ) and ( sum UnassignedProfileCountList > 0 ) and ( oneprofile > 0 ) ]
[
set oneprofile get-any-incomplete-profile habitatcover
if ( oneprofile > 0 )
[
sprout 1
[
set turtle-profiles-habitat oneprofile
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 )
]
]
]
I know that the lines of code below make it not generate the 9 equal combinations for each profile
set metabolism item 0 ( n-of 1 list1 )
set reproduction item 0 ( n-of 1 list2 )
For example, 9 turtles were born for each profile, but with repetitions or unbalanced. For example:
Profile1:
R1M3
R1M3
R1M3
R1M3
R2M2
R3M2
R3M3
R3M3
R3M1
And I want for each of the 31 turtle profiles to have 9 balanced combinations without repetitions. For example:
Profile1:
R1M1
R1M2
R1M3
R2M1
R2M2
R2M3
R3M1
R3M2
R3M3
I know that using foreach I could get the 9 desired combinations. Using the code below:
(
foreach list1
[
this_metabolism ->
foreach list2
[
this_reproduction ->
ask one-of AvailablePatch
[
sprout 1
[
set metabolism this_metabolism
set reproduction this_reproduction
setup-turtles who
]
set turtle-count count turtles-here
set AvailablePatch other AvailablePatch
]
]
]
)
But trying to merge this foreach code (above) with the general structure of the code doesn't work. I've put it in all the positions I thought would be somewhat logical. And it just doesn't work.
Can someone explain to me how I could put these two pieces of code together?
Thanks in advance :)
The complete code, below:
globals [ AvailablePatch UnassignedProfileCountList ValidHabs ]
turtles-own [ metabolism reproduction code-metabolism code-reproduction all-code turtle-profiles-habitat ]
patches-own [ turtle-count habitatcover ]
to setup
clear-all
random-seed 1
read
setup-world
setup-patches
reset-ticks
foreach sort turtles
[
t ->
ask t
[
print ( word "I am turtle:" " " who " " "my profile type:" " " turtle-profiles-habitat " " "my code reproduction level:" " " code-reproduction " " "my code metabolism level:" " " code-metabolism )
]
]
end
to read
set ValidHabs [ [ 0 0 0 0 0 ] [1] [2] [3] [4] [5] [1 2] [1 3] [1 4] [1 5] [2 3] [2 4] [2 5] [3 4] [3 5] [4 5] [1 2 3] [1 2 4] [1 2 5] [1 3 4] [1 3 5] [1 4 5] [2 3 4] [2 3 5] [2 4 5] [3 4 5] [1 2 3 4] [1 2 3 5] [1 2 4 5] [1 3 4 5] [2 3 4 5] [1 2 3 4 5]]
end
to setup-world
let pcolors []
set pcolors [ 25 65 23 53 105 ]
ask patches [
set pcolor item (random 5) pcolors
]
ask patches [
if pcolor = 25 [ set habitatcover 1 ]
if pcolor = 65 [ set habitatcover 2 ]
if pcolor = 23 [ set habitatcover 3 ]
if pcolor = 53 [ set habitatcover 4 ]
if pcolor = 105 [ set habitatcover 5 ]
]
end
to-report get-any-incomplete-profile [ habtype ]
let kkk 0
let shortlist [ ]
let validhablist [];
repeat 31
[
set kkk ( kkk + 1 )
set validhablist item kkk ValidHabs
if (
(( item kkk UnassignedProfileCountList > 0 ) and ( true = member? habtype validhablist ))
)
[
set shortlist lput kkk shortlist
]
]
let mypick -1
ifelse ( 0 < length shortlist )
[
set mypick item 0 ( n-of 1 shortlist )
;; print ( word "mypick is " mypick )
let oldcount item mypick UnassignedProfileCountList
let newcount ( oldcount - 1 )
set UnassignedProfileCountList replace-item mypick UnassignedProfileCountList newcount
]
[
set mypick -1
]
report mypick
end
to setup-patches
set AvailablePatch patches with [
( pxcor mod ( 2 + 1 ) = 0 ) and ( pycor mod ( 2 + 1 ) = 0 ) ]
set UnassignedProfileCountList [ 0 ] ;; effectively start from item 1 not zero
repeat 31
[
set UnassignedProfileCountList lput 9 UnassignedProfileCountList
]
let list1 ( list 2 4 8 )
let list2 ( list 5 10 15 )
(
foreach list1
[
this_metabolism ->
foreach list2
[
this_reproduction ->
ask one-of AvailablePatch
[
sprout 1
[
set metabolism this_metabolism
set reproduction this_reproduction
setup-turtles
]
set turtle-count count turtles-here
set AvailablePatch other AvailablePatch
]
]
]
)
end
to setup-turtles
ask turtle who [
(
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 )
]
end
In order to create turtles with all profile and R and M combinations, but also let the patches, the turtles get placed on be one-of the turtles profile, you can just reassign the patch's habitatcover. They are chosen randomly before, so it doesn't change anything about the randomness.
Just add
ask one-of AvailablePatch
[
set habitatcover one-of this-profile
if habitatcover = 1 [set pcolor 25]
if habitatcover = 2 [set pcolor 65]
if habitatcover = 3 [set pcolor 23]
if habitatcover = 4 [set pcolor 53]
if habitatcover = 5 [set pcolor 105]
;...
]
to the code I gave you in the other answer.

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
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 call count turtles-here instead of turtle-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's who number, but you can write the code as it was inside an ask turtle [...] block.
the ifelse cases inside setup-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

How to create multiple turtles in certain patches of the world quickly using NetLogo 6.2?

I'm trying to solve a problem of speed in creating turtles in the NetLogo world.
I have a model that has a world with a size of 600X600. I, too, have 31 turtle profiles (each turtle can only born in a specific habitatcover type or habitatcover set (see ValidHabs variable in the code). In the code, too, there are 2 variables that are metabolism with 2 levels (list1 in the code) and reproduction with 2 levels (list2 in the code). Also, I would like to have at least 200 turtles born in the world or more in this world.
The problem is that it is taking a long time for turtles to be born in the world. Have I already created a switch called Display? to speed up the creation of the turtles. I also set it to faster speed and it still takes a long time to create the turtles.
Does anyone know how I can speed up the creation of turtles based on my code?
I don't know what else to do to speed up the code... I appreciate any kind of help :)
Thanks in advance
The code below:
globals [ ValidHabs ValidHabsItem HotPatchSet CurrentHotPatchSet ]
patches-own [ habitatcover ]
turtles-own [ turtle-profiles-habitat metabolism reproduction all-code code-metabolism code-reproduction ]
to setup
clear-all
random-seed 1
;; ValidHabs are the habitat profiles of the turtles are all combinations of 5 types of habitatcover without repetition
set ValidHabs [[1] [2] [3] [4] [5] [1 2] [1 3] [1 4] [1 5] [2 3] [2 4] [2 5] [3 4] [3 5] [4 5] [1 2 3] [1 2 4] [1 2 5] [1 3 4] [1 3 5] [1 4 5] [2 3 4] [2 3 5] [2 4 5] [3 4 5] [1 2 3 4] [1 2 3 5] [1 2 4 5] [1 3 4 5] [2 3 4 5] [1 2 3 4 5]]
ifelse ( Display? = true ) [ display ] [ no-display ] ;; display is a switch in interface
resize-world 599 * 0 ( 599 * 1 ) ( 599 * -1 ) 599 * 0 ;; the world is 600X600 in size
setup-world
setup-patches
reset-ticks
print ( word "That setup took " timer " seconds" )
end
to setup-world
let pcolors []
set pcolors [ 25 65 23 53 105 10 ]
ask patches [
set pcolor item (random 5) pcolors
]
ask patches [
if pcolor = 25 [ set habitatcover 1 ]
if pcolor = 65 [ set habitatcover 2 ]
if pcolor = 23 [ set habitatcover 3 ]
if pcolor = 53 [ set habitatcover 4 ]
if pcolor = 105 [ set habitatcover 5 ]
if pcolor = 10 [ set habitatcover 6 ]
]
end
to setup-patches
set-patch-size 0.6 ;; view patch size
set HotPatchSet patches with [ ( habitatcover != 6 ) ]
let list1 ( list 2 4 )
let list2 ( list 5 10 )
let n0 count turtles
set CurrentHotPatchSet HotPatchSet with [ habitatcover = one-of item ValidHabsItem ValidHabs ]
while [ n0 < ( length list1 ) * ( length list2 ) * 200 ] ;; I want 200 ou more turtles in the word. Here I put 200
[
(
foreach list1
[
this-metabolism ->
foreach list2
[
this-reproduction ->
let c count CurrentHotPatchSet
if c = 0 [ stop ]
ask one-of CurrentHotPatchSet
[
sprout 1
[
set turtle-profiles-habitat item ValidHabsItem ValidHabs
set metabolism this-metabolism
set reproduction this-reproduction
setup-turtles who
]
set CurrentHotPatchSet CurrentHotPatchSet with [ not any? turtles in-radius 2 ]
]
]
]
)
set n0 count turtles
]
end
to setup-turtles [ whichTurtle? ]
set color black
ask turtle who [
(
ifelse
metabolism = 2 [set code-metabolism "M1"]
metabolism = 4 [set code-metabolism "M2"]
)
(
ifelse
reproduction = 5 [set code-reproduction "R1"]
reproduction = 10 [set code-reproduction "R2"]
)
set all-code ( word code-metabolism code-reproduction )
]
end
I'm afraid I did not look for the particular problems in your code, but there is general advice on speeding up models here:
http://jasss.soc.surrey.ac.uk/20/1/3.html
with updates here:
http://www.railsback-grimm-abm-book.com/jasss-models/
I'm pretty sure that some of the advice applies to your code.

Netlogo. Update an history list when turtles birth and

I will be very grateful if someone could give me any kind of advice about a problem that I have in my code. I am working on a model with four kind of agents. Each type of agents uses a different strategy. These strategies are Prisoner Dilemma variations. Two of these strategies (tit for tat and unforgiving) need to record (in a list) who was the partner of interaction, and depending if that partner defected or not, will be the behave (cooperative or defect) of the agent in the next encounter with the same partner. Agents move randomly through the world. The problem come when agents die or birth according of their resources level. At this point these strategies that require to record who was the previous partner and how was his behave (cooperative or defect) do not work. This is the message that show up.
I am really appreciate any support!!!!.
number of initial turtles is 42, but two ticks later the number is 52
Below is a code that reproduces the error. In order to run it, it is necessary to create the sliders: n-tit-for-tat-com; n-unforgiving-com; inc-ecological-resources; energy-consume-companies:
globals
[
;;Number of companies with each strategy¨
num-tit-for-tat-com
num-unforgiving-com
;;number of interactions by each strategy
num-tit-for-tat-com-games
num-unforgiving-com-games
;; Total score of all companies playing each strategy
tit-for-tat-com-score
unforgiving-com-score
; Total companies en each ticks
num-companies
]
breed [companies company]
;;create companies variables
companies-own [
score-com
score-com-round
strategy-com
defect-now?-com
partner-defected?-com ;;action of the partner
partnered?-com ;;am I partnered?
partner-com ;;WHO of my partner (nobody if not partnered)
partner-history-com ;;a list containing information about past interactions with other turtles (indexed by WHO values)
earnings-tit-for-tat-Com
earnings-unforgiving-Com
earnings-com
tit-for-tat-com-score-round
unforgiving-com-score-round
]
patches-own
[
resources-amount ;; resources amount in each patches
resources-to ;; aux variable to count resources taken
]
;;;Setup Procedures;;;
to setup
clear-all
setup-patches
setup-companies ;;setup the turtles and distribute them randomly
reset-ticks
end
; Setup patches
;===============
to setup-patches
ask patches [set resources-amount random 4] ;; patches represent ecological system. Resources are distributed randomly. [0,1,2 or 3] 0 -> non resources; 3 -> plenty of resources
ask patches [
set pcolor (ifelse-value
resources-amount = 0 [49]
resources-amount = 1 [58]
resources-amount = 2 [67]
resources-amount = 3 [65]
[63] ;; this case never happen. Because 4 means integer number in range [0 to 3]
)]
end
to setup-companies
make-companies ;;create the appropriate number of turtles playing each strategy
setup-common-variables-com ;;sets the variables that all turtles share
end
;;create the appropriate number of turtles playing each strategy
to make-companies
create-companies n-tit-for-tat-com
[ set strategy-com "tit-for-tat-com"
set color 135
set earnings-tit-for-tat-Com random 4 + 1 ;;randomly initial resources in the range [ 1 - 4 ]. It is sum + 1 to enforce never inicial resource = 0
]
create-companies n-unforgiving-com
[ set strategy-com "unforgiving-com"
set color 4
set earnings-unforgiving-Com random 4 + 1 ;;randomly initial resources in the range [ 1 - 4 ]. It is sum + 1 to enforce never inicial resource = 0
]
end
; Setup common variables
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
;;set the variables that all turtles share
to setup-common-variables-com
ask companies [
set shape "pentagon"
set size 1.3
set tit-for-tat-com-score-round 0
set unforgiving-com-score-round 0
set score-com 0
set partnered?-com false
set partner-com nobody
setxy random-xcor random-ycor
]
end
;;;Runtime Procedures;;;
to go
ask companies [birth-die-com]
store-initial-companies-counts ;;record the number of turtles for each strategy
setup-history-lists-com
clear-last-round
ask companies [ partner-up-com ] ;;have turtles try to find a partner
let partnered-com companies with [ partnered?-com ]
ask partnered-com [ select-strategy-com ] ;;all partnered turtles select action
ask partnered-com [ play-a-round-com ]
adjust-patches
tick
end
;Birth-die-com ; According to the level of resources companies can die or birth
;======================
to birth-die-com
(ifelse
strategy-com = "tit-for-tat-com"
[
set earnings-com earnings-tit-for-tat-com
(if earnings-com > 2 * energy-consume-companies [
hatch 1 [set earnings-tit-for-tat-com random 2 + 1 ] ;;randomly initial resources in the range [ 1 - 2 ]. It is sum + 1 to enforce never inicial resource = 0
set earnings-com (earnings-com - energy-consume-companies )])
( if earnings-com < energy-consume-companies [ die ])
]
strategy-com = "unforgiving-com"
[
set earnings-com earnings-unforgiving-com
(if earnings-com > 2 * energy-consume-companies [
hatch 1 [set earnings-unforgiving-com random 2 + 1 ] ;;randomly initial resources in the range [ 1 - 2 ]. It is sum + 1 to enforce never inicial resource = 0
set earnings-com (earnings-com - energy-consume-companies )])
( if earnings-com < energy-consume-companies [ die ])
]
)
end
;;STORE INITIAL COMPANIES¨
;===========================
;;record the number of turtles at the begining of each tick
;;The number of turtles of each strategy is used when calculating average payoffs.
to store-initial-companies-counts
set num-tit-for-tat-com (count companies with [strategy-com = "tit-for-tat-com"])
set num-unforgiving-com (count companies with [strategy-com = "unforgiving-com"])
end
;Setup-History-Lists-com;; initialize PARTNER-HISTORY list in all turtles
;========================
to setup-history-lists-com
set num-companies (count companies)
let default-history-com [] ;;initialize the DEFAULT-HISTORY variable to be a list
;create a list with NUM-TURTLE elements for storing partner histories
repeat (count companies) [ set default-history-com (fput false default-history-com) ]
;give each turtle a copy of this list for tracking partner histories
ask companies [ set partner-history-com default-history-com ]
end
;Clear-last-round
;================
to clear-last-round
let partnered-com companies with [ partnered?-com ]
ask partnered-com [ release-partners-com ]
end
; release partner between companies and turn around
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to release-partners-com
set partnered?-com false
set partner-com nobody
rt 180
set label ""
end
;;have turtles try to find a partner
;;Since other turtles that have already executed partner-up may have
;;caused the turtle executing partner-up to be partnered,
;;a check is needed to make sure the calling turtle isn't partnered.
;;partner-up. Find partner
;===========================
to partner-up-com ;;turtle procedure
if (not partnered?-com) [ ;;make sure still not partnered
rt (random-float 90 - random-float 90) fd 1 ;;move around randomly
set partner-com one-of other (companies in-radius 1 ) with [ not partnered?-com ]
if partner-com != nobody [ ;;if successful grabbing a partner, partner up
set partnered?-com true
set heading 270 ;;face partner
ask partner-com [
set partnered?-com true
set partner-com myself
set heading 90
]
]
]
end
;Select stratey company
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
;;choose an action based upon the strategy being played
to select-strategy-com ;;turtle procedure
if strategy-com = "tit-for-tat-com" [ tit-for-tat-com ]
if strategy-com = "unforgiving-com" [ unforgiving-com ]
end
;Play a round-companies
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to play-a-round-com ;;turtle procedure
get-payoff-com ;;calculate the payoff for this round
update-history-com ;;store the results for next time
end
; get-payoff-com
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
;;calculate the payoff for this round and
;;display a label with that payoff.
;; Strategy-companies
;Turtles l l
;Action l C l D
;__________l________ l__________
; C l 3 l -3
;----------l---------l----------
; D l 5 l -2
to get-payoff-com
set partner-defected?-com [defect-now?-com] of partner-com
ifelse partner-defected?-com [
ifelse defect-now?-com
[ set score-com (score-com + 1 ) set label 1 set score-com-round 1 ]
[set score-com (score-com + 0) set label 0 set score-com-round 0]
]
[ifelse defect-now?-com
[set score-com (score-com + 5) set label 5 set score-com-round 5]
[set score-com (score-com + 3) set label 3 set score-com-round 3]
]
end
;Update-history
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨
;;update PARTNER-HISTORY based upon the strategy being played
to update-history-com
if strategy-com = "tit-for-tat-com" [ tit-for-tat-com-history-update ]
if strategy-com = "unforgiving-com" [ unforgiving-com-history-update ]
end
;;;Strategies;;;
; Tit-for-tat-com
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to tit-for-tat-com
set num-tit-for-tat-com-games num-tit-for-tat-com-games + 1
set partner-defected?-com item ([who] of partner-com) partner-history-com
ifelse (partner-defected?-com)
[set defect-now?-com true]
[set defect-now?-com false]
ask companies in-radius 1 [
ask patches in-radius 1 [set resources-to (resources-amount * 1)]
ask patches in-radius 1 [set resources-amount (resources-amount - resources-to)]
set tit-for-tat-com-score-round ( tit-for-tat-com-score-round + score-com-round )
set earnings-tit-for-tat-com (earnings-tit-for-tat-Com + ((sum [ resources-to ] of patches in-radius 1)/ 2) + tit-for-tat-com-score-round - energy-consume-companies )
ask patches in-radius 1 [set resources-to 0 ]
set tit-for-tat-com-score-round 0
]
end
;Tit-for-tat-com-history-update¨
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to tit-for-tat-com-history-update
set partner-history-com
(replace-item ([who] of partner-com) partner-history-com partner-defected?-com)
end
;unforgiving-com ; Works similar to tit-for-tat, but once the another defect, always defect
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to unforgiving-com
set num-unforgiving-com-games num-unforgiving-com-games + 1
set partner-defected?-com item ([who] of partner-com) partner-history-com
ifelse (partner-defected?-com)
[set defect-now?-com true]
[set defect-now?-com false]
ask companies in-radius 1 [
ask patches in-radius 1 [set resources-to (resources-amount * 1)]
ask patches in-radius 1 [set resources-amount (resources-amount - resources-to)]
set unforgiving-com-score-round ( unforgiving-com-score-round + score-com-round )
set earnings-unforgiving-com (earnings-unforgiving-Com + ((sum [ resources-to ] of patches in-radius 1)/ 2) + unforgiving-com-score-round - energy-consume-companies)
ask patches in-radius 1 [set resources-to 0 ]
set unforgiving-com-score-round 0
]
end
;unforgiving-com-history-update
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to unforgiving-com-history-update
if partner-defected?-com [
set partner-history-com
(replace-item ([who] of partner-com) partner-history-com partner-defected?-com)
]
end
;; Adjusting Patches
;; Put limits
to adjust-patches
ask patches [ set resources-amount resources-amount + random-float Inc-Ecological-resources ] ;; Resources are renewable. They grow up again between [0-3). Use Slider
ask patches [ ;; Resources range [0-3)
set resources-amount (ifelse-value
resources-amount < 0 [0] ;;
resources-amount > 3 [3] ;;
[resources-amount]
)
]
;; Patches are colored according their level
ask patches [
set pcolor (ifelse-value
resources-amount < 0 [ black ]
resources-amount = 0 [ 38 ]
resources-amount > 0 and resources-amount <= 0.5 [ 49 ]
resources-amount > 0.5 and resources-amount <= 2 [ 67 ]
resources-amount > 2 and resources-amount <= 3 [ 65 ]
resources-amount > 3 and resources-amount <= 4 [ 63 ]
[ white ])
]
end

BehaviorSpace Output

I am simulating a classroom to find the total energy consumption from appliances of a classroom. Now I want to run the simulation in BehaviorSpace so that I get the energy consumption (energy-calculation) by varying the number of students in the classroom.
globals[
temp1 simulation-timer number-of-seats number-of-lights
number-of-fans number-of-acs gap row col x-cor y-cor half half2
student-no t-light t-fan t-ac t-energy
]
breed [seats seat]
breed [seat-teachers seat-teacher]
breed [lights light]
breed [fans fan]
breed [acs ac ]
breed [students student ]
seats-own [
seat-color
occupied?
]
seat-teachers-own [
seat-color
]
students-own [
entry-time
found-seat
]
lights-own [
l-energy
]
fans-own [
f-energy
]
acs-own [
a-energy
]
to setup
clear-all
ask patches [ set pcolor 9 ]
set gap floor ((max-pxcor) / (no-of-row-or-col) )
set half ceiling (gap / 2)
set half2 floor (gap / 2)
place-seat-teachers
place-seats-students
place-lights
place-fans
place-acs
ask patches with [ pxcor = 3 * gap + half2 ] [ set pcolor 4 ]
ask patches with [ pxcor = 6 * gap + half2 ] [ set pcolor 4 ]
create-students-classroom
reset-ticks
reset-timer
end
to place-seat-teachers
create-seat-teachers 1 [
setxy ((max-pxcor - min-pxcor) / 2) 1
set shape "square"
set size 3
set color red
]
end
to place-seats-students
set row gap
set col gap
set x-cor 0
set y-cor 0
while [ x-cor <= gap * no-of-row ]
[
ifelse (x-cor = row)[
set col gap
set y-cor 0
while [ y-cor <= gap * no-of-row-or-col ]
[
ifelse (y-cor = col)[
create-seats 1 [
set shape "square"
set size 1.5
set color blue
setxy col row
set label who
set number-of-seats number-of-seats + 1
show (word row ", " col )
]
set col col + gap
set y-cor y-cor + 1
]
[set y-cor y-cor + 1]
]
set row row + gap
set x-cor x-cor + 1
]
[set x-cor x-cor + 1]
]
end
to place-lights
set row gap + half2
set col gap + half
set x-cor 0
set y-cor 0
while [ x-cor <= gap * no-of-row-or-col ]
[
ifelse (x-cor = row)[
set col gap + half
set y-cor 0
while [ y-cor <= gap * no-of-row ]
[
ifelse (y-cor = col)[
create-lights 1 [
set shape "pentagon"
set size 1
set color red
setxy row col
set number-of-lights number-of-lights + 1
show (word row "," col )
]
set col col + ( gap * 2)
set y-cor y-cor + 1
]
[set y-cor y-cor + 1]
]
set row row + ( gap * 2)
set x-cor x-cor + 1
]
[set x-cor x-cor + 1]
]
end
to place-fans
set row ( gap * 2 ) + half2
set col gap + half
set x-cor 0
set y-cor 0
while [ x-cor <= ( gap * no-of-row-or-col ) ]
[
ifelse (x-cor = row)[
set col gap + half
set y-cor 0
while [ y-cor <= ( gap * no-of-row ) ]
[
ifelse (y-cor = col)[
create-fans 1 [
set shape "x" ;; x shape
set size 1
set color red
setxy row col
set number-of-fans number-of-fans + 1
show (word row "," col )
]
set col col + ( gap * 2)
set y-cor y-cor + 1
]
[set y-cor y-cor + 1]
]
set row row + ( gap * 2)
set x-cor x-cor + 1
]
[set x-cor x-cor + 1]
]
end
to place-acs
set row 3
set col 13
set x-cor 0
set y-cor 0
while [ y-cor <= 45 ]
[
ifelse (y-cor = col)[
create-acs 1 [
set shape "star" ;; star shape
set size 1
set color red
setxy row col
set number-of-acs number-of-acs + 1
show (word row "," col )
]
set col col + 10
set y-cor y-cor + 1
]
[set y-cor y-cor + 1]
]
end
to go
set simulation-timer 0
output-show (word "timer = "simulation-timer )
tick
move-students
while [simulation-timer < time ] [
set simulation-timer simulation-timer + 1
output-show (word "timer = "simulation-timer )
]
end
to create-students-classroom
create-students number-of-students [
set entry-time random threshold + 1
let stu-no sort-on [who] students
foreach stu-no [x -> ask x [ show (word x " -> " entry-time ) ]
]
set shape "person"
set color 3
]
end
to move-students
let s sort [who] of seats
let a first s
let l length s
while [ l > (number-of-seats - number-of-students )] [
set temp1 simulation-timer
tick
tick
ask students [ if ( entry-time = temp1 )
[
move-to seat a ; If it does the student moves to a seat
set color red
appliance-on
energy-calculation
show (word temp1 "," l "," a)
set s remove a s
set a a + 1
set l length s
]
]
set simulation-timer simulation-timer + 1
output-show (word "timer = "simulation-timer )
]
end
to appliance-on
ask students [ ask lights in-radius 4
[ set color green ]]
ask students [ ask fans in-radius 4
[ set color green ]]
ask students [ ask acs in-radius 9
[ set color green ]]
stop
end
to energy-calculation
ask lights [ ifelse ( color = green ) [ set l-energy ( light-
wattage * (time - temp1 )) ] [ set l-energy 0 ] ]
ask fans [ ifelse ( color = green ) [ set f-energy ( fan-wattage
* ( time - temp1 )) ] [ set f-energy 0 ] ]
ask acs [ ifelse ( color = green ) [ set a-energy (ac-wattage *
(time - temp1 ))] [ set a-energy 0 ] ]
let light-e sum [l-energy] of lights
let fan-e sum [f-energy] of fans
let ac-e sum [a-energy] of acs
set t-light ( light-e / 60 )
set t-fan (fan-e / 60 )
set t-ac (ac-e / 60 )
show (word "total-ac-time = " t-ac )
set t-energy ( t-light + t-fan + t-ac )
end
In the BehaviorSpace: measure runs using these reporters I am putting energy-calculation but in the spreadsheet everything is showing zero. Why is this happening? When I am seeing the energy-calculation in a monitor it shows a value. What I want to do is run this code several times with different student numbers and get the varied energy-calculation each time. Or should I use file save in .csv for this situation?
You have written energy-calculation as a command that sets the global variable t-energy rather than as a reported that reports t-energy to the caller. I suspect that in your monitor you have used the t-energy variable which is reset each time energy-calculation is called. But in BehaviorSpace, the reporter by which runs are measured must actually be a reporter. It must return a value to BehaviorSpace. This is easily done by rewriting energy-calculation as
to-report energy-calculation
ask lights [ ifelse ( color = green ) [ set l-energy ( light-
wattage * (time - temp1 )) ] [ set l-energy 0 ] ]
ask fans [ ifelse ( color = green ) [ set f-energy ( fan-wattage
* ( time - temp1 )) ] [ set f-energy 0 ] ]
ask acs [ ifelse ( color = green ) [ set a-energy (ac-wattage *
(time - temp1 ))] [ set a-energy 0 ] ]
let light-e sum [l-energy] of lights
let fan-e sum [f-energy] of fans
let ac-e sum [a-energy] of acs
set t-light ( light-e / 60 )
set t-fan (fan-e / 60 )
set t-ac (ac-e / 60 )
show (word "total-ac-time = " t-ac )
set t-energy ( t-light + t-fan + t-ac )
report t-energy
end
Whether or not you still need the global t-energy or can simply replace it with the reporter that reports its value, would depend on how else t-energy is used in the code.
But, I am actually surprised that the BehaviorSpace experiment tries to run at all. BehaviorSpace should catch that energy-calculation is not a reporter and throw an error.