本文主要是介绍[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)。以下是程序的主要步骤:
-
创建输出目录:如果不存在
output
目录,则创建它。 -
定义车辆类型:创建一个具有特定类型标识
"vehicleType"
和一个容量维度(重量)为 2 的车辆类型。 -
定义车辆:创建一个位于坐标 (10,10) 的车辆,使用上面定义的车辆类型。
-
定义服务:创建四个服务(客户地点),每个服务都有一个容量需求为 1。
-
构建问题实例:使用
VehicleRoutingProblem.Builder
构建 VRP 实例,将车辆和服务添加到问题中。 -
选择算法:使用 Jsprit 提供的默认算法来搜索解决方案。
-
搜索解决方案:运行算法并获取一系列解决方案。
-
选择最佳解决方案:从搜索到的解决方案中选择最佳(成本最低)的解决方案。
-
输出问题和解决方案:使用
VrpXMLWriter
将问题和解决方案写入 XML 文件。 -
打印解决方案详情:使用
SolutionPrinter
打印最佳解决方案的详细信息。 -
绘制解决方案:使用
Plotter
绘制解决方案的图形表示,并保存到文件。 -
使用 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学习笔记-一个简单的示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!