Storing agent coordinates in a list in NetLogo - netlogo

I'm trying to create a simple simulation of agents moving randomly but avoiding certain obstacles. I want them to store the coordinates of the places they've been so they don't go there again. This is part of what I have so far:
to move-agent
let move random 3
if (move = 0) []
if (move = 1)[ left-turn ]
if (move = 2)[ right-turn ]
set xint int xcor ;;here i'm storing the coordinates as integers
set yint int ycor
set xylist (xint) (yint)
go-forward
end
to xy_list
set xy_list []
set xy_list fput 0 xy_list ;;populating new list with 0
end
However, it keeps giving me a "SET expected 2 inputs" error. Can anyone help me with this?

It looks like you are incorrectly using xy_list as both a variable and a turtle variable.
I don't see the need for the xy_list procedure - Keep it as a turtle variable. Make sure xy_list is in the turtles-own list:
turtles-own [xy_list]
initialize it to an empty list when you create a turtle. eg:
crt 1 [set xy_list []]
When a turtle moves, you could add their current position as an xcor, ycor list with:
set xy_list fput (list int xcor int ycor) xy_list
You will then need to check if that coordinate already exists in the list before moving there.
However, as you are using integer coordinates, it would be a lot easier to use a patch-set to keep track of a turtle's history. You could try this:
turtles-own [history]
to setup
ca
crt 3 [set history (patch-set patch-here) pd]
end
to go
ask turtles [
let candidates neighbors with [not member? self [history] of myself]
ifelse any? candidates
[move-to one-of candidates stamp
set history (patch-set history patch-here)]
[die]
]
end

Related

How to find the turtle with the most neighbors in netlogo

i am struggling with a way to find the turtle with the most neigbors in a radius of 3 and change its color. At the moment i tried something with a while loop that increases an the id of the turtles and tests if the number of neighbors is higher than the last one. But It's causing anf infinite loop and travels only between the id 0 and 1.
I can't seem to find where the error comes from, Here's the code i wrote:
to election
while [var1 < 9][
ask turtle var1 [
set voisin count turtles in-radius 3
if voisin > maxi [
set maxi voisin
set idmax var1
]
show voisin
show idmax
set var1 var1 + 1
set color pink
]
]
ask turtle idmax[
set color green
]
end
You can do this with a single statement,
ask max-one-of turtles [count other turtles in-radius 3] [set color green]
max-one-of finds the turtle with the maximum value of the expression in brackets. (If there are multiple turtles with that maximum value, then a random one of those with the maximum value is chosen.) In the brackets, each turtle evaluates the number of other turtles in radius 3. The other is not strictly necessary. Without it, every turtle will count itself, adding one to the count.

Netlogo Dijkstra algorithm

to-report find-path [ init final ]
ask init [set dist_d 0]
let current init
let p_dij []
set p_dij insert-item 0 p_dij current
show "dij"
while [not (current = final)]
[
ask [neighbors with [pcolor = yellow and not (dist_d = -1)]] of current [set dist_d min (list dist_d (1 + [dist_d] of current))]
ask current [set dist_d -1]
let min_d min [dist_d] of neighbors
set current one-of neighbors with [dist_d = min_d and pcolor = yellow]
set p_dij insert-item (length p_dij - 1) p_dij current
]
ask patches with [pcolor = yellow] [set plabel dist_d set plabel-color red]
report p_dij
end
I'm trying to find the shortest path using Dijkstra's algorithm. There is a problem with the neighbors, every time the program tries to find the next current node, it comes back to init.
This is not a direct answer to your question, and won't help you if you're trying to figure out how to code Dijkstra's algorithm as an exercise for yourself, but if you're just looking for a shortest path, you can always use the nw:path-to from the network extension.
That primitive is actually using Dijkstra's algorithm under the hood.
In order to use it, however, you need an actual network, so you would have to use turtles instead of patches. That's easy to do, however. Supposing you have a turtle breed called nodes, you can put a node on each patch by saying:
ask patches [
sprout-nodes 1 [
set color pcolor ; give the node the same color as the patch
set hidden? true ; hide the node if you prefer not seeing it
]
]
Then you can create links between, say, yellow nodes:
ask nodes with [ color = yellow ] [
create-links-with (nodes-on neighbors) with [ color = yellow ]
]
The path from one yellow node another is then just:
let yellow-nodes nodes with [ color = yellow ]
show [ nw:path-to one-of other yellow-nodes ] of one-of yellow-nodes
If all you want is the distance, you can use nw-distance-to instead.

