[英]How to add constraint one car can only go in one direction in OptaPlanner?
A
was added to the car, then only employees who work in the same direction should be in itA
方向工作的员工添加到汽车上,那么只有在同一方向工作的员工才应该在车上@PlanningEntity
public class Employee {
@PlanningId
private Long id;
private String direction;
@PlanningVariable(valueRangeProviderRefs = "vehicleRange")
private Vehicle vehicle;
}
@PlanningEntity
public class Vehicle {
@PlanningId
private Long id;
@InverseRelationShadowVariable(sourceVariableName = "vehicle")
List<Employee> employees = new ArrayList<>();
private String direction;
}
What is the best constraint for this?对此最好的约束是什么?
Since the car's direction is not builtin but defined by the first passenger, I'd go with an instance variable.由于汽车的方向不是内置的,而是由第一位乘客定义的,所以我会使用实例变量 go 。
That would allow you to reuse a car once the passengers have left.这将允许您在乘客离开后重新使用汽车。
I think I have found a solution.我想我已经找到了解决办法。
private Constraint directionConflict(ConstraintFactory factory) {
return factory.forEach(Employee.class)
.groupBy(Employee::getVehicle)
.filter(vehicle -> vehicle.getEmployees().stream()
.anyMatch(employee -> !Objects.equals(employee.getDirection(),
vehicle.getEmployees().get(0).getDirection())))
.penalize("Direction conflict", HardMediumSoftLongScore.ONE_HARD);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.