Hello! Is there a way to tell random_agent which variant of a @multiagent model to return?
My specific problem is this:
@agent struct Supplier(NoSpaceAgent)
sellingPrice::Float64 = 1
end
@agent struct Buyer(NoSpaceAgent)
supplier::Supplier
end
@multiagent MarketActor(Buyer, Supplier)
# […]
# When initially adding agents to the model
supplier = random_agent(model, a -> variantof(a) <: Supplier) # `supplier` is a MarketActor
add_agent!(MarketActor ∘ Buyer, model; supplier) # which cannot be converted to a narrower type(?)
I guess I could just store IDs instead of an actual pointer to the supplier, but that seems… less clean? I don’t know.
Thank you in advance!
EDIT: I think the more general question is how to, in general, “narrow” agent variants? Is this possible?
Can you clarify whether your question is about (a) altering the return value of random_agent or (b) using the current return value in add_agent!? Sounds like different questions to me. If your question is both that’s okay as well
Well, I think it’s both. So, more specifically, I’d ideally like to:
(a) be able to narrow the returned value’s type, based on the condition function passed to random_agent
or (b) narrow it later, after it’s been returned as the more generic @multiagent
Well, I can asnwer (a) easily: since you use @multiagent, all agents are of type MarketActor. Your condtion is correct, you have specified the variant, so the MarketActor you found is of variant Supplier. You cannot narrow the type MarketActor more, as there is no narrower type. That’s how @multiagetn works.
FOr question (b) this is just about how you add an agent of a specific variant to the model? You can extract the variant of an agent with variantof, you have done so already. If so I’d recommend reading again the usage of @multiagent in the tutorial. (Idon’t remmeber the answer)
I see. Yes, there is no documentation about this in the tutorial. It kinda assummes that you know the properties of each variant, which you must, since you defined them.
I am not sure if there is an automated way to do what you want to do, perhaps @Tortar knows?
And if you do find such a way please consider contributing it to Agents.jl!
You just created a new Supplier, overwriting the one returned by random_agent, no?
Without that, I get the type error:
ERROR: MethodError: Cannot `convert` an object of type MarketActor to an object of type Supplier
The function `convert` exists, but no method is defined for this combination of argument types.
EDIT: but your code gave me an idea. It’s not very pretty, but it’s possible to do this: