学习需求
在利用matsim实现交通模拟时,需要把模拟的乘客出行数据替换成自己的,如何进行替换呢?
带着这个问题,调研学习matsim,实现population.xml的生成
调研笔记
幸运的发现matsim中实现了很多的writer工具类,population的生成就需要用到PopulationWriter这个工具类。
population.xml示例代码:
<population>
<!-- ====================================================================== --><person id="2023-05-15_0047597"><plan selected="yes"><activity type="dummy" x="609247.5564094826" y="2704822.471511054" end_time="06:14:30" ></activity><leg mode="drt"></leg><activity type="dummy" x="609161.9937043311" y="2706001.830112634" ></activity></plan></person><!-- ====================================================================== --><person id="2023-05-15_0048923"><plan selected="yes"><activity type="dummy" x="609247.5564094826" y="2704822.471511054" end_time="06:00:15" ></activity><leg mode="drt"></leg><activity type="dummy" x="608272.4476406131" y="2705462.4946062723" ></activity></plan></person>
<population>
示例代码中创建person对象需要的标签id、起点坐标、终点坐标、activity结束的时间、leg的模式,生成的person对象要包含这些值
//创建population实例
Population population = ScenarioUtils.createScenario(ConfigUtils.createConfig()).getPopulation();
//下面是创建一个person的示例代码
/*** 创建一个person对象* @param population population实例* @param id personID* @param fCoord 起点坐标-UTM格式* @param tCoord 终点坐标-UTM格式* @param end activity结束时间* @param mode 模式*/private static void createOnePerson(Population population, String id, Coord fCoord, Coord tCoord, int end,String mode) {Person person = population.getFactory().createPerson(Id.createPersonId(id));Plan plan = population.getFactory().createPlan();Activity f = population.getFactory().createActivityFromCoord("dummy", fCoord);f.setEndTime(end);plan.addActivity(f);Leg hinweg = population.getFactory().createLeg(mode);plan.addLeg(hinweg);Activity t = population.getFactory().createActivityFromCoord("dummy", tCoord);plan.addActivity(t);person.addPlan(plan);population.addPerson(person);}
//populaiton写入文件populationFile中
new PopulationWriter(population).write(populationFile);