I have basic NetLogo command questions.
I would like a set of mobile agents (turtles) to access a patch variable (called veg) and collect a certain amount of (sugar) according to their ability to load sugar (size). I'm trying to use the patch-here command, but there are no examples of how to use the patch-here in the NetLogo dictionary, and I don't know how I can ask the turtles a question to ask if they have collected enough sugar from that patch base on your size. Does anyone have any suggestions on how to do or where can I find this information?
ask turtles [
patch-here veg > 0.6
set sugar (sugar - metabolism)
]
To use patch-here, you would modify your code to:
ask turtles
[ if [veg] of patch-here > 0.6
[ set sugar (sugar - metabolism)
]
]
However, each turtle is located at one patch - even if the picture used to show the turtle is huge and crosses many patches, the turtle itself is considered to be located at a single point, which is within one patch. NetLogo uses the coordinate system in a clever way - because the turtle can only be at one place, if you use a patch variable name, it will assume that you want the patch where the turtle is located.
So you could simplify that to:
ask turtles
[ if veg > 0.6
[ set sugar (sugar - metabolism)
]
]
Note that I removed the [ ] when I removed the of patch-here.
But you can simplify that further to:
ask turtles with [veg > 0.6]
[ set sugar (sugar - metabolism)
]
So if you are using patch-here to get the variable value of the patch where the turtle is located, then you can just use the variable directly. A typical use of patch-here is to find other turtles on the same patch.
Because you are asking turtles, you can't access the patch variable directly, you need to access the patch first before you can access the variable inside the patch.
ask bees
[ ask patch-here
[ if veg > 0.6
[ set sugar (sugar - metabolism) ]
]
]
Or you can simplify the code using of command and use the patch-here as the location of the patch you want to check.
ask bees
[ if [veg] of patch-here > 0.6
[ set sugar (sugar - metabolism) ]]
Related
I'm having difficulty doing the following code: I have a piece of code that turtles inside an in-cone choose a patch that has resource > 30. When, the patch that turtle was the one that had the highest value a turtle did not move. So I put the line of code using the "other" command. However, what happens now is that if the patch the turtle is in has the highest resource value, it chooses another patch that has resource > 30. The problem is that there is, for example, a patch on the turtle's side that has resource value = 51 and another one that has a value of 31 and she chooses 31. What I would like to implement is: if the patch the turtle is in is the one with the highest resource value (and the turtle has already collected this resource) she would choose another neighbor patch that had the second highest resource value. I tried using max-one-of but got an error: "MAX-ONE-OF expected 2 inputs, an agentset and a number block.
Does anyone have any ideas, how can I solve this?
Thanks in advance
to go
ask turtles
[
let availablePatch patches in-cone 5 90 with [ resource-value > 30 ]
ask patch-here [ set availablePatch other availablePatch ] ;; remove the patch it is in, because if the patch it is in is the one with the highest value within your range of vision, the turtle does not move
; ask patch-here [ set availablePatch other max-one-of [ availablePatch ] ]
let neighAvailable count availablePatch
ifelse neighAvailable = 0
[
move-around
]
[
let target max-one-of availablePatch [ resource-value ]
face target move-to target
set step-count step-count + 1
]
]
end
to move-around
right random 360
let step-length 2
forward step-length
end
As is often the case with new programmers, you are too far caught up in a specific thought pattern. So you are making the problem really technical and the code bloated, when code should always reflect what you are trying to do. What you want is simple, so the code should be simple. try to zoom out and think of other options.
If I understand correctly, turtles should pick high-resource patches to exploit them/gather their recources. But they should not pick the same patch twice.
possible solutions that make sense:
-after a turtle has exploited a patch, resources should be below 30. that way it won't be a candidate. if it is not below 30, moving doesn't seem to make a lot of sense anyway.
-use a patches-own variable "exploited" that you set to "true" after the turtle has moved there, and to "false" after the turtle has left. Then you can use with [ resource-value > 30 & exploited = false ] instead of the current with check.
I have a question and couldn't find an answer to it. The question is:
I have a code that exports the following result:
I would like the column (energy-of-my-agent) to be exported without the brackets [], like the figure below.
The code:
globals [ output-filename ]
turtles-own [ energy my-home ]
patches-own [ patch-id my-agent energy-of-my-agent ]
to setup
ca
reset-ticks
set output-filename "energy-of-my-agent.csv"
ask patches [
sprout 1
set patch-id [ who ] of turtles-here
set my-agent turtles-here ]
ask turtles [
set my-home patch-here
set energy 0
]
initialize-data-file
end
to initialize-data-file
if output-data?
[
file-close-all
if behaviorspace-run-number <= 1 [
;; we only want to delete the existing file if we're running in console
;; when running in console, behaviorspace-run-number = 0,
;; first run in behavior space is behaviorspace-run-number = 1
if file-exists? output-filename [
file-delete output-filename
]
;; write a header to the file
file-open output-filename
file-print (word "run-number, ticks, energy-of-my-agent, pxcor, pycor" )
]
]
end
to go
ask turtles [
let times repetitions
repeat times [
let step random 5
fd step
set energy energy - step
]
]
ask patches [
set energy-of-my-agent [ energy ] of my-agent
if output-data? [
if ticks mod output-every-n-ticks = 0 [ ;;output-every-n-ticks
write-output-data energy-of-my-agent pxcor pycor
]
]
]
tick
end
to write-output-data [ #energy-of-my-agent #xpos #ypos ]
file-open output-filename
file-print (word behaviorspace-run-number ", " ticks ", " #energy-of-my-agent ", " #xpos ", " #ypos )
file-flush
end
Is it possible? If so, how can I do this? Any kind of help is very welcome.
Thanks in advance
In NetLogo, square brackets containing items represent a list.
In fact, the energy-of-my-agent variable in your output is in square brackets because it is a list (lists don't have to contain multiple items: empty lists or lists of one item are perfectly possible lists).
Why is energy-of-my-agent a list and not a single value? Because it comes from an agentset and not from an agent (as we already discussed here).
The rule is:
reporter of agent -> single value (unless the value is already a list in itself)
reporter of agentset -> list
In your case: energy-of-my-agent is made by [energy] of my-agent, and my-agent is an agentset, not an agent.
Why so? Because my-agent is made by turtles-here, and turtles-here is an agentset, not an agent, even if it contains one only agent. In the same way as lists, in fact, agentsets don't have to contain multiple agents: empty agentsets or agentsets of one agent are perfectly possible agentsets.
So you have three alternative options:
Use set my-agent one-of turtles-here. This will give you a single agent, because one-of reports a single agent. If you are sure (as it seems to be the case) that patches will always only sprout 1, then one-of turtles-here will give you the exact same agent as turtles-here - but as an agent indeed, and not as an agentset. This in turn means that [energy] of my-agent will be a single value, and not a list of one value.
When outputting values, use sum energy-of-my-agent. In fact, sum takes a list and reports a single value. Given that the sum of a list of one value is exactly that value, in this case sum will report the only value in the list, but without square brackets.
When using sprout 1, you can make the new-born turtle assign itself to the patch: sprout 1 [set my-agent self]. This is possible because turtles can read and modify the variables of the patch they are on: in this case, the new turtle is able to operate on my-agent and, in particular, on my-agent of the patch it stands on.
I think that, between the three options above...
#2 is the least preferable: it never changes the fact that my-agent is an agentset (it only converts energy-of-my-agent from list to number at the end of the simulation), and I think this is not convenient in terms of memory and time because working with agentsets is a heavier process than working with agents.
#1 does not have this problem, because from the beginning it makes sure that my-agent is an agent and not an agentset (using one-of), so it is a better solution than number two. However, although it is perfectly fine, from a stylistic point of view using one-of <agentset> reads as if you're literally looking for a single random member of the agentset, instead of looking for the specific single member of the agentset.
#3 does not even have this latter problem: even from a syntactial point of view, this approach makes it very clear that each turtle, and only such turtle, is my-agent of the patch it sprouted from.
What should I do to monitor the status of various flags assigned to all turtles? As a possibility, can we think to use the Behavior Space? But it didn't go well. Can someone who know it well?
If you want to record the value of individual turtle variables using BehaviorSpace, check out this answer:
https://stackoverflow.com/a/52406247/487946
But if you only want to monitor these values inside NetLogo while your model is running, you can use an output widget.
Here is a bit of example code:
turtles-own [ flag1? flag2? ]
to setup
clear-all
create-turtles 10 [
set flag1? one-of [ true false ]
set flag2? one-of [ true false ]
]
update-output
end
to go
; flip a couple of random flags
ask one-of turtles [ set flag1? not flag1? ]
ask one-of turtles [ set flag2? not flag2? ]
update-output
end
to update-output
clear-output
foreach sort turtles [ t ->
ask t [ output-show (list flag1? flag2?) ]
]
end
And the kind of result it would give you:
You can, of course be as fancy as like with formatting the output. You get a bit of flickering, but it does the job.
Note that it is also possible to plot values for individual turtles using dynamically created temporary plot pens. See this other answer for an example of something like that:
https://stackoverflow.com/a/41600187/487946
I am trying to model an community that engages in shifting cultivation. For that I want each household to change the patch every year. Each household can have a different crop area, depending on time and number of people working. I want them to be able to choose a patch that has the amount of forest patch need to open their crop. For example, one household has a crop area of 3, so the new location needs to be a forest patch with two other forest patch neighbors. Any idea how can I specify that?
Thanks
Here is a possible solution:
patches-own [ patch-type ]
breed [ households household ]
to setup
clear-all
ask patches [ set patch-type one-of ["forest" "rock" "sand"] ]
let forest-neighbors-needed 2
create-households 100 [
let candidate-locations patches with [
not any? households-here and
patch-type = "forest" and
count neighbors with [ patch-type = "forest" ] >= forest-neighbors-needed
]
ifelse any? candidate-locations [
move-to one-of candidate-locations
] [
error "No suitable location found!"
]
]
end
This method is not the most efficient, because it rebuilds the set of possible location for each household it creates, but if your model is not two big, it shouldn't make much of a difference.
Note that you don't give us a lot of detail about how your model is organized, so I had to make a few assumptions. Next time, please tell us bit more: what breeds to you have, what are their variables, etc. Ideally, post a bit of code showing what you already tried.
Hello i will try to be quick
I have a room with a fire that expands , and i have two exits , all i want to do is say to agents that if a door is blocked by fire then to go to the other one. i came up with something like this but not result.
to doorblock
show count neighbors with [pcolor = 77] ;; the patch color of the two doors
end
;;to go
ask smarts [ ;;smarts are the agents inside the room that need to get oout
if [ doorblock > 5 ]
[ set target one-of sexits]] ;;sexits is the other door
Anyone got a better idea? Thanks
OK, so if I understood correctly, you want your agents to take a look at the door that is their current target, check if that door has more than 5 fire agents around it, and choose another target door if that is the case.
If your fire agents are just red turtles (with no specific breed), you probably want something like this:
ask smarts [
if count ([ turtles-on neighbors ] of target) with [ color = red ] > 5 [
if-else ([ breed ] of target = sexits )
[ set target one-of nexits ]
[ set target one-of sexits ]
]
]
The key primitives here are:
neighbors, that will give you the patches around a turtle (the patches around target, in this case)
turtles-on, that will give you the turtles that are on members of a patch set (here, that will be the turtles that are on the patches that are the neighbors of target)
and finally, with allows you to get only the turtles from an agentset that satisfy some condition (here, we use it to get only the red turtles that represent the fires).
You should also try to understand the of primitive.
And I guessed you wanted to assign a new target that was of a different breed than the previous one (south if north, north if south) but that's up to you.