NetLogo agentset form links with other agentset - netlogo

I'm looking to have a turtle-set form links with other turtle-set.
My current attempt that also feels contrived, as not every hive here will be selected.Is there another way of going about this?
to link-bees-to-hives [bees-agentset hives-agentset]
ask bees-agentset [
create-link-with one-of hives-agentset
]
end
How can I create links between two agentsets netlogo, in order of the turtles in the set?

Do you want to have bees make a link with just one other hive? If you have relatively enough bees your attempt is probably fine, but if you want to weight the bee selection so that they preferentially link with hives with fewer associate bees, you could use some kind of min-one-of solution or a perhaps something from the rnd extension. For example, a bee and hive setup:
extensions [ rnd ]
breed [ bees bee ]
breed [ hives hive ]
to setup
ca
create-hives 3 [
set color white
set shape "box"
set size 2
setxy random-xcor random-ycor
]
create-bees 15 [
set color yellow
set shape "bug"
setxy random-xcor random-ycor
]
reset-ticks
end
And the weighted selection:
to link-bee-to-hive
ask bees [
create-link-with rnd:weighted-one-of hives [ 1 - count my-links / count bees ]
]
print [ count my-links ] of hives
end
Of course, if you have few enough bees and hives, you may still end up with a hive or two not getting linked-to.

Related

how to call for separate procedures using the ifelse any? function in netlogo?

In brief below is the code I wrote to show whether there is a turtle on the neighbor patches
to play-the-game
ifelse any? turtles-on neighbors4
[show "turtles-found"]
[show "turtles-not-found"]
end
I need to change it to perform the procedures I have already written out;
if they are the same breed they 'gain-energy'
different breed 'fight-opponent'
I am not sure as to how to change the first part to carry out the other procedures.
If I understand correctly, you need to check the breed of both the asking turtle and the 'opponent' turtle. You're certainly on the right track by checking if there are any neighbors present, the next step is to make the breed check and then the turtles can choose what action to take. For example, look at this toy model where wolves and cows can identify whether they've landed on a patch next to the same breed or not:
breed [ cows cow ]
breed [ wolves wolf ]
to setup
ca
ask n-of 10 patches [
sprout-cows 1 [
set shape "cow"
set color white
]
]
ask n-of 10 patches with [ not any? turtles-here ] [
sprout-wolves 1 [
set shape "wolf"
set color red - 1
]
]
reset-ticks
end
to go
ask turtles [
face one-of neighbors
fd 1
play-the-game
]
tick
end
to play-the-game
if any? turtles-on neighbors4 [
let current-neighbor-turtle one-of turtles-on neighbors4
ifelse [breed] of current-neighbor-turtle = breed [
show "I see one of my own breed!"
] [
show "I see an opponent!!!"
]
]
end
If that's not quite what you had in mind, please edit your question above to provide more detail.
I think you should consider dropping ifelse. In your example, the two possible outcomes of the any? turtles-on neighbors4 condition are mutually exclusive: either there are turtles, or there are not.
However what you want to achieve in your model is a bit different: (I imagine that) on the neighboring cells there can be turtles of the same breed AND turtles of a different breed at the same time. In this case, the two scenarios are not mutually exclusive and using ifelse (where only one or the other of the two command blocks will be executed) would potentially overlook one of the two cases, depending on how you specify the condition.
I think two if statements would be more appropriate:
to play-the-game
if (any-friends-nearby?) [gain-energy]
if (any-opponents-nearby?) [fight]
end
to-report any-friends-nearby?
report (any? (turtles-on neighbors4) with [breed = [breed] of myself])
end
to-report any-opponents-nearby?
report (any? (turtles-on neighbors4) with [breed != [breed] of myself])
end
It is still relevant for you to choose which condition will be checked first (and therefore which action, between gain-energy and fight, will be executed first in case both conditions are satisfied)

How to create a given number of turtles based on a distance criterion

We're creating code and we're having trouble doing the following:
We would like to create a specific number of turtles (for example 100 turtles) based on the following criteria:
distance between agents must be greater than or equal to 2
We've already tried:
to setup
ask n-of 100 patches [
if not any? turtles in-radius 2 [
sprout-turtles 1 [ setup-turtles ] ]
]
]
end
to setup-turtles
set energy 0
set size 1
set color orange
end
it didn't work, because less than 100 agents are being born even though the world holds the required amount of agents, which in this case is 100
Does anyone have any suggestions on how we can resolve this issue?
Thanks in advance
The approach you outline is running into issues because you are asking patches to sprout turtles if they meet some condition. Because the patches are operating in a random sequential order, you are selecting some patches that, by the time it is their turn to act, no longer fulfill the condition to sprout since other nearby patches have already sprouted a nearby turtle.
One option is to use a while loop to keep trying to sprout turtles until the desired number is reached:
to setup
ca
while [ count turtles < 100 ] [
ask one-of patches with [ not any? turtles in-radius 2 ] [
sprout 1
]
]
reset-ticks
end
Be careful with while loops- if you do not code such that the while condition can eventually become false, your model will run forever stuck in the loop.
Another option that will give a more explicit error if it fails would be to just create your number of turtles, then have them move to a space that fulfills the condition:
to setup-2
ca
crt 100 [
move-to one-of patches with [ not any? turtles in-radius 2 ]
]
reset-ticks
end

Using the command move-to with max-one-of and the error appears: MOVE-TO expected input to be an agent but got NOBODY instead

