• 【仿真建模-anylogic】动态生成ConveyorCustomStation


    1. Author:赵志乾
    2. Date2024-06-18
    3. Declaration:All Right Reserved!!!

    0. 背景

            直接使用Anylogic组件开发的模型无法动态改变运输网布局;目前需求是要将运输网布局配置化;运输网配置化的前提是将其标准化为仅包含辊道、自定义站点两种元素的网络;之后依据配置文件动态构建运输网;最后将自定义Agent逻辑关联到自定义站点实现流程串通;本次示例仅包含运输网中的辊道和自定义站点的动态生成以及组装过程;

    1. 常用函数

    函数功能
    ConveyorCustomStation()构造函数
    addVertex(double x, double y)添加点以构建2D图形,注意点:需要至少三个点,且连起的图形不得自身有交叉;通常会将CustomStation的图形隐藏,而将其内部自定义逻辑封装成Agent,用Agent的presentation替补CustomStation本身的图形;
    addConnection(ConveyorPath conveyor, PathType type)将CustomStation与conveyor连接起来
    onEnter(T agent)根据需要定义子类覆写该方法;当所运输物料进入CustomStation时,会自动执行该方法;

    2. 数据结构定义

    1. {
    2. "points": [
    3. {
    4. "code": "", // 点编码
    5. "name": "", // 点名称
    6. "x": 0.0, // 点坐标
    7. "y": 0.0,
    8. "z": 0.0
    9. }
    10. ],
    11. "conveyors": [
    12. {
    13. "code": "", // 辊道编码
    14. "name": "", // 辊道名称
    15. "pointCodes": [] // 辊道从起点至终点所经历的位置点编码列表
    16. }
    17. ],
    18. "customStations": [
    19. {
    20. "code": "", // 自定义站点编码
    21. "name": "", // 自定义站点名称
    22. "inConveyorCodes": [], // 传入站点的辊道
    23. "outConveyorCodes": [] // 传出站点的辊道
    24. }
    25. ]
    26. }

    3. 代码实现

    1. // ************************数据对象定义****************************
    2. public class PointDefinition implements Serializable {
    3. private String code;
    4. private String name;
    5. private Double x;
    6. private Double y;
    7. private Double z;
    8. // setter、getter
    9. }
    10. public class ConveyorDefinition implements Serializable {
    11. private String code;
    12. private String name;
    13. // setter、getter
    14. }
    15. public class LayoutDefinition implements Serializable {
    16. private List<PointDefinition> points;
    17. private List<ConveyorDefinition> conveyors;
    18. private List<CustomStationDefinition> customStations;
    19. // setter、getter
    20. }
    21. public class CustomStationDefinition implements Serializable {
    22. private String code;
    23. private String name;
    24. private List<String> inConveyorCodes;
    25. private List<String> outConveyorCodes;
    26. // setter、getter
    27. }
    28. //******************************子类*******************************
    29. public class CustomStation<T extends Agent> extends ConveyorCustomStation<T> {
    30. // 持有站点定义,便于onEnter中依据站点不同做不同处理
    31. CustomStationDefinition customStationDefinition;
    32. public CustomStation(CustomStationDefinition customStationDefinition) {
    33. super();
    34. this.customStationDefinition = customStationDefinition;
    35. }
    36. public void onEnter(T agent ) {
    37. // 自定义逻辑
    38. }
    39. }
    40. //**************************动态生成过程******************************
    41. // step1: 转map,方便后续使用
    42. Map<String,PointDefinition> codeToPointDefinitionMap = layoutDefinition.getPoints()
    43. .stream()
    44. .collect(Collectors.toMap(PointDefinition::getCode, Function.identity(), (a,b)->b));
    45. Map<String,ConveyorDefinition> codeToConveyorDefinitionMap = layoutDefinition.getConveyors()
    46. .stream()
    47. .collect(Collectors.toMap(ConveyorDefinition::getCode, Function.identity(), (a,b)->b));
    48. Map<ConveyorDefinition,ConveyorPath> definitionToConveyorMap = new HashMap<>();
    49. // step2: 定义网络对象
    50. ConveyorNetwork conveyorNetwork = new ConveyorNetwork(this,"conveyorNetwork");
    51. // step3: 向网络添加conveyor
    52. for(ConveyorDefinition conveyorDefinition : layoutDefinition.getConveyors()){
    53. ConveyorPath conveyor = new ConveyorPath();
    54. // 每个conveyor由若干段组成
    55. for(int index=0; index<conveyorDefinition.getPointCodes().size()-1; index++){
    56. PointDefinition startPoint = codeToPointDefinitionMap.get(conveyorDefinition.getPointCodes().get(index));
    57. PointDefinition endPoint = codeToPointDefinitionMap.get(conveyorDefinition.getPointCodes().get(index+1));
    58. double startX = scale.pixelsPerUnit(METER)*startPoint.getX();
    59. double startY = scale.pixelsPerUnit(METER)*startPoint.getY();
    60. double startZ = scale.pixelsPerUnit(METER)*startPoint.getZ();
    61. double endX = scale.pixelsPerUnit(METER)*endPoint.getX();
    62. double endY = scale.pixelsPerUnit(METER)*endPoint.getY();
    63. double endZ = scale.pixelsPerUnit(METER)*endPoint.getZ();
    64. MarkupSegmentLine segment = new MarkupSegmentLine(startX, startY, startZ, endX, endY, endZ);
    65. conveyor.addSegment(segment);
    66. }
    67. definitionToConveyorMap.put(conveyorDefinition, conveyor);
    68. conveyorNetwork.add(conveyor);
    69. }
    70. // step4: 自定义站点添加
    71. for(CustomStationDefinition customStationDefinition : layoutDefinition.getCustomStations()){
    72. // step4.1: 站点创建
    73. CustomStation<Agent> customStation = new CustomStation(customStationDefinition);
    74. List<PointDefinition> points = new ArrayList<>();
    75. // step4.2: 站点与辊道关联,并绘制站点图形
    76. for(String conveyorCode : customStationDefinition.getInConveyorCodes()){
    77. customStation.addConnection(definitionToConveyorMap.get(codeToConveyorDefinitionMap.get(conveyorCode)), PathEndType.END);
    78. ConveyorDefinition conveyorDefinition = codeToConveyorDefinitionMap.get(conveyorCode);
    79. PointDefinition pointDefinition = codeToPointDefinitionMap.get(conveyorDefinition.getPointCodes().get(conveyorDefinition.getPointCodes().size()-1));
    80. points.add(pointDefinition);
    81. customStation.addVertex(scale.pixelsPerUnit(METER)*pointDefinition.getX(), scale.pixelsPerUnit(METER)*pointDefinition.getY());
    82. }
    83. for(String conveyorCode : customStationDefinition.getOutConveyorCodes()){
    84. customStation.addConnection(definitionToConveyorMap.get(codeToConveyorDefinitionMap.get(conveyorCode)), PathEndType.BEGIN);
    85. ConveyorDefinition conveyorDefinition = codeToConveyorDefinitionMap.get(conveyorCode);
    86. PointDefinition pointDefinition = codeToPointDefinitionMap.get(conveyorDefinition.getPointCodes().get(0));
    87. points.add(pointDefinition);
    88. customStation.addVertex(scale.pixelsPerUnit(METER)*pointDefinition.getX(), scale.pixelsPerUnit(METER)*pointDefinition.getY());
    89. }
    90. // step4.3: 站点图形补充
    91. int conveyorNum = customStationDefinition.getInConveyorCodes().size()+customStationDefinition.getOutConveyorCodes().size();
    92. if(conveyorNum==0){
    93. error("不允许无连接辊道的ConveyorCustomStation");
    94. }else if(conveyorNum==1){
    95. // 已有一个点,需要额外补充两个点
    96. customStation.addVertex(scale.pixelsPerUnit(METER)*(points.get(points.size()-1).getX()+0.1), scale.pixelsPerUnit(METER)*(points.get(points.size()-1).getY()+0.2));
    97. customStation.addVertex(scale.pixelsPerUnit(METER)*(points.get(points.size()-1).getX()+0.1), scale.pixelsPerUnit(METER)*(points.get(points.size()-1).getY()+0.4));
    98. }else if(conveyorNum==2){
    99. // 已有一个点,需要额外补充一个点,借助两点的法线寻找第三点
    100. double x1 = scale.pixelsPerUnit(METER)*points.get(0).getX();
    101. double y1 = scale.pixelsPerUnit(METER)*points.get(0).getY();
    102. double x2 = scale.pixelsPerUnit(METER)*points.get(points.size()-1).getX();
    103. double y2 = scale.pixelsPerUnit(METER)*points.get(points.size()-1).getY();
    104. double kAB;
    105. if (x2 - x1 == 0) {
    106. kAB = Double.POSITIVE_INFINITY;
    107. } else {
    108. kAB = (y2 - y1) / (x2 - x1);
    109. }
    110. double kPerp;
    111. if (kAB == Double.POSITIVE_INFINITY || kAB == Double.NEGATIVE_INFINITY || kAB == 0) {
    112. kPerp = kAB == 0 ? Double.POSITIVE_INFINITY : 0;
    113. } else {
    114. kPerp = -1 / kAB;
    115. }
    116. double xC = x1 + 0.5;
    117. double yC;
    118. if (kPerp == Double.POSITIVE_INFINITY || kPerp == Double.NEGATIVE_INFINITY) {
    119. yC = y1;
    120. } else {
    121. yC = y1 + kPerp * (xC - x1);
    122. }
    123. customStation.addVertex(xC,yC);
    124. }
    125. conveyorNetwork.add(customStation);
    126. }
    127. // step5: 将生成的网络添加到演示中
    128. Level customLevel = new Level(this,"customLevel",SHAPE_DRAW_2D3D,0);
    129. customLevel.add(conveyorNetwork);
    130. customLevel.initialize();
    131. presentation.add(customLevel);

  • 相关阅读:
    paddlepaddle 实现AlexNet模型,复现原创论文
    《C++ Primer》练习9.43-练习9.46:替换字符串简写和插入前后缀
    vue2升级到vue3的一些使用注意事项记录(四)
    常用注解大全
    cx3588 文档说明
    pytest测试报告邮件发送格式调整(基于Allure的测试报告)
    Spring集成hazelcast实现分布式缓存
    C#/.NET/.NET Core技术前沿周刊 | 第 5 期(2024年9.9-9.15)
    如何利用数字创新使您的企业脱颖而出
    猿创征文 |【算法面试入门必刷】动态规划-线性dp(四)
  • 原文地址:https://blog.csdn.net/zhaoyaxuan001/article/details/139775490