i have two breeds
breed[nodes]
breed [messages]
nodes-own [data]
messages-own [basedata]
i have a network with n nodes. each node has a specific data. im selecting a random turtle as sink.in my network data distributed from a turtle(first all turtles are nodes) to sink.each node that is in the way becomes a message.and each message has a memory space too keep those data that pass from that message.messages basedata keep the original data.
i want sink find the nearest node that has for example "D1".
to setup
setup1
setup-spatially-clustered-network ;create links
ask links [set color white]
end
to setup1
__clear-all-and-reset-ticks
create-nodes number-of-nodes
[
setxy (random-xcor * 0.95) (random-ycor * 0.95)
set shape "circle"
set color green
set value ["D1" "D2" "D3" "D4" "D5" "D6"]
set data one-of value
set label data
set dontpick false
set visit false
]
end
to test1
ask one-of turtles
[
set color red
set label "sink"
set nodenumberdestination who
]
ask min-one-of turtles with [(data = "D1") or (basedata = "D1")][distance turtle nodenumberdestination]
[
]
error : NODES breed does not own variable BASEDATA
There is a conflict between your stated goals in your question and your code, which suggests you have not finished thinking through what you are doing. (The code you posted is also incomplete; for example, it does not include your globals declaration nor setup-messages.) So first a question: did you really mean to use turtles instead of nodes in test1? If yes, that means you allow a message to become your sink. So I'll assume no, as suggested by your actual question. Introduce a new global sink and
to test1a
ask one-of nodes [ ;move this to setup!
set sink self
set label "sink" set color red
]
ask sink [
let _choice min-one-of (other nodes with [data = "D1"]) [distance myself]
ask _choice [] ;do something
]
end
That answers your question as asked. If you really wanted a choice out of all turtles, as suggested by your posted code, you'll have to ask a new question.
the problem is when you do
ask min-one-of turtle with [(data = "D1") or (basedata = "D1")]
it could refers to either nodes and message, while each other doesn't share the same variable.
For example: when it runs on nodes, it will return error when checking [(basedata="D1")] since it doesn't own basedata. Likewise, when it runs on message it will return error when checking [(data="D1")] as well. However, since you haven't created any messages so it will always returning error : NODES breed does not own variable BASEDATA
So, you need to be specific when referring to agentset because any command blocks including variable used will be sensitive to as whose the variables are belong to.
Related
This is the code of a 2 player game that I manipulated
o play-the-game
if (any-friends-nearby?) [gain-energy]
if (any-opponents-nearby?) [fight-opponent]
end
to-report any-friends-nearby?
report (any? (turtles-on neighbors4) with [breed = [breed] of myself])
end
to-report any-opponents-nearby?
report (any? (turtles-on neighbors4) with [breed != [breed] of myself])
end
to gain-energy
set similar-nearby count ( turtles-on neighbors4 )
with
[color = [color] of myself]
set total-nearby count (turtles-on neighbors)
;
;
if (similar-nearby >= total-nearby - similar-nearby)
[set energy energy + 5]
end
to fight-opponent
let my-breed [breed] of green-players
let my-color [color] of green-players
let opponent-breed [breed] of red-players
;
;;
ask my-breed
[check-random-winner]
end
to check-random-winner
let pick random-float 2
let winner nobody
ask turtles
[if winner = nobody
[ ifelse size > pick
[set winner self ]
[set pick pick - size] ] ]
end
to change-opponent
ask red-players
[ set breed green-players
set color green ]
end
Sorry if it's a bit long but when I setup up and then press go "ASK expected input to be an agent or agentset but got the list [green-players...]"
How can I fix this?
Also I'm very new to Netlogo and StackOverflow, apologies if I haven't asked my question properly.
The error message tells you that you are passing a list (more specifically, a list of breeds) to ask, when it comes to ask my-breed.
This is because the my-breed local variable is determined by
let my-breed [breed] of green-players
Let's see what we have there:
breed is a turtles-own variable: it holds the turtle's breed, and being an agents' variable can be used as a reported in the of construct (see below).
of is a reporter: it takes a reporter (normally an agents' variable) on its left (in your case: breed) and either an agent or an agentset on its right (in your case: green-players). What of reports (i.e. what it outputs) is...
... a single value if there is an agent on the right.
... a list of values if there is an agentset on the right*.
*Think about it: if I ask the color of your eyes (you are a single person, i.e. a single agent), you will tell me a single color. But if I ask the color of your friends' eyes (your friends are a group of people, i.e. an agentset), the only way for you to answer my question is to tell me a list of colors.
green-players is an agentset: all of the agents whose breed is green-players (note that for NetLogo green-players is an agentset even if it contains 1 or 0 agents).
From this, we can see that in this case of reports a list of breeds, because it reports the breed of every agent that is part of green-players, hence it will report the list [green-players green-players green-players green-players ... ] which is as long as the number of green-players in the model. You can verify this by clicking setup and then running [breed] of green-players in the Command Center.
This is a list of breeds (which can also be seen as a list of agentsets), which is not an agent or an agentset (which are the only possible targets of ask).
((note that the exact same thing happens with let my-color [color] of green-players and let opponent-breed [breed] of red-players))
So, how do you construct an agentset based on a variable? The most common way to do it is by using with (read here why).
But how can you fix your code? I don't know because I don't understand what you want to achieve.
I am not sure how you would want to use it in the code you posted, as I'm not even sure you need to use ask in fight-opponent (let alone ask an agentset).
Your fight-opponent procedure is such that, apart from the problem we just discussed, the "my-" things (i.e. my-breed and my-color) always refer to the green players while opponent-breed always refers to the red players - even if fight-opponent is run by a red player! And also, it is not clear what you want to achieve with the check-random-winner procedure and if you want this procedure to be ran by an entire breed. These things make it quite confusing to understand how you could want to fix the fight-opponent procedure.
For example: who do you want to run the check-random-winner command?
A combination of two things would be beneficial: develop your model one step at a time and make sure that every new little piece of code does exactly what you expect it to do; also, when you ask for how to fix something it is useful that you explain what you want your code to do. By doing these two things I believe it will be a lot easier to answer your questions
I'm trying to implement YOYO leader election algorithm in netlogo
first step in this algorithm is to orient links ( direct link )from the minimum to the maximumbut between neighbors only !
I tried the command
[`ask turtles with [ [ who ] of self < [who] of one-of link-neighbors ]
create-direct-to turtle [who] of one-of link-neighbors ]`
this creates direct link from min to max ( neighbors ) but also creates a direct link from max to min ( neighbors )
and I don't know what's wrong :(
here's a screenshot , if you notice theres' a direct link from 0 to 2 and also from 2 to 0 and my goal is to have only from 0 to 2
Your problem is that every time you do one-of, it randomly selects. So you test against a random link-neighbor in the first line, find it's true and then randomly select a link-neighbor to connect to.
[ ask turtles with [ [ who ] of self < [who] of one-of link-neighbors ]
create-direct-to turtle [who] of one-of link-neighbors
]
More generally, this seems an odd way to achieve your goal. To start with, link-neighbors are the turtles that the turtle is already linked to. link is the generic name for all link breeds (I think you have created a breed called direct-link).
I am not entirely clear what you mean by minimum and maximum since your code is just from smaller to larger who value, regardless of what other who values are available. If you want to create a link from every turtle to every turtle with a higher who value, here is some code:
ask turtles
[ let targets turtles with [who > [who] of myself]
create-links-to targets
]
In general, it is bad practice to use who in NetLogo code. who is a completely arbitrary identifier that simply tracks the order that turtles are created. If you have turtles that die, then your code may crash because it is referring to a turtle that no longer exists. Or perhaps at some point you will have two breeds of turtles - who doesn't care if your turtle is a person or a dog or a factory or...
This may be one of the very few exceptions, but you might want to think about what you are intending who to mean. For example, as this is a leadership model, perhaps you could have a variable called 'charisma' and all the links are from turtles with lower values of charisma to higher values of charisma.
I would like to create a link between a turtle of one breed and another turtle of another breed, and then to copy the value of an attribute of the turtle at one node of the link into an attribute of the other turtle at the other node of the same link.
I tried:
to go
ask one-of apples [
create-link-to one-of oranges
ask-mood]
end
to ask-mood
set others-mood [my-mood] of other-end
end
And (obviously) got the error message "Only a link can get the OTHER-END from a turtle."
Please could you advice the code that I need? Thank you.
It's difficult to give an answer without a working example. You have a bit of a conceptual problem - if two edges link to the same turtle, which turtle is supposed to provide the value for the others-mood? In the following code I have ignored that and just randomly selected the one to provide the value.
You need to change contexts by selecting a link and then the other end of the link is available. See the following for a full model that you can adapt to your code.
turtles-own [mood others-mood]
to setup
clear-all
create-turtles 10
[ setxy random-xcor random-ycor
set mood random-float 1
]
ask turtles
[ create-link-to one-of other turtles
set others-mood [mood] of [other-end] of one-of my-links
]
end
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.