Logical Statement Determining if the intersection between two agentsets is empty - netlogo

I have seen on here how to create an intersection or union of two agentsets, but I am trying to say if any turtle in agentset a is in agentset b, return true. I was trying
ifelse (member? (one-of my-intersections) destination-intersections)
but I am pretty sure this is just testing if one element in my-intersections is in destination -intersections instead of testing every element. Is there some way to use a for each? Or is there another functionality I am unaware of?
Again, I have already referenced
NetLogo two agentsets operations
Thank you!!

The most straightforward way to write such a test is:
to-report test [ as bs ]
report any? as with [ member? self bs ]
end
To try it:
create-turtles 5
show test (turtle-set turtle 0 turtle 1 turtle 2) (turtle-set turtle 3 turtle 4)
show test (turtle-set turtle 0 turtle 1 turtle 2) (turtle-set turtle 0 turtle 3 turtle 4)
will show:
observer: false
observer: true
It's not the most efficient way, however, because the with clause builds an intermediate agentset that's not really needed.
A faster test would be:
to-report test [ as bs ]
let result false
ask as [
if member? self bs [
set result true
stop
]
]
report result
end
Edit:
I spoke too fast. As per the NetLogo wiki page on compiler architecture, the combination of any? and with does get optimized to exit early. Bottom line: you should use the first version (i.e., any? as with [ member? self bs ]).

Related

Operator > and combining it with number and a string in NetLogo

I want a little help. I want turtles to cooperate with 50% probability if their land parcels are > 5.
I am writing as
ifelse random 1 = 0 and land > 5
[set cooperate? true]
[set cooperate? false]
But it gives error that; The > operator can only be used on two numbers, two strings, or two agents of the same type, but not on a string and a number.
how to correct it?
Thanks
There's not quite enough information to diagnose the problem. Is this code inside an ask turtles block with the variable 'land' as a turtles-own attribute? Also, you might want to print off some values of 'land' to make sure you actually have numbers in it.
As you can see from the working example below, there is no error in the code you have provided.
turtles-own [ land cooperate? ]
to testme
clear-all
create-turtles 10
[ set land random 10
ifelse random 1 = 0 and land > 5
[ set cooperate? true ]
[ set cooperate? false ]
]
type "Cooperating: " print count turtles with [cooperate?]
type "Not cooperating: " print count turtles with [not cooperate?]
end

adding agentset from different agents togother into a let

