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.
Related
I am having some difficulties to create links from two different breeds, humans and zombies.
These links should be created in such a way that turtles from the first breed (humans) with more links (in-degree) have a higher probability of linking the second breeds (zombies) - i.e.,the probability of a turtle from the humans being selected is proportional to the node's in-degree k_in.
At each tick, I add a zombie in the network.
By considering the preferential attachment model, I wrote:
let thisZombie one-of [both-ends] of one-of links
create-link-with thisZombie
but it has returned me the following error:
LINKS is a directed breed.
error while zombie 10 running CREATE-LINK-WITH
called by procedure ADD-ZOMBIE
called by procedure GO
called by Button 'go'.
This is the whole code for this part:
create-zombies 1
[
if targeting = "first model"
[
let thisZombie self
ask one-of humans
[
ifelse random-float 1 < p
[create-link-from thisZombie [set color red]]
[ask thisZombie [die]]
]
]
if targeting = "second model (preferential_attachment)"
[
let thisZombie one-of [both-ends] of one-of links
create-link-with thisZombie
]
]
I have the two following questions:
How can I select the human based on its in-degree?
Is it correct to use create-link-with one-of both-ends in case of two different breeds?
Thanks
You have the correct general approach (which I suppose you've lifted from the Preferential Attachment model in the NetLogo Models Library) but there are two things that are causing you problems:
You correctly using one-of links to pick a link, but by then picking one-of [both-ends] of that link, you could be picking either the human or the zombie, which is not what you want. If your links are always from a zombie to a human (like they seem to be based on the code for your "first model", then the human will always be end2 of the link, you can write: [end2] of one-of links. If you didn't know the direction of the link, you could write [one-of both-ends with [breed = humans]] of one-of links, but that would be less efficient.
You cannot mix directed and undirected "unbreeded" links. (See the user manual for more details on this.)
Supposing, again, that your links are always from zombie to human, your code should look something like this:
if targeting = "second model (preferential_attachment)" [
let thisZombie self
; pick the human end of a random link
ask [end2] of one-of links [ create-link-from thisZombie ]
]
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 distribute resources in a way that I can specify the number of resources and make sure that they have at least one patch between them.
In this example, the resources are being distributed across a set of hex tile turtles built on the underlying patch framework and tied together by links.
set-default-shape resources "resource stone" [
ask n-of 10 tiles with [not any? resources in-radius 2] [
hatch-resources 1 [
set color brown
]]]
I cannot seem to get this to work and I think it is because the resources are not yet created in order to calculate the "not any?" bit.
I concur with "I think it is because the resources are not yet created in order to calculate the "not any?" bit". So how about:
repeat 10 [
ask one-of patches with [not any? resources in-radius 2] [
...
]
]
I am developing a model in Netlogo. I have many agents in my model. each agents must have a radio scope. Two agents can communicate with each other and transfer some critical data When they place in a common scope area.
Imagine that i have 100 agents with 5px scope and 200 agents with 3px scope. I have also 1 master agents that move around in the word. This master agent have a scope too(for example 7px) . Agents can communicate with each other when they place in common scope area. When they communicate they can transfer some of their data. At first just master agent have this critical data and just master agent can transfer this important data. But after he transfer its data to the other agents, those other agents that have this data can transfer this data too. The important condition is being in the common scope.
How can I do this?
Thank you
You just gave a very general statement of your problem. You will get better answers if you make the effort to actually start implementing something and ask about more specific difficulties that you are facing.
That being said, I made a small model that more or less fits with your description. Perhaps it could be useful for you as a starting point and you can ask separate (more precise) follow up questions if you have some.
turtles-own [ scope data ]
to setup
clear-all
; make a big world so agents don't
; bump into one another right away:
resize-world -100 100 -100 100
set-patch-size 3
; create turtles and distribute them around:
crt 100 [ set scope 5 set data "" ]
crt 200 [ set scope 3 set data "" ]
crt 1 [ set scope 7 set data "important data" ]
ask turtles [
set size 3
setxy random-xcor random-ycor
recolor
]
end
to go
ask turtles [ travel ]
ask turtles with [ not empty? data ] [ share-info ]
ask turtles [ recolor ]
end
to travel
; you haven't specified how turtles should move
; so here's a classic "wiggle":
rt random 30
lt random 30
fd 1
end
to share-info
ask other turtles in-radius scope with [ empty? data and distance myself < scope ] [
set data [ data ] of myself
]
end
to recolor
set color ifelse-value empty? data [ grey ] [ red ]
end
Edit:
Following Seth's comment that my first version probably didn't capture the idea of a common scope, I've added and distance myself < scope. This way, only turtles that can see each other can share information.
I've also added a with [ not empty? data ] clause when asking turtles to share info, because there is no use in having turtles with empty data share it.
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.