I'm new to NetLogo and I have a question that I'm sure is pretty basic. But, I'm not getting over the difficulty.
If anyone can help me overcome this difficulty, I would be very grateful.
I would like from the patch where the turtle is found to consider the 8 neighboring cells in search of the highest pveg value. If it has equally high values, choose 1 of these randomly. Upon finding the highest pveg value of the neighbors, the turtle went there.
I am using the command: max-one-of. I think it serves my purpose. But, I'm making some syntax error that shows the following error: MOVE-TO expected input to be an agent but got NOBODY instead.
Thanks in advance
extensions [ gis ]
globals [ veg ]
patches-own [pveg]
to setup
clear-all
reset-ticks
setup-patches
crt 1 [
ask neighbors [ set pcolor blue ]
set color black
]
end
to setup-patches
end
to go
ask turtles [neighboring]
end
to neighboring
let my-neighWith-pveg [ neighbors with [pveg > 0.2] ]of patch-here
ifelse neighWith-pveg = 0
[ ]
[ move-to max-one-of patches [my-neighWith-pveg] set pcolor red ;;ERROR HERE
]
end
The NetLogo dictionary says, max-one-of needs an agentset and a reporter as input:
max-one-of agentset [reporter]
In your code, you use two agentsets: turtles and my-neighWith-pveg
Since you want to chose from the neighbors (and not all turtles) with the hightes pveg, you can write:
max-one-of my-neighWith-pveg [pveg]

How would I "target" patches within a certain area?

I have set up agents and nodes to represent people and stores and it is my intention that the agents will "target" the store in their "awareness" space with the highest value ("vulnerability"). I've largely coded what I have so far through trial and error however setting the turtle's target to the patch with the highest value within a 10 unit radius is a hurdle I can't get over. Currently they target the patch with the highest value regardless of its position in the world. Could somebody suggest what I might consider to achieve this please? I have pasted what I have written so far for reference.
Thanks.
breed [shoplifters a-shoplifter]
patches-own [vulnerability]
shoplifters-own [target
awareness]
to setup
clear-all
setup-patches
setup-turtles
reset-ticks
end
to setup-patches
setup-stores
end
to setup-stores
ask n-of num-stores patches [ set pcolor lime ] ;; create 'num-stores' randomly
ask patches [
if pcolor = lime
[ set vulnerability random 100
]
]
end
to setup-turtles
setup-shoplifters
setup-target
end
to setup-shoplifters
create-shoplifters num-shoplifters [ ;; create 'num-turtles' shoplifters randomly
set xcor random-xcor
set ycor random-ycor
set shape "person"
set color red
]
end
to setup-awareness
ask turtles [
set awareness
patches in-radius 10
]
end
to setup-target
ask turtles [
set target
max-one-of patches [vulnerability]
]
end
You are on the right track using max-one-of. At the moment, however, you are sending patches as the space to search through to look for the one with maximum vulnerability value, when you really want patches in-radius 10. So you could simply do this:
to setup-target
ask turtles [
set target max-one-of patches in-radius 10 [vulnerability]
]
end
However, this is going to be inefficient because NetLogo will have to first work out which are the patches within the radius. You have already asked the turtles to work this out and assign it to their variable 'awareness'. What you really want to do is therefore:
to setup-target
ask shoplifters [
set target max-one-of patches awareness [vulnerability]
]
end
Note that I also changed ask turtles to ask shoplifters. It is only shoplifters who have the attribute 'target' so you should only be asking them to calculate it. Same thing goes for 'awareness'. At the moment you don't have any other breeds so it's not causing an error, but it is good practice to use the breed, otherwise there is no point in creating it.

NetLogo rnd:weighted-n-of by turtle variable

I am trying to create a generator that uses the (corrected) Barabasi-Albert algorithm for a preferential attachment network in NetLogo. There are two parameters: number of (eventual) nodes, and number of edges added per node. The network extension has a version, but it is restricted to the case with 1 edge added per node.
Simplified complete model:
extensions [rnd]
to makeNW-BA
clear-all
let new-edges 4
let popn 25
create-turtles new-edges [ setxy random-xcor random-ycor ]
ask turtles [ create-links-with other turtles with [not link-neighbor? myself] ]
repeat popn - count turtles
[ let targets rnd:weighted-n-of new-edges turtles [ count my-links ]
create-turtles 1
[ setxy random-xcor random-ycor
create-links-with targets
]
]
end
The line let targets rnd:weighted-n-of degree turtles [ count my-links ] is creating a java error (ClassCastException) while observer running _asm_proceduremakenwba_setprocedurevariable_11. This is the first time I have used the rnd extension so I don't know if the problem is my coding, or there's actually a bug causing the java error.
UPDATE
I have now set up a turtles-own variable for degree (ie count my-links) and trying to do let targets rnd:weighted-n-of new-edges turtles [ degree ]. This gets me a NetLogo error instead, that the observer can't access a turtle variable without specifying which turtle. However, trying to add of self doesn't help.
Does this produce the desired network?
let targets rnd:weighted-n-of new-edges turtles [ [count my-links] of ? ]
It is hard for me to eyeball it with new-eges set to 4. When I set it to 1 it seems to make a preferential attachment network.
It appears that (and let the voice of God disagree with me) [rnd:weighted-n-of] was designed to work with lists and has a hidden foreach or map in it resulting it an error in casting when given an agent set.