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

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

Related

Getting error "There is no agent for MYSELF to refer to" after new procedure is added, but worked before. (Netlogo)

I am trying to simulate an F1 race where the cars go around different tracks and need to pit when their tyres have degraded enough. They stick to the patches that are the track color (black). I have the code where I get the cars to follow the track shown below:
breed[cars car]
cars-own[tyre-level
tyre-type
tyre-total
speed
maxSpeed
draftSpeed
speed
]
;part of the setup
create-cars num-cars [
setxy xcord ycord
set shape "car top"
set size 5
set heading head
set tyre-level one-of (list soft_tyre_durability medium_tyre_durability hard_tyre_durability)
if tyre-level = soft_tyre_durability [set tyre-type "Soft"]
if tyre-level = medium_tyre_durability [set tyre-type "Medium"]
if tyre-level = hard_tyre_durability [set tyre-type "Hard"]
if tyre-level = soft_tyre_durability [set tyre-total soft_tyre_durability]
if tyre-level = medium_tyre_durability [set tyre-total medium_tyre_durability]
if tyre-level = hard_tyre_durability [set tyre-total hard_tyre_durability]
set maxSpeed 30
set draftSpeed 50
]
end
to move ;; turtle procedure
ask cars [face-chosen-neighbor
fd 0.5
set tyre-level tyre-level - 0.01]
end
to face-chosen-neighbor ;; turtle procedure
;; turtle faces the patch that requires the least change in
;; heading to face
let closest-border-patch min-one-of different-colored-neighbors [abs turn-amount]
rt [turn-amount * track-help] of closest-border-patch
end
;; computes the turn the calling turtle would have to make to face this patch
to-report turn-amount ;; patch procedure
let this-patch self
report [subtract-headings (towards this-patch) heading] of myself
end
to-report different-colored-neighbors ;; patch procedure
;; report neighbors that are a different color than me
report neighbors in-cone 2 180 with [pcolor = [pcolor] of myself] ;; report patches or neighbors in cone
end
This runs okay. However I now change the move procedure and add a speed procedure for the accelerating and decelerating - they slow down on turns and speed up if they aren't. They also have an increased max speed if drafting another car.
to move ;; turtle procedure
ask cars [face-chosen-neighbor
control-speed
fd speed / 200
set tyre-level tyre-level - 0.01]
end
to control-speed
let drafting any? other cars in-cone 4 130
ifelse drafting = nobody [set maxSpeed maxSpeed * ( (tyre-level + 50 ) / tyre-total)] [set maxSpeed draftSpeed * ( (tyre-level + 50 ) / tyre-total)]
if turn-amount * track-help > 60 [set speed speed - (deceleration * turn-amount * 0.5)]
let car-ahead any? other cars in-cone 1 130
ifelse car-ahead = nobody [
ifelse speed < maxSpeed [set speed speed + acceleration] [set speed speed - deceleration]
]
[
ifelse [speed] of car-ahead >= maxSpeed [
set speed maxSpeed
set speed speed - deceleration
] [
;try to overtake
ifelse [pcolor] of patch-left-and-ahead 90 1 = [pcolor] of myself and not any? turtles-on patch-left-and-ahead 90 1
[move-to patch-left-and-ahead 90 1] [
ifelse [pcolor] of patch-right-and-ahead 90 1 = [pcolor] of myself and not any? turtles-on patch-right-and-ahead 90 1
[move-to patch-right-and-ahead 90 1] [
set speed [speed] of car-ahead
set speed speed - deceleration]
]
]
]
end
Now if I run this, I get the error "There is no agent for MYSELF to refer to". I assume its got to do with the different definitions of self and myself. I have tried my best to understand it all and have scoured the forums. I'm not sure if I still understand it correctly.
Any help will be greatly appreciated!
I suspect that the error occurs in the lines where you look at the [pcolor] of myself. self and myself can be confusing. Here you have asked cars to look at the color of the patches that they are on, so it is self that you want. Agents can directly determine the color of the patch they are on, in a sense, the patch's pcolor is one of the agent's built-in variables, like its own color, so in fact, you could just ask the car to look at pcolor. Since an agent, in this case a car, can be on only one patch at a time, there is no confusion as to which patch is being referred to. Equivalently, you could use [pcolor] of patch-here.
myself would be used if the car asked the patch to then look at one of the car's variables. E.g., if the car asked the patch to do something with the tyre-type of the car who asked, it would be [tyre-type] of myself.

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 Creating a Link with a turtle when they both hit the same patch

Maybe i worded the question wrong but what i want to do in the code is when another turtle meets another turtle they create links with each other
I know its
to go
tick
make-circle
move
if ticks >= timer1 [stop]
end
to move
ask turtles
[
create-links-with other turtles ;here is where i want to put the code
set heading random 360 fd 1]
create-network
end
to create-network
ask links [
set thickness 0.01 * counter
if [patch-here] of end1 = [patch-here] of end2
[set counter (counter + 1)]
]
end
but im not sure how to word it correctly to link when they meet how do i do that
Establish a breed variable for your counts. Then create a link between all turtles that are on the same patch with other turtles-here. Then increment that count variable for when the others turtles have met the original calling turtle. I'll note that I increment the count variable by .5 because each turtle in the link will increment it (there's 2 turtles, so .5 * 2 = 1).
links-own [meets]
to setup
clear-all
crt 100 [setxy random-xcor random-ycor ]
end
to go
ask turtles [fd 1]
ask turtles [rt random 90]
ask turtles [ create-links-with other turtles-here]
ask turtles [ ask other turtles-here [ask link-with myself [ set meets meets + .5]]]
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: identification of valleys = rivers location in artificial landscape

