Why netlogo gives me error with set [] of x - netlogo

i have a question that i cant find answer anywhere , i have my code like this:
exemple-own[energy]
ask exemple[
let me one-of exemple-on patch-ahead 1
set [energy] of me [energy] of me + energy
]
"This isn't something you can use set on"
am i use set right?

You can only change the value of an agent's attribute from the perspective of that agent and you change perspectives with an ask. I think you want this:
exemple-own[energy]
ask exemple [
let me one-of exemple-on patch-ahead 1
ask me [ set energy energy + [energy] of myself ]
]
The keyword myself refers to the asker.

Related

Agent-based and individual-based modeling (Railsback and Grimm, 2019) NetLogo Business Model

I am trying to learn agent-based modeling with this book, but I'm having troubles implementing the model from chapter 10. There has already been posted a question about this model, but a second edition of the book has since been released and I think the model has changed since the utility function and some values from the previously asked question don't match up.
My model does work, but when I try running the experiments in BehaviourSpace my results are widely different from those in the book. Has anyone else tried this model or can spot other mistakes?
Thanks!
My code:
globals [ decision-time-horizon ]
turtles-own [ location wealth ]
patches-own [ annual-profit annual-risk ]
to setup
clear-all
set decision-time-horizon 5
ask patches
[
set annual-profit random-exponential 5000
set annual-risk 0.01 + random-float ( 0.1 - 0.01 )
set pcolor scale-color grey annual-profit 1 5000
]
create-turtles 25
[
setxy random-xcor random-ycor
move-to one-of patches with [not any? turtles-here]
set wealth 0
set color red
set shape "house"
]
reset-ticks
end
to go
ask turtles
[
reposition
pen-down
]
tick
end
to reposition
let potential-destinations neighbors with
[ not any? turtles-here ]
set potential-destinations
( patch-set potential-destinations patch-here)
; identify the best one of the destinations
let best-patch max-one-of potential-destinations
[ utility-for myself]
; now move there
move-to best-patch
end
to-report utility-for [ a-turtle ]
; a patch-context reporter that calculates utility
; for an investor turtle in this patch
; first get the turtle's current wealth
let turtles-wealth [ wealth ] of a-turtle
; then calculate the investor's utility given by its wealth and
; relevant patch variables
let utility ( turtles-wealth + ( annual-profit * decision-time-horizon )) * (( 1 - annual-risk ) ^ decision-time-horizon)
report utility
end
You are correct that this model is different in our 2nd edition.
I did not check your code extremely carefully but I did not see any real mistakes. However, you are missing the code to update the turtles' wealth each time step, including determining whether or not the investment fails (wealth becomes zero).
The web site for our book (www.railsback-grimm-abm-book.com) includes an invitation for people like you teaching themselves to ask us for the solutions. If you want access to them, contact us.

Getting turtles to spend energy while moving on a least cost path in a network environment

I’m creating a model in which turtles choose and traverse a least cost path in a network environment, one node at a time.
I’m using the nw:turtles-on-weighted-path-to primitive from the NW extension to let my turtles choose a path. Relevant snippets of my code (which so far works fine) are below:
undirected-link-breed [ routes route ]
undirected-link-breed [ tracts tract ]
breed [ stalkers stalker ]
stalkers-own [ energy ]
links-own [ difficulty ]
to setup-network
ask routes [
set thickness 2
set color white
set difficulty 1
]
ask tracts [
set thickness 2
set color black
set difficulty 2
]
end
to enter-the-zone
ask stalkers [
let path nobody
let target one-of nodes-on patch -10 42
ask nodes-on patch-here [ set path but-first nw:turtles-on-weighted-path-to one-of nodes-on patch -10 42 difficulty ]
ifelse patch-here != ( patch -10 42 )
[
face first path
move-to first path
]
[ return_to_base
]
]
tick
end
to return_to_base
move-to patch -258 -231
end
However, I would like to add an “energy” attribute to my turtles that depletes a little every time they reach a new node. The catch is: I would like this loss of energy to be equal to the difficulty of the link between the node they are now and the node in which they just were.
What would be the best way to implement that?
Thanks in advance for the help.

Setting new target patch but excluding current in Netlogo

