void AgentManager::morphDrone(Unit unit) { for (auto &a : agents) { if (a->matches(unit)) { agents.erase(a); addAgent(unit); return; } } //No match found. Add it anyway. if (unit->exists()) { addAgent(unit); } }
void AgentManager::morphDrone(Unit* unit) { for (int i = 0; i < (int)agents.size(); i++) { if (agents.at(i)->matches(unit)) { agents.erase(agents.begin() + i); addAgent(unit); return; } } //No match found. Add it anyway. if (unit->exists()) { addAgent(unit); } }
/************************************************************************* Create all agent create param to file. *************************************************************************/ bool System::loadAgentsCreateParam(const char* szFileName) { if(!::PathFileExists(szFileName)) return false; //get agent counts int nCounts = ::GetPrivateProfileInt("Global", "AgentCounts", 0, szFileName); if(0==nCounts) return false; for(int i=0; i<nCounts; i++) { //read param AgentCreateParam& theParam = *(new AgentCreateParam); char szAgentName[32] = {0}; _snprintf(szAgentName, 32, "Agent%d", i); char szTemp[1024] = {0}; ::GetPrivateProfileString(szAgentName, "Template", "", szTemp, 1024, szFileName); theParam.strTemplate = szTemp; int nParamNum = ::GetPrivateProfileInt(szAgentName, "ParamNum", 0, szFileName); theParam.vParams.resize(nParamNum); for(int j=0; j<nParamNum; j++) { char szParam[32]; _snprintf(szParam, 32, "ParamName%d", j); ::GetPrivateProfileString(szAgentName, szParam, "", szTemp, 1024, szFileName); theParam.vParams[j].first = szTemp; _snprintf(szParam, 32, "ParamValue%d", j); ::GetPrivateProfileString(szAgentName, szParam, "", szTemp, 1024, szFileName); theParam.vParams[j].second = szTemp; } //add a agent addAgent(1, &theParam); } return true; }
// Sets the actor/behavior list bool MultiActorAgent::setSlotAgentList(Basic::PairStream* const msg) { bool ok = false; if (msg != nullptr) { // First clear the old list clearAgentList(); // Now scan the pair stream and put all Ntm objects into the table. Basic::List::Item* item = msg->getFirstItem(); while (item != nullptr) { Basic::Pair* pair = static_cast<Basic::Pair*>(item->getValue()); //std::cerr << "MultiActorAgent::setSlotagentList: slot: " << *pair->slot() << std::endl; Basic::Ubf::Behavior* b = dynamic_cast<Basic::Ubf::Behavior*>( pair->object() ); if (b != nullptr) { // We have an object, so put it in the table addAgent(pair->slot(), b); } item = item->getNext(); } ok = true; } return ok; }
static void endElemCallback(void *userData, const XML_Char *name) { struct controlData *data = (struct controlData *)userData; switch (data->state) { case TAG_OUTSIDE: break; case TAG_CIRCLE: /* if within partition, add agent */ if (data->x >= partData[env_rank][XMIN] && data->x < partData[env_rank][XMAX] && data->y >= partData[env_rank][YMIN] && data->y < partData[env_rank][YMAX] ) { addAgent(data->id, data->x, data->y, data->fx, data->fy, data->radius); } /* change state */ data->state = TAG_OUTSIDE; break; default: data->content[data->index] = '\0'; /* end the string */ /* convert string to double and process */ switch (data->state) { case TAG_X: data->x = atof(data->content); break; case TAG_Y: data->y = atof(data->content); break; case TAG_FX: data->fx = atof(data->content); break; case TAG_FY: data->fy = atof(data->content); break; case TAG_ID: data->id = atoi(data->content); break; case TAG_RADIUS: data->radius = atof(data->content); break; case TAG_NAME: if (0 != strcmp((const char *)data->content, "Circle")) { printf("** Found unexpected agent (%s)\n", data->content); } break; default: printf("** Corrupted input data ? (%d)\n", data->state); break; } /* change state */ data->state = TAG_CIRCLE; break; } }
int propagate_agents(void) { int status = OK; /* agent propagation only needed for parallel runs */ #ifndef _PARALLEL return status; #else /* ------------------------------------------------ */ /* more variable definitions */ MPI_Request *in_req, *out_req; double xmax, xmin, ymax, ymin, x, y; struct agent_obj *a_obj; struct agent_node **outbox; struct agent_node *prev, *current, *toMove; void **outbuf, **inbuf; int *outbox_count; int *inbox_count; int i, j, bufsize, from, recv_count; /* nothing to propagate if only one proc */ if (env_size < 2) return status; /* allocate required memory */ outbox = (struct agent_node **)malloc(sizeof(struct agent_node *) * env_size); outbox_count = (int *)malloc(sizeof(int) * env_size); inbox_count = (int *)malloc(sizeof(int) * env_size); outbuf = (void *)malloc(sizeof(void *) * env_size); inbuf = (void *)malloc(sizeof(void *) * env_size); out_req = (MPI_Request *)malloc(sizeof(MPI_Request) * env_size); in_req = (MPI_Request *)malloc(sizeof(MPI_Request) * env_size); if (!outbox || !outbox_count || !inbox_count || !outbuf || !inbuf || !in_req || !out_req ) { if (outbox) free(outbox); if (outbox_count) free(outbox_count); if (inbox_count) free(inbox_count); if (outbuf) free(outbuf); if (inbuf) free(inbuf); if (in_req) free(in_req); if (out_req) free(out_req); return FAIL; } /* some data initialisation */ for (i = 0; i < env_size; i++) { outbox_count[i] = 0; inbox_count[i] = 0; outbox[i] = NULL; outbuf[i] = NULL; inbuf[i] = NULL; } xmax = partData[env_rank][XMAX]; xmin = partData[env_rank][XMIN]; ymax = partData[env_rank][YMAX]; ymin = partData[env_rank][YMIN]; /* determine agents that need to be migrated */ prev = NULL; current = agent_list; while(current) { x = current->agent->x; y = current->agent->y; /* is agent still in partition? */ if (x >= xmin && x < xmax && y >= ymin && y < ymax) { prev = current; current = current->next; continue; } /* this agent has to be moved */ toMove = current; /* remove agent from main linked list */ if (prev == NULL) agent_list = current->next; else prev->next = current->next; /* select next node for following iteration */ current = current->next; /* determine which partition agent has to be sent to */ for (i = 0; i < env_size; i++) { if (i == env_rank) continue; if (x >= partData[i][XMIN] && x < partData[i][XMAX] && y >= partData[i][YMIN] && y < partData[i][YMAX] ) { /* add to proper outbox */ toMove->next = outbox[i]; outbox[i] = toMove; outbox_count[i]++; break; } } } /* broadcast send counts */ MPI_Alltoall(outbox_count, 1, MPI_INT, inbox_count, 1, MPI_INT, MPI_COMM_WORLD); /* prep input buffers and post non-blocking receives */ recv_count = 0; for (i = 0; i < env_size; i++) { if (inbox_count[i] == 0) { in_req[i] = MPI_REQUEST_NULL; inbuf[i] = NULL; continue; } if (i == env_rank) return FAIL; /* allocate memory for incoming data */ bufsize = sizeof(struct agent_obj) * inbox_count[i]; inbuf[i] = (void *)malloc(bufsize); /* post non-blocking receive */ MPI_Irecv(inbuf[i], bufsize, MPI_BYTE, i, PROP_AGENT_TAG, MPI_COMM_WORLD, &(in_req[i])); recv_count++; } /* prep and populate output buffer. post non-blocking sends */ for (i = 0; i < env_size; i++) { if (outbox_count[i] == 0) { out_req[i] = MPI_REQUEST_NULL; outbuf[i] = NULL; continue; } /* allocate memory for outgoing data */ bufsize = sizeof(struct agent_obj) * outbox_count[i]; outbuf[i] = (void *)malloc(bufsize); /* populate outbuf */ current = outbox[i]; outbox[i] = NULL; j = 0; while(current) { /* copy agent data */ memcpy((void*)((char *)outbuf[i] + (j * sizeof(struct agent_obj))), (void*)current->agent, sizeof(struct agent_obj)); j++; if (j >= outbox_count) return FAIL; /* free node and proceed */ prev = current; current = current->next; free(prev->agent); free(prev); } /* post non-blocking synchronous send */ MPI_Issend(outbuf[i], bufsize, MPI_BYTE, i, PROP_AGENT_TAG, MPI_COMM_WORLD, &out_req[i]); } /* complete and process receives */ for (i = 0; i < recv_count; i++) { /* wait for any receive to complete */ MPI_Waitany(env_size, in_req, &from, MPI_STATUS_IGNORE); for (j = 0; j < inbox_count[from]; j++) { a_obj = (struct agent_obj*)((char *)inbuf[from] + (j * sizeof(struct agent_obj))); addAgent(a_obj->id, a_obj->x, a_obj->y, a_obj->fx, a_obj->fy, a_obj->radius); } free(inbuf[from]); } /* wait for all sends to complete */ MPI_Waitall(env_size, out_req, MPI_STATUSES_IGNORE); /* free output buffers */ for (i = 0; i < env_size; i++) if (outbuf[i] != NULL) free(outbuf[i]); /* deallocate memory */ free(outbox); free(outbox_count); free(inbox_count); free(outbuf); free(inbuf); free(in_req); free(out_req); /* ------------------------------------------------ */ #endif return status; }