-
Notifications
You must be signed in to change notification settings - Fork 216
docs: Improve model enrichment docs #2476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TomCools
wants to merge
6
commits into
TimefoldAI:main
Choose a base branch
from
TomCools:docs/model-enrichment
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cb54e1e
docs:
TomCools 2f3eece
Potential fix for pull request finding
TomCools 9f2e58c
Apply suggestions from code review
TomCools 57ccebb
Merge branch 'main' into docs/model-enrichment
TomCools 31fba8b
docs: extend model-enrichtment docs
TomCools 2f54098
Potential fix for pull request finding
TomCools File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
204 changes: 204 additions & 0 deletions
204
docs/src/modules/ROOT/pages/running-timefold-solver/service/model-enrichment.adoc
This file contains hidden or 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,204 @@ | ||
| = Model enrichment | ||
| :page-aliases: service/modeling-changes.adoc, running-timefold-solver/service/modeling-changes.adoc | ||
| :description: Enrich your SolverModel with additional information before solving starts. | ||
| :doctype: book | ||
| :sectnums: | ||
| :icons: font | ||
|
|
||
| [#solverModelEnrichment] | ||
| == What is model enrichment? | ||
|
|
||
| Model enrichment is the process of adding extra information to the `SolverModel` before solving starts. | ||
| The service receives the planning problem as submitted through the REST API, | ||
| but sometimes that raw model is incomplete: it may be missing derived fields, external lookups, or pre-computed values | ||
| that are too expensive or impractical to calculate during solving. | ||
|
|
||
| Enrichers run once, just before the solver begins, and produce an augmented model that constraints can use directly. | ||
|
|
||
| Common use cases include: | ||
|
|
||
| * Looking up data from an external service or database (e.g. travel times, holiday calendars, price lists). | ||
| * Pinning entities that represent work already completed or committed. | ||
| * Pre-computing expensive derived values (e.g. distance matrices, compatibility scores) so constraints stay fast. | ||
|
|
||
| [#whyEnrich] | ||
| == Why enrich the model instead of computing inside constraints? | ||
|
|
||
| Constraint streams execute many times per second during solving. | ||
| Any computation that can be done once before solving starts should be moved into model enrichment. | ||
|
|
||
| WARNING: Never do computation which involves a remote call, a database query, | ||
| or a complex algorithm while the solver is running (e.g. inside the constraint streams or shadow variables). | ||
| This is an antipattern and will make the solver unusably slow. | ||
|
Comment on lines
+30
to
+32
|
||
|
|
||
| [#bestPractices] | ||
| == Best practices | ||
|
|
||
| Do:: | ||
| + | ||
| * Make enrichers idempotent: calling `enrich()` twice should produce the same result. | ||
| * Fail fast: if a required external resource is unavailable, throw an exception rather than silently returning an incomplete model. | ||
| * Use the `SolverModelEnrichmentDirector` to control ordering when one enricher depends on another. | ||
|
|
||
| Don't:: | ||
| + | ||
| * Do not perform heavy computation inside constraint streams. Move it to an enricher instead. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Section already starts with "don't". |
||
| * Do not keep mutable state in enrichers; they may be invoked concurrently from multiple threads. | ||
| * Do not use enrichment for validation that should happen at input time; reject bad input at the API boundary, not here. | ||
|
|
||
| [#implementingAnEnricher] | ||
| == Implementing an enricher | ||
|
|
||
| To enrich the model, implement `SolverModelEnricher` and annotate it so it is picked up by your Dependency Injection (DI) container. | ||
|
|
||
| In this example, the `Timetable` planning solution contains `Timeslot` objects. | ||
| Each `Timeslot` has an `isHoliday` field that must be populated by consulting an external calendar service. | ||
|
|
||
| .Timeslot class with the field to be enriched | ||
| [tabs] | ||
| ==== | ||
| Java:: | ||
| + | ||
| -- | ||
| [source,java,options="nowrap"] | ||
| ---- | ||
| public class Timeslot { | ||
|
|
||
| private LocalDateTime startTime; | ||
| private LocalDateTime endTime; | ||
|
|
||
| private boolean isHoliday; // <1> | ||
|
|
||
| public void setHoliday(boolean isHoliday) { | ||
| this.isHoliday = isHoliday; | ||
| } | ||
|
|
||
| // other Getters/Setters/Constructors excluded | ||
| } | ||
| ---- | ||
| <1> This field is not provided by the caller — it is populated by the enricher. | ||
| -- | ||
|
|
||
| Kotlin:: | ||
| + | ||
| -- | ||
| [source,kotlin,options="nowrap"] | ||
| ---- | ||
| data class Timeslot( | ||
| val startTime: LocalDateTime? = null, | ||
| val endTime: LocalDateTime? = null | ||
| ) { | ||
| var isHoliday: Boolean = false // <1> | ||
| } | ||
| ---- | ||
| <1> This field is not provided by the caller — it is populated by the enricher. | ||
| -- | ||
| ==== | ||
|
|
||
| .Enricher implementation | ||
| [tabs] | ||
| ==== | ||
| Java:: | ||
| + | ||
| -- | ||
| [source,java,options="nowrap"] | ||
| ---- | ||
| @ApplicationScoped | ||
| public class TimeslotHolidayEnricher implements SolverModelEnricher<Timetable> { | ||
|
|
||
| @Override | ||
| public Timetable enrich(Timetable solverModel) { | ||
| for (Timeslot timeslot : solverModel.getTimeslots()) { | ||
| boolean isHoliday = overlapsWithKnownHoliday(timeslot.getStartTime(), timeslot.getEndTime()); | ||
| timeslot.setHoliday(isHoliday); | ||
| } | ||
| return solverModel; | ||
| } | ||
|
|
||
| private boolean overlapsWithKnownHoliday(LocalDateTime start, LocalDateTime end) { | ||
| // Call an external service or database here. | ||
| return false; | ||
| } | ||
| } | ||
| ---- | ||
| -- | ||
|
|
||
| Kotlin:: | ||
| + | ||
| -- | ||
| [source,kotlin,options="nowrap"] | ||
| ---- | ||
| @ApplicationScoped | ||
| class TimeslotHolidayEnricher : SolverModelEnricher<Timetable> { | ||
|
|
||
| override fun enrich(solverModel: Timetable): Timetable { | ||
| for (timeslot in solverModel.timeslots) { | ||
| timeslot.isHoliday = overlapsWithKnownHoliday(timeslot.startTime, timeslot.endTime) | ||
| } | ||
| return solverModel | ||
| } | ||
|
|
||
| private fun overlapsWithKnownHoliday(start: LocalDateTime?, end: LocalDateTime?): Boolean { | ||
| // Call an external service or database here. | ||
| return false | ||
| } | ||
| } | ||
| ---- | ||
| -- | ||
| ==== | ||
|
|
||
| [#enrichmentDirector] | ||
| == Controlling enrichment order | ||
|
|
||
| When you have multiple enrichers, register a `SolverModelEnrichmentDirector` to control the order in which they run. | ||
| This matters when one enricher builds on the output of another. | ||
|
|
||
| //TODO add link to maps component docs https://github.com/TimefoldAI/timefold-solver/issues/2348 | ||
|
|
||
|
Comment on lines
+156
to
+157
|
||
| .Enrichment director that sequences enrichers explicitly | ||
| [tabs] | ||
| ==== | ||
| Java:: | ||
| + | ||
| -- | ||
| [source,java,options="nowrap"] | ||
| ---- | ||
| @ApplicationScoped | ||
| public class TimetableEnrichmentDirector implements SolverModelEnrichmentDirector<Timetable> { | ||
|
|
||
| private final TimeslotHolidayEnricher timeslotEnricher; | ||
|
|
||
| @Inject | ||
| public TimetableEnrichmentDirector(TimeslotHolidayEnricher timeslotEnricher) { | ||
| this.timeslotEnricher = timeslotEnricher; | ||
| } | ||
|
|
||
| @Override | ||
| public Timetable enrich(Timetable solverModel) { | ||
| var enrichedModel = timeslotEnricher.enrich(solverModel); | ||
| // Chain additional enrichers here, in dependency order. | ||
| return enrichedModel; | ||
| } | ||
| } | ||
| ---- | ||
| -- | ||
|
|
||
| Kotlin:: | ||
| + | ||
| -- | ||
| [source,kotlin,options="nowrap"] | ||
| ---- | ||
| @ApplicationScoped | ||
| class TimetableEnrichmentDirector @Inject constructor( | ||
| private val timeslotEnricher: TimeslotHolidayEnricher | ||
| ) : SolverModelEnrichmentDirector<Timetable> { | ||
|
|
||
| override fun enrich(solverModel: Timetable): Timetable { | ||
| val enrichedModel = timeslotEnricher.enrich(solverModel) | ||
| // Chain additional enrichers here, in dependency order. | ||
| return enrichedModel | ||
| } | ||
| } | ||
| ---- | ||
| -- | ||
| ==== | ||
168 changes: 0 additions & 168 deletions
168
docs/src/modules/ROOT/pages/running-timefold-solver/service/modeling-changes.adoc
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did we not say that DB integration should sit independently of the service?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not if it directly "enriches" the model. We build the travel-matrix like this.