Get patches-own values - netlogo

how can I get the value of a given netlogo patches-own. I can only get the patches-own names (with .word.program.patchesOwn()) but I don't know how to get their values.
thanks

You want the values from all of the patches, or the value from a particular patch, or what?
I'll suppose you want the value from a particular patch.
Assuming, to begin with:
import org.nlogo.headless.HeadlessWorkspace;
HeadlessWorkspace workspace = HeadlessWorkspace.newInstance();
workspace.open("models/Sample Models/Biology/Ants.nlogo");
workspace.command("setup");
Then you don't need anything other than HeadlessWorkspace.report to retrieve a value from a patch, so e.g.:
double food = ((Double) workspace.report("[food] of patch -17 -19")).doubleValue();
Another, more cumbersome solution path involves accessing engine data structures directly:
Patch p = workspace.world().getPatchAt(-17, -19);
int foodIndex = workspace.world().program().patchesOwn().indexOf("FOOD");
double food = ((Double) p.getPatchVariable(foodIndex)).doubleValue();

Related

rnd:weighted-one-of-list work doesn't return the distribution I think it should

I trying to use the rnd extension's function weighted-one-of-list.
My list looks like
observer>show female-yr-run-stats
observer: [[117 0.001169] [118 0.002684] [119 0.004697] [120 0.003368] [121 0.004871] [122 0.018738] [123 0.034986] [124 0.070616] [125 0.035608] [126 0.012939] [127 0.011883] [128 0.016594] [129 0.068837] [130 0.044391] [131 0.028422] [132 0.053251] [133 0.023741] [134 0.042111] [135 0.035811] [136 0.022447] [137 0.031563] [138 0.024253] [139 0.030213] [140 0.024372] [141 0.033266] [142 0.059869] [143 0.028711] [144 0.030863] [145 0.04043] [146 0.008819] [147 0.012308] [148 0.008638] [149 0.021345] [150 0.016176] [151 0.009815] [152 0.017242] [153 0.014362] [154 0.010717] [155 0.015868] [156 0.003865] [157 0.008441] [158 0.004358] [159 0.003113] [160 0.002464] [161 0.001768]]
The first item is day-of-the-year and the second is the probability (all sum to one).
I am using the code
repeat 50000[
let tempo first rnd:weighted-one-of-list female-yr-run-stats [ [p] -> last p ]
file-open "C://temp//check_wgt_random.csv"
file-print (word tempo)
file-close
]
to randomly select a day-of-the-year 50000 times to check that the rnd:weighted-one-of-list function is doing what I want it to do. From the results, I get a distribution of day-of-the-year. But when I compare the resulting distribution to the original probability distribution, they are quite different.
I am thinking that the rnd:weighted-one-of-list with my list would be equivalent to randomly drawing from a multinomial distribution like R's rmultinom function. Am I correct? Am I doing something wrong?
Any help would be appreciated.
The graph below shows the comparisons
The rnd:weighted-one-of-list primitive works the way (I think) you think it works. From your data, it draws the value 112 with probability 3.04E-4, value 113 with probability 0.001236 etc. The probabilities don't have to add to 1 (I have assumed that you are correct that yours do). I can't see anything wrong with your use of this primitive for your draws.
But I am not sure about the file construction. You have the file-open and file-close inside the repeat so the file is opened, the new data is appended, the file is closed. It is not clear whether you ever clear the dataset. So perhaps the problem is that you have some data left over from when you were building your code. Do you get the same problem if you clear out all the old data and start again?
Sorry to waste your time. I discovered what happened. I had two probabilities stream mixed up.

Accessing owned traits in netlogo with nested ask

