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

2024-08-28 14:28

本文主要是介绍[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);

这篇关于[Jsprit]Jsprit学习笔记-一个简单的示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

pandas中位数填充空值的实现示例

《pandas中位数填充空值的实现示例》中位数填充是一种简单而有效的方法,用于填充数据集中缺失的值,本文就来介绍一下pandas中位数填充空值的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是中位数填充?为什么选择中位数填充?示例数据结果分析完整代码总结在数据分析和机器学习过程中,处理缺失数

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Pandas统计每行数据中的空值的方法示例

《Pandas统计每行数据中的空值的方法示例》处理缺失数据(NaN值)是一个非常常见的问题,本文主要介绍了Pandas统计每行数据中的空值的方法示例,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是空值?为什么要统计空值?准备工作创建示例数据统计每行空值数量进一步分析www.chinasem.cn处

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Mysql用户授权(GRANT)语法及示例解读

《Mysql用户授权(GRANT)语法及示例解读》:本文主要介绍Mysql用户授权(GRANT)语法及示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql用户授权(GRANT)语法授予用户权限语法GRANT语句中的<权限类型>的使用WITH GRANT