Check turtle's neighborhood over some patches - netlogo

I am struggling with coding identifying an expanding neighborhood based on an attribute of a patch. I need to check if there are a wall in my turtle's vision, if it is the case, my turtle should not be able to see through this wall.
Currently my recursive code works only with a vision's distance of 1 (that corresponds to neighbors), but over of 2 I get this error : A patch can't access a turtle or link variable without specifying which agent.
I don't know how it is possible to do this with an agent, do someone have an idea to do this?
breed[robots robot]
robots-own[step]
globals [ max-dist]
patches-own [ dist ]
to setup
ca
init-environement
create-robots 1 [init-robots]
end
to init-robots
set shape "person"
set size 4
move-to one-of patches with [no-wall? and (not any? turtles-here)]
set step 0
end
to init-environement
ask patches with [ (abs pxcor = max-pxcor) or (abs pycor = max-pycor) ]
[ set pcolor brown ]
ask patches with [ (abs pxcor = 20 and abs pycor > 15)
or (abs pycor = 10 and pxcor > 25)
or (pycor = 0 and pxcor < 1)][ set pcolor brown ]
ask n-of nbObstacles patches [ask patches in-radius random-float 2 [ set
pcolor brown ]]
end
to move-robot
let k 0
let v (neighbors with [no-wall? and (not any? turtles-here)])
if (any? v)[ move-to min-one-of v [dist] paint-agents k neighbors]
set step (step + 1)
output-show step
end
to paint-agents [k case]
let w ([neighbors] of case with [no-wall? and (not any? turtles-here)])
if (k < radius) [
set k k + 1
foreach w [
[x] ->
ask neighbors with [pcolor != brown][ set pcolor [color] of myself - 2
paint-agents k x]
]
]
end
to go
propagate
if any? patches with [pcolor = black] [ clear-output ask robots [move-robot] ]
end
to propagate
ask patches with [ no-wall? ][ set dist -1]
let p (patch-set patches with [pcolor = black])
let d 0
while [ any? p ]
[ ask p [ set dist d ]
set d d + 1
set p (patch-set [ neighbors with [no-wall? and ((dist = -1) or (dist > d))]] of p)
]
set max-dist max [ dist ] of patches
if (max-dist < 0) [ set max-dist 0 ]
ifelse (show-labels?)
[ ask patches with [no-wall?]
[ set plabel-color white
set plabel dist]
]
[]
end
to-report no-wall?
report pcolor != brown
end
There, my function which contains this problem is "paint-agents"

One way would be to get rid of patches in the sight that intersect a wall. You can imagine if you draw a line between each patch and if the wall lies on the drawn line, then that patch should be removed from things in vision.
This link may be useful on an implementation: Not see through the walls

Related

Netlogo: find next target discounting previous targets

