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.
Related
I would like to know how I can count the degrees of separation among nodes within a network.
I have created a network with two breeds that spread virus through time.
to setup
create-people_inf 5 [set label "Infected"]
create-people_well 20 [set label "Good health"]
end
Then I added new nodes to the preexisting ones as follows:
ask people_inf
[ create-links-to other people_inf
[set color red ]
let this_turtle self
ask people_well
[
if random-float 1 < 0.5
[
create-link-to this_turtle [set color green]
]
]
]
This is just a default network. My question would be on how I can count the degrees of separation between one selected node and another one, randomly chosen. I thought of adding a flag and consider a logical condition (if connected?=true), but I do not know to consider the nodes in between. My approach would give me only information on one node and its directed connections.
Any suggestion is more than welcomed. Thanks.
You need to use the Network (nw) extension, see documentation at https://ccl.northwestern.edu/netlogo/docs/nw.html. From that, you can use the nw:distance-to for any turtle to find the number of hops to any specified turtle
I have a programme that sets up a number of different breeds of turtles. Each breed needs to have a leader turtle and a follower turtle. I have assigned these as global variables as they come up a lot in the code further down.
I need to assign these variables to turtles in the breeds and then create a link from the leader to the follower. There are a lot of conditions in the interface that determine how many and which breeds are created so i cannot assign by turtle number.
I am receiving an error (not all of the time) 'turtle cannot link with itself' which i presume occurs when they overwrite the first set command and assign the same turtle to the two variables. Does anybody know a condition i can put in that will allow it to set up everytime without the error. ( I have tried if statements, is-turtle?, one-of other, other)
breed [flinks flink] ;; linked turtles that will turn away from sources
globals [
flink-leader
flink-followers]
to set-up
clear-all
setup-turtles
reset-ticks
end
to setup-turtles
create-flinks 2 [
set flink-leader one-of flinks
set flink-followers one-of other flinks
ask flink-followers [create-link-with flink-leader]
ask flink-followers [set color pink]
ask flink-leader [
setxy 10 4]
ask flink-followers [
setxy 19.5 4]
]
end
to go
fd 1
end
There would be many different ways to approach this. Here is one that doesn't stray too far from the code you have provided:
to setup-turtles
create-flinks 2
set flink-leader one-of flinks
ask flink-leader [
set flink-followers one-of other flinks
setxy 10 4
]
ask flink-followers [
create-link-with flink-leader
setxy 19.5 4
set color pink
]
end
Note that your intuition about using other to make sure that the follower(s) is/are different from the leader was correct.
To understand what was going on, you need to grasp the notion of "context" in NetLogo. Some primitives, like ask, of and create-turtles, are "context switching": one of their argument is a code block (the part between [ and ]) that runs in the context of a particular turtle. Other primitives depend on the context in which the code is running: the primitive named other, for example, will report all the agents from a given agentset, except the one in the context of which the block is running.
In your version, you wrapped most of the code inside a code block provided for create-flinks. That meant that the code block was run once for each turtle that was created. So your calls to set flink-leader, set flink-followers and so on were all run twice, each time in a different turtle context. Can you see how that was messing things up?
Keeping track of the different context in NetLogo can be challenging at first (the frequent confusion between self/myself being a case in point), but once you get it, it should become easy and natural.
One last point as an addendum. You say:
i cannot assign by turtle number
Good! Never¹ assign anything by turtle number! It leads to brittle, error prone, more complex, less general, unnetlogoish code. If you think you need to use turtle numbers anywhere in your code, come ask another question here. Someone will most likely suggest a better way to do it.
¹ Well, almost never.
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.
There is a model in NetLogo based on simple preferential attachment. Agents create links thusly:
to go
if count turtles > num-nodes [ stop ]
let partner one-of [both-ends] of one-of links
create-turtles 1 [
set color red
move-to partner
fd 1
create-link-with partner
]
layout
tick
end
In this model, a node's chance of being selected is directly proportional to the number of connections it already has. I want to edit this code so that there is a variable, prestige, which also determines the probability that a node will be selected. I understand that I need to create a variable for turtles called prestige, but I'm stuck on how to make it so that the probability that a link will be created is also proportional to the amount of prestige a turtle has.
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.