Search code examples
javaschedulingoptaplanner

Get identifiers of entities violating constraints


My problem is scheduling appointments in a Timetable of N days. I am aware of the fact that for some really unfavorable cases I won't ever find a solution with no violation of constraints. What I would like to do is run the solver on the problem, get the appointments that violate hard constraints, remove them from the solution and re-execute the solver with those appointments on a Timetable of the following N days (that is, rescheduling them). Is there a way of getting the identifiers of the appointments violating the constraints and removing them from the solution in Java? I read the documentation but I didn't find anything.


Solution

  • Look for Constraint Justification.

    You will not necessarily receive the entities, but you will receive the objects which caused the penalty. Consider the following constraint:

    Constraint computerCost(ConstraintFactory constraintFactory) {
        return constraintFactory.forEach(CloudComputer.class)
                .ifExists(CloudProcess.class, equal(Function.identity(), CloudProcess::getComputer))
                .penalize("computerCost",
                        HardSoftScore.ONE_SOFT,
                        CloudComputer::getCost);
    }
    

    The constraint justification will include the CloudComputer that is being penalized. (The constraint stream is UniConstraintStream<CloudComputer>.) The constraint justification will not include the CloudProcess, as that does not enter into the penalty.