NETLOGO: pass on water amount along the nodes of a river network - netlogo

I would like to simulate the water flow along a river with NETLOGO. Therefore I do have many water nodes which are linked to each other; and each water node having the variable "amount_water". Every tick, the variable "amount_water" should be passed on to the next water node. At each node waterusers (various agents) can interact with the stream flow and extract some water, which would change the variable "amount_water"; but now I would like show you the modelled river flow only, without the water users.
if you have a model world with min-pycor -6 and max-pycor 6:
breed [waternodes waternode]
waternodes-own
[
amount_water
]
to setup
clear-all
reset-ticks
; create the waternodes
create-waternodes 13 [setxy 0 (who - 6) set shape "dot" set color blue]
ask waternodes
[
let neighborbelow waternodes-on neighbors4 with [pycor < [ycor] of myself]
create-links-to waternodes-on neighborbelow
]
end
to go
move-water
update-inflow
tick
end
to move-water
ask waternodes
[
ask out-link-neighbors [set amount_water [amount_water] of myself]
]
end
to update-inflow
ask waternode 12 [set amount_water ticks]
end
(in my model, the inflow is of course not the number of ticks, but it is read in from a csv-file)
My problem: With this code, the amount of water is NOT continuously passed on from node to node (and I do not know why!!) ????
And generally, I am not so sure if this network is the best idea to represent a stream flow. Can you think of other solutions?
Many thanks

I have solved the problem so far. It seems that when I just call the waternodes in move-water like this
to move-water
ask waternodes
[
ask out-link-neighbors [set amount_water [amount_water] of myself]
]
end
, the waternodes are not always called in order from 0 to 11.
Therfore I changed the code to
to move-water
(foreach sort-on [who] waternodes
[the-turtle -> ask the-turtle [ask out-link-neighbors [set amount_water [amount_water] of myself]]])
end
and now it is working!

Related

Is there a way to kill a certain number of turtles within a radius of a patch with a certain color in Netlogo?

