Delete links between turtles with different breeds - netlogo

I would like to drop links between one turtle and another one with different breed if a condition is satisfied.
I am using the following code to do this:
ask one-of turtle1 [
if any? my-out-links with [breed = turtle2]
[ask one-of out-link-neighbors with [breed = turtle2 and value < 0.7] ;; value should refer to a neighbour's characteristics and it represents my condition
[die]
]
]
But the links seems to be kept. No link has been deleted.
Could you please tell me what it is wrong in my code and how to fix it accordingly?

I'm assuming that turtle1 and turtle2 are two breeds of turtles. If so,
if any? my-out-links with [breed = turtle2]
will never find any such out-links as you are checking to see if any of my-out-links are of breed turtle2, and of course no links are of that breed. What you want is to know if the turtle at the other end of the link is of breed turtle2. Since these are directed links, the turtle you want to check is at end2.
if any? my-out-links with [[breed] of end2 = turtle2]
will let you know if the any of the turtles at the other end of my-out-links are of breed turtle2.
The line
ask one-of out-link-neighbors with [breed = turtle2 and value < 0.7] [die]
may do more than you intend. It asks a link-neighbor turtle of breed turtle2 and with value < 0.7 to die. That will indeed kill the link as well (since it has lost its end2), but if you want to kill the link and not the turtle at the other end you could do all this in a single line
ask one-of turtle1 [
ask one-of my-out-links with [[breed = turtle2 and value < 0.7] of end2] [die]
]
If there are no such out-links, then you will be asking nobody and the ask will do nothing.

Related

Remove myself from an agentset in netlogo

