Reputation: 79
In NetLogo I would like to iterate through each turtle one-at-a-time, with two breeds; bigs and smalls. In looking at one turtle, I would like to assign its neighbours probabilities, then make those probabilities into a list, multiply the list and then use that value to decide if the turtle should be moved or not. Then once the loop has finished working on one turtle, these values should be lost so they don't impact on the next central turtles neighbourhood. I have been using this code, but I realise now it seems to be overwritting the values as the 'ask smalls' comes last in the Probability_Product but I'm not sure how to fix it. Most of the undefined variables here are on a slider in the GUI. Thanks!
breed [ bigs big ]
breed [ smalls small ]
bigs-own [ probability]
smalls-own [ probability]
to setup
clear-all
set-default-shape bigs "square"
set-default-shape smalls "square"
ask n-of bigs-number patches with [ abs (min-pxcor - pxcor) > 2]
[sprout-bigs 1 [ set color red ]]
ask n-of smalls-number patches with [not any? bigs-here]
[sprout-smalls 1 [ set color blue ]]
reset-ticks
end
to go
ask turtles with [count turtles-on neighbors4 < 4 ][
;; so only turtles will a space in the neighbours 4 can move
let vacant-patches neighbors4 with [not any? turtles-here ]
;print Probability_Product
if count turtles-on neighbors4 < 4 [
if random 1001 <= Probability_Product * 1000 ;;if random number in the probability range, the following happens
[ move-to one-of vacant-patches ]]
]
tick
end
to-report Probability_Product
if ( count turtles-on neighbors4 < 4 ) or ( count turtles-on neighbors4 = 0 ) [
ifelse breed = bigs
[ ask bigs-on neighbors4 [set probability Prob_big_big_breaking] ask smalls-on neighbors4 [set probability Prob_small_big_breaking]
let prob-list ( sentence ([probability] of turtles-on neighbors4))
print prob-list
ifelse prob-list != []
[ report reduce * prob-list ] ;; multiplies all the probabilities together
[report 1 ]]
[ ask smalls-on neighbors4 [set probability Prob_small_small_breaking] ask bigs-on neighbors4 [set probability Prob_small_big_breaking]
let prob-list ( sentence ([probability] of turtles-on neighbors4))
print prob-list
ifelse prob-list != []
[ report reduce * prob-list ] ;; multiplies all the probabilities together
[report 1 ]]]
end
Upvotes: 2
Views: 1010
Reputation: 8854
This isn't really an answer (EDIT: Maybe it is now?), but it's a necessary response, and it won't fit into a comment. I have other questions in comments.
I'm still not sure whether I've answered your question in comments. I think there are a few things that need to be done first to clarify what your code is supposed to do. First, I've edited the code in your question to format it in order make it clearer (while trying to preserve your style as much as possible). Second, I added closing brackets on the line after ask bigs
and after ask smalls
. Most importantly, I think that it would help if you could provide a Minimal Working Example (MWE)--the smallest, simplest version of your program that still contains code that (a) runs, and (b) illustrates the problem you're trying to solve. It takes some work to construct an MWE, because you have to figure out what you can take out (because leaving it in will just confuse readers), and what you have to leave in, because it's essential to creating the problem. (However, sometimes you'll figure out the answer to your question on your own when you try to create an MWE.)
For example, here is an MWE, in the sense that it runs, but you'll have to change it to illustrate your problem.
breed [bigs big]
breed [smalls small]
bigs-own [probability]
smalls-own [probability]
globals [Prob_bigs_bigs_breaking Prob_smalls_smalls_breaking]
to setup
reset-ticks
ask n-of 20 patches [sprout-bigs 1 []]
ask n-of 20 patches [sprout-smalls 1 []]
end
to go
ask turtles with [count turtles-on neighbors4 < 4 ][
;; so only turtles will a space in the neighbours 4 can move
move-turtle
]
tick
end
to-report Probability_Product
ask bigs with [count turtles-on neighbors4 < 4 ] [
;; self is each big in turn
ask bigs-on neighbors4 [set probability Prob_bigs_bigs_breaking]
]
ask smalls with [count turtles-on neighbors4 < 4 ][
;; self is each small, in turn
ask smalls-on neighbors4 [set probability Prob_smalls_smalls_breaking]
]
;; Here self is the turtle that called move-turtle, which called Probability_Product.
if any? turtles with [count turtles-on neighbors4 < 4 ][
let prob-list ( sentence ([probability] of turtles-on neighbors4))
if prob-list != []
[ report reduce * prob-list ] ;; multiplies all the probabilities together
]
report 0
end
to move-turtle
if random 1001 <= Probability_Product * 1000 [
;.....
]
end
Next, I suggest that the ask bigs
and ask smalls
lines should be done in a separate procedure from the function containing if any? ...
. Putting this all in one procedure is confusing, because in the ask
blocks in Probability_Product
, we refer to turtles defined by those ask
s, but in the if any?
, we refer to the turtle defined by the ask
two procedures "above", i.e. defined by the ask
in go
, which then calls move-turtle
, which then calls Probability_Product
. When we get to the if any?
, it was hard to figure out what turtle neighbors4
was relative, because it's defined two procedures above, and because Probability_Product
also refers to all bigs
and all smalls
.
In addition to being confusing, I'm not sure that Probability_Product
is doing what you want. For each turtle, this procedure goes and asks all bigs
and all smalls
to do something. So, if the bigs and smalls are the only turtles, Probability_Product
asks all turtles to do something, and then for the next turtle, again asks all turtles to do the same thing, and so on.
EDIT:
It seems to me that what Probability_Product
is supposed to do in the MWE can be done more simply with the following function. Maybe I'm misinterpreting your intention, and it may be that in the full program there is more that needs to be done, since the function below doesn't set or use the probability
variables at all. Even if this isn't what you want, perhaps this example will help you think through what you need to do.
to-report new-Probability_Product
let neighbor-count count turtles-on neighbors4
let big-neighbor-count count bigs-on neighbors4
let small-neighbor-count count smalls-on neighbors4
if-else neighbor-count < 4 and neighbor-count > 0
[ if-else is-big? self ; self is turtle from ask in go procedure
[ report (Prob_big_big_breaking ^ big-neighbor-count) * ; if self is a big
(Prob_small_big_breaking ^ small-neighbor-count) ]
[ report (Prob_small_big_breaking ^ big-neighbor-count) * ; if self is a small
(Prob_small_small_breaking ^ small-neighbor-count) ] ]
[ report 1]
end
The idea behind this function is that the only thing that the probability
variable is doing in Probability_Product
is holding the values set in the sliders. Then those values are multiplied. But we can just multiply those values directly.
(A number of my other comments still seem applicable.)
Upvotes: 2