Choice based on other turtles' variables - netlogo

I'm stuck with my ABM. The situation is very simple: there are some turtles, producers, and others, choosers. Producers produce a product with a certain quality, choosers choose based on perceived quality.
Perceived quality is a function "quality-of-product-of-producers - beta-of-chooser", where beta is an individual adaptive variable. All these variables are turtles-own, and they all own them (at each round roles are switched).
I'd like the following to happen: quality-of-product is a producers' variable, beta is a choosers' variable. How to code the operation?
I've tried the following things:
ask turtles with [group = "producers"] [
set perceived-product-quality ((product-quality - beta))
]
However, like beta is producers' one (incorrect, should be choosers).
Then I've tried this:
ask turtles with [group = "choosers"] [
let quality [product-quality] of turtles with [ group = "producers" ]
set perceived-product-quality quality - beta
]
However, it doesn't work, runtime error: “expected input to be a number but got the list“.
How can I do it?

I'm not sure what your exact use case is, if you're trying to query the mean product quality / beta from all producers and choosers, but the general answer I think is that you need to be explicit with which turtles you're actually trying to pull values from. When you use ask, the agent that you are asking to do something will default to using its own variables if it owns them. So, if you are needing to stick with turtles-own variables rather than making breeds and breed-specific variables, you'll need to explicitly state which turtle you're trying to reference. Here is a toy example that has choosers pick the product quality of whatever producer is nearest for use in the formula:
turtles-own [ group product-quality beta perceived-product-quality]
to setup
ca
ask n-of 10 patches [
sprout 1 [
set group one-of [ "producers" "choosers" ]
]
]
ask turtles [
ifelse group = "producers" [
set product-quality random 500 + 500
set beta random 200 + 100
set color red
] [
set product-quality random 50 + 50
set beta random 20 + 10
set color blue
]
]
reset-ticks
end
to set-perceived
let current-producers turtles with [ group = "producers" ]
let current-choosers turtles with [ group = "choosers" ]
ask current-choosers [
; Pick the closest producer
let closest-producer min-one-of current-producers [ distance myself ]
; Pull the product quality of that closest producer
let closest-product-quality [ product-quality ] of closest-producer
; Set MY perceived product quality to be closest-product-quality - MY beta
set perceived-product-quality closest-product-quality - beta
show (
word "I used the turtle " closest-producer
" to determine my perceived product quality of: " perceived-product-quality
)
]
end
Edit:
Comment update:
I'd like the following to happen: choosers do the operation “perceived-product-quality - beta“ for all turtles in the Moore neighbour. After doing this, they are going to choose the product with highest value of perceived-product-quality.
In this version, there is a to-report procedure that reports the perceived product quality from the perspective of from-who. This can then be used with max-one-of to return the turtle with the highest perceived value.
turtles-own [ group product-quality beta perceived-product-quality]
to setup
ca
resize-world 0 5 0 5
set-patch-size 40
ask patches [
sprout 1 [
set group one-of [ "producers" "choosers" ]
]
]
ask turtles [
ifelse group = "producers" [
set product-quality random 500 + 500
set beta random 200 + 100
set color red
] [
set product-quality random 50 + 50
set beta random 20 + 10
set color blue
]
]
reset-ticks
end
to choose-best-moore
let current-choosers turtles with [ group = "choosers" ]
ask current-choosers [
let current-producers ( turtles-on neighbors ) with [ group = "producers" ]
ifelse any? current-producers [
let turtle-with-highest-perceived max-one-of current-producers [ perceived-target-value myself ]
let real-product-quality [product-quality] of turtle-with-highest-perceived
let false-product-quality [ perceived-target-value myself ] of turtle-with-highest-perceived
show ( word "I surveyed " count current-producers " and found that " turtle-with-highest-perceived
" had the best product quality (real: " real-product-quality ", perceived: " false-product-quality ")" )
] [
show "I found no producers in my neighboring cells"
]
]
end
to-report perceived-target-value [ from-who ]
let target-product-quality product-quality
let target-perceived-quality ( target-product-quality - [beta] of from-who )
report target-perceived-quality
end

Related

