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

2024-06-19 02:04

本文主要是介绍【仿真建模-anylogic】动态生成ConveyorCustomStation,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Author:赵志乾
Date:2024-06-18
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. 数据结构定义

{"points": [{"code": "", // 点编码"name": "", // 点名称"x": 0.0, // 点坐标"y": 0.0,"z": 0.0}],"conveyors": [{"code": "", // 辊道编码"name": "", // 辊道名称"pointCodes": [] // 辊道从起点至终点所经历的位置点编码列表}],"customStations": [{"code": "",     // 自定义站点编码"name": "",     // 自定义站点名称"inConveyorCodes": [], // 传入站点的辊道"outConveyorCodes": [] // 传出站点的辊道}]
}

3. 代码实现

// ************************数据对象定义****************************
public class PointDefinition implements Serializable {private String code;private String name;private Double x;private Double y;private Double z;// setter、getter
}public class ConveyorDefinition implements Serializable {private String code;private String name;// setter、getter
}public class LayoutDefinition implements Serializable {private List<PointDefinition> points;private List<ConveyorDefinition> conveyors;private List<CustomStationDefinition> customStations;// setter、getter
}public class CustomStationDefinition implements Serializable {private String code;private String name;private List<String> inConveyorCodes;private List<String> outConveyorCodes;// setter、getter
}//******************************子类*******************************
public class CustomStation<T extends Agent> extends ConveyorCustomStation<T> {// 持有站点定义,便于onEnter中依据站点不同做不同处理CustomStationDefinition customStationDefinition;public CustomStation(CustomStationDefinition customStationDefinition) {super();this.customStationDefinition = customStationDefinition;}public void onEnter(T agent ) {// 自定义逻辑}
}//**************************动态生成过程******************************
// step1: 转map,方便后续使用
Map<String,PointDefinition> codeToPointDefinitionMap = layoutDefinition.getPoints().stream().collect(Collectors.toMap(PointDefinition::getCode, Function.identity(), (a,b)->b));
Map<String,ConveyorDefinition> codeToConveyorDefinitionMap = layoutDefinition.getConveyors().stream().collect(Collectors.toMap(ConveyorDefinition::getCode, Function.identity(), (a,b)->b));
Map<ConveyorDefinition,ConveyorPath> definitionToConveyorMap = new HashMap<>();// step2: 定义网络对象
ConveyorNetwork conveyorNetwork = new ConveyorNetwork(this,"conveyorNetwork");// step3: 向网络添加conveyor
for(ConveyorDefinition conveyorDefinition : layoutDefinition.getConveyors()){ConveyorPath conveyor = new ConveyorPath();// 每个conveyor由若干段组成for(int index=0; index<conveyorDefinition.getPointCodes().size()-1; index++){PointDefinition startPoint = codeToPointDefinitionMap.get(conveyorDefinition.getPointCodes().get(index));PointDefinition endPoint = codeToPointDefinitionMap.get(conveyorDefinition.getPointCodes().get(index+1));double startX = scale.pixelsPerUnit(METER)*startPoint.getX();double startY = scale.pixelsPerUnit(METER)*startPoint.getY();double startZ = scale.pixelsPerUnit(METER)*startPoint.getZ();double endX = scale.pixelsPerUnit(METER)*endPoint.getX();double endY = scale.pixelsPerUnit(METER)*endPoint.getY();double endZ = scale.pixelsPerUnit(METER)*endPoint.getZ();MarkupSegmentLine segment = new MarkupSegmentLine(startX, startY, startZ, endX, endY, endZ);conveyor.addSegment(segment);}definitionToConveyorMap.put(conveyorDefinition, conveyor);conveyorNetwork.add(conveyor);
}// step4: 自定义站点添加
for(CustomStationDefinition customStationDefinition : layoutDefinition.getCustomStations()){// step4.1: 站点创建CustomStation<Agent> customStation = new CustomStation(customStationDefinition);List<PointDefinition> points = new ArrayList<>();// step4.2: 站点与辊道关联,并绘制站点图形 for(String conveyorCode : customStationDefinition.getInConveyorCodes()){customStation.addConnection(definitionToConveyorMap.get(codeToConveyorDefinitionMap.get(conveyorCode)), PathEndType.END);ConveyorDefinition conveyorDefinition = codeToConveyorDefinitionMap.get(conveyorCode);PointDefinition pointDefinition = codeToPointDefinitionMap.get(conveyorDefinition.getPointCodes().get(conveyorDefinition.getPointCodes().size()-1));points.add(pointDefinition);customStation.addVertex(scale.pixelsPerUnit(METER)*pointDefinition.getX(), scale.pixelsPerUnit(METER)*pointDefinition.getY());}for(String conveyorCode : customStationDefinition.getOutConveyorCodes()){customStation.addConnection(definitionToConveyorMap.get(codeToConveyorDefinitionMap.get(conveyorCode)), PathEndType.BEGIN);ConveyorDefinition conveyorDefinition = codeToConveyorDefinitionMap.get(conveyorCode);PointDefinition pointDefinition = codeToPointDefinitionMap.get(conveyorDefinition.getPointCodes().get(0));points.add(pointDefinition);customStation.addVertex(scale.pixelsPerUnit(METER)*pointDefinition.getX(), scale.pixelsPerUnit(METER)*pointDefinition.getY());}// step4.3: 站点图形补充int conveyorNum = customStationDefinition.getInConveyorCodes().size()+customStationDefinition.getOutConveyorCodes().size();if(conveyorNum==0){error("不允许无连接辊道的ConveyorCustomStation");}else if(conveyorNum==1){// 已有一个点,需要额外补充两个点customStation.addVertex(scale.pixelsPerUnit(METER)*(points.get(points.size()-1).getX()+0.1), scale.pixelsPerUnit(METER)*(points.get(points.size()-1).getY()+0.2));customStation.addVertex(scale.pixelsPerUnit(METER)*(points.get(points.size()-1).getX()+0.1), scale.pixelsPerUnit(METER)*(points.get(points.size()-1).getY()+0.4));}else if(conveyorNum==2){// 已有一个点,需要额外补充一个点,借助两点的法线寻找第三点double x1 = scale.pixelsPerUnit(METER)*points.get(0).getX();double y1 = scale.pixelsPerUnit(METER)*points.get(0).getY();double x2 = scale.pixelsPerUnit(METER)*points.get(points.size()-1).getX();double y2 = scale.pixelsPerUnit(METER)*points.get(points.size()-1).getY();double kAB;  if (x2 - x1 == 0) { kAB = Double.POSITIVE_INFINITY;} else {  kAB = (y2 - y1) / (x2 - x1);  }  double kPerp;  if (kAB == Double.POSITIVE_INFINITY || kAB == Double.NEGATIVE_INFINITY || kAB == 0) {  kPerp = kAB == 0 ? Double.POSITIVE_INFINITY : 0;  } else {  kPerp = -1 / kAB;  }  double xC = x1 + 0.5;  double yC;  if (kPerp == Double.POSITIVE_INFINITY || kPerp == Double.NEGATIVE_INFINITY) {  yC = y1;  } else {  yC = y1 + kPerp * (xC - x1);  }  customStation.addVertex(xC,yC);}conveyorNetwork.add(customStation);
}// step5: 将生成的网络添加到演示中
Level customLevel = new Level(this,"customLevel",SHAPE_DRAW_2D3D,0);
customLevel.add(conveyorNetwork);
customLevel.initialize();
presentation.add(customLevel);

这篇关于【仿真建模-anylogic】动态生成ConveyorCustomStation的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1073699

相关文章

IDEA自动生成注释模板的配置教程

《IDEA自动生成注释模板的配置教程》本文介绍了如何在IntelliJIDEA中配置类和方法的注释模板,包括自动生成项目名称、包名、日期和时间等内容,以及如何定制参数和返回值的注释格式,需要的朋友可以... 目录项目场景配置方法类注释模板定义类开头的注释步骤类注释效果方法注释模板定义方法开头的注释步骤方法注

Python如何自动生成环境依赖包requirements

《Python如何自动生成环境依赖包requirements》:本文主要介绍Python如何自动生成环境依赖包requirements问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录生成当前 python 环境 安装的所有依赖包1、命令2、常见问题只生成当前 项目 的所有依赖包1、

MySQL中动态生成SQL语句去掉所有字段的空格的操作方法

《MySQL中动态生成SQL语句去掉所有字段的空格的操作方法》在数据库管理过程中,我们常常会遇到需要对表中字段进行清洗和整理的情况,本文将详细介绍如何在MySQL中动态生成SQL语句来去掉所有字段的空... 目录在mysql中动态生成SQL语句去掉所有字段的空格准备工作原理分析动态生成SQL语句在MySQL

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

Java利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

C#如何动态创建Label,及动态label事件

《C#如何动态创建Label,及动态label事件》:本文主要介绍C#如何动态创建Label,及动态label事件,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#如何动态创建Label,及动态label事件第一点:switch中的生成我们的label事件接着,

SpringCloud动态配置注解@RefreshScope与@Component的深度解析

《SpringCloud动态配置注解@RefreshScope与@Component的深度解析》在现代微服务架构中,动态配置管理是一个关键需求,本文将为大家介绍SpringCloud中相关的注解@Re... 目录引言1. @RefreshScope 的作用与原理1.1 什么是 @RefreshScope1.

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S