How to extract a highly linked node from a network

I want to extract a node with highest degree centrality from the network. I don't want to extract a node with max links only. I want to extract the node along with the nodes adjacent to it.
Below is the code. In this code, I have loaded a network using nw extensions.
extensions [nw]
turtles-own [ explored? ]
to setup
ca
crt 25
ask turtles [fd random 15]
load-graph
extract_deg
end
to load-graph
let filename user-file
if (filename != false) [
nw:load-graphml filename [
set shape "circle"
set size 1
]
nw:set-context turtles links
]
end
to extract_deg
let n turtles with [my-links = max [count link-neighbors] of turtles]
ask n [show other turtles network:in-link-radius 1 turtles]
end
to layout
ask turtles [ set size sqrt count my-links ]
layout-spring turtles links 0.5 2 1
ask turtles [
facexy 0 0
fd (distancexy 0 0) / 100 ]
end
The code below will choose one of the nodes with largest degree (just remove one-of if you want all of them), turn it red and make its network neighbours green.
You don't need the expression [my-links = max [count link-neighbors] of turtles], standard NetLogo includes the very useful with-max primitive. However, I think your construction would have worked if you had counted my-links (like let n turtles with [count my-links = max [count link-neighbors] of turtles]). Then you have some syntax errors in the next line (the extension is nw and you don't need the turtles.
to extract_deg
let maxk-set one-of turtles with-max [count my-links]
ask maxk-set
[ set color red
ask other nw:turtles-in-radius 1 [set color green]
]
end

netlogo: have patches calculate influence value using reporter

I'm modeling a cityscape for looking at movement, where patches equate to buildings and have different influence values. I need to use a reporter function that calculates the value of each patch with something like:
(its own influence) plus (the influence of neighbors) divided by the
distance to a specific turtle (max-one-of distance myself)
The turtle will then move towards the patch with the highest influence value, but along a defined street.
I'm new to using netlogo and have gotten completely stuck.
I've included a portion of what I have so far, but I can't figure out how to write the reporter function that will compute each patches influence value (in-cone) so that the turtles can then move towards the best option.
to setup-influence-field
ask patches with [pcolor = green] [set influence commercial-influence]
ask patches with [pcolor = orange] [set influence production-influence]
ask patches with [pcolor = yellow] [set influence domestic-influence]
ask patches with [pcolor = pink] [set influence religious-influence]
ask patches with [pcolor = blue] [set influence public-influence]
end
to go
move-serapis
end
to move-serapis
ask serapis [set-procession-heading]
repeat 2 [ ask serapis [ fd .25 ] display ]
tick
end
;;;;; the reporter values are need for this part of the code so that the turtles (serapis) can move towards the patches with the highest influence value;;;;
to set-procession-heading
let direction patches in-cone 4 40 with [influence-field > 0]
if any? influence-field
[face max-one-of influence-field] ;;;; face towards the highest computed influence value
ifelse any? patches with [pcolor = black] in-cone 1 25
[process]
end
Any help would be greatly appreciated!
I don't think this is completely correct and I can't test it, but it should start you off and maybe someone here can fix it if you let us know the errors.
to set-procession-heading
let direction-targets patches in-cone 4 40 with [influence-field > 0]
if any? direction-targets
[ face max-one-of direction-targets [ influence-amount self ] ]
end
to-report influence-amount [ target-node ]
report ( [ influence-field ] + sum [ influence-field] of neighbors ) / distance target-node
end
What I have done is set up a separate procedure to report the results of your calculation. That procedure takes an argument (named target-node) because you need to be able to pass the identity of the turtle being influenced. Once you have that procedure, then you can simply pass the agent-set of potential directions to the calculation procedure and pick the one with the largest value.

How to pick random agents from an agentset to create a new agenset?

I have an agent-set of all the turtles. I want to randomly pick 5 turtles from this agent-set and assign a value of 1 to them. The other 5, ones that were not selected should take the value of 0.
I tried using the random function but it's not working.
turtles-own [attr]
to set-attr
ask turtles [set attr 0]
let my-agentset n-of 5 turtles
ask my-agentset [set attr 1]
end