AnyLogic技术问询:如何在单个Source块中同时使用到达表与参数数据库
Great question—this is a really common pain point when building production process models in AnyLogic, since the built-in Source options seem to force you to choose between arrival timing control and dynamic parameter loading. But don’t worry, there are two straightforward workarounds to get both working together perfectly.
The simplest approach is to merge your arrival schedule data and agent parameter data into one unified table (either in an Excel file you import, or directly in AnyLogic’s built-in database). Here’s how to set it up:
- Create a table with columns for your arrival time (e.g.,
arrivalTime) plus all the parameters you need for your agents (e.g.,partType,processingDuration,priority). - In your Source block, select Arrival schedule from table and point it to this combined table, setting the time column to
arrivalTime. - Leave the "Set agent parameters from DB" option unchecked. Instead, go to the On arrival code field and assign the parameters directly from the table row to your agent:
// Replace "ProductionPart" with your agent type ProductionPart newPart = (ProductionPart) getAgent(); newPart.partType = row.getString("partType"); newPart.processingDuration = row.getDouble("processingDuration"); newPart.priority = row.getInt("priority"); - This way, you get precise control over arrival times and custom parameters for every agent.
If you prefer to keep your arrival schedule and parameter data in separate tables (e.g., for data management clarity), you can link them using a unique identifier:
- Create an arrival schedule table with columns for
arrivalTimeand a uniquearrivalId(this will link each arrival to a row in your parameters table). - Create a parameters table with
arrivalIdplus all your agent parameter columns. - In the Source block, use Arrival schedule from table to control timing via the arrival schedule table.
- In the On arrival code field, run a database query to fetch the matching parameters for the current arrival’s
arrivalId:ProductionPart newPart = (ProductionPart) getAgent(); int currentArrivalId = row.getInt("arrivalId"); // Use AnyLogic's database query helper to fetch matching parameters ResultSet params = selectFrom(parametersTable) .where(parametersTable.arrivalId.eq(currentArrivalId)) .firstResult(); if (params != null) { newPart.partType = params.getString("partType"); newPart.processingDuration = params.getDouble("processingDuration"); params.close(); // Always close ResultSets to avoid resource leaks }
If you want to streamline the parameter assignment, add a constructor to your agent class that accepts all the necessary parameters. Then, in the Source block’s Agent type dropdown, select Custom and create the agent directly with the table/DB data:
// Assuming your agent has a constructor like: ProductionPart(String type, double duration) return new ProductionPart(row.getString("partType"), row.getDouble("processingDuration"));
This skips the post-arrival assignment step and keeps your code more organized.
Just make sure to test a few iterations of your model to verify that arrival times are accurate and parameters are being assigned correctly—adding a logger or displaying parameters on agent shapes can help with debugging.
内容的提问来源于stack exchange,提问作者Alex