This is what i want to do:
patches-own[
trait1
trait2
trait3
]
let similarityCounter 0
ask one-of patches[
ask one-of neighbors[
**for-each trait[
if neighborTrait = patchTrait**[
set similarityCounter (similarityCounter + 1)
]
]
]
]
The part between ** is what I'm unsure about. How does one iterate over the patch-own parameters and compare between patch and neighbor?
How about you create a list for each patch of their trait values and count matches in the two lists? It would looks something like this.
to testme
let similarityCounter 0
ask one-of patches
[ let mytraits (list trait1 trait2 trait3)
let theirtraits [(list trait1 trait2 trait3)] of one-of neighbors
set similarityCounter length filter [ xx -> xx ] (map = mytraits theirtraits)
]
end
The final line is a little dense. What it does is compare the two lists of traits using the map function with the = operator, which will return a list of true and false values indicating whether that specific trait matches. The filter then creates a list of just the true values and the length counts the number of those true values.
Unfortunately, NetLogo doesn't do the trick of treating a true as 1 and false as 0 that you see in some languages, so you can't simply sum the match results list.
I really like Jen's answer, but just for fun, I'd like to provide an alternative way to approach the problem that uses Jen's idea of treating true as 1 and false as 0.
But first, I think that, depending on the rest of your model, it could have been a good idea for you to store your traits directly in a list instead of separate variables. In programming, having variable names with a numeric suffix like trait1, trait2, etc. is usually a hint that a list should be used instead.
Nevertheless, we will leave your general design alone for now and just provide a small function that makes it easy to package your traits into a list:
to-report traits ; patch reporter
report (list trait1 trait2 trait3)
end
Once you have that, you can write something like [ traits ] of one-of patches to get a list of the patche's traits.
Now let's attack the problem of turning true and false into ones and zeros in a similar way. It's true that NetLogo doesn't provide that conversation automatically (which I think is a good thing) but it's easy to write our own function for that:
to-report bool-to-int [ yes? ]
report ifelse-value yes? [ 1 ] [ 0 ]
end
We are now ready to write our main function. We will use Jen's approach of mapping over the = operator to convert our lists of traits to a list of boolean (i.e., true/false) values, and we will then use map again to convert that list into a list of 1 and 0. Once we have that, all that is left is to sum it! Here we go:
to-report similarity-with [ other-patch ] ; patch reporter
report sum map bool-to-int (map = traits [ traits ] of other-patch)
end
Having that reporter makes it really easy to get the similarity between two patches. You can now say things like:
print [ similarity-with one-of neighbors ] of one-of patches
Notice how I have approached the problem by building small pieces that be combined together. I really like this way of proceeding: it allows me to concentrate on one part of the problem at a time, but it's also more easy to test and results in code that I find very readable. NetLogo's to-report procedures are a great tool to achieve that kind of modularity.

How to compare two turtles in Netlogo by going through a list of their attributes?

My turtles have more than 30 attributes of boolean values and I would like to use a foreach loop to compare turtles and rank them based on their similarity without the need to compare each attribute individually. I might be missing an obvious point here, I have tried having a list of attributes, but it didn't work and all turtles got the maximum similarity score.
Here's some code that calculates the Hamming distance between two lists. Note that the very clever reduce code is taken directly from the NetLogo dictionary.
to testme
let ll1 (list TRUE TRUE FALSE FALSE)
let ll2 (list TRUE FALSE TRUE FALSE)
let ll3 ( map = ll2 ll1 )
show ll3
show reduce [ [occurrence-count next-item] ->
ifelse-value (next-item) [occurrence-count + 1] [occurrence-count] ] (fput 0 ll3)
end
If you were wanting to calculate the similarity score of a pair of turtles, you could turn this into a reporter that takes the two turtles as arguments. But it's not clear that comparing two turtles is what you want to do, so I haven't written code for that.

Roulette Wheel Selection in Netlogo using Agent Variables, not Constants

