First thing that can easily be optimized:
if any? new-patches
[
ask new-patches
[
if any? avail-fem-patches
[set target one-of avail-fem-patches]
]
move-to target ; I let the animal move so you can see which patch he chose
]
You're having all new-patches
set target
. However, they just keep overwriting the same target
variable. In fact, you don't even need the new-patches
to do this. Thus, you can replace the above code with:
if any? avail-fem-patches [
set target one-of avail-fem-patches
move-to target
]
Next, this:
ask territory
[
set pcolor ([color] of myself) - 2
if owner-male != myself [ user-message "Something wrong" ]
]
keeps setting the entire territory
to the same color over and over (each iteration of the loop), but you only need to set the color of the target patch, right? So this can become:
ask target [ set pcolor ([color] of myself) - 2 ]
It you keep doing map
s over the territory
's ycor
s (which is expensive in a tight loop like this), but you never actually use the results: X
, Y
, and XY
. Bare minimum, you should do that only at the very end, outside of all the loops. But you may just be able to remove it completely.
A quick side note: new-patches
effectively is just set to avail-fem-patches
, since it's set to no-patches
at the beginning of each iteration of the while loop, is then set to (patch-set avail-fem-patches new-patches)
. Is this a bug?
Finally, right now you're looking at at every single neighbor of every single territory patch to add a single patch to territory
. Instead you could as many patches as possible each iteration of the loop. Something like (replacing target
with targets
):
let needed-patches 0.9 * (count [territory] of ?) - (count patches with [(owner-fem = ?) and (owner-male = this-male)])
ifelse count new-patches < needed-patches [
set targets new-patches
] [
set targets n-of needed-patches new-patches
]
Then you'd add all the targets
to the territory. This would be behaviorally different. Right now, you can little trails cut into females' territories. Here, male's territories will expand as broadly as possible so to speak. Not sure if that's bad or not.