-
I've made some progress, but there's an odd issue which probably means I've done something silly. I have an agent status (which might be better to set as an agent state?), and it does successfully update based on some conditions, but I've outlined the flow here with the initial status of 0, and if there's any messages, it means there's a neighbouring agent, and if not, then it might be possible to move. For some reason, all agents end up with the playing status (hardcoded to 1 in the example below). I actually think spatial 2D messaging could be better for this code as well, but I don't think that is the issue here? # ...
agent.newVariableUInt("agent_status", 0)
# ... FLAMEGPU_AGENT_FUNCTION(search, flamegpu::MessageNone, flamegpu::MessageArray2D) {
FLAMEGPU->message_out.setVariable<flamegpu::id_t>("id", FLAMEGPU->getID());
FLAMEGPU->message_out.setIndex(FLAMEGPU->getVariable<unsigned int>("x_a"), FLAMEGPU->getVariable<unsigned int>("y_a"));
return flamegpu::ALIVE;
} FLAMEGPU_AGENT_FUNCTION(interact, flamegpu::MessageArray2D, flamegpu::MessageNone) {
const unsigned int my_x = FLAMEGPU->getVariable<unsigned int>("x_a");
const unsigned int my_y = FLAMEGPU->getVariable<unsigned int>("y_a");
unsigned int num_neighbours = 0;
for (auto &message : FLAMEGPU->message_in.wrap(my_x, my_y, 1)) {
++num_neighbours;
}
if (num_neighbours == 0) {
FLAMEGPU->setVariable<unsigned int>("agent_status", 0);
} else {
FLAMEGPU->setVariable<unsigned int>("agent_status", 1);
}
return flamegpu::ALIVE;
} class step_fn(pyflamegpu.HostFunctionCallback):
def __init__(self):
super().__init__()
def run(self, FLAMEGPU: pyflamegpu.HostAPI):
prisoner: pyflamegpu.HostAgentAPI = FLAMEGPU.agent("prisoner")
n_ready = prisoner.countUInt("agent_status", 0)
n_playing = prisoner.countUInt("agent_status", 1)
print(f"step: {FLAMEGPU.getStepCounter()}, n_ready: {n_ready}, n_playing: {n_playing}") Output:
I tried with a small grid and low number of agents to investigate, and there are agents with zero other agents around them: minimal reproducible working example: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Spatial 2D is intended for continuous space agents (e.g. I think your problem is misunderstanding array messages. They are literally an array, any element which does not have a message output to it, still exists, it just contains zero for every variable. So retrieve variable |
Beta Was this translation helpful? Give feedback.
Spatial 2D is intended for continuous space agents (e.g.
float
coords, rather thanint
). It's generally alot more expensive in terms of performance.I think your problem is misunderstanding array messages. They are literally an array, any element which does not have a message output to it, still exists, it just contains zero for every variable. So retrieve variable
"id"
and check that it's none-zero (0 is only used as an ID for agents who's ID has not yet been set).