NetLogo let target min-one-of neighbors math operation produced a non-number - netlogo

I am new to NetLogo and I am trying to reproduce https://github.com/YangZhouCSS/Pedestrian_Model_Krasnow. When I open, it says that it was produced in NetLogo 5.2 and I am running NetLogo 6.3 so I may need to update code. When I try to run I get a math operation produced a non-number on the following line in the to go section:
let target min-one-of neighbors [ elevation + ( count turtles-here * 9999999) ]
This is the complete to go section:
to go
if count turtles > 0 [set move-speed count turtles with [moved? = true] / count turtles]
if count turtles = 0 [stop]
ask patches with [exit = 1] [ask turtles-here[die]]
ask turtles [
set moved? false
let target min-one-of neighbors [ elevation + ( count turtles-here * 9999999) ]
if [elevation + (count turtles-here * 9999999)] of target < [elevation] of patch-here
[ face target
move-to target
set moved? true
ask target [set path path + 1]]
]
if Show_path? [ask patches with [elevation < 9999999][let thecolor (9.9 - (path * 0.15)) if thecolor < 0.001 [set thecolor 0.001] set pcolor thecolor]]
tick
end
And the complete code is here: https://github.com/YangZhouCSS/Pedestrian_Model_Krasnow/blob/master/pedestrian_floor-2exits.nlogo
Thank you in advance for any guidance

Many of the patches have invalid (NaN) values for their elevation, which then causes the error you are getting when you try to use them in an arithmetic operation. I'm not conversant in the gis extension, so I can't tell you why it seems to be assigning invalid elevations to these patches, but if you run setup and then enter in the command center
ask patches [show elevation]
you will see which ones are invalid and may then be able to trace how they came to be that way. Perhaps the gis extension has changed the way it works with the data files since NetLogo v5?
Charles

Not exactly sure why it worked but I replaced
let target min-one-of neighbors [ elevation + ( count turtles-here * 9999999) ]
With
let target min-one-of neighbors [ elevation ]
And the simulation ran

Related

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.

Netlogo: How to make turtles stop moving when a condition (suitable patch is found) is met?

I'm trying to construct a model that simulates patterns of housing selection in a given area.
I am asking turtles to seek a settlement patch in radius-3 that offers the most resource, if one is found, they should settle there and stop moving; otherwise, they should move to somewhere else and stop moving. However, no matter what I do, no turtle seems to stop; every turtle is constantly moving around regardless of whether a settlement is found.
Below is my seek-settlement module under "go". Please advise on how to make a turtle stop moving if a condition is met (i.e. a suitable settlement is found)? I tried sticking “stop” in various places but with no difference.
to seek-settlement-1
ask turtles with [wealth >= com]
[
ifelse any? patches in-radius 3 with [pcolor = green and count turtles-here = 0]
[ ;;; if there are any neighboring empty green patches with resource larger than wealth of the turtle
move-to max-one-of patches in-radius 3 with [pcolor = green and count turtles-here = 0] [resource] ;+ (0.5 * count turtles in-radius 3 with [color = blue])]
set settlement patch-here ;;; move to the one with the most utility (defined as an even combination of resource and number of blue turtles in radius 1) and settle there
stop ;;; this stop appears to make no difference
]
[right random 360
forward 3
set wealth (wealth - com)
stop] ;;; this stop appears to make no difference
if settlement != nobody
[
set wealth (wealth - com + (0.5 * [resource] of settlement)) ;;; update the turtle's wealth by subtracting the cost of movement and adding 20% of the resource of the patch here
ask settlement
[set resource (0.5 * resource)] ;;; update the settlement patch's resource by subtracting 20%
stop ;;; this stop appears to make no difference
]
]
end
Please review the documentation of stop:
http://ccl.northwestern.edu/netlogo/docs/dict/stop.html
The stop will merely exit your ask.
Your code does not match your description. Trying to follow your description, I get something like the following:
globals [com]
patches-own [resource]
turtles-own [wealth settlement]
to setup
ca
set com 10
crt 50 [
set wealth random 500
set settlement nobody
setxy random-xcor random-ycor
]
ask patches [
if random-float 1 < 0.2 [set resource 100 set pcolor green]
]
end
to go
;you can change the filter any way you wish
ask turtles with [wealth >= com and settlement = nobody] [seek-settlement]
end
to seek-settlement
let _candidates (patches in-radius 3 with [pcolor = green and count turtles-here = 0])
if any? _candidates [
set settlement max-one-of _candidates [resource]
move-to settlement
]
ifelse (settlement = nobody) [ ;this conditional shd probably be in a separate proc
right random 360
forward 3
set wealth (wealth - com)
][
set wealth (wealth - com + (0.5 * [resource] of settlement))
ask settlement [set resource (0.5 * resource)]
]
end

face to minimum heading difference

How can I face or rotate or set the heading of an agent (turtle for example) to that an element of a patch-set which needs minimum rotation.
So the agent has a initial heading, and we have a patch-set (for example 5 patches in a cone), and I would like to face the agent that one which is at minimum in angle difference. I don't want to use patch-ahead because it can be one patch backward if that is the only one.
I tried some combinations with these commands:
min-one-of towards myself self heading subtract-headings towardsxy face - 180
Thank you in advance.
Are you having trouble getting the heading differences with subtract headings? Then you can try this:
to-report abs-hdiff [#t #p]
let _current [heading] of #t
let _new [towards #p] of #t
report abs (subtract-headings _current _new)
end
For example:
to test
ca
ask n-of 5 patches [set pcolor red]
let _patches (patches with [pcolor = red])
crt 1
ask turtle 0 [
hatch 1 [pen-down fd 10 die] ;just to see old heading
face min-one-of _patches [abs-hdiff myself self]
]
end

On netlogo, what command do I use to make a turtle stop if the patch it wants to move to is a certain color

I'm making a maze on netlogo and I want to do it so that once it tries to walk into the violet lines, it'll stay on its own patch instead of moving forward. What command would that be? I tried bk 1 to reverse the fd 1 but it doesn't work all the time
You can undo your step like this:
ask turtles [
fd 1
if pcolor = violet [fd -1]
]
Or you can check ahead of time as Marzy answered. Basically it's the difference of asking for forgiveness vs permission :-)
I hope this example answer your questions:
turtles-own [target]
to setup
clear-all
reset-ticks
ask n-of 100 patches [
set pcolor red
]
create-turtles 1
[ move-to one-of patches with [pcolor != red]
set heading 90
set target one-of patches with [pcolor != red]
ask target
[
set pcolor green
]
]
end
to go
ask turtles
[ifelse pcolor != green
[
ifelse [pcolor] of patch-ahead 1 != red
[
Your-Move-Function
]
[
Your-Bounce-Function
]
leave-a-trail
]
[stop
print ticks
]
]
tick
end
to Your-Move-Function
let t target
face min-one-of all-possible-moves [distance t]
fd 1
end
to Your-Bounce-Function
let t target
face min-one-of all-possible-moves [distance t]
end
to-report all-possible-moves
report patches in-radius 1 with [pcolor != red and distance myself <= 1 and distance myself > 0 and plabel = "" ]
end
to leave-a-trail
ask patch-here [set plabel ticks]
end
This is how it works:
Random patches are colored Red to show walls or obstacles, one turtle is created in a random location with a random target which is colored green:
I have used a variable to store all available patches which turtle can step on , but since I have considered a target for the turtle, turtle chooses the one patch which is closest to the target, and since I have noticed in some cases it might go in circle I have asked the turtle to leave tick number which is its move number as a plabel, you can use a variable for that for specifying if that path was already selected or not.