Thanks to Nicholas and Seth for their help on my issue yesterday (Assign each number to 3 turtles). I've encountered a second problem and could use some advice.
To give you an idea of the model:
there are 500 "disciplines" of knowledge.
there are 1,500 "scientists" (Turtles), each with a single "discipline".
a question will be asked containing 3 disciplines.
scientists will answer if their "my-discipline" is one of those contained in the question.
for each of the three disciplines in the question, the scientist with the highest score (based on random 10 set of quality of answer), will be selected and their results printed.
I am having trouble creating an agent set for each discipline contained in the question. I am using:
to give-an-answer ; turtle proc 2
if member? my-discipline question ; if the turtle's ID is one of the disicplines in the question
[ set expert my-discipline ] ; mark as expert.
end
I know that by creating an agent set for each discipline, I can use max-one-of to find the highest rated answer... but I can't get to that point!
Any help would be greatly appreciated!
John
turtles with [ member? my-discipline question ]
will be an agentset containing the turtles whose discipline is in the question.
Related
My simulation is of farms. Farmer agents own the farms and farms are created from a random number of patches around the agent that then "belong to" the farmer.
Each patch owns a Rate of Production variable (random number up to 50).
How do I then assign a value to an agent-owned variable that sums the RoP for each patch in the farm and makes that the farmer's total RoP? Something like... ask farm to [ set farm-RoP ... to the sum of all patches RoP in-radius ]. I am unsure of how to create the syntax.
Thank you, in advance, for any help anyone can provide!
I am new to programming and using netlogo so this question is probably too simple.
I am creating a model in which households decide their farming area each year (changing the location every year). I need to make them change the type of vegetation of n patches, according to the size they decided previously.
I know primitives like in-radius, neighbor and neighbor4 but all of them don't give me the freedom to change the exact number I want. For example, if the farming area is 3 hectares, how can i make one household change 3 patches vegetation type? How can I do that?
Thanks
How about:
n-of 3 neighbors
That would give you three random patches among the patche's neighbors, which may or may not be what you want, but your question isn't precise enough to say...
Let us know if that works for you. If not, give us a bit more detail.
I want to add a slider that shows the chance to spread the message to "Communication T-T Example" on Netlogo.
It's supposed to show the probability of the second turtle to receive the message from the first one who has the message, and it should be adjustable by the user.
Yet I don't know how I should modify the code in such a case.
I couldn't find any similar question in previous ones, I would appreciate your recommendations and helps, thank you.
Suppose you define your slider like this:
then it's just a matter of adding a simple condition to the communicate procedure:
to communicate
if any? other turtles-here with [ message? ]
and random 100 < transmission-probability
[ set message? true ]
end
You could also define your probability to be from 0 to 1 and use random-float 1.0 instead of random 100.
How equivalent are the following two lines of code?
ask agentset [if (attr > 0) [dosomething]]
ask agentset with [attr > 0] [dosomething]
Are there any expected (and explainable) differences
in speed or the use of memory?
In particular, does the use of with in the second
case lead to the creation of a temporary agentset,
or does the ask ... with combination ensure
lazy evaluation?
Just to expand on what Nicolas said:
ask-with is intentionally not lazy. Consider code where the agents are modifying the attribute of other agents that determines whether or not they should be asked, so to speak:
to go
ask patches with [ pcolor = red ] [
ask neighbors4 [ set pcolor red ]
]
tick
end
Because with creates a temporary agentset, which agents are asked is not changed mid-ask. This causes the red region to grow evenly as go is called repeatedly:
Now, consider it with if instead.
to go
ask patches [
if pcolor = red [
ask neighbors4 [ set color red ]
]
]
tick
end
Because patches that have not been asked yet might have have their color changed to red, the red region grows unevenly:
Thus, ask-with actually has different behavior then ask-if, and so cannot be optimized to it. That said, as Nicolas mentioned, some similar optimizations are done.
(Disclaimer: my familiarity with the compiler is very superficial, so I might be missing something.)
A (possibly outdated) list of the optimizations performed by the NetLogo compiler is available here:
https://github.com/NetLogo/NetLogo/wiki/Compiler-architecture#optimizer
The actual code for those (in NetLogo 6.0) is here:
https://github.com/NetLogo/NetLogo/blob/bd3da2bf5495674ce5690cbb2992de4036c9db03/netlogo-core/src/main/compile/middle/optimize/package.scala
As far as I can tell, there is no optimization for ask ... with. It means that ask ... with does create an intermediate agentset and, as such, probably requires more memory and time than ask ... [ if ... ].
Which one you should use probably depends on where you stand on the efficiency/readability trade-off. (I personally find ask ... with more readable.)
Would it be a good idea to optimize ask ... with? In my opinion, absolutely. That being said, there might have been a technical reason not to do it at the time the compiler was written. I just don't know about it.
There is, indeed, a very good reason why ask ... with is not optimized. And there are behavioral differences between the two versions; it's not just a matter of readability vs. efficiency. See Bryan's answer.
Here I give you my problem and my code.
Problem:
Each agent i attaches to each position m (each m has a fixed amount of food, but some are worse, some are better) a score U_im that is updated in the course of time. Initially the scores are set to zero. Every time a player is chosen to access resources, with probability epsilon he choses a position at random, otherwise he choses positions sequentially in order of the highest score registered so far.
For every position visited, the score is updated as U_im -> U_im - c (the cost) if the position is found occupied
and U_im -> U_im + u_mi if it is found free.
How can I create this array u_mi? (that saves the best resources (=the ones with more food) that I have visited so far)
EDIT//: Ok, I did it!
If anyone needs this kind of code, feel free to contact me :)
that's very impressive code for one day's experience !
yes, the list of payoff-memory as a nomads_own variable is how I would do it. But you will have to keep both the position and the payoff or you won't be able to sort them. You may want to limit the memory (eg only last 5 or best 5 or similar) if it starts slowing down too much because you are basically asking every turtle to keep a list of all patches.
On the question of 'not doing anything' - it is possible your while loop is stuck if there aren't any available locations. Try doing it this way (which is likely better anyway as it only has to select once):
to move-to-empty-one-of [locations] ;; nomads procedure
let candidates locations with [not any? nomads-here]
if any? candidates [ move-to one-of candidates ]
end
NOTE: not tested