英文:
Anylogic: Sort agents in a collection by a parameter of the agents
问题
我正在进行一项多次卡车配送的最后一英里交付模拟。为了找到最短的路线,我正在使用外部程序来优化交付顺序,该程序的输出会按照一个数字对交付进行排序。我保存了这个排序并通过名为"deliveryOrder"的参数将其输入到我的"Order"代理中。
当我创建卡车代理并通过"pickup"块选择订单时,我将订单代理存储在每辆卡车内的一个集合中,称为"route",以进行多次交付。
我最大的担忧是如何按照"deliveryOrder"升序对每辆卡车内"route"集合中的订单代理进行排序。我不清楚是否可以使用"deliveryOrder"参数对集合中的代理进行排序。我该如何做?
我已经阅读了AnyLogic的帮助文件,但还是无法理解如何实现这一点。
英文:
I am working on a siumulation of last mile delivery with multiple deliveries by truck. In order to make the shortest routing I'm working with an external program which optimize the order of the deliveries which output of the program sort the delivers by a number. I save this and input it in anylogic by a parameter called deliveryOrder in my Order agent.
When I create the truck agent and pick the orders by a pickup block, I store the orders agents in a collection inside each truck called route for the multiple delivery.
My biggest concern is when I want to sort the order agents inside route collection of everytruck by deliveryOrder ascending. I could not understand if i can sort agents in a collection with the parameter deliveryOrder. How can i do this?
pickup block where i want to sort
I read the anylogic help but I couldnt figure it out how to do it.
答案1
得分: 1
route = (ArrayList
一些注意事项:
- sortAscending 将返回一个新的已排序列表,所以确保将 route 赋值给 sortAscending。我曾经犯过这样的错误,只是说 sortAscending(collection, value),然后惊讶地发现我的列表没有排序。
- sortAscending 将返回一个列表,而在AnyLogic画布上的集合将是ArrayList、LinkedHashMap或比List更具体的东西,因此你需要进行强制类型转换。
- 你的 route 集合需要指定它是 order 类型的项,否则 Java 不知道 value 表达式中的 o 是一个 order,并且它无法看到 o.deliveryOrder。
英文:
route = (ArrayList<Order>)sortAscending( route, o -> o.deliveryOrder );
A few notes:
- sortAscending will return a new sorted list, so be sure to say route = sortAscending. I made the mistake more than once of just saying sortAscending( collection, value ) and being surprised when my list did not sort.
- sortAscending will return a list, and a collection on the AnyLogic canvas will be an ArrayList, LinkedHashMap, or something more specific than just List, so you need to cast.
- your collection of route needs to specify that it is items of type order, or else java doesn't know that the o in the value express is an order and it cannot see o.deliveryOrder
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论