forked from TimefoldAI/timefold-solver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: constraint weight override (TimefoldAI#946)
Planning solution gets an optional field of type `Map` (or more likely a custom map), with keys being the `constraintName` from `asConstraint(constraintName)`, and the value being the constraint weight. Using the map is optional. Constraint weights not present in the map use their default constraint weights as defined by `penalize(...)`. Constraints present in the map but not present in the `ConstraintProvider` trigger a fail-fast. `asConstraintDescribed(constraintName, constraintDescription)` is introduced to keep constraint documentation where it belongs, with the constraints. `ConstraintWeight`, `ConstraintConfiguration` and `penalizeConfigurable(...)` are deprecated. They continue working, but the new system doesn't use them. --------- Co-authored-by: lee-carlon <[email protected]> Co-authored-by: Frederico Gonçalves <[email protected]> Co-authored-by: Christopher Chianelli <[email protected]>
- Loading branch information
1 parent
cc9630f
commit 2c74a11
Showing
111 changed files
with
2,542 additions
and
1,823 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
.../src/main/java/ai/timefold/solver/core/api/domain/solution/ConstraintWeightOverrides.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package ai.timefold.solver.core.api.domain.solution; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import ai.timefold.solver.core.api.score.Score; | ||
import ai.timefold.solver.core.api.score.stream.ConstraintProvider; | ||
import ai.timefold.solver.core.api.score.stream.uni.UniConstraintBuilder; | ||
import ai.timefold.solver.core.api.score.stream.uni.UniConstraintStream; | ||
import ai.timefold.solver.core.api.solver.change.ProblemChange; | ||
import ai.timefold.solver.core.impl.domain.solution.DefaultConstraintWeightOverrides; | ||
|
||
/** | ||
* Used to override constraint weights defined in Constraint Streams, | ||
* e.g., in {@link UniConstraintStream#penalize(Score)}. | ||
* To use, | ||
* place a member (typically a field) of type {@link ConstraintWeightOverrides} | ||
* in your {@link PlanningSolution}-annotated class. | ||
* <p> | ||
* Users should use {@link #of(Map)} to provide the actual constraint weights. | ||
* Alternatively, a JSON serializers and deserializer may be defined to interact with a solution file. | ||
* Once the constraint weights are set, they must remain constant throughout the solving process, | ||
* or a {@link ProblemChange} needs to be triggered. | ||
* <p> | ||
* Zero-weight will be excluded from processing, | ||
* and the solver will behave as if it did not exist in the {@link ConstraintProvider}. | ||
* <p> | ||
* There is no support for user-defined packages, which is a deprecated feature in itself. | ||
* The constraint is assumed to be in the same package as the top-most class implementing this interface. | ||
* It is therefore required that the constraints be built using {@link UniConstraintBuilder#asConstraint(String)}, | ||
* leaving the constraint package to its default value. | ||
* | ||
* @param <Score_> | ||
*/ | ||
public interface ConstraintWeightOverrides<Score_ extends Score<Score_>> { | ||
|
||
static <Score_ extends Score<Score_>> ConstraintWeightOverrides<Score_> none() { | ||
return of(Collections.<String, Score_> emptyMap()); | ||
} | ||
|
||
static <Score_ extends Score<Score_>> ConstraintWeightOverrides<Score_> of(Map<String, Score_> constraintWeightMap) { | ||
return new DefaultConstraintWeightOverrides<>(constraintWeightMap); | ||
} | ||
|
||
/** | ||
* Return a constraint weight for a particular constraint. | ||
* | ||
* @param constraintName never null | ||
* @return null if the constraint name is not known | ||
*/ | ||
Score_ getConstraintWeight(String constraintName); | ||
|
||
/** | ||
* Returns all known constraints. | ||
* | ||
* @return All constraint names for which {@link #getConstraintWeight(String)} returns a non-null value. | ||
*/ | ||
Set<String> getKnownConstraintNames(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.