Reputation: 31
I have two breeds buyers and sellers. (1) Each buyer will look for the closest seller and go there. (2) Buyer will do a transact and then look for next nearest seller and go there and so on. I tried with taking a attribute of the sellers "visited?", have the buyer have visited the seller? Setup code:
breed [ buyers buyer ]
buyers-own [ target ]
patches-own [ visited? ]
to setup
clear-all
ask patches [ set visited? false ]
repeat 4 [
ask one-of patches [ ;;used patches as sellers
set pcolor red
set visited? true
]
]
create-buyers 1 [
set shape "person"
move-to one-of patches
]
reset-ticks
end
I have tried the 1st part of the problem as "go-to-nearest-seller" like this,
to go-to-nearest-seller
ask buyers [
let nearest-seller min-one-of patches with [visited?] [distance myself]
move-to nearest-seller
]
end
But the part (2) is a bit tricky. I have done like this; "visit-all-seller"
to visit-all-seller
while [any? patches with [visited?]]
[
ask turtles [
let nearest-seller min-one-of patches with [visited?] [distance myself]
if nearest-seller!= nobody
[
move-to nearest-seller
ask nearest-seller [set visited? false]
]
]
]
end
For a single buyer it is working fine. But when I am bringing more than one buyer, then this model is not working. Every customer is setting the attribute to true, so others are not visiting those sellers. Please guide me how to do it differently. Should I have to use dynamic lists or agent sets? Simple agentsets are not working properly.
Upvotes: 3
Views: 67
Reputation: 2305
Edit: This answer is still valid for Netlogo 6.3.0. As of Netlogo 6.4.0, an easier solution has presented itself using who-are-not
.
Before I solve your issue, I have a few general suggestions.
I find it very confusing that you change visited?
from true to false after they are visited. This should definitely be the other way around, otherwise you or someone else working on the code will eventually get confused and make an error.
Instead of repeat 4 [ ask one-of patches [...] ]
you can also do ask n-of 4 patches [...]
Your initial code also runs the risk of asking the same patch to change color twice, resulting in only 3 coloured patches instead of 4.
Now on to your problem:
You are correct in that you can't do it the same way anymore for multiple buyers. One option would be to make a separate patches-own
variable for each buyer, but that can get clunky quite easily.
Instead, I suggest working with a turtles-own
variable that stores all the patches that a turtle still wants to visit. I call it destinations
. This is a patch-set and thus you can use it with the same primitives such as min-one-of
.
That leaves the issue of removing a patch from this patch-set, which is actually quite annoying. Subsetting a patch-set can be done using primitives like min-n-of
, max-n-of
, n-of
and other
. Of these other
is the only one that removes just one patch from the set. If we want to target a patch-set with other
, it has to be called by a patch, but the patch itself can't freely access the destinations variable of the turtle. To circumvent this, I let the turtle create a local patch-set using let
.
let new-destinations destinations
The patch can then remove itself from this set, and the turtle can finally use this set to get its new destinations
ask nearest-seller [ set new-destinations other new-destinations]
set destinations new-destinations
Altogether that gives:
breed [ buyers buyer ]
buyers-own [ target ]
turtles-own [ destinations ]
to setup
clear-all
ask n-of 4 patches [ ;;used patches as sellers
set pcolor red
]
create-buyers 3 [
set shape "person"
move-to one-of patches
set destinations patches with [pcolor = red]
]
reset-ticks
end
to go-to-nearest-seller
ask buyers [
let nearest-seller min-one-of destinations [distance myself]
move-to nearest-seller
]
end
to visit-all-seller
while [any? turtles with [any? destinations]] [
ask turtles [
let nearest-seller min-one-of destinations [distance myself]
if nearest-seller != nobody
[
move-to nearest-seller
let new-destinations destinations
ask nearest-seller [ set new-destinations other new-destinations]
set destinations new-destinations
]
]
]
end
Upvotes: 3
Reputation: 2305
Two weeks after this answer, Netlogo announced a new updated version (Netlogo 6.4.0 beta)!
This version adds a primitive called who-are-not
that seems to be specifically intended for subsetting agent-sets.
to visit-all-seller
while [any? turtles with [any? destinations]] [
ask turtles [
let nearest-seller min-one-of destinations [distance myself]
if nearest-seller != nobody
[
move-to nearest-seller
set destinations destinations who-are-not patch-here
]
]
]
end
Upvotes: 1