I'm working on a hospital model, and I want nurses to choose one of the patients as their target and move forward until they reach their patients in one tick.
I use while in my code, but it only moves one patch forward in one tick.
here is my code:
to nurse-visit
ask nurses
[ let target one-of patients
if distance target = 0
[face target]
;;move towards target. once the distance is less than 1
ifelse distance target < 1
[move-to target]
[while [any? patients in-radius 1]
[ fd 1 ]]
Is there anyone here that can help me?
It might be helpful to break down the lines into pseudocode to figure out it's not working. At the moment, your code is saying:
if distance target = 0
[face target]
If the distance to the target is 0, face the target. In NetLogo, facing the target once the distance to the target is 0 is meaningless since they share a location.
ifelse distance target < 1
[move-to target]
If the target is less than 1 patch away, move to it. That's great- we can reuse this bit later, we just need to change the order a little.
[while [any? patients in-radius 1]
[ fd 1 ]]
While any patients are nearby, move forward. Here, we probably want to refer to the target instead of the general patients.
So overall, it seems like the logic and order of operations is not quite right. What we really want to do is for each nurse to:
Pick a target patient
Determine the distance to that patient
Until the distance to the target is exactly 0, do one of two things:
If the distance to the target > 1, face the target and move forward 1
If the distance to the target is < 1, move right to the target
Translating that to code, you could do something like:
breed [ nurses nurse ]
breed [ patients patient ]
to setup
ca
ask n-of 5 patches [ sprout-patients 1 ]
create-nurses 1
reset-ticks
end
to nurse-visit
ask nurses [
; Pick a patient
let target one-of patients
; Until my distance to my target is 0, do the following:
while [ distance target > 0 ] [
ifelse distance target > 1 [
; If the distance to my target is greater than 1, face
; the target and move forward 1
face target
fd 1
wait 0.1
] [
; Otherwise, move *exactly* to the patient
move-to target
]
]
]
Note that I've put a wait timer in there so you can see the nurse moving regardless of your display speed.
Related
Netlogo is honestly very weird for me, but I've been trying to make Space Invaders for a project. I need to be able to check the distance between multiple turtles of the same breed in order to run a collision check. How do I do this?
to shoot
ask turtle 0 [
hatch 1
[
set shape "dot"
set size 2
set color red
set ctrl "projectile"
set xcor [xcor] of turtle 0
set ycor [ycor] of turtle 0 + 2
repeat 40
[
ifelse ycor < 15 and distancexy [xcor] of turtle 1 [ycor] of turtle 1 > 0.5
[
set ycor ycor + 1
]
[
ifelse distancexy [xcor] of turtle 1 [ycor] of turtle 1 < 0.5
[
ask turtle 1 [die]die][die]
]
wait .01
]
]
]
end
So you want something like a 'hero' turtle to shoot all the turtles within some distance. I have modified your code a little but the basic idea is sound. You didn't really have a step for identifying the targets. I also simplified the movement by using face and used a while because there is no guarantee it will take 40 steps to get to the target.
to shoot
let shooter turtle 0 ; or however the shooter is selected
ask shooter
[ let targets other turtles with [distance myself < 0.5 ; finds the targets
if any? targets
[ let this-target one-of targets
face this-target ; so shooter is facing the target so trail better
; do the shooting
[ hatch 1
[ set shape "dot"
set size 2
set color red
set ctrl "projectile"
let step-distance 0.02
while distance this-target > step-distance
[ face this-target ; you don't need to worry about coordinates
forward step-distance
wait .01
]
die ; once close enough, projectile dies
]
ask this-target [ die ] ; and kills the target
]
]
]
end
This code is completely untested and needs another loop to get all the targets. Usually you don't use loops much in NetLogo, but an ask would change this into the perspective of the target rather than the shooter.
I am doing boarding processes model with anti-Covid measures. I would like to know if any of you could help me.
I would like to enforce that the minimum distance between turtles will be 4 patches.
I would like to identify if there is an interference, that is, that a previous turtle occupies a seat that prevents a new agent from passing through.
I don't know how to insert this in my code:
to go
; stop when all the pessengers have found a seat
if not any? turtles with [seated? = false]
[stop]
set counter counter + 1
(foreach (sort turtles with [seated? = false] ) [a ->
ask a [
; check if we have reached the correct row of seats
ifelse [seat-row] of patch-at 1 -1 = assigned-seat-row
[
; only seat the passenger if he/she has stored the luggage OR if we don't take luggages into account
ifelse luggage-store-ticks = 0 or storing-luggage-takes-time = false
[
let seat patch-at 1 -1
set xcor [pxcor] of seat
set ycor assigned-seat-number
set seated? true
]
[
set luggage-store-ticks luggage-store-ticks - 1
]
]
[
let passenger-ahead one-of turtles-on patch-ahead 1
ifelse passenger-ahead != nobody
[
set speed [speed] of passenger-ahead
if xcor != airplane-door-x-cor
[fd speed]
]
[
set speed default-speed
fd speed
]
]
]
])
end
You could extend your existing patch-ahead 1 to also look at the patches 2, 3 and 4 ahead. But I think it would be easiest to use in-cone 5 10 or similar. That will look ahead in a cone shape, 10 degrees on either side of the heading and a distance of 5. So you could do something like:
let potential-blockers turtles in-cone 5 10
let blocker min-one-of potential-blockers [distance myself]
That should (not tested) find the closest turtle approximately in front and name it "blocker" so that you can do things like check if it's far enough away, match speed (see the basic traffic model in the NetLogo models library)
I would like a turtle to go to the closest patches with most turtles if a threshold of a given variable is met for 5 ticks.
My code is:
to move
let count-tick 5
if var >= 9.5 [
set count-tick count-tick - 1
if count-tick = 0 [
ask turtle [
let nearest-group min-one-of (patches with [sum turtles >= 3] in-radius 3 ) [ distance myself ]
move-to nearest-group ;; go to the biggest crowd near you
ask turtle [ ;; once there do the following
set shape "star"
set color red
]
]
]
]
end
The issue I have is that a) I am unsure how to say the patch with >= 3 turtles closest to you at the given range of 3 (attempted code above) and b) how to say once there, change your shape.
Revised to keep a permanent variable to track whether the variable is high enough 5 times in a row.
turtles-own
[ count-tick
]
; wherever you create the turtles, you need to `set count-tick 5`
to move
ifelse var >= 9.5
[ set count-tick count-tick - 1 ]
[ set count-tick 5 ]
if count-tick = 0
[ let nearest-group min-one-of (patches with [count turtles >= 3] in-radius 3 ) [ distance myself ]
move-to nearest-group ;; go to the biggest crowd near you
set shape "star"
set color red
]
end
First, you are already within an ask turtles code block from the procedure calling this move procedure. So you don't need the additional ask turtles. Look up ask in the NetLogo Dictionary, it iterates through the turtles, running all the code for each turtle in turn.
Second, you need count turtles rather than sum turtles as sum is to add up values.
Note that there is no error checking in this, you may have problems if there are no patches within radius of 3 that have at least 3 turtles.
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.
In my model I have some agents which act as food items with a set energy. These are fed upon by a number of turtle breeds who each have their own food-energy which is less than the energy of the food item.
The code for the feeding agents is as follows:
to eat
ifelse [food-energy] of myfood > 1.5 [
set food-energy 1.5]
end
and the associated code for the food item to decay is:
to decay
if any? turtles-here [set food-energy
(1.5 * count feeders-here with [myfood = myself]
end
The problem occurs if the energy of the food is not an exact multiple of the amount of energy the feeders can consume. So for example it can go down to 1 and this results in the feeders taking 1.5 units which should be impossible. This is exacerbated when I have different breeds with different food energies (i.e. < or > 1.5).
So my question is how can I get this things to balance?
You need to study the Wolf-Sheep Predation model. This is the first NetLogo tutorial: http://ccl.northwestern.edu/netlogo/docs/tutorial1.html There are five versions of increasing complexity in the NetLogo Models Library, which are covered in chapter 4 of Wilensky and Rand (2015), which you should read.
See some related material here:
http://jasss.soc.surrey.ac.uk/14/2/5.html
Some hints follow, but many details need filling in.
breed [feeders feeder]
patches-own [ food-energy ]
feeders-own [ myfood ]
to setup
ca
ask patches [set food-energy random 50]
create-feeders 500 [
move-to one-of patches
set myfood one-of patches
]
end
to go
ask feeders [move]
ask feeders [feed]
ask patches [growback]
end
to move ;how shd they move?
rt random 20
left random 40
fd 1
;shd movement cost energy?
end
to feed
if (patch-here = myfood) [
let _extracted min (list food-energy 1.5)
set food-energy (food-energy - _extracted)
]
end
to growback
;do you want growback?
end
thanks for your responses. I'll try to implement them. This is one inelegant workaround that worked for me:
to eat
ifelse (food-energy / capacity) < 1 and [meat] of myfood > capacity [
set food-energy 1.5] [set food-energy [meat] of myfood
ask myfood [set shape "square"]]
if (food-energy / capacity) = 1 [
set color white]
if (food-energy > 0 and food-energy / capacity < 1)
[ set color white ]
end
This was initially causing a problem such that when the food energy went down to 0 and I asked it to die any animal looking at the [meat] of myfood lost it's target and I got an error. So I made the animals break this connection once their colour was white.
to ignore
set myfood nobody
set food-energy food-energy * 1
end