I want to produce the artificial landscape, containing hills, slopes and valleys. So far, so good. Now, I want to place a rivers in the bottom of the valley. I thought that the easiest way will be ask turtle to move uphill/downhill by elevation as drops here: http://modelingcommons.org/browse/one_model/2352#model_tabs_browse_info
However, as my landscape is not real, my valleys are also not linears, but just the "depressions" in the terrain, and thus my river localisation is really weird looking?
Please, is there a way to create hills and valleys reflecting the real terrain in netlogo without using GIS extension? I've found great models of Erosion, Watershed and GIS gradient example, but how can I initially place my turtles to stay only in valleys?
EDIT
the GIS gradient example provide excellent problem solving of movement of all the cells over the space, and they aggregate in valleys. However, as I want to by movement of my turtle "create" the river bed on the bottom of the valley, how can I place it? maybe start at the lowest point and stop after several ticks to not to allow to get on the top of the hill? thank you !
globals [
low
high
range ]
patches-own [
altitude ]
to setup
clear-all
setup-hills
scale-patches
color-patches
end
to setup-hills
ask n-of 2 patches [
set pcolor magenta ]
ask patches [
let max_dist sqrt (world-width ^ world-width + world-height ^ world-height)
set altitude world-width - distance patch 10 10
set altitude world-width - distance min-one-of patches with [pcolor = magenta][distance myself]
]
crt 1 [ ; create a turtle, needed to identify the lowest slope
set color red
let bottom_valley min-one-of patches [altitude]
move-to bottom_valley
]
end
to scale-patches
set low [altitude] of min-one-of patches [altitude]
set high [altitude] of max-one-of patches [altitude]
set range high - low
ask patches [
set altitude altitude - low ; shift every patch down so lowest altitude is 0
set altitude altitude * 99.0 / range ; scale every patch so that the lowest is 0 and highest is 999
]
repeat 5 [
diffuse altitude 0.5 ]
end
to color-patches
ask patches [
set pcolor scale-color green altitude 0 100]
end
to create-river
ask turtles [
let p max-one-of neighbors in-radius 1 [altitude]
if [altitude] of p >= altitude [
face p
move-to p
set pcolor blue
]
]
end
You have the right general idea. I recommend checking out the "GIS Gradient" model on your landscape. You can find it in the Models Library under Code Examples/GIS. Then think how to filter patches that have a minimum number of water particles passing through it over time.
Identification of system of rivers on the landscape using GIS Gradient Example:
http://modelingcommons.org/browse/one_model/2352#model_tabs_browse_info
The whole process of identification of the river system on the complex landscape is composed by 3 subprocesses:
identify the highest points of the landscape
place here the river source (create-source)
identify patches for a downhill stream
from the highest river source, by decreasing elevetion of neighboring cells to the lowest elevation of the river valley
when turtle reaches the edge of the world, turtle dies (go-downhill)
remove top hill points (as the river does not read the summit of the hill)
the river does not start at the top of the hill, but at the highest point of the valley
the highest "river" patches from the river beds system are removed (remove-river-from-top-hill)
The number of sources depends of type of GIS data you have available. The same for the number of patches you wish to remove from the top of the hill.
;; ----------------------------------------------
;; create river
;; ----------------------------------------------
to create-source
; identify the 5 highest points of the landscape
repeat 5 [
; avoid the highest points to be close to each other
ask max-one-of patches with [ not any? turtles-here and not any? turtles in-radius 20 ] [p_elev] [
;ask patches to sporout turtles
sprout 1 [
set size 1
set color orange
]
]
]
end
to go-downhill
; stop if there is no more turtles
if not any? turtles with [color = orange]
[ stop ]
; ask turtles located on top of thehills to move downhill
ask turtles with [color = orange] [
; die when you reach the edge of the world
if ([pxcor] of patch-here = max-pxcor) or ([pycor] of patch-here = max-pycor)
or ([pxcor] of patch-here = min-pxcor) or ([pycor] of patch-here = min-pycor) [
die ]
move-to patch-here ; go to patch center
set pcolor blue ; identify the use of patch
set p min-one-of neighbors with [pcolor != blue ] [p_elev] ;; or neighbors4 with [pcolor != blue]patches in-radius 3
if p != nobody [
; move downhill if elevation of patch you are standing on is higher then one of the neighboring patches
ifelse [p_elev] of p <= p_elev
[
face p
move-to p
set pcolor blue ; identify the use of patch
]
[
;move-to min-one-of patches with [pcolor != blue] [distance myself]
move-to min-one-of patches in-radius 2 with [pcolor != blue] [p_elev]
set pcolor blue ; identify the use of patch
]
]
]
end
to remove-river-from-top-hill
; remove 5% of the blue (river) patches placed on the top of the hill
let total_blue count patches with [pcolor = blue]
repeat total_blue * 0.05 [
ask max-one-of patches with [pcolor = blue] [p_elev] [
set pcolor yellow ]
]
end
Complex river system identified:
(river bed = BLUE, removed top of the hill = YELLOW):