当前位置: 首页 > news >正文

[Jsprit]Jsprit学习笔记-一个简单的示例

学习官网提供的例子
示例代码

public class SimpleExample {public static void main(String[] args) {/** some preparation - create output folder*/File dir = new File("output");// if the directory does not exist, create itif (!dir.exists()) {System.out.println("creating directory ./output");boolean result = dir.mkdir();if (result) System.out.println("./output created");}/** get a vehicle type-builder and build a type with the typeId "vehicleType" and one capacity dimension, i.e. weight, and capacity dimension value of 2*/final int WEIGHT_INDEX = 0;VehicleTypeImpl.Builder vehicleTypeBuilder = VehicleTypeImpl.Builder.newInstance("vehicleType").addCapacityDimension(WEIGHT_INDEX, 2);VehicleType vehicleType = vehicleTypeBuilder.build();/** get a vehicle-builder and build a vehicle located at (10,10) with type "vehicleType"*/Builder vehicleBuilder = VehicleImpl.Builder.newInstance("vehicle");vehicleBuilder.setStartLocation(Location.newInstance(10, 10));vehicleBuilder.setType(vehicleType);VehicleImpl vehicle = vehicleBuilder.build();/** build services at the required locations, each with a capacity-demand of 1.*/Service service1 = Service.Builder.newInstance("1").addSizeDimension(WEIGHT_INDEX, 1).setLocation(Location.newInstance(5, 7)).build();Service service2 = Service.Builder.newInstance("2").addSizeDimension(WEIGHT_INDEX, 1).setLocation(Location.newInstance(5, 13)).build();Service service3 = Service.Builder.newInstance("3").addSizeDimension(WEIGHT_INDEX, 1).setLocation(Location.newInstance(15, 7)).build();Service service4 = Service.Builder.newInstance("4").addSizeDimension(WEIGHT_INDEX, 1).setLocation(Location.newInstance(15, 13)).build();VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();vrpBuilder.addVehicle(vehicle);vrpBuilder.addJob(service1).addJob(service2).addJob(service3).addJob(service4);VehicleRoutingProblem problem = vrpBuilder.build();/** get the algorithm out-of-the-box.*/VehicleRoutingAlgorithm algorithm = Jsprit.createAlgorithm(problem);/** and search a solution*/Collection<VehicleRoutingProblemSolution> solutions = algorithm.searchSolutions();/** get the best*/VehicleRoutingProblemSolution bestSolution = Solutions.bestOf(solutions);new VrpXMLWriter(problem, solutions).write("output/problem-with-solution.xml");SolutionPrinter.print(problem, bestSolution, SolutionPrinter.Print.VERBOSE);/** plot*/new Plotter(problem,bestSolution).plot("output/plot.png","simple example");/*render problem and solution with GraphStream*/new GraphStreamViewer(problem, bestSolution).labelWith(Label.ID).setRenderDelay(200).display();}}

代码解读
这段代码是一个 Java 程序,它演示了如何使用 Jsprit 库来解决一个简单的车辆路径问题(VRP)。以下是程序的主要步骤:

  1. 创建输出目录:如果不存在 output 目录,则创建它。

  2. 定义车辆类型:创建一个具有特定类型标识 "vehicleType" 和一个容量维度(重量)为 2 的车辆类型。

  3. 定义车辆:创建一个位于坐标 (10,10) 的车辆,使用上面定义的车辆类型。

  4. 定义服务:创建四个服务(客户地点),每个服务都有一个容量需求为 1。

  5. 构建问题实例:使用 VehicleRoutingProblem.Builder 构建 VRP 实例,将车辆和服务添加到问题中。

  6. 选择算法:使用 Jsprit 提供的默认算法来搜索解决方案。

  7. 搜索解决方案:运行算法并获取一系列解决方案。

  8. 选择最佳解决方案:从搜索到的解决方案中选择最佳(成本最低)的解决方案。

  9. 输出问题和解决方案:使用 VrpXMLWriter 将问题和解决方案写入 XML 文件。

  10. 打印解决方案详情:使用 SolutionPrinter 打印最佳解决方案的详细信息。

  11. 绘制解决方案:使用 Plotter 绘制解决方案的图形表示,并保存到文件。

  12. 使用 GraphStream 可视化:使用 GraphStreamViewer 显示问题和解决方案的动态可视化。

以下是代码中关键类的简要说明:

  • VehicleTypeImpl:车辆类型的实现。
  • VehicleImpl:车辆的实现。
  • Service:服务或客户请求的实现。
  • VehicleRoutingProblem.Builder:构建 VRP 问题的构建器。
  • VehicleRoutingAlgorithm:用于搜索 VRP 问题解决方案的算法。
  • Solutions:提供解决方案工具方法的类。
  • VrpXMLWriter:用于将 VRP 问题和解决方案写入 XML 文件的工具。
  • SolutionPrinter:用于打印解决方案的类。
  • Plotter:用于绘制解决方案的图形表示的类。
  • GraphStreamViewer:用于动态显示问题和解决方案的 GraphStream 可视化工具。

这个示例程序提供了一个基本的框架,展示了如何使用 Jsprit 解决 VRP 问题,并提供了一些基本的可视化和输出选项。
核心部分
1、创建车辆类型

VehicleTypeImpl.Builder vehicleTypeBuilder = VehicleTypeImpl.Builder.newInstance("vehicleType").addCapacityDimension(0, 2);
VehicleType vehicleType = vehicleTypeBuilder.build();

2、创建1个车辆

Builder vehicleBuilder = VehicleImpl.Builder.newInstance("vehicle");
vehicleBuilder.setStartLocation(Location.newInstance(10, 10));
vehicleBuilder.setType(vehicleType);
VehicleImpl vehicle = vehicleBuilder.build();

3、创建多辆车