I am very new to Netlogo and I am trying to simulate culling of turtles within a certain region based on the characteristics of the patch. Within my model landscape, I have one single patch that is orange. I want 50% of turtles within 5 pixels of that patch to die. I would also like this process to continue every year after the first year of the simulation (this part I think I have). Can anyone help me?
if d = 10 [
if year >= 2 [
let ring nobody
set ring patches in-radius 5 patches with [pcolor = orange]
ask turtles-on ring [die]
]
]
in-radius reports agents that are within a certain distance from the caller, so the orange patch must be the one calling in-radius.
The code in your example is not reproducible, so I'll make an arbitrary example:
to setup
clear-all
ask one-of patches [
set pcolor orange
]
create-turtles 100 [
setxy random-xcor random-ycor
]
end
to go
ask patches with [pcolor = orange] [
ask turtles-on patches in-radius 5 [
if (random 2 < 1) [die]
]
]
end
Note that in-radius reports agents within a certain distance, where the unit of measure of distance is a patch`s side.
Identifying patches within a certain distance and then asking all turtles on those patches to die (as in the example above, which follows the approach in your question) has a different effect than asking all turtles within a certain distance to die.
Which of the two approaches fits your case is something that you have to decide. Below, the code that would address turtles directly:
to go
ask patches with [pcolor = orange] [
ask turtles in-radius 5 [
if (random 2 < 1) [die]
]
]
end

getting a particular colored turtle to do something on a particular colored patch in Netlogo

I am putting together a Netlogo model where students and criminals are interacting on a college campus. If criminals are close to students, a chase ensues so that when a criminal is distance < 1 the student dies. In addition to dying when criminal are to close I also want students(leader) to die during the chase when they are on a green patch. Green patches in this sense equate 'safe' areas. I have procedures set up for criminals chasing(follower) and students running away(leader), but I am at a complete block with getting the students being chased to die when coming across a green patch. One thought I had was making students being chased blue with the follower procedure:
ask students[
set no-threat criminals in-radius 25
ifelse any? no-threat[set color blue][set color black]
]
Then having blue students die when they encounter a green patch. I think I am missing something obvious to get this to work. Any nudge in the right direction would be much appreciated!
FYI, I am also relatively new to asking questions here so all apologize for any forum faux pas
here is the meat of the program I have been working on for reference
'''
to move-to-student
ask criminals[
set candidate students in-radius 25
if any? candidate[
set leader min-one-of candidate [distance myself]
if leader != nobody [ set color red]
;set follower myself
face leader
fd .3
if [pcolor] of patch-ahead 5 = 2.2 [
set heading heading - 100]
]
]
end
to run-from-criminal
ask students[
;while[distance follower > 1][
set threat criminals in-radius 25
if any? threat[
set follower min-one-of threat [distance myself]
if distance follower < 1 [ set number-dead number-dead + 1 die]; set candidate leader set threat follower ] ;students 'die' if criminal gets to close
if threat != nobody [set color blue]
face follower
rt 180 lt random 20 rt random 20 fd .2
if [pcolor] of patch-ahead 5 = 2.2 [
set heading heading - 100]
]]
end
to safe-die
ask students[
if follower = blue and if [pcolor] of patch-here = green [die]
]
end
to revert-leader
ask students[
set no-threat criminals in-radius 25
ifelse any? no-threat[set color blue][set color black]
]
end
to create-new-students
ask students[
if count students < student-count [hatch 1 setxy random-xcor random-ycor]
]
end
I figured it out. Maybe writing it down helped.
Looks like my solution was a combination of a nested if statement (something I've tried and failed at previously) and assigning the agents I want to die to an agent-set. There might be a better way to get this done but I wanted to share the solution I got...
ask students[
if any? no-threat[if [pcolor] of patch-here = green [die]
]

Pathfinding on Netlogo - Robotic lawn mower simulation project

I am trying to simulate a robotic lawn mower on Netlogo. My goals were that :
It goes randomly on green patches (grass) to cut them (green turns grey).
It finds its way home to recharge itself when the battery is low.
It does not go away the defined perimeter (garden) narrowed by red patches and goes arround obstacles placed by user with mouse clicks.
I have now a working code for my two first objectives(thanks to Luke !).
However making the lawn mower respect my whish of not going on red patches is getting complicated after getting in the to check death step.
Indeed it seems that it does not take into account my previous orders in the to move-cars step.
Some people told me that if I wanted to make it navigate around the walls it would be actually pretty involved and will require some kind of pathfinding.
Through my research I found some documentation about what is called an A*.
I understood the concept of the algorithm well but its translation on Netlogo is very difficult for me. What I did without a course so far was pretty basic, based on existing codes and following the advice I was given. But from now on, adapting an A* for my code makes me lose my hair. I hope to be able to finally understand the intricacies of Netlogo by understanding how pathfinding would be implemented in my code.
I understand that some people will not want to dwell on explaining how it works. I just hope to find someone patient enough to explain to me how things work with an A* step by step.
Thanks a lot
Here's my code :
breed [cars car]
cars-own [target charging? charge-time car-energy]
breed [houses house]
to setup
clear-all
setup-patches
setup-cars
setup-house
reset-ticks
end
to setup-patches
ask patches [set pcolor green] ;; Setup grass patches
ask patches with [
pxcor = max-pxcor or
pxcor = min-pxcor or
pycor = max-pycor or
pycor = min-pycor ] [
set pcolor red ;; Setup garden perimeter
]
end
to setup-cars
set-default-shape cars "car"
create-cars 1 [
setxy 8 8 ;; Place the lawn mower near the house
set target one-of houses
set charging? false
set car-energy energy
]
end
to setup-house
set-default-shape houses "house"
ask patch 7 8 [sprout-houses 1]
end
to place-walls
if mouse-down? [
ask patch mouse-xcor mouse-ycor [ set pcolor red ]
display ;; Permits the user to place red patches as new obstacles in garden.
]
end
to go
move-cars
cut-grass
check-death ;; Verify % battery.
tick
end
to move-cars
ask cars [
ifelse charging? [
set charge-time charge-time + 1 ;; Counts the time spent to charge
if charge-time > 15 [ ;; Defined charge time here
set car-energy 1000
set charging? false
set charge-time 0
]
] [
ifelse [pcolor] of patch-ahead 1 = red
[ lt random-float 360 ] ;; See a red patch ahead : turn left randomly
[ fd 1 ] ;; Otherwise, its safe to go foward.
set car-energy car-energy - 1
]
]
end
to cut-grass
ask cars [
if pcolor = green [ ;; cut grass (green patches go grey)
set pcolor gray
]
]
end
to check-death ;; checking battery level
ask cars [
ifelse car-energy >= 300
[ set label "Energy ok" ]
[ set label "Low Energy, returning to base"
set target min-one-of houses [distance myself]
face target
ifelse distance target < 1
[ move-to target
set charging? true
set label "Charging"
]
[ fd 1 ]
]
]
end

Changing Node ID with every Setup in Netlogo

We try to show a simple infection via Netlogo. For our purpose we need to start the infection with the same turtle for several times.
But right now with every setup another turtle begins with the infection. We already tried to work with the Node ID, but unfortunately the ID of the different turtles changes with every setup, too. We are out of ideas but
maybe there is a way to sove this problem I am happy for any answers :)
This is our Code so far:
extensions [nw]
globals
[
num-informed
informed-size
]
turtles-own
[
informed?
]
to setup
clear-all
nw:load-graphml "JK_nachnamen.graphml"
ask turtles [ set size 1.5 ]
layout-radial turtles links turtle 61
ask turtles [set color red]
ask turtles [set shape "dot"]
ask links [set color grey + 1.5]
ask patches [set pcolor white]
ask turtles [set label-color black]
ask turtles [set informed? false]
ask turtle 72
[
set informed? true
set color green
]
set num-informed 1
set informed-size 2
reset-ticks
nw:save-graphml "JKnachnamennetlogo.graphml"
end
to spread
if (count turtles with [informed? = true] > .7 * count turtles) [stop]
ask turtles with [ informed? = true ]
[
ask link-neighbors with [not informed?]
[
if (random-float 1 <= 0.01)
[
set informed? true
show-turtle
set color green
]
]
]
set num-informed count turtles with [informed? = true]
tick
end
Thank you a lot.
I am a little unclear so am giving bits of different answers for different situations.
If the turtles are different each time, what do you mean by 'the same turtle'. For example, do you mean the turtle in a particular position? If so, you could select the turtle on the appropriate patch.
If it doesn't matter which particular turtle it is (just that it's the same turtle), then the simplest approach is to set the random-seed. Then every time you run any random process (including choosing one-of the turtles to select the starting infection, or ask turtles to do something), NetLogo will use the same chain of random numbers. Of course, if you are still building your model, then adding new pieces of code that change how many calls are made to the random number generator will lead to a different chain, but rerunning with the same code will give the identical run.
You may need to use with-local-randomness and random-seed new-seed if you want to have some parts actually change.
The problem is that nw does not store the WHO variable this is to avoid conflict with already existing turtles in a model.
A work-around would be assigning each turtle a separate id variable and setting that to who.
turtles-own [informed? id]
in turtles creation asign them each the id thus
set id who
you may want to write a conversion procedure like this
to convert
nw:load-graphml "JK_nachnamen.graphml"
ask turtles [set id who]
nw:save-graphml file-name "JK_nachnamen(id).graphml"
end
and use the copy. Of course you would not use
turtle 74
but
one-of turtles with [id = 74]

How to ensure turtles follow heading to leader's heading and avoid the obstacles at the same time

I'm trying to set turtles heading equal to leader's heading and at the same all turtles including leaders must avoid obstacle( avoid_obstacles function). My problem is, when i add the set heading code
in flock function:
[set heading [heading] of one-of nearby-leaders ]
it cause my avoid-obstacle code to break. if i comment out this code, avoid obstacle work fine. below is my complete code.
to go
set time-to-evacuate time-to-evacuate + 1
ask turtles [check fd 0.1]
ask turtles [avoid_obstacles fd 0.1]
ask turtles with [pcolor = red][die] ;;turtles exit thru red door will die
if all? turtles [ pcolor = red ] ;; stop simulation
[ stop ]
tick
end
to check
if not leader?
[let beings-seen patches in-cone vision vision-angle with [pcolor = red]
ifelse any? beings-seen
[let target one-of beings-seen face target fd 1 ]
[flock]]
if leader?
[let beings-seen patches in-cone leader-vision leader-vision-angle with [pcolor = red]
ifelse any? beings-seen
[let target one-of beings-seen face target fd 1 ]
[flock]]
end
to flock
let nearby-leaders turtles with [leader? ]
if any? nearby-leaders in-radius vision
[ set heading [heading] of one-of nearby-leaders ]
end
to avoid_obstacles ;; all obstacles set as green patches
let i 1
while [[pcolor] of patch-ahead i != green and i <= vision]
[set i (i + 1) ]
if ([pcolor] of patch-ahead i = green)
[
ifelse [pcolor] of patch-at-heading-and-distance (heading - 20) i + 1 = green
[
ifelse [pcolor] of patch-at-heading-and-distance (heading + 20) i + 1 = green
[
ifelse random 1 = 0
[ rt 30 ]
[ lt 30 ]
]
[ rt 60 ]
]
[lt 60]
]
end
can somebody point what wrong with my code
First of all, consider abandoning the concept of having "leaders" as such. In crowd escape panic situations, they don't stop and hold elections. LOL. I don't know what your model is trying to model, so I have no suggestions here. My model "
Anyway, what your "followers" need to do is integrate to desires, the desire to go the same way as the leader, and the desire to avoid obstacles.
For the first, the follower simply needs a place to remember the "desired" direction. So, rather than set heading directly, you store the heading of the leader./ Then you perhaps use that heading to influence the movement around obstacles.j
For the second, well, obstacle avoidance is a complex discussion on its own. Whatever you have put together, you need to modify it so that it takes the "desired" heading into account, while still effectively avoiding obstacles. This is can be very difficult to do simply.
My model "homing particles 2009" uses one method. This model is designed to explore a particular homing/avoidance behavior. When a particle can't move in the desired direction, it is allowed to move in a limited number of other directions, instead.
Here is the link: http://www.turtlezero.com/models/view.php?model=homing-particles_2009
Unless you have added http://www.turtlezero.com to your list of allowed sites in your Configure Java console (not recommended, but you can trust me, right?), you will not be able to run my models in the browser. I haven't checked what that model needs to run in NetLogo 5.
The ultimate solution to that problem may be to use a path-finding algorithm, like a* (a-star).
In that case you provide your follower with a preferred destination (and that can be vaguely defined as "a point somewhere in 'that' direction"), and the route-planner algorithm plots a route. You can make the route-planner unaware of obstacles until they are "visible" or whatever you define (for example, in a press of bodies, a follower might not be aware of an obstacle until stumbled upon).
Hope this helps!