[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

相关文章

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

CSS will-change 属性示例详解

《CSSwill-change属性示例详解》will-change是一个CSS属性,用于告诉浏览器某个元素在未来可能会发生哪些变化,本文给大家介绍CSSwill-change属性详解,感... will-change 是一个 css 属性,用于告诉浏览器某个元素在未来可能会发生哪些变化。这可以帮助浏览器优化

C++中std::distance使用方法示例

《C++中std::distance使用方法示例》std::distance是C++标准库中的一个函数,用于计算两个迭代器之间的距离,本文主要介绍了C++中std::distance使用方法示例,具... 目录语法使用方式解释示例输出:其他说明:总结std::distance&n编程bsp;是 C++ 标准

前端高级CSS用法示例详解

《前端高级CSS用法示例详解》在前端开发中,CSS(层叠样式表)不仅是用来控制网页的外观和布局,更是实现复杂交互和动态效果的关键技术之一,随着前端技术的不断发展,CSS的用法也日益丰富和高级,本文将深... 前端高级css用法在前端开发中,CSS(层叠样式表)不仅是用来控制网页的外观和布局,更是实现复杂交

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

用js控制视频播放进度基本示例代码

《用js控制视频播放进度基本示例代码》写前端的时候,很多的时候是需要支持要网页视频播放的功能,下面这篇文章主要给大家介绍了关于用js控制视频播放进度的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言html部分:JavaScript部分:注意:总结前言在javascript中控制视频播放

Mysql表的简单操作(基本技能)

《Mysql表的简单操作(基本技能)》在数据库中,表的操作主要包括表的创建、查看、修改、删除等,了解如何操作这些表是数据库管理和开发的基本技能,本文给大家介绍Mysql表的简单操作,感兴趣的朋友一起看... 目录3.1 创建表 3.2 查看表结构3.3 修改表3.4 实践案例:修改表在数据库中,表的操作主要

Java中StopWatch的使用示例详解

《Java中StopWatch的使用示例详解》stopWatch是org.springframework.util包下的一个工具类,使用它可直观的输出代码执行耗时,以及执行时间百分比,这篇文章主要介绍... 目录stopWatch 是org.springframework.util 包下的一个工具类,使用它

Spring Boot 3.4.3 基于 Spring WebFlux 实现 SSE 功能(代码示例)

《SpringBoot3.4.3基于SpringWebFlux实现SSE功能(代码示例)》SpringBoot3.4.3结合SpringWebFlux实现SSE功能,为实时数据推送提供... 目录1. SSE 简介1.1 什么是 SSE?1.2 SSE 的优点1.3 适用场景2. Spring WebFlu

springboot security快速使用示例详解

《springbootsecurity快速使用示例详解》:本文主要介绍springbootsecurity快速使用示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录创www.chinasem.cn建spring boot项目生成脚手架配置依赖接口示例代码项目结构启用s