[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调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

oracle DBMS_SQL.PARSE的使用方法和示例

《oracleDBMS_SQL.PARSE的使用方法和示例》DBMS_SQL是Oracle数据库中的一个强大包,用于动态构建和执行SQL语句,DBMS_SQL.PARSE过程解析SQL语句或PL/S... 目录语法示例注意事项DBMS_SQL 是 oracle 数据库中的一个强大包,它允许动态地构建和执行

redis群集简单部署过程

《redis群集简单部署过程》文章介绍了Redis,一个高性能的键值存储系统,其支持多种数据结构和命令,它还讨论了Redis的服务器端架构、数据存储和获取、协议和命令、高可用性方案、缓存机制以及监控和... 目录Redis介绍1. 基本概念2. 服务器端3. 存储和获取数据4. 协议和命令5. 高可用性6.

Python中顺序结构和循环结构示例代码

《Python中顺序结构和循环结构示例代码》:本文主要介绍Python中的条件语句和循环语句,条件语句用于根据条件执行不同的代码块,循环语句用于重复执行一段代码,文章还详细说明了range函数的使... 目录一、条件语句(1)条件语句的定义(2)条件语句的语法(a)单分支 if(b)双分支 if-else(

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操

Python中Markdown库的使用示例详解

《Python中Markdown库的使用示例详解》Markdown库是一个用于处理Markdown文本的Python工具,这篇文章主要为大家详细介绍了Markdown库的具体使用,感兴趣的... 目录一、背景二、什么是 Markdown 库三、如何安装这个库四、库函数使用方法1. markdown.mark

MySQL数据库函数之JSON_EXTRACT示例代码

《MySQL数据库函数之JSON_EXTRACT示例代码》:本文主要介绍MySQL数据库函数之JSON_EXTRACT的相关资料,JSON_EXTRACT()函数用于从JSON文档中提取值,支持对... 目录前言基本语法路径表达式示例示例 1: 提取简单值示例 2: 提取嵌套值示例 3: 提取数组中的值注意