I am creating a simulation which copies shoplifter behaviour. Turtles are split between "professional" and "novice" shoplifters and if "professionals" are apprehended by store security they might (1/2) want to select a new store to target "new-store-required".
"professional" shoplifters target the store with the lowest "security" value in a certain radius, all values are set as they are created.
I am trying to set a new "target-store", a "store" in-radius 10 with the second lowest "security", i.e. excluding the current "target-store" but I am having difficulty.
So far I have attempted to made several additions to the following code to exclude the current target store, this includes variations of "member? my patches" as the "store" where the shoplifter has been apprehended is added to this "patch-set" which will inform a later command. Also I have made a list of ascending "security" values to tell the "shoplifter" to target the "store" with "security" (the value which determines store vulnerability) the same as item 1 on the list, but I fear this might not work because their original target-store might not necessarily be item 0 as they target the store with the lowest "security" in a 10 unit radius.
These are the lines of code I am working with at the moment, any suggestions would be greatly appreciated.
***Edit: I could ideally like the code to make use of "mypatches" so each time a professional shoplifter is apprehended at a store that store can be added to mypatches and the subsequent target-store can exclude all stores which are members of mypatches.
to new-target-store
ask turtles [ if
new-store-required = 1 and professional = 1 and (random-float 1 < 0.5) [
set target-store min-one-of store in-radius 10 [security]
]
]
end
Edit 2: I've fixed what was wrong.
You may want to include your setup code, or a stripped-down version of it if it's really long, to make sure that answers conform to the structure you've used. I would approach this by having a turtles-own variable to store their current target, which they can try to fill if it is empty (rather than using an extra boolean for that purpose). Also, you may want to convert your 1/0 options to true/false for cleaner code. Check out this example setup:
globals [ stores ]
patches-own [ security ]
turtles-own [
current-target
professional?
mypatches
]
to setup
ca
ask n-of 20 patches [
set pcolor green
set security 1 + random 10
]
set stores patches with [ pcolor = green ]
crt 5 [
setxy random-xcor random-ycor
pd
set professional? one-of [ true false ]
set current-target nobody
set mypatches ( patch-set )
]
reset-ticks
end
This sets up a world with some green patches that are grouped into the patch-set called stores. Also, you have some turtles that have the boolean professional? set to either true or false. They initialize with no current-target store, and an empty mypatches patch-set variable.
Now, you can have turtles check if their current-target exists. If it does not, they can assign a store to that variable from the set of stores (possible-targets, here) that are not equal to the patch-here of the asking turtle. Professional thieves can further refine possible-targets to exclude any stores at which they have been apprehended, by excluding any stores that are a member of their mypatches patch-set variable. More details in comments:
to go
ask turtles [
; if you have no target currently
if current-target = nobody [
; Create a temporary patch set made up of all stores except for
; the one that I'm currently in
let possible-targets stores with [ self != [patch-here] of myself ]
ifelse professional? [
; Have professional thieves revise their possible targets to exclude
; any in the patchset mypatches, then choose a possible target
set possible-targets possible-targets with [ not member? self [mypatches] of myself ]
set current-target min-one-of possible-targets in-radius 10 [ security ]
] [
; Have amateur thieves choose randomly from the possible targets
set current-target one-of possible-targets
]
]
; move closer to your current target, or
; move to it exactly if you're near enough
ifelse current-target != nobody [
face current-target
ifelse distance current-target > 1 [
fd 1
] [
move-to current-target
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; *** do your shoplifting attempt/check here ***
; For this example, just have professional thieves sometimes
; add this patch to their mypatches patchset
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
if professional? and random-float 1 < 0.5 [
set mypatches ( patch-set mypatches patch-here )
]
; Reset current-target to nobody
set current-target nobody
]
] [
; if no suitable nearby targets, wander randomly
rt random 61 - 30
fd 1
]
]
tick
end
If you run that long enough, eventually your professional thieves will stop being able to find target stores as they have added all stores to their mypatches variable.

Copy the link breed variable in the patch below