How can I modify this to-report procedure?

The situation is straightforward: there are some turtles, producers, and others, choosers. Producers produce a product with a certain objective quality. Choosers choose based on their perceived quality.
Perceived quality is a function "quality-of-product-of-producers - beta-of-chooser", where beta is an individual adaptive variable. All these variables are turtles-own, and they all own them (at each round, roles are switched).
So, choosers will select the best product with a max-one-of, based on their perceived quality (that is why beta has to be the value of the choosers).
Here's how the code is working (also thanks to Stack user #Luke C):
turtles-own [ group product-quality beta perceived-product-quality]
to setup
ca
resize-world 0 5 0 5
set-patch-size 40
ask patches [
sprout 1 [
set group one-of [ "producers" "choosers" ]
]
]
ask turtles [
ifelse group = "producers" [
set product-quality random 500 + 500
set beta random 200 + 100
set color red
] [
set product-quality random 50 + 50
set beta random 20 + 10
set color blue
]
]
reset-ticks
end
to choose-best-moore
let current-choosers turtles with [ group = "choosers" ]
ask current-choosers [
let current-producers ( turtles-on neighbors ) with [ group = "producers" ]
ifelse any? current-producers [
let turtle-with-highest-perceived max-one-of current-producers [ perceived-target-value myself ]
]
end
to-report perceived-target-value [ from-who ]
let target-product-quality product-quality
let target-perceived-quality ( target-product-quality - [beta] of from-who )
report target-perceived-quality
end
So far, so good. Now, I'd like the following thing to happen.
I introduced two kinds of producers: t-producers and f-producers. Choosers always choose based on “perceived-product-quality”. However, for t-producers, there is no “minus beta”, because they are trustworthy, so “product-quality = perceived-quality”.
How can I make choosers select the product with the highest perceived quality, but in one case, there is “- beta” and not in the other?
Thank you!
Currently, in my model, this process is twofold. First, choosers select the best product from t-producers, then select the best product from f-producers, compare the two and pick the highest. However, this has problems because it requires at least one type of producer in the neighborhood. How can I do it? Is there a simpler way?
The way you are doing it, getting two different values and comparing those two in the end, does work and there is not really anything wrong with it. The only problem you had was that you would need to check which ones are present before trying to do anything else with them.
if any? turtles with [group = "producers" and trustworthy? = true] [ ...]
A more elegant solution is probably to shove the condition into the reporter instead. An ifelse inside the reporter, evaluating the trustworthyness of the producer who is being assessed, can allow you to decide whether or not to detract beta from the product quality
to-report perceived-target-value [ from-who ]
let target-perceived-quality FALSE
ifelse trustworthy? [
set target-perceived-quality product-quality
][
set target-perceived-quality product-quality - [beta] of from-who
]
print (word "actual quality: " product-quality " , beta: " [beta] of from-who " , perceived quality: " target-perceived-quality " , trustworthy?: " trustworthy?)
report target-perceived-quality
end
I've also taken the liberty of changing your code a bit so that it now uses breeds instead of groups. I personally feel like it is much easier and cleaner to work with those. It also let me remove a few of your local variables
You can still let them change their breed every round, using set breed choosers and set breed producers.
breed [choosers chooser]
breed [producers producer]
turtles-own [beta product-quality trustworthy?]
to setup
ca
resize-world 0 5 0 5
set-patch-size 40
ask patches [
sprout 1 [
set beta random 20 + 10
set product-quality random 500 + 500
set trustworthy? one-of [true false]
]
]
ask turtles [
ifelse random-float 1 < 0.5 [
set breed producers
set color red
] [
set breed choosers
set color blue
]
]
reset-ticks
end
to choose-best-moore
ask choosers [
if any? producers [
let turtle-with-highest-perceived max-one-of producers [ perceived-target-value myself ]
]
]
end
to-report perceived-target-value [ from-who ]
let target-perceived-quality FALSE
ifelse trustworthy? [
set target-perceived-quality product-quality
][
set target-perceived-quality product-quality - [beta] of from-who
]
print (word "actual quality: " product-quality " , beta: " [beta] of from-who " , perceived quality: " target-perceived-quality " , trustworthy?: " trustworthy?)
report target-perceived-quality
end

Netlogo: How to make a turtle move towards an unique patch target?

I have turtles (patients), and they can only use only one bed each (white patch). Since patients are randomly generated in a waiting room (green patches), sometimes two or more of them get at the same distance and therefore they find the same patch as its target. I tried to add an attribute to the patch with the purpose of assigning that particular bed to a specific patient. The idea is something like this (please indulge on the ugly code, I'm learning :P):
globals [
waitxmax
waitxmin
waitymax
waitymin
box
]
breed [ patients patient ]
patients-own [ target ]
patches-own [ assigned ]
to setup-wait-room
set waitxmax -15
set waitxmin 15
set waitymax 11
set waitymin 15
ask patches with [
pxcor >= waitxmax and
pxcor <= waitxmin and
pycor >= waitymax and
pycor <= waitymin
] [ set pcolor green ]
end
to setup-beds
let cmy 7
let cmx 15
let dst 3
let nbox 7
ask patch cmx cmy [ set pcolor white ]
let i 1
while [ i < nbox ] [
ask patch (cmx - dst) cmy [ set pcolor white ]
set i i + 1
set cmx cmx - dst
]
ask patches with [ pcolor = white ] [ set assigned false ]
set box patches with [ pcolor = white ]
end
to setup-patients
create-patients start-patients [
set shape "person"
set target nobody
move-to one-of patches with [ pcolor = green ] ]
end
to setup [
clear-all
setup-wait-room
setup-beds
reset-ticks
]
to go
ask patients [ go-to-bed ]
tick
end
to go-to-bed
let _p box with [ self != [ patch-here ] of myself ]
if target = nobody [
set target min-one-of _p [ distance myself ]
ask target [ set assigned myself ]
]
;;; FIXME
if ([ assigned ] of target) != self [ show "not true" ]
if target != nobody [
face target
fd 1
]
end
When I print the two sides of the comparison below FIXME, from the command center I actually get the expected result. For example: both patient 0 and patient 1 have the same target (patch -3 7), but that patch is assigned to (patient 0). I would have expected that comparison to force patient 1 to get a new target since the bed doesn't have his name (I haven't written that code yet), but it always evaluates to true. This is more notorious as more patients I add over available beds (if no beds available, they should wait as soon as one gets free).
When inspecting trough the interface I also see that the patch -3 7 says (patient 0), so I don't know what's happening. Command center example:
observer> show [ assigned ] of patch -3 7
observer: (patient 0)
observer> if ([ assigned ] of patch -3 7) = [self] of patient 0 [ show "true" ]
observer: "true"
observer> if ([ assigned ] of patch -3 7) = [self] of patient 1 [ show "true" ]
;;;; SETUP AND GO
(patient 0): (patch -3 7)
(patient 0): (patient 0)
(patient 0): "true"
(patient 2): (patch 12 7)
(patient 2): (patient 2)
(patient 2): "true"
(patient 1): (patch -3 7)
(patient 1): (patient 1)
(patient 1): "true"
Maybe I'm just overthinking this and there are is a simpler way to assign a bed to a patient and vice versa?
There seems to be a chunk or two missing from your code above (I can't copy-paste and run it), so please have a look at the option below.
This version works by having a single place to store the 'claimed' beds- in the turtle variable. Since the turtle variables can be queried as a list using of, a bed-less turtle can check if there are any beds that are not already present in that list and, if so, claim one.
turtles-own [ owned-bed ]
to setup
ca
ask n-of 5 patches [
set pcolor green
]
crt 10 [
set owned-bed nobody
claim-unclaimed-bed
if owned-bed != nobody [
print word "I own the bed " owned-bed
]
]
reset-ticks
end
to claim-unclaimed-bed
; If I have no bed
if owned-bed = nobody [
; Pull the current owned beds for comparison
let all-owned-beds [owned-bed] of turtles
; Pull those beds that are green AND are not found in 'all-owned-beds'
let available-beds patches with [
pcolor = green and not member? self all-owned-beds
]
; If there are any beds available, claim one
ifelse any? available-beds [
set owned-bed one-of available-beds
] [
; If there are none available, print so
print "There are no available beds."
]
]
end
Edit: Forgot the actual question title- to actually move to their owned-bed (if they have one) in the example above, they simply face it and move how you like- for example:
to go
ask turtles with [ owned-bed != nobody ] [
ifelse distance owned-bed > 1 [
face owned-bed
fd 1
] [
move-to owned-bed
]
]
tick
end
Edit: added complexity
Ok, for an added element of severity, you will likely want to avoid using multiple with [ ... = x statements, and instead to use a primitive called min-one-of which returns the agent with the minimum value of some reporter. Then, you want to tell NetLogo to keep asking the next most severe and the next most severe, etc. One way to do this is with a while loop, which basically says "While this condition is met, continue evaluating the following code." Be careful with while loops- if you forget to write your code such that eventually the condition is no longer true, the while loop will just continue running until you will eventually Tools > Halt your model (or, as has happened to me with a large model, NetLogo crashes).
I've reworked the code (much of what was above is unchanged) to include such a while loop. Note as well that, since the model needs to check which beds are available multiple times, I've made that code into a to-report chunk to condense / simplify.
turtles-own [ owned-bed severity ]
to setup
ca
ask n-of 5 patches [
set pcolor green
]
crt 10 [
set owned-bed nobody
set severity random-float 10
]
let current-available-beds report-available-beds
while [any? current-available-beds] [
; From the turtles with no bed, ask the one with the lowest severity number to
; claim an unclaimed bed. Then, reset the current-available-beds
ask min-one-of ( turtles with [owned-bed = nobody] ) [ severity ] [
claim-unclaimed-bed
if owned-bed != nobody [
show ( word "I have a severity of " severity " so I am claiming the bed " owned-bed )
]
]
set current-available-beds report-available-beds
]
reset-ticks
end
to-report report-available-beds
let all-owned-beds [owned-bed] of turtles
report patches with [
pcolor = green and not member? self all-owned-beds
]
end
to claim-unclaimed-bed
; If I have no bed
if owned-bed = nobody [
let available-beds report-available-beds
; If there are any beds available, claim one
ifelse any? available-beds [
set owned-bed one-of available-beds
] [
; If there are none available, print so
print "There are no available beds."
]
]
end

Netlogo: How to ask link-neighbor to do something

I am simulating a classroom. In a classroom there are about 90 seats, 20 lights, 20 fans and 4 ACs. I create num-of-students where each student has an assigned random entry-time. When each student enters after a random time and sits on a seat the corresponding (in-radius)light or fan or ac turn on and a link is established between the the student and the in-radius appliance. Each appliance (e,g light, fan and AC) has their own wattage value. After all the students sits down the total energy consumption (in KWh) will be calculated for all the fans, lights and ACs.
How can I store the on-time of each appliance e.g (time minus entry-time), where time is the total class time. So that afterwards I can sum all the on-time of each appliance(e.g fan) and multiply it with its watt value. This is the part of the code.
globals[
temp1 simulation-timer number-of-seats number-of-lights number-
of-fans number-of-acs ]
breed [lights light]
breed [fans fan]
breed [acs ac ]
breed [students student ]
to setup
...
...
place-seats-students
place-lights
place-fans
place-acs
create-students-classroom
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 )
] ]
ask students [
set shape "person"
set color 3
] ]
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 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
set color red
ask students
[create-links-with lights in-radius 5
create-links-with fans in-radius 5
create-links-with acs in-radius 9 ]
show (word "number of links is" count links)
appliance-on
store-on-time
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 with [color = red ]
[ask my-links
[ask other-end [set color green] ] ]
stop
end
to store-on-time
ask students [
ask link-neighbor fan ifelse it is on [
let on-time [ time - entry-time ]
[do nothing]
ask students [
ask link-neighbor light ifelse it is on [
let on-time [ time - entry-time ]
[do nothing]
end
How can I write the store-on-time procedure, so that later I will be able sum to all the on-times for all the appliance to calculate the KWh consumed. Any help will be greatly appreciated.
If you need to store something, then you need to create a variable for it. Since they might turn on, off, on etc, I would personally have two variables for each appliance (eg lights-own and fans-own etc). For lights, they could be named light-on-time, light-on-duration and similarly for others.
The way this works in code is add
set light-on-time ticks
whenever you turn the light on. And have
set light-on-duration light-on-duration + ticks - light-on-time + 1
whenever you turn the light off. The reporter ticks is just the current state of the clock.

How to select up to a maximum number of turtles using roulette wheel selection

In my model the turtles have two sexes where the males have two potential tactics. The females count the number of males in a set radius.
I want the females to weight their probability of selecting from the group of males (without replacement) depending on the relative frequency of the two male tactics.
I already have the code for the probability of selecting from the males (matingPoolProbAnad and matingPoolProbRes) but I don't know how to implement it, though the rnd extension seems the way to go, specifically rnd:weighted-n-of size agentset [ reporter ].
It's complicated by three things (1) the males can mate with more than one female but (2) only once with a given female and (3) females can only mate with a maximum of five males.
to count-mates ; ask the females to count the number of males in a 10 patch radius & then
; determine the frequency of the resident males in their patch
ask turtles with [sex = "female"]
[
if any? turtles with [sex = "male"] in-radius 10
[ set potentialMates turtles with [sex = "male"] in-radius 10
ifelse any? potentialMates with [anadromousM = 1]
[ set FA count potentialMates with [anadromousM = 1] / count potentialMates ]
[ set FA 0]
ifelse any? potentialMates with [anadromousM = 0]
[ set FR count potentialMates with [anadromousM = 0] / count potentialMates ]
[ set FR 0]
]
]
end
to mating-pool-prob ; negative frequency dependency which is based on the number of male
; resident turtles
ask turtles with [sex = "female"]
[
ifelse (FA = 1) and (FR = 0)[
set matingPoolProbAnad 1
set matingPoolProbRes 0
]
[ifelse (FA > 0) and (FR < 1)
[
set matingPoolProbRes exp(a - b * (FR - c ))/(1 + exp(a - b * (FR - c)))
set matingPoolProbAnad 1 - matingPoolProbRes
]
[
set matingPoolProbAnad 0
set matingPoolProbRes 1
]
]
]
end
This example may approach what you're getting at, but obviously would need to be adapted from this toy version. This setup sprouts 75% of males with strategy A and the rest with strategy B, and gives all turtles an empty agentset of mates to start off:
breed [ males male ]
breed [ females female ]
turtles-own [ mates ]
males-own [ strategy ]
females-own [ max-mate-count mate-count ]
to setup
ca
ask n-of 200 patches [
sprout-males 1 [
ifelse random-float 1 < 0.75 [
set strategy "A"
set color orange
] [
set strategy "B"
set color violet
]
]
]
ask n-of 50 patches with [ not any? turtles-here ] [
sprout-females 1 [
set color green
]
]
ask turtles [
set mates ( turtle-set )
]
reset-ticks
end
Use a while loop to have each female iteratively assess the strategy proportions of the males available to her, then add them to her 'mates' list. More detail in comments:
to choose-mates
ask females [
; set a cap on possible mates for females; 5, or the number
; available within the radius if less than 5
let availa-males males in-radius 10
let n-max count availa-males
set max-mate-count ifelse-value ( n-max < 5 ) [ n-max ] [ 5 ]
; Until a female has chosen up to her maximum number of mates:
while [ mate-count < max-mate-count ] [
; determine which available males are not already in her 'mates' agentset
set availa-males availa-males with [ not member? self [mates] of myself ]
; assess the proportion of B strategy in remaining available males
let prop_B ( count availa-males with [ strategy = "B" ] ) / n-max
; example probability choice, just meant to choose B males
; with a frequency disproportionate to availability
let proba_B ifelse-value ( prop_b * 2 < 0.6 ) [ prop_b * 2 ] [ 0.6 ]
; use a random float to determine which strategy type is chosen
set mates ( turtle-set mates ifelse-value ( random-float 1 < proba_B )
[ one-of availa-males with [ strategy = "B" ] ]
[ one-of availa-males with [ strategy = "A" ] ] )
; count the current mates to break the while loop once
; the maximum number of mates is reached
set mate-count count mates
]
; have the female's males add her to their own mates agentset
ask mates [
set mates ( turtle-set mates myself )
]
]
end
To check that 'B' males are being chosen disproportionately to their availability:
to check-values
let all-mates map [ i -> [strategy] of i ] [mates] of females
print word "Average proportion of 'B' mates chosen: " mean map b-proportion all-mates
print word "Actual proportion of 'B' males: " ( ( count males with [ strategy = "B" ] ) / count males )
end
to-report b-proportion [ input_list ]
let tot length input_list
let nb length filter [ i -> i = "B" ] input_list
report nb / tot
end
I'm not 100% sure that that's what you're after- maybe you can use the rnd package to clean up the loop.
Edit in response to comment
If you modify the end of the `choose-mates like so:
...
...
; have the female's males add her to their own mates agentset
ask mates [
set mates ( turtle-set mates myself )
]
if n-max < count mates [
print "Fewer available males than mates"
]
]
end
And your go looks like:
to go
choose-mates
end
You can run setup and go as many times as you like and you should never see the printout "Fewer available males than mates":
to repeat-1000
repeat 1000 [
setup
go
]
end
I ran that a few times and never had count availa-males be less than the count of mates. However, if you add in movement without allowing the females to reset their mates agentset, you do start to see it- for example, try running this a few times:
to go
choose-mates
ask turtles [ fd 1 ]
end
Now, because the turtles are moving around, you have some cases where females held on to their mates from the previous function call and then moved into a space where there were fewer availa-males. The quick and easy fix is to have females clear their mates each time. Where you do that depends on your model goals (how often do females choose mates? Do they only forget some of their previous ones? etc), but here's a very simple way:
to go
ask turtles [ set mates ( turtle-set ) ]
choose-mates
ask turtles [ fd 1 ]
end
Now you can run that as many times as you like and shouldn't see the "Fewer available males than mates" printout.

Subtract. SET variableX-variableY only once

I'm trying to set a resource variable. It will be time and will function like sugar in sugarscape. Its setup is: ask agentes [set time random-in-range 1 6].
The thing is... I want the agentesto participate in activities linking like we said here. But, with each participation, it should subtract a unity of agentes's time. I imagine it must be with foreachbut I seem to be unable to grasp how this works.
ask n-of n-to-link agentes with [n-t-activity = [n-t-activity] of myself] in-radius sight-radius [
while [time >= 2] [
create-participation-with myself [ set color [color] of myself ] ]
foreach (command I don't know)[
set time time - count participations]]
Essentially, I want the agentes to look if they have time to participate. If they do, they create the link and subtract 1 to their time. Only ONE per participation. If they have 3 time, they'll have 2 participations and 1 time. If they have 1 time, they won't have links at all.
EDIT
You're right. I don't need while. About foreach, every place I looked said the same thing but I can't think of other way. About colors, they're only for show purpose.
The relationship between time and participation counts is as follows: the agentes have time they can spend in activities. They participate if time>=2. But every participation (link with activity) consumes 1 time when the link is active (I didn't write the decay code yet; they'll regain their time when it is off).
EDIT V2
Nothing, it keeps subtracting even with the []. Maybe the best choice is if I give you the code so you can try it. You'll have to set 5 sliders: prob-female (53%), initial-people (around 200), num-activity (around 20), n-capacity (around 25) and sight-radius (around 7). And two buttons, setup and go. I also set a patch size of 10 with 30 max-pxcor and max-pycor. Here is the code. Sorry if I'm not clear enough!
undirected-link-breed [participations participation]
turtles-own [
n-t-activity
]
breed [activities activity]
activities-own [
t-culture-tags
shared-culture
]
breed [agentes agente]
agentes-own [
gender
time
culture-tags
shared-culture
]
to setup
clear-all
setup-world
setup-people-quotes
setup-activities
reset-ticks
END
to setup-world
ask patches [set pcolor white]
END
to setup-people-quotes
let quote (prob-female / 100 * initial-people)
create-agentes initial-people
[ while [any? other turtles-here ]
[ setxy random-xcor random-ycor ]
set gender "male" set color black
]
ask n-of quote agentes
[ set gender "female" set color blue
]
ask agentes [
set culture-tags n-values 11 [random 2]
set shared-culture (filter [ i -> i = 0 ] culture-tags)
]
ask agentes [
set time random-in-range 1 6
]
ask agentes [
assign-n-t-activity
]
END
to setup-activities
create-activities num-activity [
set shape "box"
set size 2
set xcor random-xcor
set ycor random-ycor
ask activities [
set t-culture-tags n-values 11 [random 2]
set shared-culture (filter [i -> i = 0] t-culture-tags)
]
ask activities [
assign-n-t-activity]
]
END
to assign-n-t-activity
if length shared-culture <= 4 [
set n-t-activity ["red"]
set color red
]
if length shared-culture = 5 [
set n-t-activity ["green"]
set color green
]
if length shared-culture = 6 [
set n-t-activity ["green"]
set color green
]
if length shared-culture >= 7 [
set n-t-activity ["black"]
set color black
]
END
to go
move-agentes
participate
tick
end
to move-agentes
ask agentes [
if time >= 2 [
rt random 40
lt random 40
fd 0.3
]
]
end
to participate
ask activities [
if count my-links < n-capacity [
let n-to-link ( n-capacity - count my-links)
let n-agentes-in-radius count (
agentes with [
n-t-activity = [n-t-activity] of myself ] in-radius sight-radius)
if n-agentes-in-radius < n-to-link [
set n-to-link n-agentes-in-radius
]
ask n-of n-to-link agentes with [
n-t-activity = [n-t-activity] of myself] in-radius sight-radius [
if time >= 2 [
create-participation-with myself [
set color [color] of myself ]
ask agentes [set time time - count my-participations] ]
]
ask activities [
if not any? agentes in-radius sight-radius [
ask participations [die]
]
]
]
]
end
to-report random-in-range [low high]
report low + random (high - low + 1)
END
EDIT V3
I asked Bill Rand to help me and he solved the problem. The issue was in this line: let candidates agentes with [ n-t-activity = [n-t-activity] of myself ] in-radius sight-radius. He solved the problem this way: let candidates agentes with [ n-t-activity = [n-t-activity] of myself and not participation-neighbor? myself ] in-radius sight-radius. Being this and not participation-neighbor? myself the condition to make sure that the agente is not already a part of that activity.
You almost never need foreach in NetLogo. If you find yourself thinking you need foreach, your immediate reaction should be that you need ask. In particular, if you are iterating through a group of agents, this is what ask does and you should only be using foreach when you need to iterate through a list (and that list should be something other than agents). Looking at your code, you probably don't want the while loop either.
UPDATED FOR COMMENTS and code - you definitely do not need while or foreach.
Your problem is the following code. You ask agentes that satisfy your conditions to create the links, but then you ask ALL AGENTES to change their time (line I have marked), not just the agentes that are creating participation links.
ask n-of n-to-link agentes with [
n-t-activity = [n-t-activity] of myself] in-radius sight-radius [
if time >= 2 [
create-participation-with myself [
set color [color] of myself ]
ask agentes [set time time - count my-participations] ] ; THIS LINE
]
The following code fixes this problem. I have also done something else to simplify reading and also make the code more efficient - I created an agentset (called candidates) of the agentes that satisfy the conditions. In this code, the candidates set is only created once (for each activity) instead of twice (for each activity) because you are creating it to count it and then creating it again to use for participation link generation.
to participate
ask activities
[ if count my-links < n-capacity
[ let candidates agentes with [
n-t-activity = [n-t-activity] of myself ] in-radius sight-radius
let n-to-link min (list (n-capacity - count my-links) (count candidates ) )
ask n-of n-to-link candidates
[ if time >= 2
[ create-participation-with myself [ set color [color] of myself ]
set time time - count my-participations ] ; REPLACED WITH THIS LINE
]
ask activities [
if not any? agentes in-radius sight-radius [
ask participations [die]
]
]
]
]
end