I'm trying to simulate a car production factory and trying to keep separate lists of turtles who are on jobs and who are not on jobs.
I have a turtles-own variable: is-on-job?
I want to have a agent set of turtles not on jobs not-working-turtles
I want to have a agent set of turtles on jobs working-turtles
Whenever a turtle is on a job, is there a way to remove that specific turtle from the not-working-turtles agentset and add it to the working-turtles agentset?
Also on a different topic, is there a way to sort turtles based on distance to a specific patch and then if the closest turtle is on a job then go to the second turtle and so on?
You can create your two agentsets as:
let not-working-turtles turtles with [not is-on-job?]
let working-turtles turtles with [is-on-job?]
To move a turtle from one to the other, you will need code along the lines of:
to make-me-work
let free one-of not-working-turtles
ask free
[ set is-on-job? true
set not-working-turtles other not-working-turtles
]
set working-turtles (turtle-set working-turtles free)
end
Here I used the very convenient other primitive to remove the turtle doing the asking. You are more likely to want to do this as a procedure that takes arguments and pass the turtle to the procedure rather than randomly select one within the procedure, but the code would be similar.
On your question about choosing the closest, you don't need to sort. Instead, look at min-one-of in the NetLogo dictionary. If you can't work it out, please do a new question (StackOverflow should be only one question per question) with the relevant code.
Related
I'm trying to measure the mean value of agents that are performing a certain acitivity. To calculate this value, I tried to use: set mean-powerdemand mean [ powerdemand ] of agents with [ powerdemand > 0 ]
To give some contexT: My model concerns charging electric cars. I want to measure the powerdemand of agents that are actually charging their car and thus have a powerdemand > 0. I want to exclude the non-charging agents for this mean calculation, as this would bring down the mean value. However, as there are moments in time (specifically at the start of the run) where no cars are charging, I am getting the error: Can't find mean of a list with no numbers: []
Does someone know a way to work around calculations with agent-own variables and/or lists that are sometimes empty?
I started of by calculating this value in a plot by using the same code. By doing so, it does not prohibit me from running the model, but since I want to use it as a reporter in the BehaviourSpace set-up, I want to make a global value of it.
Thanks.
I am trying to write a code in which the probability of a collaboration being successful increases the more similar the agents are. on each run, a local variable computes the difference between agents and creates a link between agents if that difference doesn't exceed a threshold. the agents are subsequently stored in 2 separate lists based on whether a link was created or not. now, i would like to create another list that contains a successful vs unsuccessful value for each agent with whom a link was created. the probability of that value being 'successful' increases the more similar agents are (the more their difference value approaches 0).
the closest i've come to implementing this is the rnd extension. however, it seems to me that rnd:weighted-one-of only takes agent sets or lists as input and i do not have a predefined list for the similarities of agents. it is the whole range of 0 (complete similarity) and 1 (dissimilarity) that i would like the local variable to be compared to. is this possible in the way i'm currently thinking about it?
let difference 0
let initiator one-of turtles
ask initiator [
let potential one-of other turtles
if random-float 1.0 <= [my-activation] of self [ ;; different probability
set difference [my-profile] of self - [my-profile] of potential] ;; of initiating
;; collab
ifelse difference <= threshold [ ;; if threshold is met
create-link-with potential ;; link is initiated
set collaborators fput potential collaborators][ ;; the initiator adds the
set failures fput potential failures] ;; potential to their list of
;; either collaborators
I recommend considering logistic functions as a way to relate similarity to probability. A logistic function is a "s"-shaped function that ranges from 0 to 1 as some X value (e.g., similarity) varies over a wide range. You can define the logistic function by assuming, for example, what values of similarity produce probabilities of 10% and 90%.
There is a complete discussion of using and programming logistic functions in NetLogo models in chapter 16 of: Railsback and Grimm 2019, "Agent-based and Individual-based Modeling".
I'm working on making my program interact with an overarching framework that transfers information with lists.
I have a breed called 'people' and I export information about them with
to srti-lists
ask people [ foreach [self] of people [
set traits-list (list (who) (color) (heading)(xcor)(ycor)(shape)]]
set master-list [traits-list] of people
end
It works great for exporting that information. What I'm having trouble with is creating or updating people with information that I then receive in the form of a master-list.
I've been approaching it as probably a foreach problem, but the problem is that while that lets me execute commands for every item of the master-list, I haven't figured out how to then access the individual nested items.
So, say:
foreach master-list
[create person
set who item 0 master-list
etc. The problem is that syntax would create a person and then set the who as an entire sublist. Omitting the list throws up an error, and selecting the items more specifically is untenable since it'd be a list of variable length.
Any ideas how to iteratively select items from nested lists? Is foreach even the right approach?
I can't test it but this looks wrong to me:
to srti-lists
ask people [ foreach [self] of people [
set traits-list (list (who) (color) (heading)(xcor)(ycor)(shape)]]
set master-list [traits-list] of people
end
ask already loops through the people, so this looks like it is assigning to each turtle the list of traits for all turtles, and then the master-list is a list of those lists. You probably want:
to srti-lists
ask people [
set traits-list (list (who) (color) (heading)(xcor)(ycor)(shape)]
set master-list [traits-list] of people
end
On the setting of traits, the who number cannot be set - it is assigned automatically when the turtle is created and can never be changed. For example, try the following complete (broken) model and you will get an error:
to testme
create-turtles 5
ask one-of turtles [set who 10 ]
end
So I don't know what you mean when you say that who is assigned an entire sublist - who is always an integer and assigned sequentially.
Concerning your stated question - your syntax set who item 0 master-list would be find if you were trying to set a variable that you could actually set.
In Netlogo, I have turtles-own lists, which means I set a turtle's variable to be a list. Each tick, another value is added to the list. After a few thousand ticks, these lists are quite long... and the problem arises that I can't open the agent monitor in the GUI any more because it takes too long to load the list.
reproducible code:
breed [persons person]
turtles-own [examplelist]
to setup
clear-all
reset-ticks
create-persons 1 [setxy 0 0]
ask turtles [set examplelist []]
end
to go
ask turtles [set examplelist lput ticks examplelist]
tick
end
I would need the agent monitor to watch another turtle-own variable; I don't need to watch the lists (they are just used to do a calculation every 8760 ticks).
Is there maybe a possibility, to e.g. hide the list from the agent monitor? Or do I need to handle the lists as global variables instead? Being quite unhandy as I would need to create and name separate lists for every turtle...
I can see three options:
1/ If you are creating a modelling framework, I assume that your user cannot actually code in NetLogo. This means that you have to predefine the scenarios for them anyway (for example, they could choose the calculation), so you only need to have the possible calculations stored instead of all the input values to those calculations.
2/ It is not clear from your question why any user would open an inspect window or otherwise access the individual turtle. If the user doesn't need it directly, instead of adding all this information to the turtles, you could export it to a file, adding a line each tick. The user would do the analysis of the simulation in R or Excel or whatever.
3/ You could create a shadow turtle for every turtle. This is not something I would recommend, but the idea is that the shadow turtle has a subset of variables (not the lists) and the variable values it does have are identical to the turtle it is shadowing. The limited set of variables version of the turtle is the one that would accessible to monitor.
I have a problem in Netlogo, where i should check if a neighbor patch of a specific patch is already colored black.
This is how i thought it could work:
ifelse [ask patch xcoord-temp ycoord-temp [ask patches in-radius rnd_radius [pcolor = black]]]?
[print "true"]
[print "false"]
Error Message is: Expected a TRUE/FALSE here, rather than a list or block.
xcoord-temp and ycoord-temp are calculated coordinates.
rnd_radius is a random radius between 1 and 15.
So does someone has an idea how to solve this?
Thanks!
Regards,
John
Okay first, the reporter patch receives an identificator that's linked to a patch. If you want to ask something to a patch at a certain position you want to use [patch-at]1.
Second, I don't understand the use of question mark in your code ?.
Third, by using [pcolor = black] the compiler understands you want to set rather than get.
Lastly, you can use the word of to access values of an agent.
So I would put it out like this:
to myprocedure
ask patch-at xcoord-temp ycoord-temp
[
check-color
]
end
to check-color
ifelse [pcolor] of (patches in-radius rnd_radius) = black
[print "true"]
[print "false"]
end
Note than I'm using an extra procedure that may not be complete, perhaps it would have to receive the temporal coordinates you use; unless these coordinates are global variables that change overtime, then you can use the procedures like that.
Please tell us if this helps you, I don't have NetLogo installed right now, I did that based on the dictionary.
Edit:
As stated in the comments
[pcolor] of (patches in-radius rnd_radius) will return a list, thus it can never equal black.
You'll have to ask for an specific neighbor of the patch or perhaps use one-of in the list resulting from the previous statement..
The simplest and least verbose way would be to write a reporter procedure like this
to-report any-black-neighbors-in-radius? [r]
report any? patches in-radius r with [pcolor = black]
end
You can then ask a patch to run this procedure using r as a parameter, and either print the value, or use it for something else. Like this:
ask one-of patches [print any-black-neighbors-in-radius 5]
If you want to ask a particular patch to run it, just ask that patch using patch, like so:
ask patch 5 -5 [print any-black-neighbors-in-radius 3]