I hope this is a simple solution, but I'm having a difficult time with it.
Problem:
I would like to weight the probability of something occurring by an variable not a constant
Setup
My agent is a farm.
Farms own four variables that represent the
number of cows, goats, pigs, and sheep on it.
When a farm wants to
remove an animal, I'd like the likelihood to remove a member of a
particular species to be directly proportional to quantity of each
species on the farm (i.e. if there are 7 goats, 2 cows, and 1 pig,
there is a 70% probability of taking a goat and a zero percent
probability of taking a sheep)
I have found formula like this for when you know the exact numerical weight that each value will have:
to-report random-weighted [values weights]
let selector (random-float sum weights)
let running-sum 0
(foreach values weights [
set running-sum (running-sum + ?2) ; Random-Weighted Created by NickBenn
if (running-sum > selector) [
report ?1
]
])
end
and the methods described in the rnd extension. But both of these throw the "expected a constant" error when i put "Cow" in instead of a constant.
Something like:
to example1
let values ["Cow" "Sheep" "Goat" "Pig"]
let probabilities [2 0 7 1]
let indices n-values length values [ ? ] ; Made by Nicolas Payette
let index rnd:weighted-one-of indices [ item ? probabilities ]
let loca item index values
end
works well, but if I were to replace it with:
to example1
let values ["Cow" "Sheep" "Goat" "Pig"]
let probabilities [Num-Cows Num-Sheep Num-Goats Num-Pigs]
let indices n-values length values [ ? ] ; Made by Nicolas Payette
let index rnd:weighted-one-of indices [ item ? probabilities ]
let loca item index values
end
it fails.
Alan is right: you need to use the list primitive (as opposed to just brackets) when you want to construct a list from anything else than constants.
I would add two things to that:
The latest version of the rnd extension has two sets of primitives: one for agentsets, and one for lists. So you should probably update and use the rnd:weighted-one-of-list primitive.
Your code is based around using indices to pick an item. That's fine, but that's not the only way to do it.
You could also have something like:
to example1
let values ["Cow" "Sheep" "Goat" "Pig"]
let probabilities (list Num-Cows Num-Sheep Num-Goats Num-Pigs)
let loca first rnd:weighted-one-of-list (map list values probabilities) last
end
This may be a bit trickier to understand, but here is the gist of it:
The (map list values probabilities) expression takes both your values list and your probabilities list and "zips" them together using the list primitive, resulting in a list of pairs: [["Cow" 2] ["Sheep" 0] ["Goat" 7] ["Pig" 1]].
We pass the last reporter to the rnd:weighted-one-of-list primitive to tell it that the last (i.e., second) item of each of these pairs should be used as the probability.
Since rnd:weighted-one-of-list operates on a list of pairs, the item it returns will be a pair (e.g., ["Goat" 7]). We are only interested in the first item of the pair, so we extract it with the first reporter.
Note that we use the NetLogo's concise syntax for tasks when passing list as an argument to map and last as an argument to rnd:weighted-n-of. You could replace list with [ (list ?1 ?2) ] and last with [ last ? ], but it would be uglier.

Compare value of property of all instances of class in python

I'm trying to write an object oriented program (as a learning exercise, I know there may be simpler ways to do it) in which beads bounce around a 2D plane bounded by a ring. Each bead is an object defined by a class ball. In setting the initial positions of the balls I need to check that no other ball has already been placed at the same x and y coordinates.
#Class for the beads
class ball:
NumbBalls = 0
#Constructor
def __init__(self,Beads):
self.ball = sphere(pos = vector(0,0,0), radius = radiusBall,color=color.red)
ball.NumbBalls += 1
self.ball.pos = self.createInitialPosition(Beads)
#Assign ball its initial position
def createInitialPosition(self,other):
#CODE to compare self.ball.pos and other.ball.pos which returns a unique position coordinate
#Main program
NumbBeads = 100
Beads = []#Create empty list for all the beads
#create balls in random initial positions
Beads = [ball(Beads) for item in range(NumbBeads)]
I can get this to work if I create two objects bead1 and bead2 and then pass bead1 and bead2 as arguments ie bead2 = ball(bead1) but how do I do this if I am generating a list of beads and want all of them to be compared with self. Hopefully that makes sense.
Perhaps rather than the approach you are currently taking, you should consider something along these lines (of course, with the necessary changes to your class definition/methods):
N = 100
initialPositions = []
while len(initialPositions) <= N:
newPosition = generateRandomPosition()
if newPosition in initialPositions:
pass
else:
initialPositions.append(newPosition)
ballList = [ ball(p) for p in initialPositions ]
In other words, generate a list of initial positions outside of the creation of your objects, and do whatever validation/restriction you need during that creation. Then just create your objects from that list. If N is really large, you might want to consider a dictionary (or a mutable set of some sort) instead of a list for initialPositions, to help speed up the membership testing, but it's still the same concept...