I am creating an urban environment that is robust from interruption for my university project.
The problem I am facing is that I am not sure how to make each turtle follow a specific network line. The black line represents the electricity cable and the blue line represent the underground water pipe. I am not sure how to create a turtle to follow the network line to be consumed by the house or the big buildings. Also, please bear in mind that I only just started learning Netlogo so any improvements in the coding itself are much appreciated.
This is the current code for the urban environment model. Also, is it even possible for a turtle to follow a specific line?
breed [ houses house ]
breed [ big-buildings big-building ]
breed [ commercial-buildings commercial-building ]
breed [ water-pumping-stations water-pumping-station ]
breed [ sewage-plant-stations sewage-plant-station ]
breed [ electricity-power-stations electricity-power-station ]
breed [ overhead-cable-stations overhead-cable-station ]
to setup
clear-all
reset-ticks
setup-landscape
setup-network
if (show-grid = true ) [
draw-grid ;for the switch to hide or show the gride
]
if (show-eletricity = true) [
draw-electricity ; for the swtich to hide or show the gride
]
if (show-sewage = true) [
draw-sewage ; for the swtich to hide or show the gride
]
if (show-water = true) [
draw-water ; for the swtich to hide or show the gride
]
end
to setup-network
draw-networks ; designing lines and colour coordinate them to show what each network are.
end
to draw-networks ; list of networks that has been drawn.
draw-electricity
draw-sewage
draw-water
end
to draw-water
create-turtles 1 [ ;FOR THE HOUSE AND BIG BUILDINGS
setxy 37 -18
set heading 90
set color blue
set pen-size 2.5
pen-down
left 180
fd 10
right 90
fd 9
right 90
fd 3
right 90
fd 3
back 6
fd 3
right 90
fd 7
right 90
fd 3
back 6
fd 3
left 90
fd 7
right 90
fd 3
back 6
fd 3
left 90
fd 3
right 90
fd 19
right 90
fd 3
right 90
fd 4
back 9
fd 5
left 90
fd 4
right 90
fd 8
back 8
left 90
fd 3
right 90
fd 4
back 9
fd 5
left 90
fd 4
right 90
fd 8
back 8
left 90
fd 3
right 90
fd 4
back 9
die
]
create-turtles 1 [ ;FOR THE COMMERICAL BUILDING
setxy -37 -18
set heading 90
set color blue
set pen-size 2.5
pen-down
left 90
fd 14
right 90
fd 12
right 90
fd 3
back 6
fd 3
left 90
fd 10
left 90
fd 3
back 6
fd 3
right 90
fd 10
right 90
fd 3
back 6
fd 3
die
]
end
to draw-sewage
create-turtles 1 [ ;FOR HOUSE AND BIG BUILDINGS
setxy 38 -1
set heading 90
set color green
set pen-size 2.5
pen-down
right 90
fd 8
right 90
fd 8
right 90
fd 3
back 6
fd 3
left 90
fd 7
right 90
fd 3
back 6
fd 3
left 90
fd 7
right 90
fd 3
back 6
fd 3
left 90
fd 3
right 90
fd 19
right 90
fd 3
right 90
fd 4
back 9
fd 5
left 90
fd 4
right 90
fd 8
back 8
left 90
fd 3
right 90
fd 4
back 9
fd 5
left 90
fd 4
right 90
fd 8
back 8
left 90
fd 3
right 90
fd 4
back 9
die
]
create-turtles 1 [ ;FOR COMMERICAL BUILDING
setxy -18 -18
set heading 90
set color green
set pen-size 2.5
pen-down
right 180
back 7
right 90
fd 7
right 90
fd 9
left 90
fd 7
left 90
fd 3
right 90
fd 3
back 6
fd 3
left 90
fd 10
right 90
fd 3
back 6
fd 3
left 90
fd 10
right 90
fd 3
back 6
fd 3
die
]
end
to draw-electricity
;HOUSE
create-turtles 1 [ ;house 1
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
fd 54
right 90
fd 3
die
]
create-turtles 1 [ ;house 2
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 61
right 90
fd 3
die
]
create-turtles 1 [ ; house 3
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 68
right 90
fd 3
die
]
create-turtles 1 [ ; house 4
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 8
right 90
fd 2
left -90
back 4
die
]
create-turtles 1 [ ; house 5
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 8
right 90
fd 9
left -90
back 4
die
]
create-turtles 1 [ ; house 6
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 8
right 90
fd 16
left -90
back 4
die
]
create-turtles 1 [ ; house 7
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 18
left 90
back 5
right 90
back 2
die
]
create-turtles 1 [ ; house 8
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 18
left 90
back 12
right 90
back 2
die
]
; BIG BUILDINGS ROW
create-turtles 1 [ ; building 1
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 8
right 90
fd 19
left -90
back 19
right 90
fd 3
right 90
back 3
die
]
create-turtles 1 [ ; building 2
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 8
right 90
fd 19
left -90
back 19
right 90
fd 10
right 90
back 3
die
]
create-turtles 1 [ ; building 3
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 8
right 90
fd 19
left -90
back 19
right 90
fd 17
right 90
back 3
die
]
create-turtles 1 [ ; building 4
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 8
right 90
fd 19
left -90
back 19
right 90
fd 3
right 90
fd 3
die
]
create-turtles 1 [ ; building 5
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 8
right 90
fd 19
left -90
back 19
right 90
fd 10
right 90
fd 3
die
]
create-turtles 1 [ ; building 6
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 8
right 90
fd 19
left -90
back 19
right 90
fd 17
right 90
fd 3
die
]
;COMMERICAL BUILDINGS
create-turtles 1 [ ;COMMERICAL BUILDINGS 1
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
right 90
fd 22
left 90
fd 33
die
]
create-turtles 1 [ ;COMMERICAL BUILDINGS 2
setxy -25 -4
set heading 90
set color black
set pen-size 2.5
pen-down
right 90
fd 2
die
]
create-turtles 1 [ ;COMMERICAL BUILDINGS 3
setxy -25 -4
set heading 90
set color black
set pen-size 2.5
pen-down
right 90
back 3
die
]
create-turtles 1 [ ;COMMERICAL BUILDINGS 4
setxy -15 -4
set heading 90
set color black
set pen-size 2.5
pen-down
right 90
fd 2
die
]
create-turtles 1 [ ;COMMERICAL BUILDINGS 5
setxy -15 -4
set heading 90
set color black
set pen-size 2.5
pen-down
right 90
back 3
die
]
create-turtles 1 [ ;COMMERICAL BUILDINGS 6
setxy -5 -4
set heading 90
set color black
set pen-size 2.5
pen-down
right 90
fd 2
die
]
create-turtles 1 [ ;COMMERICAL BUILDINGS 7
setxy -5 -4
set heading 90
set color black
set pen-size 2.5
pen-down
right 90
back 3
die
]
create-turtles 1 [ ;COMMERICAL BUILDINGS 8
setxy -25 -4
set heading 90
set color black
set pen-size 2.5
pen-down
right 90
fd 2
die
]
end
to setup-landscape
ask patches [
set pcolor pink
]
draw-entities
end
to draw-entities
draw-houses
draw-big-buildings
draw-water-pumping-stations
draw-sewage-plant-stations
draw-electricity-power-stations
draw-commercial-buildings
end
to draw-houses ; setting house as stationary not going to move at all
ask patch 30 15 [ sprout-houses 1 ]
ask patch 23 15 [ sprout-houses 1 ]
ask patch 16 15 [ sprout-houses 1 ]
ask patch 30 6 [ sprout-houses 1 ]
ask patch 23 6 [ sprout-houses 1 ]
ask patch 16 6 [ sprout-houses 1 ]
ask patch 20 2 [ sprout-houses 1 ]
ask patch 27 2 [ sprout-houses 1 ]
ask houses [
set size 1.5
set shape "house"
set color red
]
end
to draw-big-buildings ; setting big buildings as stationary not going to move at all
ask patch 30 -6 [ sprout-big-buildings 2 ]
ask patch 23 -6 [ sprout-big-buildings 2 ]
ask patch 16 -6 [ sprout-big-buildings 2 ]
ask patch 30 -12 [ sprout-big-buildings 3 ]
ask patch 23 -12 [ sprout-big-buildings 3 ]
ask patch 16 -12 [ sprout-big-buildings 3 ]
ask big-buildings [
set size 2
set shape "bigbuildings"
set color white
]
end
to draw-water-pumping-stations ; setting water pumping statoins as stationary not going to move at all
ask patch 37 -18 [ sprout-water-pumping-stations 3 ]
ask patch -37 -18 [ sprout-water-pumping-stations 3 ] ;FOR THE COMMERICAL BUILDINGS
ask water-pumping-stations [
set size 2
set shape "producewater"
set color blue
]
end
to draw-sewage-plant-stations ; setting sewage plant station as stationary not going to move at all
ask patch 38 -1 [ sprout-sewage-plant-stations 4 ]
ask patch -18 -18 [sprout-sewage-plant-stations 4] ;FOR THE COMMERICAL BUILDING
ask sewage-plant-stations [
set size 2
set shape "sewage"
set color green
]
end
to draw-electricity-power-stations ; setting eletricity power station as stationary not going to move at all
ask patch -38 18 [ sprout-electricity-power-stations 5]
ask electricity-power-stations [
set size 2
set shape "electrcitystation"
set color 44
]
end
to draw-commercial-buildings ; setting commerical buildings as stationary not going to move at all
ask patch -5 -1 [ sprout-commercial-buildings 7 ]
ask patch -5 -6 [ sprout-commercial-buildings 7 ]
ask patch -15 -6 [ sprout-commercial-buildings 7 ]
ask patch -15 -1 [ sprout-commercial-buildings 7 ]
ask patch -25 -1 [ sprout-commercial-buildings 7 ]
ask patch -25 -6 [ sprout-commercial-buildings 7 ]
ask commercial-buildings [
set size 2
set shape "commercialbuilding"
set color orange
]
end
to draw-grid ; allows me to pinpoint where each turtle is properly ( helps me to debug the code)
ask patches [
sprout 1 [
set heading 0
set color black
set pen-size 1
forward 3
right 90
pen-down
repeat 8 [
forward 3 right 90 fd 3
]
die
]
]
end
to go
end
This is the image of what my model currently looks like and NetLogo whole image is what I imagine how my model will work as I haven't started doing the go procedure.
The model can be found in the following GitHub repo:
https://github.com/omoaka1738/Netlogo-help.git
So, my comment above aside, drawing the network so it can be traversed is a whole other thing. You don't want to get bogged down in having to make a path-seeking model, on top of what you are already trying to do. So it's important to not just draw the lines, but give them information to make using them easier. This is why you've really got to think about your model, all its parts, how they interact (not in terms of netlogo, but just like, in terms of if it was a game, what are the rules?)
First question:
Do we really need to model the “physical” location of the wire/pipe or do we just need to know what connects to what?
Maybe we are modeling the effect of breaks in the physical lines/pipes, so we do ned to model the pipes and lines.
Next: do we really need to model each step that the water/elec takes in the pipe? That’s not how electricity works. It’s only sort of how water works. That’s mostly about pressure.
Instead, we can “cook” or “preload” the wires and pipes with information about where the stuff is coming from, and where it goes. This lets the model know what it needs to know without us doing extra work while the model is running.
So:
First: instead of PD and FD etc, we can use turtles to be the pipes and wires, or possibly, just the terminals, access points and joints. If we need to, the pipes too.
Each member will know:
what services it provides, like water or electric
Whether it is a terminal, in-line access point, joint, or pipe (if we need to model pipes)
An indicator of which connection leads to the source, created while we create the network.
Note that we can also "pre-calculate" the path from the source to any destination by recording the steps to trace backwards from the destination to the source, then reversing the steps. Store this map in the destination.
The more I think about it, the more this looks like a job for linked turtles. Then you don't need to mark or create turtles for the "pipes" just for the terminals, bends, joints, and access points.
So consider:
a breed of turtle, maybe called [nodes node].
This is common jargon for networks (aka graphs), they have nodes and edges.
add a breed of directed links called [ edges edge](common jargon again)
nodes will own:
service -- will be "w" or "e" (for exampleor watever you want)
Source — contains the turtle that is at the source
Input-direction (or something) l—-contains the node that is in the direction towards the source
maybe some other stuff.
We can reuse some of your line-drawing code, and maybe make it more consider and easier to copy and change.
POSTING THIS ANSWER FOR NOW
will com back and add on letter
Add some comments to this answer to give more info about what the model is doing.
Related
I'm trying to create a yellow color gradient for the turtles. I have 31 turtle profiles and I would like each of the turtles to be a yellow color. I'm trying more what remains are all in white. And I think I'm complicating the code and the result is not coming out. Has anyone done something similar? If so, could you suggest a way?
globals [ AvailablePatch UnassignedProfileCountList ValidHabs MidpointnlColor2 TotalShades2 ]
turtles-own [ turtle-profiles-habitat ]
patches-own [ turtle-count habitatcover ]
to setup
clear-all
set ValidHabs [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ]
set UnassignedProfileCountList [ 0 ]
repeat 31
[
set UnassignedProfileCountList lput 9 UnassignedProfileCountList
]
(
foreach ValidHabs [
this-profile ->
set MidpointnlColor2 yellow
set TotalShades2 2
ask one-of AvailablePatch
[
sprout 1
[
set turtle-profiles-habitat this-profile
set color make-nl-color-shade2 MidpointnlColor2 ValidHabs TotalShades2
set size 1
]
set turtle-count count turtles-here
set AvailablePatch other AvailablePatch
]
]
)
end
to-report make-nl-color-shade2 [ nl-color shade-value num-shades ]
set shade-value min list num-shades max list 0 shade-value
report scale-color nl-color shade-value num-shades 0
end
You can shade the colors using scale-color color number range1 range2.
When you want to scale if based on the ValidHabs, you want the range to go from the smallest (min ValidHabs) to the highest value (max ValidHabs) of ValidHabs.
For every turtle, you want to have their actual habitat, that is turtle-profiles-habitat.
So anywhere in a turtle procedure, you can:
set color scale-color yellow turtle-profiles-habitat (min ValidHabs) (max ValidHabs)
How do you direct turtle movement into a cone? I want turtles to move into a cone of highest chemical. I tried face cone, but it triggered the error that
nothing named cone has been defined.
I am aware that uphill-chemical is an existing way to direct turtle movement, but it is not working in my model, since the turtles wander in and out of the trail, instead of clearly following it towards one direction.
My proposed code is:
to uphill-total-chemical-scent-in-cone
let gradient-color red ;; gradients are a temporary debugging feature
let scent-ahead total-chemical-scent-in-cone 20 0 10
let scent-ahead-2 total-chemical-scent-in-cone 20 -45 10
let scent-ahead-3 total-chemical-scent-in-cone 20 45 10
ifelse (scent-ahead-2 > scent-ahead) or (scent-ahead-3 > scent-ahead)
[ ifelse scent-ahead-3 > scent-ahead-2
[ face cone 20 45 10 hatch-gradients 1 [set color gradient-color ]] ;;
not sure how to direct movement into the cone, but could try face or some variation/ask for help
[ face cone 21 -45 10 hatch-gradients 1 [set color gradient-color ]]]
[face cone 20 0 10 hatch-gradients 1 [ set color gradient-color fd 1]]
end
I'm actually developing a simulation to study social phenomena.
My idea is a population of agents choosing the closer factory of them, walking forwards it and (when they reach it) go back to their initial position.
However, I'm having some trouble to program it. Some of the agents just pass by their initial position and reach the borders of my simulation, stopping there.
Here is my code:
breed [population person] ;create the population
breed [all-fac factory] ;create the factories
população-own [
myneighbor ;defines which factory is closer
myNND ;calculate the distance between the person and the closer factory
home-x ;initial x position
home-y ;initial y position
]
to setup
clear-all
reset-ticks
create-population 83 ; creates the population with 83 people in it
[
setxy random-xcor random-ycor ; set people initial position randomly
set shape "person"
set home-x xcor
set home-y ycor
]
create-all-fac 9
[
ask all-fac [ set color white]
ask facture 83 [ setxy 0 0 ] ; defines the initial position of every factory
ask facture 84 [ setxy 70 0]
ask facture 85 [ setxy -70 0 ]
ask facture 86 [ setxy 70 70 ]
ask facture 87 [ setxy 0 70 ]
ask facture 88 [ setxy -70 70 ]
ask facture 89 [ setxy 70 -70 ]
ask facture 90 [ setxy 0 -70 ]
ask facture 91 [ setxy -70 -70 ]
set shape "house"
]
end
to go
move
tick
choose-facture
end
to choose-facture
ask population [
set myneighbor min-one-of other fábricas [distance myself] ; choose my nearest neighbor based on distance
set myNND distance myneighbor
]
end
to move
ask population [
if xcor = home-x and ycor = home-y [
face myneighbor
fd 1
]
if any? all-fac in-radius 5 [
facexy home-x home-y
fd 1 ]
fd 1
]
end
If someone could help me with that I would be really thankful :) .
Arthur!
Your code was almost right!
I changed the names for consistency, to fix some typos.
I rearranged the commands in the go step so that they checked their positions before moving, not after. Usually we put "tick" as the last statement in the go procedure.
I manually reset the view to have max-xcor and max-ycor 100, and shut off wrapping, so the factories were within the view, and made the patch size 3 instead of 13, so the view fit on my laptop small screen.
Finally, I changed the test of whether someone was home to whether they were within a distance of 5 of home, not whether they were exactly at home -- just the same as the test you use for being at a factory.
With those changes, the code seems to do what you want it to -- the people walk to the factory and then back home, over and over. No one gets stuck on the border of the view.
;; NOTE -- the view has been changed to shut off horizontal and vertical wrapping
;; and max-pxcor = max-pycor = 100
;; and patch-size = 3 pixels, not 13
;; so population and factory size have been increased.
breed [population person] ;create the population
breed [all-fac factory] ;create the factories
;;população-own [
population-own [
myneighbor ;defines which factory is closer
myNND ;calculate the distance between the person and the closer factory
home-x ;initial x position
home-y ;initial y position
]
to setup
clear-all
reset-ticks
create-population 83 ; creates the population with 83 people in it
[
setxy random-xcor random-ycor ; set people initial position randomly
set shape "person"
set home-x xcor
set home-y ycor
set size 5
]
create-all-fac 9
[
ask all-fac [ set color white]
ask factory 83 [ setxy 0 0 ] ; defines the initial position of every factory
ask factory 84 [ setxy 70 0]
ask factory 85 [ setxy -70 0 ]
ask factory 86 [ setxy 70 70 ]
ask factory 87 [ setxy 0 70 ]
ask factory 88 [ setxy -70 70 ]
ask factory 89 [ setxy 70 -70 ]
ask factory 90 [ setxy 0 -70 ]
ask factory 91 [ setxy -70 -70 ]
set shape "house"
set size 5
]
end
to go
choose-factory ;; moved this command up from below tick
move
tick
;; choose-factory
end
to choose-factory
ask population [
set myneighbor min-one-of all-fac [distance myself] ; choose my nearest neighbor based on distance
set myNND distance myneighbor
]
end
to move
ask population [
;; if xcor = home-x and ycor = home-y [ ;;<<<<<<<<<<<<<<<<<<<< replaced this test
if (distancexy home-x home-y) <= 5 [
face myneighbor
fd 1
]
if any? all-fac in-radius 5 [
facexy home-x home-y
fd 1 ]
fd 1
]
end
One last suggestion. The code you wrote is quite "brittle", because if you change the number of people to some smaller number like 5, for testing, there will be no factory #83 to set anywhere and the code will crash.
You may want to have some slider to set the number of people and want your code to keep working for any number of people.
Also, it take a lot of lines to position all the factories. What is a good way to do it that is short, but will not break if you change the number of people?
Here's some code that does that. It makes a list of the factories when it creates them, so it then has a list of the who numbers of the 9 factories it just created.
Then, it finds those on the list and uses the numbers to position the factories.
It works. Here is the modified section of code:
let factory-list [] ;; make an empty list
create-all-fac 9
[
set shape "house"
set color white
set size 5
set factory-list fput who factory-list ; add this factory to the growing list
]
;; OK, now we have a list of all 9 factories.
;; Confirm that by printing it
print "here is the list of who-numbers of the new factories:"
show factory-list
;; now we need to insert those numbers into the following commands:
ask factory item 0 factory-list [ setxy 0 0 ] ; defines the initial position of every factory
ask factory item 1 factory-list [ setxy 70 0]
ask factory item 2 factory-list [ setxy -70 0 ]
ask factory item 3 factory-list [ setxy 70 70 ]
ask factory item 4 factory-list [ setxy 0 70 ]
ask factory item 5 factory-list [ setxy -70 70 ]
ask factory item 6 factory-list [ setxy 70 -70 ]
ask factory item 7 factory-list [ setxy 0 -70 ]
ask factory item 8 factory-list [ setxy -70 -70 ]
I have the following code which creates a grid of agents and place links between pairs if they are in a distance of interference.
breed [ readers reader ]
undirected-link-breed [ rris rri ]
globals [
interf-rri-radius
num-readers
distance-var-x
distance-var-y
readers-per-row
readers-per-column
num-checkouts
]
to setup
ca
setup-globals
ask patches [ set pcolor blue - 3 ]
spawn-by-row-col
reset-ticks
end
to setup-globals
set interf-rri-radius 1005
set num-readers 40
set distance-var-x 12
set distance-var-y 22
set readers-per-row 8
set readers-per-column 5
set num-checkouts 0
end
to spawn-by-row-col
let half-step 0.5 * distance-var-x
let d-vals ( range ( min-pxcor + half-step ) ( min-pxcor + (readers-per-row * distance-var-x)) distance-var-x )
let dc-vals ( range ( min-pxcor + half-step ) ( min-pycor + (readers-per-column * distance-var-y)) distance-var-y )
show dc-vals
; Create an empty list to build into
let possible-coords []
; For each possible vertical value, map all horizontal values in order and
; combine these into an ordered list starting at the lowest px and py coords
foreach dc-vals [
d ->
set possible-coords ( sentence possible-coords map [ i -> (list i d) ] d-vals )
]
show (word "possible-coords = " possible-coords)
; Use the number of readers to sublist the possible coordinates, and
; create a turtle at each of the coordinate combinations left.
let max-positions length possible-coords
if max-positions > (num-readers + num-checkouts) [ set max-positions (num-readers + num-checkouts) ]
let use-coords sublist possible-coords num-checkouts max-positions
foreach use-coords [
coords ->
create-readers 1 [
setxy item 0 coords item 1 coords
set shape "square 2"
set size 2
set color 15
]
]
ask readers [ create-rris-with other readers in-radius (interf-rri-radius / 10) ]
end
The neighbors of reader 0 are
show [sort [who] of rri-neighbors] of reader 0
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39]
However, the distance between reader 0 and reader 39 is
show [distance reader 0] of reader 39
121.6552506059644
and the connection radius is 1005/10= 100.5 so they should not be connected with an rri link.
Notice, I use a origin-centered world of -50 to 50 in X and Y dimensions.
I have tested the code in previous Netlogo version 6.0.4 and the reader 39 is not a neighbour of the reader 0.
I do not know what can be the problem. I believe is the new version but I would like to be sure.
In case anyone else runs into this, there is a confirmed bug with in-radius in the NetLogo 6.1.0 release, and the details are in the bug report on GitHub.
The issue only affects in-radius when used in a non-wrapped world, and only when used on turtles, and only when used with a radius that's a significant % of the world width. If you're using a world with wrapping, or using in-radius with patches, or using a small radius relative to the world size, your data will be correct and you don't need the below workaround.
As a workaround if your model is affected, you can use a simple user-defined NetLogo procedure in your model until the fix is published. It's not going to be super-fast if your having turtles calculate in-radius many times per tick, but if you have few turtles or are just using it during setup, it should be okay:
to-report temp-in-radius [agentset r]
report agentset with [ distance myself <= r ]
end
Then, instead of create-rris-with other readers in-radius (interf-rri-radius / 10) you would do create-rris-with (temp-in-radius other readers (interf-rri-radius / 10)).
Or more generally, instead of something like count other turtles in-radius 5 would become count temp-in-radius (other turtles) 5.
I am currently building a model for people walking on a certain path (road patches, color = white). The agents' movement will only be limited on the white patch and when they encounter black patch they will rotate 180 deg.
The problem I found was when I tried to set the heading of the agents. I want the heading to walk to the nearest red patch (destination), but when I use:
1
set heading towards min-one-of patches with [pcolor = red ] [ distance myself ] <
the agents wont move from their patch. It turned out that the "heading" generated by the above code has the value of -360 to 360 with decimals in it. The walking procedure that I have built only allows heading with multiplies of 90 (-360, -180 ... 90, 180, 270, 360). This is the full code for my walking procedure:
2
globals [ flagl flagr ]
ask agents [
;reset flag
set flagr 0
set flagl 0
; check and note if there is a path on the left or the right
ask patch-left-and-ahead 90 1 [if (pcolor = white ) [set flagl 1]]
ask patch-right-and-ahead 90 1 [if (pcolor = white ) [set flagr 1]
]
;in T-junction, decide to turn left or right (random)
if((flagl = 1) and (flagr = 1))
[
ifelse random 100 > 50
[set heading heading - 90]
[set heading heading + 90]
]
;if it's only applicable to turn right, then turn right
if((flagl = 0) and (flagr = 1)) [set heading heading + 90]
;if it's only applicable to turn left, then turn right
if((flagl = 1) and (flagr = 0)) [set heading heading - 90]
;return if there's no mor path
if [pcolor] of patch-at-heading-and-distance heading 1 = black [rt 180]
;agent movement
**;face min-one-of patches with [pcolor = red ] [ distance myself ]**
fd 1
;stopping procedure
if [pcolor] of patch-here = red [fd 0]
]]
I tried to round the heading generated by #1 like this:
let direction heading towards min-one-of patches with [pcolor = red ] [ distance myself ]
if direction != mod 90
set direction 90 * round(arah/ 90)
However it gives error notice since "let" command cannot take that kind of input.
This is the screenshot of the map I have built:
Map
Any help to solve this problem is hugely appreciated. Thank you!
This might get you started- with minimal checking it seems to do the trick, but you will probably want to check for exceptions.
The cardinal-4 reporter takes whatever input is given and checks if the modulo is equal to 0. If not, it will check if modulo is less than/equal to 45- if so, subtracts that value from the direction (dir). If modulo is greater than 45, the subtract the modulo from the direction and add 90.
The check procedure just prints the reporter output for the values listed.
to setup
ca
reset-ticks
end
to-report cardinal-4 [ heading-input ]
let dir heading-input
let remain dir mod 90
if remain != 0 [
ifelse remain <= 45 [
set dir dir - remain
]
[
set dir dir - remain + 90
]
]
report dir
end
Edit: improved check procedure:
to check
let testVals [ -361 -359 -271 -269 -181 -179 -91 -89 -1 0 1 89 91 179 181 269 271 359 360 361 ]
print map [ i -> ( word i " becomes " cardinal-4 i "\n" ) ] testVals
end