Anylogic智能体种群与SD住房示例add_people()报错解决咨询
ArrayIndexOutOfBoundsException: -1 When Calling add_people() in Anylogic Hey there, let's work through this error you're hitting in the Agent Population + SD Housing example. That ArrayIndexOutOfBoundsException: -1 is a clear hint that the model is trying to access an array with an invalid negative index—usually because it can't find a matching dimension entry for the agent you're trying to add. Here are the most common fixes to try:
1. Verify Dimension Matching for Your Population
If your people agent population is linked to a dimension (like a stock dimension for housing types in the SD part of the model), calling add_people() without ensuring the agent fits that dimension will cause this error. Anylogic will return -1 when it can't find the agent's attribute value in the dimension's predefined set, then try to use that invalid index to place the agent in the population.
- Fix: Either:
- Ensure the agent's dimension-related attribute (e.g.,
housingType) exactly matches one of the values in the population's dimension list. - Explicitly specify the dimension value when calling the function, like:
add_people(newPerson, housingDimensionValue);
- Ensure the agent's dimension-related attribute (e.g.,
2. Check Agent Initialization Before Adding
If you're creating a new People agent instance but haven't properly set its dimension-linked properties (leaving them as null or an unrecognized default), Anylogic can't map it to a valid dimension index.
- Fix: Always initialize the required attributes before adding the agent to the population. For example:
// Create the agent People newResident = new People(); // Assign a valid dimension value (from your predefined dimension collection) newResident.housingStatus = housingStatuses.get(1); // Now add it to the population add_people(newResident);
3. Review Population's Auto-Assign Dimension Settings
If your population is set to "Auto-assign to dimension based on agent property", double-check that:
- The selected property actually corresponds to the population's dimension.
- All possible values of that property exist in the dimension's member list.
- Fix: Either update the dimension to include all possible property values, correct the property values to match existing dimension members, or disable auto-assign and manually specify the dimension when calling
add_people().
4. Ensure the Event Triggers at the Right Time
If your event runs before the model's dimensions and population are fully initialized (e.g., during early startup before the SD structure is loaded), Anylogic won't be able to resolve the dimension index.
- Fix: Adjust your event's trigger condition to run after initialization completes. For example, set the event's start time to
time() >= 0(or a small positive value like0.1) to ensure all model components are ready.
Start with checking dimension matching—it's the most frequent cause of this specific error. Let me know if you need to dig deeper into any of these steps!
内容的提问来源于stack exchange,提问作者Elnaz

