Currently I am working on developing an infrastructure model in NetLogo and I have difficulty making the turtle follow the road. This I do using the gis extension.
The goal of this model is to simulate traffic movement, and for that the cars within the model need to follow the roads. However, the cars do not follow the roads at all when I try to make this work.
to load
ca
clear-patches
show "Wegenbestand laden..."
set data gis:load-dataset Input_bestand_shp
show "Wegen geladen"
show "Data importen..."
let timing 1
foreach gis:feature-list-of data
[[b] -> ask patches gis:intersecting b
[
set pspeed gis:property-value b "snelheid"
set road gis:property-value b "gid"
set status gis:property-value b "status"
set a_dir_x gis:property-value b "s_d_x"
set a_dir_y gis:property-value b "s_d_y"
set b_dir_x gis:property-value b "e_d_x"
set b_dir_y gis:property-value b "e_d_y"
if status = "gesloten" [ set pspeed 0]
if pspeed = 30 [ set maxdruk Algemene_capaciteit_straat / 5 ]
if pspeed = 80 [ set maxdruk Algemene_capaciteit_autoweg / 5]
if pspeed = 130 [ set maxdruk Algemene_capaciteit_snelweg / 5]
set fid gis:property-value b "did"
show timing
set timing (timing + 1)
]
]
show "Data geimporteerd"
ask patches [ set pcolor scale-color blue pspeed 0 130 ]
set-default-shape turtles "car"
;set exp-count 1
;set border patches with [ count neighbors != 8 ]
end
to setup
clear-turtles
clear-all-plots
reset-ticks
setup-turtles
end
to setup-turtles
;een turtle is 5 autos
ask patch Start-X Start-Y
[sprout Verkeersintensiteit_geslotenweg / 5 [ set color red ]]
;1 tick = 1 minute
ask patches[
ask turtles-here[
set speed pspeed / 60
set currentroad road
set dir_x b_dir_x
set dir_y b_dir_y
]
]
end
to go
setup-turtles
;1 tick = 1 minute
ask turtles [
facexy dir_x dir_y
fd speed
]
end
With the above code I first tested to see if any direction would work. However, the cars either randomly stop, go in completely the wrong direction and I do not know what to do.
The input are line segments that are split on vertices. The a_dir_x and the others show the front and back of each segment to give a direction. The coordinate system is WGS 84 UTM 31N.
Another thing that I was considering was to use the move-to function, which should be easier for use. In that case I would require to know the cellsize and I do not know how this is determined in NetLogo.
Does anyone have any ideas or experience with this? Thank you!
here is an image of the interface tab
Related
I'm working on a smaller project and got stuck on an issue, I'm not really sure if it's possible to solve it in NetLogo but I want to give StackOverflow a go!
I got a model that divides the world into different parts and randomly add physical features (such as rivers). If a feature goes through the whole region, I want it to separate the region and make into two regions. As an example, in the picture below, I want to separate the purple region into two unique regions accordingly to the physical feature (black).
The code I used to generate the picture above, can be found below.
to setup
ca
;Setting world.
resize-world 0 19 0 19
;Creating regions.
let x 5
let y 5
let col 45
while [y <= max-pycor + 1][
while [x <= max-pxcor + 1 ][
ask patches with [pxcor < x and pxcor >= x - 5 and pycor < y and pycor >= y - 5][
set pcolor col
]
set x x + 5
set col col + 10
]
set x 5
set y y + 5
]
;Generating physical features.
ask n-of 5 patches[ sprout 1[
set pcolor black]
]
let i 0
while [ i < (max-pycor * 2 )][
ask turtles [
fd 1
set pcolor black
ifelse (random 20 <= 1)
[
rt one-of [-90 0 90]
forward 1
]
[
fd 1
set pcolor black
fd 1
set pcolor black
]
set pcolor black
set i i + 1]
]
ask turtles [die]
end
My strategy for handling this is to realize that all we really need to do is "flood" a patch out by color and tag all the found adjacent patches, then repeat for any un-tagged, non-black patches until they are all done.
NetLogo does not have a "flood" command to get all patches adjacent to a patch meeting a criteria, so we make a special reporter of our own to handle it, patches-adjacent. Then it's just easy to ask those patches-adjacent to set their region to the currently chosen region.
I don't love this code, it's a little finicky and would be prone to infinite loops if tweaked incorrectly, but it should work. I bet there is a cleaner way to do this that I'm not thinking of at the moment.
; add a variable to track the different regions
; the default value will be `0` for each patch when `clear-all` is called
patches-own [ region ]
to set-regions
let current-region 1
; only act on non-black patches that haven't yet been assigned a region
let untagged patches with [ region = 0 and pcolor != black ]
while [any? untagged] [
ask one-of untagged [
ask patches-adjacent [
set region current-region
]
]
; update the region and the untagged patches we have left to process
set current-region current-region + 1
set untagged patches with [ region = 0 and pcolor != black ]
]
; this is just to get a view of the regions to quickly see if our code worked, it can be removed
ask patches [ set plabel region ]
end
to-report patches-adjacent
report patches-adjacent-ex (patch-set self) pcolor
end
to-report patches-adjacent-ex [found pc]
let newly-found neighbors4 with [ (not member? self found) and pcolor = pc and region = 0 and pcolor != black ]
set found (patch-set found newly-found)
ask newly-found [
; use recursion to find the patches adjacent to each newly-found one
; relying on updating the `found` agentset as we go to avoid duplicates
; or looping forwarder
set found (patches-adjacent-ex found pc)
]
report found
end
I solved this by using the Patch Clusters model that can be found in the NetLogo model library.
I am new in using NetLogo, so I hope you can help me with this code.
I would like to build a network with two subnetwork, A and B, where A has N nodes and B beta*N nodes, where beta=0.5. Each network should be initialised with five fully connected nodes. I need to add a new node at a time, assuming that each has fixed out-degree k. Once a new node i comes into the network, it links to a randomly selected target node j. The other remaining k-1 nodes should be selected as following: with probability p, i should be linked to a random node of j's; with probability 1-p, i should be connected to another randomly selected node in A.
On the other hand, the nodes in B should be linked (directed link) to each node in A with probability P. P can vary from 0 to 1.
What I already tried is built the two networks with N and alpha*N nodes respectively. But, as I said, I am new in using NetLogo and I am finding many difficulties to build this network that should be really easy in a different programming language, I would be more familiar with.
; Global variables
breed [agents agent]
breed [bagents bagent]
to setup
clear-all
setup-agent
setup-bagent
end
; Defining agents
to setup-agent
set-default-shape turtles "person" ; agent shape
create-agents n-of-agents ; # of agents
[set size 2 ; agent size
set color white
setxy (random-xcor) (random-ycor)
]
; Random Network
ask agents [create-link-with one-of other agents with [not link-neighbor? myself]
ask links [set color white]
]
end
; Defining bagents
to setup-bagent
set-default-shape turtles "circle" ; bagents shape
set beta=0.5
let n-of-bagents beta*n-of-agents
create-bagents beta*n-of-agents ; # of bagents
[set size 2 ; bagent size
set color red
setxy (random-xcor) (random-ycor)
; Network
ask bagents [create-link-with one-of other bagents with [not link-neighbor? myself]
ask links [set color yellow]
]
end
to go
end
I hope you can help me to understand how to build such a network in NetLogo.
Many thanks
This does what you said. I don't think it's actually what you want as your algorithm is much better but still somewhat confused. But hopefully this gets you on the correct path.
UPDATED to make one node add each tick
globals
[ beta
prob
k
]
breed [A-agents A-agent]
breed [B-agents B-agent]
to setup
clear-all
set beta 0.5
set prob 0.2
set k 3
setup-A-seed
setup-B-seed
reset-ticks
end
to go
add-A-node
if random-float 1 < beta [add-B-node]
tick
end
; Defining A seed network
to setup-A-seed
create-A-agents 5
[ set shape "person"
set size 2
set color white
setxy random-xcor random-ycor
]
ask A-agents
[ create-links-to other A-agents
[ set color white ]
]
end
; Defining B seed network
to setup-B-seed
create-B-agents 5
[ set shape "circle"
set size 2
set color red
setxy random-xcor random-ycor
]
ask B-agents
[ create-links-to other B-agents
[ set color yellow ]
]
end
to add-A-node
create-A-agents 1
[ set shape "person"
set size 2
set color white
setxy random-xcor random-ycor
let target one-of other A-agents ; target is j in description
create-link-to target
repeat k - 1
[ let candidates (other [link-neighbors] of target) with [not link-neighbor? myself]
ifelse random-float 1 < prob or not any? candidates
[ create-link-to one-of other A-agents with [not link-neighbor? myself]
[ set color white ]
]
[ create-link-to one-of candidates
[ set color white ]
]
]
]
end
to add-B-node
create-B-agents 1
[ set shape "circle"
set size 2
set color red
setxy random-xcor random-ycor
let thisB self
ask A-agents
[ if random-float 1 < prob
[ create-link-from thisB
[ set color yellow
]
]
]
]
end
Some of the NetLogo issues I noticed in your code:
NetLogo does not use = for set
you must have space around mathematical operators (or NetLogo will think it's part of the name)
Some of the things you need to think about in your algorithm:
why do you have an initial B network if all Bs connect to each A with fixed probability?
what do you do if the selected A doesn't have any edges to follow?
As general advice, don't try writing something this complicated in one piece. Create your seed networks with 5 fully connected nodes. Make that work. Then do network A and make that work. Then bring in B. This iterative building is important for all programming languages. It is particularly important when using a new language so that you only have to debug one or two errors at a time and you know where the bugs are.
I am new to NetLogo, so my apologies in advance if that question is very stupid. I would like to create an Agent-Based Model where animals move around in a complex terrain looking for water sources. Movement should be downhill, constrained by steep slopes (>25°) and targets should be lakes. I am using a real-world example from GIS data for this, and I have already managed to setup a world containing an ASCII elevation grid, a shapefile containing lines that represent slopes steeper 25degrees and a shapefile containing areas representing lakes. I have created animals (cows) and found a code line telling them to move downhill. Now, I would like to tell them
a) to avoid slopes >25° by using the slope shapefiles as obstacles and
b) to go to the lakes by using the lake shapfiles as targets
Can someone help me how to code this?
Many thanks in advance!
Here is the code I have put together so far
breed [ cows cow ]
extensions [ gis ]
patches-own [ elevation ]
globals [
slope-dataset
lake-dataset
elevation-dataset
]
to setup-terrain
clear-all
reset-ticks
set slope-dataset gis:load-dataset "FILENAME.shp" ;;extent of GIS datasets is N42.3-43.4 and W120.0-121.1
set lake-dataset gis:load-dataset "FILENAME.shp"
set elevation-dataset gis:load-dataset "FILENAME.asc"
gis:set-world-envelope gis:envelope-of slope-dataset
gis:set-world-envelope gis:envelope-of lake-dataset
gis:set-world-envelope gis:envelope-of elevation-dataset
end
to display-slopes
gis:set-drawing-color red
gis:draw slope-dataset 0.5
end
to display-lakes
gis:set-drawing-color blue
gis:draw lake-dataset 2
end
to display-elevation-in-patches
gis:apply-raster elevation-dataset elevation
let min-elevation gis:minimum-of elevation-dataset
let max-elevation gis:maximum-of elevation-dataset
ask patches
[ ; note the use of the "<= 0 or >= 0" technique to filter out
; "not a number" values, as discussed in the documentation.
if (elevation <= 0) or (elevation >= 0)
[ set pcolor scale-color black elevation min-elevation max-elevation ] ]
end
to setup-cows
set-default-shape cows "cow"
create-cows 100 [
setxy random-pxcor random-pycor
set size 1
set color white
]
end
to move
move-to patch-here ;; go to patch center
let p min-one-of neighbors [elevation]
if [elevation] of p < elevation [
face p
move-to p ;; makes cows move to the next lower elevation patch, if no lower
elevetion is present, cow doesn't move
]
end
to go
ask cows [
move
]
end
Thanks Seth, I have found the solution using the gis:intersects? primitive you suggested:
to display-slopes
ask patches gis:intersecting slope-dataset
[ set pcolor red ]
end
to go
ask cows
[fd 1
avoid-ostacles]
tick
end
to avoid-obstacles
ask cows [
if [pcolor] of patch-ahead 1 = red
[rt 90 fd 1]]
end
This way I can color the patches in red that intersect with the shapefile containing the slope vector dataset and then ask the cows to avoid and move around red colored patches.
Thanks again!
Based on the “sheep eat grass” model. The patches are changing color with the agents move. I want to check if a patch has remain a centain color for a while (such as 5 ticks). If a patch has stayed in a centain color for a centain times without any change it will turn into black.
I try to use countup but it didn’t work. I need a accumulated state. Thanks a lot
If pcolor=green[
Ifelse countup>=5[
Set pcolor black
Set countup 0]
[set countup countup+1]]
Can you give a little more detail as to what is going wrong with the code you've shown? For example, with this setup:
patches-own [ time-spent-green ]
to setup
ca
crt 3
reset-ticks
end
Something very similar to your example works fine for me:
to go
ask turtles [
rt random 61 - 30
fd 1
ask patch-here [
set pcolor green
]
]
ask patches with [ pcolor = green ] [
ifelse time-spent-green >= 5 [
set pcolor black
set time-spent-green 0
] [
set time-spent-green time-spent-green + 1
]
]
tick
end
Where the patches stay green for 5 ticks then turn back to black.
Platfrom : NetLogo
Question
I want to move my flag specific 3 point
A(-12 8)
B(-5 12)
C(6 4)
-While travelling this point plot energy/time randomly decrease.
-When reach C flag will die.
I asked before and found this solution for moving. ( When turtle reached 2. point it will not stop ) - LINES1 -
breed [cities city]
breed [flag person]
flag-own [target]
to setup
clear-all
create-flag 1
[ set size 6
set shape "by"
setxy -5 3
set target patch -10 5
face target
]
< other commands >
end
to go
ask flag-on patch -10 5
[ set target patch <next place you want it to go>
face target
]
ask flag with [ shape = "by" ]
[ forward 1 ]
end
People suggest this code for towarding any target.
to go
ask people [
;; if at target, choose a new random target
if distance target = 0
[ set target one-of houses
face target ]
;; move towards target. once the distance is less than 1,
;; use move-to to land exactly on the target.
ifelse distance target < 1
[ move-to target ]
[ fd 1 ]
]
tick
end
In this code they will travel randomly and i dont want this. I cant implement this part at -LINES1-
I'm try to explain this with an image.
Well, this is the question : How can i move turtle along these points and connect the graph for energy/time or energy/distance.
CC : #Seth Tisue #JenB #yacc
UPDATE 1
-Guys i finished my movement part of my program aid of community. In this code your turtle will move specific point and it will die when it reach last point. While travel it is plotting the number of turtle
breed [cities city]
breed [flag person]
flag-own [target] ;;set features flag only
to setup
clear-all
reset-ticks
print "Setting up model."
set-default-shape cities "house" ;; set all cities shape by house
create-flag 1
[
set SIZE 2
set shape "turtle"
setxy -11 13
set target patch -3 12
face target
]
create-cities 1
[set color yellow set SIZE 2 setxy 8 2]
create-cities 1
[ set color yellow set SIZE 2 setxy -3 12]
create-cities 1
[ set color yellow set SIZE 2 setxy 3 3]
ask patch 3 3 [set pcolor red]
end
to go
ask flag-on patch -3 12 [
set target patch 8 2
face target
]
ask flag-on patch 8 2 [
set target patch 3 3
face target
]
ask flag-on patch 3 3 [
if distance target < 1 ;; check distance for last point
[die]]
ask flag with [ shape = "turtle" ]
[fd 1]
tick
end
Have you tried to understand the answers you have already been given? In the setup, replace -10 5 with the first place you want to go to (which is -12 8). Then update the go code accordingly.
to go
ask flag-on patch -12 8
[ set target patch -5 12
face target
]
ask flag-on patch -5 12
[ set target patch 6 4
face target
]
ask flag with [ shape = "by" ]
[ forward 1 ]
end
This is just the direction and moving. You need to try and do some code for the energy and dying etc. But do things gradually, get something working and then add the next piece.