此问题已在此处有答案:
What is a NullPointerException, and how do I fix it?(12个回答)
4天前关闭。
我尝试使用Optaplanner,并使用Optaplannerspring-boot-starter创建了一个简单的项目,以解决目前只有一个硬约束的简单VRP问题。
我的域模型非常简单,一个包含体积、重量容量的车辆类和包含体积和重量值的订单类。
@Getter
@Setter
@ToString
@PlanningEntity
public class Vehicle {
@PlanningId
private Long id;
private int weightCapacity;
private int volumeCapacity;
private double guaranteeCheck;
@PlanningListVariable(valueRangeProviderRefs = "orderRange")
private List<Order> orders;
public Vehicle() {
}
public Vehicle(long id, int volume, int weight) {
this.id = id;
this.volumeCapacity=volume;
this.weightCapacity=weight;
}
public int getTotalVolumeCapacity() {
int totalDemand = 0;
for (Order order : orders) {
totalDemand += order.getVolume();
}
return totalDemand;
}
public int getTotalWeightCapacity(){
int totalDemand = 0;
for (Order order : orders) {
totalDemand += order.getWeight();
}
return totalDemand;
}
}
@Getter
@Setter
@ToString
public class Order {
private Long id;
private String reference;
private double weight;
private double volume;
// private double amount;
private Location location;
public Order() {
}
public Order(long id, int volume, int weight, Location location) {
this.id = id;
this.reference = this.id.toString();
this.volume = volume;
this.weight = weight;
this.location = location;
}
}
我创建了一个vrp解决方案类,如下所示:
@NoArgsConstructor
@AllArgsConstructor
@PlanningSolution
@Getter
@Setter
public class VehicleRoutingSolution {
@ProblemFactCollectionProperty
@ValueRangeProvider(id = "orderRange")
private List<Order> orders;
@ValueRangeProvider
@PlanningEntityCollectionProperty
private List<Vehicle> vehicles;
@PlanningScore
private HardSoftLongScore score;
public VehicleRoutingSolution(List<Order> orders, List<Vehicle> vehicles) {
this.orders = orders;
this.vehicles = vehicles;
}
}
这里的问题很简单,我试图根据一个硬约束为车辆分配订单,该硬约束基于quarkus quickstart官方示例制定如下:
public class VehicleRoutingConstraintProvider implements ConstraintProvider {
@Override
public Constraint[] defineConstraints(ConstraintFactory constraintFactory) {
return new Constraint[]{
// Hard constraints
vehicleCapacity(constraintFactory),
// Soft constraints
};
}
protected Constraint vehicleCapacity(ConstraintFactory factory) {
return factory.forEach(Vehicle.class)
.filter(vehicle -> vehicle.getTotalVolumeCapacity() > vehicle.getVolumeCapacity())
.penalizeLong(HardSoftLongScore.ONE_HARD,
vehicle -> vehicle.getTotalVolumeCapacity() - vehicle.getVolumeCapacity())
.asConstraint("vehicleCapacity");
}
}
当我启动求解程序在数据集上查找解决方案时,它显示以下问题:Cannot invoke "java.util.List.size()" because the return value of "org.optaplanner.core.impl.domain.variable.descriptor.ListVariableDescriptor.getListVariable(Object)" is null
我在想,要么是我的一个解决方案变量没有正确初始化,要么是最新版本(自4月22日起可用)发生了变化。
求解器执行代码如下:
List<Order> orders = createOrdersDataSet();
List<Vehicle> vehicles = createVehicleDataSet();
UUID problemId = UUID.randomUUID();
VehicleRoutingSolution problem = new VehicleRoutingSolution(orders, vehicles);
// Submit the problem to start solving
SolverJob<VehicleRoutingSolution, UUID> solverJob = solverManager.solve(problemId, problem);
VehicleRoutingSolution solution;
try {
// Wait until the solving ends
solution = solverJob.getFinalBestSolution();
} catch (InterruptedException | ExecutionException e) {
throw new IllegalStateException("Solving failed.", e);
}
log.info("Your best score is: {}", solution.getScore());
1条答案
按热度按时间ccrfmcuu1#
实际上,错误消息是非常不言自明的-您的列表变量是
null
。如果你像这样声明你的规划变量,这个问题就会消失:或者,可以在运行时设置该字段,然后再启动求解器。