I'm trying to get my agents to calculate the next patch target once they have reached their first target. A target is defined as the (highest patch influence value / distance to agent). The next target needs to be calculated using this same procedure, but also discounting any previous targets from the equation.
I have attempted to create a patch-set to include all targets, although I'm not sure if it works.
My issue is how to create a to-report function that calculates the next highest-influence-value. I've gotten completely stuck with this final part of my procedure.
My initial reporter function is:
to-report highest-influence
let available-target patches with [influence > 0] ;and not any? patches with [pcolor = green]
report max-one-of available-target [influence / distance myself]
end
But I'm not sure how to have the next reporter function use the same parameters while also discounting any previous targets
to-report next-highest-influence
; patches with [influence > 0] and not part of patch set
end
Any help would be greatly appreciated. I've provided the full code.
breed [walkers walker]
walkers-own [traveled? ;; parameter to keep track of if a walker has traveled to a target yet or not
target ]
patches-own [influence influence-set] ;highest-influence
to setup
clear-all
reset-ticks
define-patches
create-walkers num-walkers
ask walkers [
setxy 0 0
set heading 90
let streets neighbors with [pcolor = black]
ifelse any? streets in-cone 1 25
[fd 1]
[move-to one-of streets in-radius 1]
set traveled? false ;; so that walkers know they have not yet moved to a target
]
ask walkers [
define-target ]
end
to define-patches
ask n-of 100 patches [
set pcolor white
]
ask n-of 40 patches with [pcolor = white] [set influence random 5 set pcolor blue set plabel influence]
ask patches [set influence-set patches with [pcolor = green] ]
end
to define-target ;; this defines the initial destination of walkers
if traveled? = false [
set target highest-influence
ask target [set pcolor green]
face target ]
end
to new-target ;; this defines subsequent desinations after walkers have reached inital target
if traveled? = true [
set target next-highest-influence
ask target [set pcolor green + 2]
face target ]
end
;;;;;;;;;;;;run-time procedure;;;;;;;;;;
to go
ask walkers [
if distance target > 1
[ travel-walkers
leave-a-trail
set traveled? true ]
if distance target = 1
[ stop
new-target
travel-walkers
leave-a-trail
]
]
tick
end
to travel-walkers
ask walkers [
move-towards-target
]
end
to move-towards-target
ask walkers [
ifelse [pcolor] of patch-ahead 1 != white or any? other turtles-here
[ Move-Function ]
[ Avoid-Function ]
]
end
to Move-Function
let t target
face min-one-of all-possible-moves [distance t]
fd 1
end
to Avoid-Function
let t target
face min-one-of all-possible-moves [distance t]
end
to leave-a-trail
ask patch-here [set pcolor grey]
end
;;;;;; reporter calculations ;;;;;;
to-report highest-influence
let available-target patches with [influence > 0] ;and not any? patches with [pcolor = green]
report max-one-of available-target [influence / distance myself]
end
to-report next-highest-influence
; patches with [influence > 0] and not part of patch set
end
to-report all-possible-moves
report patches in-radius 1 with [pcolor != white and distance myself <= 1 and distance myself > 0 and plabel = "" ]
end
You can make use of the member? primitive to do that. Check out the simplified toy model below (I didn't use your code as I'm not sure how you'd want to implement and there are some interface pieces i would need to set up to run it. Check out the MCVE guidelines).
turtles-own [ visited-list ]
patches-own [ influence ]
to setup-member?
ca
crt 1 [ set visited-list [] ]
ask patches [ set influence 0 ]
ask n-of 5 patches [ set influence 5 + random 50 ]
reset-ticks
end
to go-member?
ask turtles [
let target highest-influence
ifelse target != nobody [
face target
ifelse distance target > 1 [
fd 1
] [
move-to target
ask target [ set pcolor red]
set visited-list lput target visited-list
]
] [ print "No targets remaining." ]
]
tick
end
to-report highest-influence
let to-visit patches with [
influence > 0 and
not member? self [visited-list] of myself ]
report max-one-of to-visit [ influence / ( distance myself )]
end
The reported returns the patch with the highest influence/distance value that is not a member of the turtle's visited-list. Initially, no patches are a member of the list, but as the turtle visits each target, it adds the target to the visited-list so that patch is no longer considered.

Netlogo: simulation of cellular differentiation pattern?

I am trying to simulate the differentiation pattern of a simple organism. This is how I'd like my breeds and variables to work:
Breeds: vegetatives and heterocysts. Vegetatives can divide, heterocysts can't. Vegetatives can become heterocysts. Ideally, once a heterocyst is formed, the closer a vegetative is to it, the less likely it is for it to become a heterocyst in turn.
Variables:
age: + 1 per tick, - 1 for newly-hatched turtles
bump: A means by which to displace all the turtles located 'ahead' of a newly-hatched turtle, so they don't overlap. I imagined the system a bit like a Newton's Cradle (http://s.hswstatic.com/gif/newtons-cradle-1.jpg)
pro: promoter. Accumulates partially randomly. Once it reaches a certain value ('concentration'), a vegetative would change breed to become a heterocyst. Value decreased by inh.
proL: label for pro, with rounded values.
inh: inhibitor. Ideally this value should form a 'gradient' (highest in turtles near heterocysts, lowest further away).
The obvious problem that I can see is that I get a lot of contiguous heterocysts. Which is sort of what I've been trying to avoid. But I can't see what went wrong...Please help?
to setup
clear-all
setup-turtles
reset-ticks
ask turtles [ set size 1 ]
end
to setup-turtles
create-vegetatives 1
ask turtles [
setxy random-xcor random-ycor
set shape "circle"
set color 65]
end
to go
divide
add-age
move
differentiate
tick
end
turtles-own [age
bump
inh
pro
proL]
breed [vegetatives vegetative]
breed [heterocysts heterocyst]
to add-age
ask turtles [
set age age + 1
ifelse show-age?
[ set label age ]
[ set label "" ]
]
end
to divide
ask vegetatives [
if random 100 < 2 [
hatch 1[
set bump 1
set age age - 1
set inh 0
]
]]
end
;;Trying to get only one turtle per patch, making the others move
to move
ask turtles[
while [bump = 1] [
ifelse not any? turtles-on patch-right-and-ahead 180 1[
rt 180
fd 1
set bump 0
if any? other turtles-here[
ask other turtles-here
[set bump 1]
]
]
[fd 1
set bump 0
if any? other turtles-here[
ask other turtles-here[
set bump 1]
]
]
]]
end
to differentiate
ask turtles[
set pro (pro - inh + (random 2))
set proL round pro
ifelse show-proL?
[ set label proL ]
[ set label "" ]
create-links-with other turtles-on patch-ahead 1
create-links-with other turtles-on patch-right-and-ahead 180 1
if breed = vegetatives [
if any? link-neighbors[
ifelse any? link-neighbors with [breed = heterocysts]
[]
[set inh mean [inh] of link-neighbors]
]
if any? vegetatives with [pro > 50]
[ask vegetatives with [pro > 50]
[set breed heterocysts
set color brown
set shape "circle"
if any? link-neighbors[
ask link-neighbors with [breed != heterocysts]
[set inh 2]]
]]
]]
clear-links
end

Changing patch colors except one color

I'm creating a program in Netlogo which has shoppers (turtles) moving through a grocery store layout. When they step on a patch it increases in color and when it has no agent on it, it decreases in color, as this will show the paths shoppers take through a store.
My code is:
ask turtles
[ rt random 360
fd 1
set pcolor pcolor + 1 ]
ask patches with [ (pcolor > 9.9) or (pcolor < 0.1) ]
[set pcolor 0]
ask patches with [ (count turtles-here = 0) and (pcolor <= 9.9) and (pcolor > 0) ]
[ set pcolor pcolor - 0.1 ]
However, as the aisle patches are blue this is turning them back to black as well. I was wondering what code I could use so patches with pcolor = 105 will stay blue and not change to black?
Don't change the color of the patches with pcolor = 105. You'll just need to add an additional condition to anywhere you modify the patch color.
ask turtles
[ rt random 360
fd 1
if pcolor != 105[set pcolor pcolor + 1 ]
]
ask patches with [ pcolor != 105 and ((pcolor > 9.9) or (pcolor < 0.1))]
[set pcolor 0]
ask patches with [pcolor != 105 and (count turtles-here = 0) and (pcolor <= 9.9) and (pcolor > 0) ]
[ set pcolor pcolor - 0.1 ]

Avoid visited patch netlogo

Sir i am working on a project and time to time i use to post questions here related to that problem. I have created a simulation scenario i which turtles (robots) move around in the space and also each robot keep track of its visited patches. On the movement if patch-ahead 1 is visited patch then i need to turn the robot to 45 degree left and check again for patch-ahead until it checks all 8 neighbors if any one of the 8 is UN-visited then it should move to that patch and continue its exploration. But if all 8 are visited then it should move to patch which is in front of current heading after checking the all 8 neighbors no matter it is visited.
Here is the piece of code i am using.
breed [robots robot ]
robots-own[ state memory ]
patches-own [ is-obstacle? ]
to setup
__clear-all-and-reset-ticks
create-robots num [
set memory (list patch-here)
]
draw-obstacles
ask patches [if pxcor = 0 and pycor = 0 [ set pcolor black ]]
end
to draw-obstacles
ask patches with [ pxcor mod 6 = 0 and pycor mod 6 = 0 ] [set pcolor red set is-obstacle? true]
; set pcolor red
ask patches [ ifelse pcolor = red [ set is-obstacle? true ][ set is-obstacle? false ] ]
ask patches with [ count neighbors != 8 ] [ set pcolor red set is-obstacle? true ]
end
to make-obstacle
if mouse-down?
[ ask-concurrent patches
[ if ((abs (pxcor - mouse-xcor)) < 1) and ((abs (pycor - mouse-ycor)) < 1)
[set pcolor red]]
]
end
to remove-obs
if mouse-down?
[ ask-concurrent patches
[ if ((abs (pxcor - mouse-xcor)) < 1) and ((abs (pycor - mouse-ycor)) < 1)
[set pcolor black]]
]
end
to go
ask patches [if ( pcolor = red )[set is-obstacle? true]]
ask patches [if ( pcolor = black )[set is-obstacle? false]]
ask-concurrent robots ; wanderers instructions
[
rt random-float rate-of-random-turn
lt (rate-of-random-turn / 2)
set-state
move-robots
]
tick
end
to move-robots ;;turtle proc
if (not member? state ["disperse" "explore"]) [
error "Unknown state"
]
if (state = "disperse") [
disperse
]
if (state = "explore") [
explore
]
end
to set-state ;;turtle proc
ifelse (any? other turtles in-radius 1) [
set state "disperse"
] [
set state "explore"
]
end
to disperse ;;turtle proc
avoid-obstacle
move1
; move-to one-of patch-set [neighbors] of neighbors
end
to explore
move
;search-open-room
avoid-obstacle
;move-to one-of neighbors
end
to move
fd speed
set memory lput patch-here memory
if ( (member? patch-ahead 1 memory) or ([is-obstacle?] of patch-ahead 1 ) )
[ lt random 45
]
end
to move1
fd speed
end
to avoid-obstacle
set memory lput patch-here memory
if ([is-obstacle?] of patch-ahead 1 )
[
ifelse [is-obstacle?] of patch-at-heading-and-distance (heading - 5) 1
[
ifelse [is-obstacle?] of patch-at-heading-and-distance (heading + 5) 1
[
ifelse random 1 = 0
[ rt 40 ]
[ lt 40 ]
]
[ rt 60 ]
]
[lt 60]
]
end
to search-open-room
ask robots[
ifelse ([is-obstacle?] of patches in-cone 2 150 )
[ rt 45 ] [ move ]
]
end
But in the move procedure i am just able to lt random 45. How to change it according to above mentioned scenario. I tried many with while loop and repeat statement but code does not seems to be working for me.
You could do it using while or repeat, but I think this a case where recursion works well.
The idea is to have a procedure that keeps on calling itself until the desired state is achieved:
to turn-until-free [ n ]
let target ifelse-value (patch-ahead 1 = patch-here)
[ patch-ahead 2 ]
[ patch-ahead 1 ]
let seen? member? target memory
let obstacle? [ is-obstacle? ] of target
if-else n < 8
[ if seen? or obstacle? [ lt 45 turn-until-free n + 1 ] ]
[ if obstacle? [ lt 45 turn-until-free n + 1 ] ]
end
The n parameter represents the number of calls that we've already made to the procedure (or, in other words, the number of neighbors that we've checked so far). When you call the procedure for the first time, you start with n = 0:
to move
turn-until-free 0
fd 1
set memory lput patch-here memory
ask patch-here [ set pcolor black + 2 ] ; just to show what's visited
end
A couple of things that were not part of your original specification:
It can happen that patch-ahead 1 is the same patch that the robot is already on. A patch has sides of length 1, but its diagonal is a bit longer (√2). So if a robot is in the bottom left corner, for example, and facing towards the top right, patch-ahead 1 = patch-here will be true. In these cases, we look a bit further and set the target to patch-ahead 2.
It can happen that, after checking all 8 neighbors, you end up facing an obstacle. If that's the case, you need to keep on turning until you're clear of the obstacle. And as a matter of fact, doing this takes care of obstacle avoidance nicely, and you might be able to get rid of the avoid-obstacle procedure in your code.
Edit:
Here is the code needed (in addition to the two procedures above) for a fully working example:
breed [ robots robot ]
robots-own [ memory ]
patches-own [ is-obstacle? ]
to setup
ca
ask patches [ set is-obstacle? false ]
ask patches with [ pxcor mod 6 = 0 and pycor mod 6 = 0 ] [
set is-obstacle? true
set pcolor red
]
ask n-of 5 patches with [ not is-obstacle? ] [
sprout-robots 1 [ set memory [] ]
]
reset-ticks
end
to go
ask robots [ move ]
tick
end
If you are having trouble with runtime errors or other unwanted behavior, I suggest you start from this and add back whatever else you had in your simulation one piece at a time. Then, you can see exactly where the problem comes from.
I also changed if-else n < 7 for if-else n < 8 in the turn-until-free procedure above. This way, the robot comes back to its original heading if all neighbors are explored instead of turning a bit right. This avoids going in circles once the whole territory is explored.

Netlogo Sprouting turtles at regular intervals

I want to place turtles on each of the black patches(below Figure) at a specified step size:
Therefore if step size less more turtles will be created/sprouted and more step size will result in less turtles.
Code I use right now:
ask patches with [pcolor = black][sprout-dead-turtles wall-agents [set color red]]
This gives the following result:
Previous question asked on same lines:Netlogo Sprouting turtles spaced at less than one patch
Here:
to fill-wall [ d ]
set d precision d 1 ; make sure d is a multiple of 0.1
let n precision (d / 0.1) 0 ; interval at which to hatch
ask one-of possible-next-patches [
sprout 1 [
hatch 1
let i 0
let next-patch my-next-patch
while [ next-patch != nobody ] [
face next-patch
while [ patch-ahead 0.55 != nobody and [ pcolor ] of patch-ahead 0.55 = black ] [
fd 0.1
setxy precision xcor 1 precision ycor 1 ; avoid floating point imprecisions
set i i + 1
if i mod n = 0 [ hatch 1 ]
]
set next-patch my-next-patch
]
die
]
]
end
to-report possible-next-patches
let empty-black-patches patches with [ pcolor = black and not any? turtles-here ]
report empty-black-patches with [
count neighbors4 with [ member? self empty-black-patches ] = 1
]
end
to-report my-next-patch
report one-of possible-next-patches with [ member? self [ neighbors4 ] of myself ]
end
Here is how you would use it:
to setup
ca
; draw the background:
ask patches with [ abs pxcor != max-pxcor and abs pycor != max-pycor ] [ set pcolor grey ]
ask patches with [ pycor = max-pycor and abs pxcor <= 1 ] [ set pcolor white ]
set-default-shape turtles "circle 2"
fill-wall 0.3
end
Constraints:
d has to be a multiple of 0.1
world wrapping needs to be turned off