my model is a network of agents connected to each other with links.
I try to create a agentset from the neighbors of an agents and their neigbors and so on (I need this to assign different values to it).
However when I create a let with the agentset in it. the agents asked to make this agentset all have their own, this is so far so good. But when I want the original agent to ask him his second line neighbors he just returns an agentset from one of this neighbors instead of the combined agentsets of all his second line neighbors
I want the neighbors to store their own neighbors into a agentset with all the neighbors from the different agents in that set.
I cant ask the let agentset to simple do turtleset current-agentset new-agentset since in a let you cant ask to let variable. So a code which would normally be set second-neighbors (turtle-set second-neighbors other-nieghbors doesnt work since I cant ask second-neighbors already in a let
I also cant make this a global or somethins since it is agent specific.
the code I have so far looks like this
ask companies [
let this-company self
let b link-neighbors
ask b [ let c link-neighbors with [self != this-company]
ask c [ let d link-neighbors with [not member? self b]
ask this-company [
set iburen b
set iiburen c
set iiiburen d
]
]
]
]
so what I want is that all the agents in the agentset c report their link-neighbors like they do now. But also store these link-neighbors into a new agentset which has all the link-neighbors of all the agents in c. like a simple i i + 1. but than with turtle-set (what I have) (what is new from the next agent asked)
the same goes for d
If I run the model now agents report different agentset almost every tick. They just pick one agentset from any of these agents instead of combining them all togother.
Here is what I think you need:
extensions [ nw ]
breed [ companies company ]
companies-own [
buren ; a list of agentsets, with one item for each "level" of neighbors
]
to setup
clear-all
; create a random network and lay it out:
create-companies 20 [ create-links-with n-of 3 other companies ]
repeat 30 [ layout-spring turtles links 0.2 5 1 ]
let num-levels 3
ask companies [
let all-neighbors other nw:turtles-in-radius num-levels
set buren (list) ; initialize to empty list
foreach range num-levels [ i ->
let neighbors-at-this-level all-neighbors with [
nw:distance-to myself = i + 1
]
set buren lput neighbors-at-this-level buren
]
]
; demonstrate how to access the levels (sorted only for display purposes)
ask one-of companies [
show sort item 0 buren ; first level neighbors
show sort item 1 buren ; second level neighbors
show sort item 2 buren ; third level neighbors
]
end
This might not be the most efficient code possible, because it goes through the list of all neighbors once for each level, but unless you have a humongous network, you should not notice.
If you really wanted to use variables like iburen, iiburen and iiiburen, you could always alias the items of the list:
set iburen item 0 buren
set iiburen item 1 buren
set iiiburen item 2 buren
...but I don't recommend it. Having your agentsets in a list should encourage you to think of your levels in a more general way.

NetLogo in-radius

i have this code and it's not clear to me what is it doing:
patches-own [ field ]
let a max-one-of patches in-radius b [field]
ifelse ([field] of a > 0.1) and ([field] of a < 0.5)
[
;; do something
]
[
;; do something else
]
Thanks,
Marco
This is apparently code to be run by a turtle or patch, it isn't apparent which.
patches in-radius b is an agentset of the circle of patches, of radius b, around the calling agent. max-one-of ... [field] finds the patch in that agentset that has the largest value for field. That patch is then stored in the new local variable a. (A better name than a might have been winner or peak or best-patch.)
[field] of a is then that maximum value of field, the same one that max-one-of found. The ifelse checks to see if that value is in a certain range or not, and does something different, depending.
Does the code inside the ifelse make any further use of a? If it does, cool. If it doesn't, well, the code could be more easily and simply written as:
let m max [field] of patches in-radius b
ifelse m > 0.1 and m < 0.5
[
;; do something
]
[
;; do something else
]
perhaps seeing it in this form will help making the meaning clear.

how to sprout around a turtle in netlogo?

I have been looking to spread around turtles and I don't get it.
The idea is that I have an insect population (a type of turtle) and this insect population check around if there's a nesting patch available. If there's one and there's no other insect population I would like this patch to generate a new insect population. So far I have come with this idea :
ask insect-populations
[
ask patches in-radius 2
[
if lay? = 1
[
if not any? insect-populations [ask self [sprout-insect-populations 1]]
]
]
]
Thanks in advance for any tip
ask insect-populations
[
ask patches in-radius 2 with [lay? = 1 and not any? insect-populations-here]
[sprout-insect-populations 1]
]
should be what you want if I understand your intent correctly.
The trick is in the [with]. It takes a true/false block. So any agent for which the boolean statement inside the square brackets is included in the set.

nw:weighted-path-to, nw:turtles-on-weighted-path-to and multiple equally weighted paths

I apologise upfront if this is a stupid question.
When one calls nw:weighted-path-to a list of links is returned describing the shortest path between origin and destination turtles.
Similarly, calling nw:turtles-on-weighted-path-to returns a list of the turtles on the shortest path between origin and destination.
It is my understanding that if there are 2 equally weighted paths between origin and destination both functions returns one of these paths at random. This happens independently and as such one set of links can be produced for the shortest path, but another set of turtles. This can be replicated using the following code:
extensions [nw]
links-own [ weight ]
to go
clear-all
create-turtles 4
ask turtle 0 [ create-link-with turtle 1 [ set weight 2 ] ]
ask turtle 0 [ create-link-with turtle 2 [ set weight 2 ] ]
ask turtle 1 [ create-link-with turtle 3 [ set weight 2] ]
ask turtle 2 [ create-link-with turtle 3 [ set weight 2] ]
ask turtle 0
[
let pathLinks nw:weighted-path-to turtle 3 "weight"
let pathNodes nw:turtles-on-weighted-path-to turtle 3 "weight"
let pathUtility nw:weighted-distance-to turtle 3 "weight"
show pathLinks
show pathNodes
show pathUtility
]
end
Which will happily produce:
(turtle 0): [(link 0 2) (link 2 3)]
(turtle 0): [(turtle 0) (turtle 1) (turtle 3)]
(turtle 0): 4
Obviously, this is not an error but it has unfortunately tripped me up.
My question is - what is the most sensible way to link these two procedures to produce lists of links and turtles that make up a single randomly selected shortest path?
I am assuming it would be best to return the links with nw:weighted-path-to, then ask the links to return both-ends and do some sort of unique operation to produce a set of turtles on that path, if that is the case I'm not sure how to preserve the order of turtles. Does this make sense? Is that how you would do it?
As ever, thanks for reading.
Edit: this also applies to path-to and turtles-on-path-to in a topological network with multiple equal length paths.
Good question! You can generate either list from the other, but I think turtle-path to link-path is easier:
;; Construct the turtle path, putting the current turtle on the front:
let turtle-path fput self nw:turtles-on-weight-path-to turtle 3 "weight"
;; Iterate through pairs of turtles, getting the link connecting them
let link-path (map [[link-with ?2] of ?1] but-last turtle-path but-first turtle-path)
Edit:
Nicolas is absolutely right about "link-path to turtle-path". However, his comment made me realize you could use the almighty reduce and the ever-useful other-end to do it!
reduce [ lput [[other-end] of ?2] of (last ?1) ?1 ] fput (list self) nw:weighted-path-to turtle 3 "weight"
Edit2: The "link-path to turtle-path" code is pretty opaque. Here's an attempt to clarify it:
to-report add-link-to-turtle-path [ turtle-path next-link ]
let last-turtle last turtle-path
report lput [[other-end] of next-link] of last-turtle
end
;; turtle-procedure - Assumes the current turtle is the starting point of the path
to-report link-path-to-turtle-path [ link-path ]
let start-of-path (list self)
report reduce add-link-to-turtle-path fput start-of-path link-path
end