How to prevent agents' ability to 'jump' barriers in NetLogo? - netlogo

I'm asking this question in reference to a previous question answered here: Is there a way to create impassable barriers in NetLogo?
A user helpfully suggested a way to generate off-limits patches (i.e. barriers) in NetLogo. However, as he mentioned, agents will still possess the ability to occasionally 'jump' over these barriers if they approach from particular angles. This is an undesirable behavior for my model, as I'm modeling fences and movement, so jumping barriers is unrealistic.
I have attempted to alter this code:
ask patches in-radius 1
[if f = 1
[ask myself
[set heading towards min-one-of patches [distance myself] + 180 - random 10 + random 10 ]
]
]
by decreasing the "in-radius" to less than one, in hopes that it would prevent the agent from seeing far enough to move across barriers, but this does not work. The "f = 1" is just a patch variable used to denote which patches are fences.
Is there any method of preventing this behavior?
Here is the behavior I want - navigation around barriers
Here is the behavior I don't want - jumping barriers if approached from certain angles
The actual movement procedure that agents are following is as follows:
ask turtles[
let moveset patches in-radius 30 with [f = 0 and n > 0]
pen-down
let target max-one-of moveset [n]
ifelse patch-here != target
[set heading towards target]
[]
ask patches in-radius 1
[if f = 1
[ask myself
[set heading towards min-one-of patches [distance myself] + 180 - random 10 + random 10]
fd 1]

Related

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: ask turtle to perform calculations from patch-sets

I'm trying to get the hang of NetLogo and am in need of some assistance. I'm having turtles pick patches based on which is most "valuable." Value is determined by benefits versus costs of selecting patches, so I need turtles to do a bit of math. Basically, a turtle needs to:
Identify a patch-set of available patches (patches not yet owned).
Determine the benefit-to-me of each available patch. Each patch has a benefit of 0-1. To determine benefit-to-me, the turtle should calculate the mean benefit of the available patch plus that patch's neighboring patches in-radius 2 (to identify a cluster of high benefit patches in a moving windows fashion).
Determine the cost-to-me of each available patch. For now, this is the distance of the patch from the turtle. (Cost-to-me will include additional considerations in future versions of this model.)
Calculate which patch is most valuable as benefit-to-me / cost-to-me.
This works (or so I think--is this doing what I think it is?):
patches-own [
benefit
owner ]
to go
ask turtles [pick-patch]
end
to pick-patch
move-to highest-value
end
to-report highest-value
let available-destinations patches with [owner = 0] ;; I think this accomplishes step 1.
report max-one-of available-destinations [(mean [benefit] of patches in-radius 2) / (distance myself + 1)] ;; and I believe(?) this accomplishes step 2-4. (Note, "distance myself + 1" seems required since can't divide by 0.)
end
But I'd like to separate out the benefit and cost portions:
to-report highest-value
let available-destinations patches with [owner = 0]
let benefit-to-me ...?? ;; code to assess "(mean [benefit] of patches in-radius 2)" for the patch-set of available-destinations?
let cost-to-me ...?? ;; and code to assess "(distance myself + 1)" of available destinations?
report max-one-of available-destinations (benefit-to-me / cost-to-me) ...?? ;; code to perform calculations based on benefit-to-me and cost-to-me?
end
How do I complete this code to achieve the desired effect? And I'm guessing there could be multiple ways to approach this type of coding. Given that turtles will repeat finding "highest-value" thousands of times, what option is likely to be fastest to run? Thank you in advance!
Attempted revision to code: (Following up from Luke's answer, below.)
patches-own [
benefit
owner ]
to setup
ask patches [ set owner nobody ]
end
to go
ask turtles [pick-patch]
tick
end
to pick-patch ;;<-----noticing turtle will change _destination midway to traveling there...why?
let _destination highest-value
ifelse _destination != nobody [
ask _destination [set pcolor red] ;; added this as a check, and yes, red patch switches around before the turtle gets there sometimes.
face _destination forward 1
if patch-here = _destination
[ claim-patch _destination ]
]
[stop] ;; if there is no _destination
end
to claim-patch [_patch]
ask _patch [set owner myself]
;; and etc., turtle does several more things to claim patch, e.g., turn patch color of turtle
end
;;;; --reporters for calculations:--
to-report highest-value
let available-destinations patches with [owner = nobody]
report max-one-of available-destinations [benefit-to-me / cost-to-me]
end
to-report benefit-to-me
report mean [benefit] of patches in-radius 2
end
to-report cost-to-me
report distance myself
end
It seems like the use of distance myself in your "highest-value" reporter might be problematic- you should not get a divide-by-zero error unless the distance between the two agents referenced is actually 0, so it looks like your available patches are including the patch that the asking-turtle is currently on. If you are using a turtle's who to designate ownership, remember that there is almost always a turtle 0 around who will "own" all patches with "owner = 0". Avoid this by having patches set their "owner" variable to -1 or nobody, for example, in the initial setup.
If benefit of a patch is not based on the turtle itself, you could simplify a bit by having your reporter be just the "moving-window value" of that patch, by cutting the distance part of the reporter. That way, a turtle can just query the patch for its moving-window-value divided by distance. Quick example:
patches-own [
benefit
owner ]
to setup
ca
reset-ticks
ask patches [
set owner nobody
set benefit random-float 1
set pcolor benefit + 54
]
crt 5 [ setxy (random 30 - 15)(random 30 - 15)
ask patch-here [
set owner [who] of myself
set pcolor [color] of myself
]
]
end
to go
ask turtles [
pick-patch
]
tick
end
to-report moving-window-value
report mean [benefit] of patches in-radius 2
end
to pick-patch
let available-destinations patches with [ owner = nobody ]
let best-patch max-one-of available-destinations [ moving-window-value / (distance myself) ]
if best-patch != nobody [
move-to best-patch
ask patch-here [
set owner [who] of myself
set pcolor [color] of myself
]
]
end
As far as separating out the cost and benefit- you could, but if cost is just distance, you don't need to calculate it as it's already a primitive. Unless you're using some other gradient like elevation, terrain type, etc, you can probably get away without storing your distance cost.

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

Turtles move to nearest patch of a certain color - how can this process be sped up?

I am trying to build a butterfly movement model in which butterflies are attracted to patches of their host plant. This attraction is expressed as a probability which is stored in a variable called "attr-prob". If a butterfly is located within 25 m of a host-plant patch (pcolor = 9.9), it will move to the nearest host-plant patch with the probability of attr-prob.
I wrote the following code:
if (distance (min-one-of (patches with [pcolor = 9.9]) [distance myself]) ) <= 25
[if random-float 1 < attr-prob [move-to min-one-of (patches with [pcolor = 9.9]) [distance myself]]]
This code seems to be doing what I want it to do, however, when I add this part into my model, it slows it down immensely. Does anyone have any alternative suggestions for coding this that might be quicker?
I am running Netlogo in 64-bit Java.
Try something like this:
if random-float 1 < attr-prob [
let target-patch min-one-of (patches in-radius 25 with [pcolor = 9.9]) [distance myself]
if target-patch != nobody [
move-to target-patch
]
]
This should be faster for a couple reasons.
First, the fastest code is the code that never runs. So, doing the probability check at the very beginning allows you to skip computing the closest patch whenever you can.
Second, using in-radius upfront rather than checking the distance at the end reduces the number of patches that you're looking over. Basically, you'll only be checking the color and distance of the patches in the radius, rather than all of the patches in the world.
Finally, in your original code, you were finding the closest patch twice. Instead, you can store the patch in a local variable (target-patch in the code I provided) so you only have to find it once. This alone should double the speed of the code (depending on the value of attr-prob). It also increases readability.

Volunteer coordination system for modelling natural disaster emergencies [closed]

This model is a system model of centralized coordination of volunteers for the Response Phase of a volcanic eruption. Agent (Volunteers) were placed randomly will try to reach the point of barracks (red colored patches). Each agent (volunteers) has energy capacity will be channelled to the point barracks if a volunteer has been able to achieve. Point barracks have demand could be reduced by the capacity of volunteers if the volunteer is able to reach the point of the refugee camps.
Problems on the program I created is a volunteer movement was not run in accordance with a predetermined color patch and move back and forth. Volunteers can’t choose to turn left or turn right if the color patch in front of it instead of the specified color. Is there a solution for the movement of agents (volunteers) about this model?
Besides how volunteers can measure the shortest distance to the agent could reach the point quickly barracks?
Furthermore, is there any way that volunteers can perform a search where the evacuation point that must be reached in advance if the demand of each point varies barracks or upon priorities?
This is my code:
to go
if ticks = 180 [ stop]
ask Volunteers
[ifelse capacity = 0 [ fd 0 frozen][ move search ]]
;search
;update-demand
tick
display-labels
do-plots
end
to move
ask Volunteers
[
move-to one-of patches in-radius 2 with [pcolor = 0]
ifelse [pcolor] of patches in-radius 2 = 0 [move-to one-of patches in-radius 2 with [pcolor = 0]]
[
ifelse [pcolor] of patch-ahead 1 = 105 [set heading -180 move-to one-of patches in-radius 2 with [pcolor = 0] ]
[
ifelse [pcolor] of patch-ahead 1 = 8 [set heading -180 move-to one-of patches in-radius 2 with [pcolor = 0]]
[
if [pcolor] of patch-ahead 1 = red [move-to one-of patches in-radius 2 with [pcolor = red] fd 0 ]
]
]
]
]
end
to search
if any? turtles-on patches with [ pcolor = red ]
[
ifelse capacity < demand
[
set demand (( 1 + Rate-of-Demand) * (demand - (capacity * (1 + Rate-of-Capacity))))
set capacity 0
]
[set capacity (( 1 + Rate-of-Capacity) * (capacity - demand ))
set demand 0 ]
]
end
to frozen
if capacity = 0
[ fd 0
set waiting-time waiting-time + 1
if waiting-time > 5 [set capacity 1000 / Jumlah-Relawan set waiting-time 0]
]
end
I've just recently finish, not the exact same but mostly the same, this kind of model.
The model I made is about evacuation of the people. and just like you, I am a student in UGM too.
The solution I used to solve the agent's movements is implementing a shortest-path algorithm in the netlogo code.
You could look up, Djikstra Algorithm or other. In my case, I use a*(aStar) algorithm. There are some example of a* implementation in netlogo as well. you could look it up.
I could only help this far, just like Seth Tisue Said, any details answer would take hours to write.
Hope it helps.

Resources