This is quite a simple problem but I don't know why I can't solve it, nor do my searches on the Net are suitable to my code. I've been stuck for weeks because of this simple problem ・゜・(ノД`)
.
Here is the problem,
I have an agentset called myteamset where in this agentset, it include myself and 2 other turtles, what I need now is to remove myself from this set and name this new agentset as myteamset1
I have tried
set myteamset1 myteamset [not member? self myself] - got error;
set myteamset1 myteamset with [self != myself] - didn't get error but the agentset is empty
Code:
ask turtles [
if myteamset = nobody [
let possible-teammates other turtles with [ myteamset = nobody ]
ifelse count possible-teammates > 1 [
set myteamset (turtle-set self n-of 2 possible-teammates )
; set myteamset1 myteamset [not member? self myself] here is problem
Please help me, I don't know why I can't do this simple code, it's like my brain stuck and I feel so desperate now, Thanks in advance.
Try set myteamset1 other myteamset. The primitive other means everyone in the agentset except me.
Here is a full example
turtles-own [myteamset myteamset1]
to testme
clear-all
create-turtles 20
ask turtles
[ set myteamset (turtle-set self n-of 2 turtles)
set myteamset1 other myteamset
]
end

NetLogo: select neighboring patches to a patch-set

I'm modeling home range selection in NetLogo. I'd like a turtle to identify the subset of patches that are neighbors to its current home range. The turtle should then identify which neighboring patch is best (defined for now as the patch with highest food benefit) and move to that patch to claim it. It continues these steps (identify available patches neighboring the home range, select best option, add to homerange, repeat) until it has enough food within the home range.
Part of the code is as follows, but I get the error that "FACE expected input to be an agent but got NOBODY instead." I think something is off with the reporter or my use of it (I'm new to reporters; I'm trying to make the code modular to minimize procedure length).
Any idea where I'm going wrong? Thanks!
patches-own [
owner
benefit
used?
]
to-report edge-patches-of [my-homerange] ;; reporter to find patches along edge of homerange
report my-homerange with [
any? neighbors with [
owner = [owner] of myself
]
with [used? = false] ;; (only want neighboring patches not already owned)
]
end
to pick-homerange
ifelse food < food-needed ;; (turtle builds homerange until it has enough food)
[ let my-homerange patches with [owner = myself]
ask edge-patches-of my-homerange [set pcolor red ] ;; (this to just see the options)
;; now that we know the options, pick the best:
let available-destinations edge-patches-of my-homerange
set destination max-one-of available-destinations [([benefit] of patches)]
face destination
forward 1
if patch-here = destination
[ add-destination ]
]
[stop]
end
You have not included enough code or description to be sure, but it seems that your logic is overcomplicated and contains conceptual errors. Try this as a starting point. Note in particular the test for a nonempty set of candidate edge patches.
globals [food-needed]
patches-own [owner capacity]
turtles-own [homerange]
to setup ;make sure all turtles have an initial homerange
ca
ask patches [set owner nobody]
crt 10 [
set-initial-ownership
;do the following just once, not over and over
set homerange (patches with [owner = myself])
]
;etc
end
to-report benefit [_t] ;patch proc
;patch reports benefit to turtle _t
report 1 ;or whatever
end
to-report food ;turtle proc
report sum [capacity] of homerange
end
to pick-homerange ;turtle proc
;; (turtle builds homerange until it has enough food)
if (food >= food-needed) [stop]
let _candidates edge-patches
if any? _candidates [ ;you almost certainly need to add this
let _destination max-one-of _candidates [benefit myself]
face _destination forward 1
if (patch-here = _destination) [augment-homerange _destination]
]
end
to augment-homerange [_p] ;turtle proc
ask _p [set owner myself]
;just add to the homerange as needed; don't keep recreating it
set homerange (patch-set homerange _p)
end
to-report edge-patches ;turtle proc
;; (only want neighboring patches not already owned)
report (patch-set [neighbors] of homerange) with [owner = nobody]
]
end

How to choose the not previously used links by an agent

I have breed [walkers walker] which walk roads in a road map represented by links in Netlogo.
The links-own [ guiri-ids ] which I intend to be an turtle-set of walkers that have already walked for the link.
I would like to use this guiri-ids to select, from the set of possible next links, which I call nextlinks, the links that the walker has not been walked before (the new ones).
If all possible links have been walked before then choose one of them.
How could obtain the set of next links which guiri-ids set does not contain myself (the walker) ?.
I am trying this line
let new-nextlinks nextlinks with [ guiri-ids != myself ]
but the keeps taking old paths.
Thank your very much for your help
breed [walkers walker]
links-own [ guiri-ids ]
to test
ca
crt 25 [setxy random-xcor random-ycor]
ask turtles [
create-link-with one-of other turtles
]
create-walkers 10
ask links [set guiri-ids n-of 3 walkers]
ask walkers [walk]
end
to walk
let _next one-of links with [not member? myself guiri-ids]
ifelse (_next != nobody) [
ask _next [set guiri-ids (turtle-set myself guiri-ids)]
] [
;do whatever you wish in this case
]
end

How to create links in netlogo and ask link-neighbors to execute commands according to link-length?

I've been trying to link turtles from BREED1 (still) to turtles from breed2 (mobile) who are on neighbors of BREED1. I want to do so in order to change a variable according the link-length between BREED1 and breed2.
(you can say that BREED1 represent houses and breed2 represents people, I would like to change the fact that the people are "protected" or not, according to the distance that separates them from their house (BREED1 that they are linked to))
I don't know if this is the best way to do it, but here's my code, I know it's not working because the "protected" variable is always false by default.
to protect
ask n-of total-number-BREED1 BREED1
[ if any? breed2-on neighbors
[ create-link-with [who] of breed2-on neighbors]
ask link-neighbors
[ set protected true]
]
I would also like to add a part concerning the link's length
ask link-neighbors
[ ifelse link-length < 2
[set protected true]
[set protected false]]
Thank you for your help !
Try this to create links with the breed2-on the neighboring patches:
ask BREED1
[
if any? breed2-on neighbors [ create-links-with breed2-on neighbors]
ask link-neighbors [ set protected true]
]
and this, which gets the link-length between the breed1 and it's neighbors
ask BREED1
[
ask link-neighbors
[
if [link-length] of link-with myself < 2 [ do something]
]
]
Note: link-length is called from a link's perspective, so you need to get the link that's connecting two things.

Moving turtles to specific coordinates of other agents in NetLogo

I have individuals (turtles) and I have households (turtles with fixed xy)
I have a variable address stored at households. I have a number of a family attached to individuals. The households has the same number.
How can I ATTACH or MOVE the individuals to their corresponding household?
I tried something like:
ask individuals
[ if family = [family-place] of household
[
move-to [address] of household
]
]
Since it is a slow monday morning, here is how I would do it.
I assume familiy-number to be the name of the common number in both moving and sessile turtles. I would use let to create a local variable that only works within the procedure. (See the procedure go-home for this)
breed [walkers walker]
breed [houses house]
houses-own [family-number]
walkers-own [family-number]
to setup
clear-all
set-default-shape houses "house"
create-houses 10 [
setxy random-xcor random-ycor
set family-number random 10000
]
reset-ticks
end
to leave-home
ask houses [
hatch-walkers 1 [
set family-number [family-number] of myself
set color [color] of myself
set heading random 360
fd 1
]
]
end
to go
ask walkers [
rt random 120
lt random 120
fd 1
]
tick
end
to go-home
ask walkers [
let family-place one-of houses with [family-number = [family-number] of myself]
move-to family-place
fd 1 ;; walker will step away one step so we can see him.
]
end
Just copy it into NetLogo, make a button for each procedure and play. Works best if in the order
setup
leave-home
go
go-home
Hope this helps!
move-to household should do it.
I'm having trouble understanding your question, so I've having to guess at what you want, but with the help of your comment on Bryan's answer, maybe I've guessed right?
ask individuals [
move-to one-of households with [address = [family-place] of myself]
]
if this seems confusing because of the myself, you could also write it as:
ask individuals [
let f family-place
move-to one-of households with [address = f]
]