I have a network of nodes and links. This figure
is a capture of the world. The graph represents streets of a city. I have imported a shapefile with the gis extension. The gray lines are links, black dots are nodes and red dots represent people. The people move heading to the next node. In a street corner, the red dot chooses next street by examining the variable popularity owned by the link.
The links breed has a variable, popularity, whose value I would like to copy in the patches that are below.
If I try, for example, something like this to access patches under links will produce an error
ask links [show [(list pxcor pycor)] of patch-here]
Another approach can be to access links variable popularity from patches, but I do not know how to do it.
The reason why I want this is because I want to write in a file a matrix of popularity values and its position in the matrix should correspond with the position of the link in the world. Thus, the patches below the links would give me the matrix form. I have a procedure that for each patch writes the value of the patch in a file. However, I do not know how to pass the popularityvalue from the link to the patch below it.
Is there any way to copy a link owned variable to its patch?
Regards
If someone has a better way of doing this (or can simplify my code), feel free. Here is a complete working example. Copy it into an empty NetLogo model and run it to see it work.
The setup procedure just creates some nodes and links with appropriate test values and then calls the transfer-link-values procedure, which does what I think you want. The setup procedure then puts the values into the patch labels to display them and see the results.
The way the transfer-link-values procedure works is to create a turtle at one end of the link, and that turtle moves toward the other end of the link transferring the value as it goes. When it gets to the other end, the turtle dies.
patches-own [patch-popularity]
links-own [link-popularity]
to setup
clear-all
create-turtles 10 [ setxy random-xcor random-ycor]
while [ any? turtles with [not any? my-links] ]
[ let to-pair turtles with [not any? my-links]
let thisNode one-of to-pair
ask thisNode
[ create-link-with one-of other to-pair
[ set link-popularity 5 + random 5 ]
]
]
transfer-link-values
ask patches [ if patch-popularity != 0 [set plabel patch-popularity ] ]
end
to transfer-link-values
ask links
[ let start-node one-of both-ends
let this-link self
let end-node nobody
ask start-node [ set end-node [other-end] of this-link ]
let transfer-value link-popularity
ask start-node
[ hatch 1
[ face end-node
if transfer-value > patch-popularity
[ ask patch-here [ set patch-popularity transfer-value ] ]
while [ not member? end-node turtles-here ]
[ forward 1
if transfer-value > patch-popularity
[ ask patch-here [ set patch-popularity transfer-value ] ]
]
if transfer-value > patch-popularity
[ ask patch-here [ set patch-popularity transfer-value ] ]
die
]
]
]
end

NetLogo: calculate perimeter of a patch-set

I'm modeling territory selection in NetLogo, and would like my turtles to calculate the perimeter of their territory once established. I've been trying to come up with ideas for how to do this, but haven't found a good means yet. Any ideas?
patches-own
[ owner ] ;; patches know who owns them
turtles-own
[ territory ;; agentset of patches I own
food ;; food acquired in my territory
threshold ] ;; food required, will build territory until meet this
to go
tick
ask turtles [ build-territory ]
end
to build-territory
if food > threshold [ calculate-perimeter ] ;; stop building when enough food
pick-patch ;; keep picking patches until meet threshold.
end
to calculate-perimeter
;; what could I use to add up the perimeter of the territory?
end
Thanks in advance for any suggestions!
A modification of my last answer to you:
to setup
ca
ask patches with [pxcor > 0 ] [
set pcolor white
]
crt 1
end
to go
ask turtles [
let blacklist patches with [ pcolor = black ]
let northpatches patches with [ pycor > 0 ]
let northred ( northpatches with [ member? self blacklist = false ] )
ask northred [ set pcolor red ]
let border northred with [ any? neighbors4 with [ pcolor != red ] ]
ask border [
set pcolor blue
]
print count border
]
end
You can designate border/perimeter patches as any of you territory patches with neighbors that are not territory. For you it might look something like:
ask turtles [
print count territory with [ any? neighbors4 with [owner != myself ]
]
]
Again, I can't test it without your setup so you would have to modify.
Edited below
To count the edges of patches that are on the border, you could have them count their neighbors4 that belong to another turtle. Then, they can add them to that turtle's perimeter length. For example:
to assess-perimeter ;;; must be called by a turtle
print ("Assessing perimeter")
let current-turtle who
let temp-per-len 0
let border-patches patches with [ owner = current-turtle and any? neighbors4 with [ owner != current-turtle ] ]
show (word "I have " count border-patches " border patches")
ask border-patches [
;; One way to get each border patch to check each of its neighbors
let nobodies 4 - count neighbors4 ;; if any patches are on the edge of the world, returns the number of those edges
let non-territory-edges count neighbors4 with [ owner != current-turtle ]
let border-edges nobodies + non-territory-edges
set temp-per-len temp-per-len + border-edges
]
show (word "My perimeter length: " temp-per-len )
set perimeter-length temp-per-len
end
If that is called after all turtles have chosen their entire home range, the idea is that each turtle assesses the border of its home range. Then, it has each of those border patches count its neighbors4 that have a different owner. I used "temp-per-len" as a summing variable within the loop, which is then used to set the turtles-own "perimeter-length". Full model code, including setup and definitions, here. Note- you'll have to download or copy the code, the model is too bulky to run well in the HTML format.
Also, I didn't actually count to make sure this worked perfectly- I did a quick version and crossed my fingers, but I think the idea makes sense and hopefully gets you started.