Using random_agent with a condition

I am working on reproducing the Zero Information trader in Julia using Agents.jl. My agents include a field named :traded, which is of type Bool and is initialized to false for all agents. This is changed to true once an agent has been involved in a trade. In the model_step! function, I need to select a random agent that has not been involved in a trade. The code I have used is as follows:

trading_agent = random_agent(model, :traded == false)

where model is the instantiated model passed to the model_step! function.

This results in a MethodError indicating that object of type Bool are not callable. Looking at the documentation for Agents.jl I could not find an example where random_agent was used with a condition; nor could I find one in the Example Zoo, So, I tried a number of variations, to no avail, and finally used the following workaround, which is:

i = true
while i == true
     trading_agent = random_agent(model)
     i = trading_agent.traded
end

This works but is not very elegant and still leaves me wondering how to properly use random_agent.

:traded == false is not a function. It is an expression which is equivalent with the constant literal false. As :traded is a symbol, and false is a boolean, they can never be equal to each other.

You need to provide a function as a second argument to random_agent.

Try random_agent(model, agent -> agent.traded == false) instead. The expression agent -> agent.traded == false creates an anonymous function taht given an agent it returns true if it hasn’t traded yet.

Works perfectly. Thanks.