        int nuOfVehicles = 4;int capacity = 80;Coordinate firstDepotCoord = Coordinate.newInstance(20, 20);Coordinate second = Coordinate.newInstance(30, 40);Coordinate third = Coordinate.newInstance(50, 30);Coordinate fourth = Coordinate.newInstance(60, 50);int depotCounter = 1;for (Coordinate depotCoord : Arrays.asList(firstDepotCoord, second, third, fourth)) {for (int i = 0; i < nuOfVehicles; i++) {VehicleTypeImpl vehicleType = VehicleTypeImpl.Builder.newInstance(depotCounter + "_type").addCapacityDimension(0, capacity).setCostPerDistance(1.0).build();VehicleImpl vehicle = VehicleImpl.Builder.newInstance(depotCounter + "_" + (i + 1) + "_vehicle").setStartLocation(Location.newInstance(depotCoord.getX(), depotCoord.getY())).setType(vehicleType).build();vrpBuilder.addVehicle(vehicle);}depotCounter++;}}

4、创建1个服务的对象

 Service service1 = Service.Builder.newInstance("1").addSizeDimension(WEIGHT_INDEX, 1).setLocation(Location.newInstance(5, 7)).build();

5、创建多个服务对象

vrpBuilder.addJob(Service.Builder.newInstance("1").addSizeDimension(0, 18).setLocation(Location.newInstance(22, 22)).build());
vrpBuilder.addJob(Service.Builder.newInstance("2").addSizeDimension(0, 26).setLocation(Location.newInstance(36, 26)).build());

6、封装vrp问题

VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
vrpBuilder.addVehicle(vehicle);
vrpBuilder.addJob(service1).addJob(service2).addJob(service3).addJob(service4);VehicleRoutingProblem problem = vrpBuilder.build();

7、选择算法

VehicleRoutingAlgorithm algorithm = Jsprit.createAlgorithm(problem);

8、算法求解

		/** and search a solution*/Collection<VehicleRoutingProblemSolution> solutions = algorithm.searchSolutions();/** get the best*/VehicleRoutingProblemSolution bestSolution = Solutions.bestOf(solutions);

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 93.WEB渗透测试-信息收集-Google语法(7)
  • HIVE 数据仓库工具之第一部分(讲解部署)
  • 第二章-物理层
  • 断网关机命令(linus)
  • 通过websock实现实时刷新前端(可实现进度条)
  • 尚硅谷Java面试题第四季-Java基本功
  • 【石子合并】
  • 国产游戏崛起:面临的挑战与未来的机遇
  • AI依赖的隐患:技术能力退化、安全风险与社会不平等的未来
  • kafka发送消息-生产者发送消息的分区策略(消息发送到哪个分区中?是什么策略)
  • 深度学习-批量与动量【Datawhale X 李宏毅苹果书 AI夏令营】
  • [Algorithm][综合训练][kotori和气球][体操队形][二叉树中的最大路径和]详细讲解
  • 软件设计原则之开闭原则
  • 【大数据】深入解析向量数据库Faiss:搭建与使用指南
  • Swift-UITableView列表动态设置高度,根据不同的内容长度,设置heightForRowAt
  • 【MySQL经典案例分析】 Waiting for table metadata lock
  • Apache Spark Streaming 使用实例
  • es6
  • gitlab-ci配置详解(一)
  • java第三方包学习之lombok
  • JS函数式编程 数组部分风格 ES6版
  • Netty源码解析1-Buffer
  • opencv python Meanshift 和 Camshift
  • SpiderData 2019年2月25日 DApp数据排行榜
  • 高性能JavaScript阅读简记(三)
  • 经典排序算法及其 Java 实现
  • 前端工程化(Gulp、Webpack)-webpack
  • 前端面试之闭包
  • 如何合理的规划jvm性能调优
  • 设计模式走一遍---观察者模式
  • 《码出高效》学习笔记与书中错误记录
  • 选择阿里云数据库HBase版十大理由
  • ​LeetCode解法汇总2670. 找出不同元素数目差数组
  • #APPINVENTOR学习记录
  • #Linux(make工具和makefile文件以及makefile语法)
  • #我与Java虚拟机的故事#连载17:我的Java技术水平有了一个本质的提升
  • (+3)1.3敏捷宣言与敏捷过程的特点
  • (1)Hilt的基本概念和使用
  • (C语言)输入自定义个数的整数,打印出最大值和最小值
  • (floyd+补集) poj 3275
  • (NO.00004)iOS实现打砖块游戏(九):游戏中小球与反弹棒的碰撞
  • (二) Windows 下 Sublime Text 3 安装离线插件 Anaconda
  • (附源码)springboot助农电商系统 毕业设计 081919
  • (附源码)ssm本科教学合格评估管理系统 毕业设计 180916
  • (几何:六边形面积)编写程序,提示用户输入六边形的边长,然后显示它的面积。
  • (四)activit5.23.0修复跟踪高亮显示BUG
  • (一)pytest自动化测试框架之生成测试报告(mac系统)
  • (一)使用Mybatis实现在student数据库中插入一个学生信息
  • (一)项目实践-利用Appdesigner制作目标跟踪仿真软件
  • ***监测系统的构建(chkrootkit )
  • .NET / MSBuild 扩展编译时什么时候用 BeforeTargets / AfterTargets 什么时候用 DependsOnTargets?
  • .net Signalr 使用笔记
  • .NET/C# 获取一个正在运行的进程的命令行参数
  • .net反编译工具
  • .vue文件怎么使用_我在项目中是这样配置Vue的