Each job has own location (city). Each employee can take jobs only in one location. If an employee took a job in one location, he cannot take orders from another location. How I can do this? Given:
@Data
@PlanningEntity
public class Job {
@PlanningId
private Long id;
private String location;
@PlanningVariable(valueRangeProviderRefs = "employeeRange")
private Employee employee;
}
@Data
@PlanningEntity
public class Employee {
@PlanningId
private Long id;
@InverseRelationShadowVariable(sourceVariableName = "employee")
private List<Job> jobs = new ArrayList<>();
}
@Data
@PlanningSolution
public class Solution {
@PlanningEntityCollectionProperty
@ValueRangeProvider(id = "employeeRange")
private List<Emoloyee> employees;
@PlanningEntityCollectionProperty
private List<Job> jobs;
@PlanningScore
private HardMediumSoftScore score;
public Solution(List<Emoloyee> employees, List<Job> jobs) {
this.employees = employees;
this.jobs = jobs;
}
}
Here is constraint for considering the location:
private Constraint locationIsConsidered(ConstraintFactory constraintFactory) {
return constraintFactory.forEach(Job.class)
.filter(job -> !job.getEmployee().getLocation()
.equals(job.getLocation()))
.penalize("Location conflict", HardMediumSoftScore.ONE_HARD);
}
the another constraint (location is not considered): But it's not working
private Constraint directionIsNotConsidered(ConstraintFactory constraintFactory) {
return constraintFactory.forEach(Job.class)
.groupBy(Job::getInspector)
.filter(employee -> employee.getJobs().stream()
.anyMatch(job -> !Objects.equals(job.getLocation(),
employee.getJobs().get(0).getLocation()))
).penalize("Location Conflict", HardMediumSoftScore.ONE_HARD);
}
Thanks in advance!