use constraints dynamically sent by api's body

123 views Asked by At

I make the constraint provider implementation class as component Spring and i inject it in the controller also i make a configuration bean implementation of ConstraintFactory and i inject it also in my controlleur.In the web service i fetch all constraints using constraintProvider and constraintFactory , i filter this constraints with the data body from the web service an i set constraint in the constraintProvider implementation class but it still use all constraints due to the defineConstraints method which i think execuded once in the execution of project. how i do dynamic constraints with Spring boot and Timefold also i want an exemple of code how i do that i read some stack-overflow's questions:

Optaplanner : Add / remove constraints dynamically

How to choose dynamically which constraints should be applied on the optimization problem based on the frontend input in Timefold Spring Boot?

I want an example of code. How i make dynamic constraints?

example of my code :

@Component public class PlanningConstraintsProvider implementsConstraintProvider {

@Override
public Constraint[] defineConstraints(ConstraintFactory constraintFactory) {
    return new Constraint[]{
            noMissingSkills(constraintFactory),
            employeeTaskOverlapConstraint(constraintFactory),
            locationConstraint(constraintFactory)
    };
}

private Constraint noMissingSkills(ConstraintFactory constraintFactory) {
    return constraintFactory.forEach(Task.class)
            .filter(task -> task.getMissingSkillCount() > 0)
            .penalize(HardMediumSoftScore.ONE_HARD, Task::getMissingSkillCount)
            .asConstraint("NO_MISSING_SKILL");
}

private Constraint locationConstraint(ConstraintFactory constraintFactory) {
    return constraintFactory.forEach(Task.class)
            .penalize(HardMediumSoftScore.ONE_HARD,
                    task -> calculateWeightLocation(task.getAssignedEmployee().getLocation(), task.getLocation()))
            .asConstraint("LOCATION");
}

} i want to calculate dynamicaly the weight for each 3 method with penalizeConfigurable and the constraint configuration class how to do that and justifyWith what is her role and for my solution i set attribute in construtor with the ConstraintConfiguration object then i do the solve with solve manager but it doesn't work respond with the correct solution how i do? i based this to this stack overflow response : How to choose dynamically which constraints should be applied on the optimization problem based on the frontend input in Timefold Spring Boot?

my goal is to give the constraints that i want to take in consideration in solving from my API

2

There are 2 answers

0
Geoffrey De Smet On

Take a look at @ConstraintConfiguration and @ConstraintWeight.

8
Diallo Francis Patrick On

If I understood the answer correctly (How to choose dynamically which constraints should be applied on the optimization problem based on the frontend input in Timefold Spring Boot?), in the ConstraintProvider you write all the constraints and in the ConstraintConfiguration you can do something like this:

  1. Set all the constraints from ConstraintConfiguration to have a ZERO weight
  2. In the initialization phase of the ConstraintConfiguration (in the constructor or @PostConstruct) you can take all the constraints from the database (which will be a list of String)
  3. For every constraint from the list, check if the constraint can be found also in the ConstraintProvider. If the constraint can be found there, put some weight to be taken into account by the solver.

Please correct me if I am wrong. I hope this answer helps you.

You can find more information here: https://timefold.ai/docs/timefold-solver/latest/constraints-and-score/constraint-configuration#constraintWeight

Updated answer:

I provided a solution here: How to choose dynamically which constraints should be applied on the optimization problem based on the frontend input in Timefold Spring Boot?